Skip to content

Commit

Permalink
Merge pull request elastic#50 from ruflin/test-environment-variables
Browse files Browse the repository at this point in the history
Environment Variables for service integration tests
  • Loading branch information
tsg committed Sep 3, 2015
2 parents c6f223f + 379c6d0 commit 1d006a6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
32 changes: 24 additions & 8 deletions outputs/elasticsearch/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ import (
"testing"
)

func GetTestingElasticsearch() *Elasticsearch {
var es_url string
const ElasticsearchDefaultHost = "localhost"
const ElasticsearchDefaultPort = "9200"

// read the Elasticsearch port from the ES_PORT env variable
func GetEsPort() string {
port := os.Getenv("ES_PORT")
if len(port) > 0 {
es_url = "http://localhost:" + port
} else {
// empty variable
es_url = "http://localhost:9200"

if len(port) == 0 {
port = ElasticsearchDefaultPort
}
return port
}

// Returns
func GetEsHost() string {

host := os.Getenv("ES_HOST")

if len(host) == 0 {
host = ElasticsearchDefaultHost
}

return host
}

func GetTestingElasticsearch() *Elasticsearch {

var es_url = "http://" + GetEsHost() + ":" + GetEsPort()

return NewElasticsearch([]string{es_url}, "", "")
}

Expand Down
48 changes: 18 additions & 30 deletions outputs/elasticsearch/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,37 @@ import (
"github.com/elastic/libbeat/outputs"
)

const elasticsearchAddr = "localhost"
const elasticsearchPort = 9200

func createElasticsearchConnection(flush_interval int, bulk_size int) ElasticsearchOutput {

index := fmt.Sprintf("packetbeat-unittest-%d", os.Getpid())

var es_port int
var err error
esPort, err := strconv.Atoi(GetEsPort())

// read the Elasticsearch port from the ES_PORT env variable
port := os.Getenv("ES_PORT")
if len(port) > 0 {
es_port, err = strconv.Atoi(port)
if err != nil {
// error occurred, use the default
es_port = elasticsearchPort
}
} else {
// empty variable
es_port = elasticsearchPort
if err != nil {
logp.Err("Invalid port. Cannot be converted to in: %s", GetEsPort())
}

var elasticsearchOutput ElasticsearchOutput
elasticsearchOutput.Init("packetbeat",
outputs.MothershipConfig{
Enabled: true,
Save_topology: true,
Host: elasticsearchAddr,
Port: es_port,
Username: "",
Password: "",
Path: "",
Index: index,
Protocol: "",
Flush_interval: &flush_interval,
Bulk_size: &bulk_size,
}, 10)

elasticsearchOutput.Init("packetbeat", outputs.MothershipConfig{
Enabled: true,
Save_topology: true,
Host: GetEsHost(),
Port: esPort,
Username: "",
Password: "",
Path: "",
Index: index,
Protocol: "",
Flush_interval: &flush_interval,
Bulk_size: &bulk_size,
}, 10)

return elasticsearchOutput
}

func TestTopologyInES(t *testing.T) {

if testing.Short() {
t.Skip("Skipping topology tests in short mode, because they require Elasticsearch")
}
Expand Down
26 changes: 22 additions & 4 deletions outputs/redis/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package redis

import (
"os"
"testing"
"time"
)

const redisAddr = ":6379"
const RedisDefaultHost = "localhost"
const RedisDefaultPort = "6379"

func GetRedisAddr() string {

port := os.Getenv("REDIS_PORT")
host := os.Getenv("REDIS_HOST")

if len(port) == 0 {
port = RedisDefaultPort
}

if len(host) == 0 {
host = RedisDefaultHost
}

return host + ":" + port
}

func TestTopologyInRedis(t *testing.T) {
if testing.Short() {
Expand All @@ -14,7 +32,7 @@ func TestTopologyInRedis(t *testing.T) {

var redisOutput1 = RedisOutput{
Index: "packetbeat",
Hostname: redisAddr,
Hostname: GetRedisAddr(),
Password: "",
DbTopology: 1,
Timeout: time.Duration(5) * time.Second,
Expand All @@ -23,7 +41,7 @@ func TestTopologyInRedis(t *testing.T) {

var redisOutput2 = RedisOutput{
Index: "packetbeat",
Hostname: redisAddr,
Hostname: GetRedisAddr(),
Password: "",
DbTopology: 1,
Timeout: time.Duration(5) * time.Second,
Expand All @@ -32,7 +50,7 @@ func TestTopologyInRedis(t *testing.T) {

var redisOutput3 = RedisOutput{
Index: "packetbeat",
Hostname: redisAddr,
Hostname: GetRedisAddr(),
Password: "",
DbTopology: 1,
Timeout: time.Duration(5) * time.Second,
Expand Down

0 comments on commit 1d006a6

Please sign in to comment.