Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit a756744

Browse files
author
Jonathan Chauncey
committed
chore(tests): Add more tests to increase coverage
Adds an integration test that talks to nsq
1 parent 7e6701f commit a756744

4 files changed

Lines changed: 131 additions & 11 deletions

File tree

Makefile

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ IMAGE_PREFIX ?= deis
3131
include versioning.mk
3232

3333
REDIS_CONTAINER_NAME := test-redis-${VERSION}
34+
NSQ_CONTAINER_NAME := test-nsq-${VERSION}
3435

3536
SHELL_SCRIPTS = $(wildcard _scripts/*.sh)
3637

@@ -78,15 +79,19 @@ update-manifests:
7879

7980
test: test-style test-unit
8081

81-
test-cover: start-test-redis
82+
test-cover: start-test-redis start-test-nsq
8283
docker run ${DEV_ENV_OPTS} \
8384
-it \
8485
--link ${REDIS_CONTAINER_NAME}:TEST_REDIS \
85-
${DEV_ENV_IMAGE} bash -c 'DEIS_LOGGER_REDIS_SERVICE_HOST=$$TEST_REDIS_PORT_6379_TCP_ADDR \
86-
DEIS_LOGGER_REDIS_SERVICE_PORT=$$TEST_REDIS_PORT_6379_TCP_PORT \
87-
test-cover.sh' \
88-
|| (make stop-test-redis && false)
86+
--link ${NSQ_CONTAINER_NAME}:TEST_NSQ \
87+
${DEV_ENV_IMAGE} bash -c \
88+
'DEIS_LOGGER_REDIS_SERVICE_HOST=$$TEST_REDIS_PORT_6379_TCP_ADDR \
89+
DEIS_LOGGER_REDIS_SERVICE_PORT=$$TEST_REDIS_PORT_6379_TCP_PORT \
90+
DEIS_NSQD_SERVICE_HOST=$$TEST_NSQ_PORT_4150_TCP_ADDR \
91+
DEIS_NSQD_SERVICE_PORT_TRANSPORT=$$TEST_NSQ_PORT_4150_TCP_PORT \
92+
test-cover.sh'
8993
make stop-test-redis
94+
make stop-test-nsq
9095

9196
test-style: check-docker
9297
${DEV_ENV_CMD} make style-check
@@ -104,21 +109,32 @@ style-check:
104109
shellcheck $(SHELL_SCRIPTS)
105110

106111
start-test-redis:
107-
docker run --name ${REDIS_CONTAINER_NAME} -d redis:latest
112+
docker run --name ${REDIS_CONTAINER_NAME} -d redis:latest || true
113+
114+
start-test-nsq:
115+
docker run --name ${NSQ_CONTAINER_NAME} -d nsqio/nsq nsqd || true
108116

109117
stop-test-redis:
110118
docker kill ${REDIS_CONTAINER_NAME}
111119
docker rm ${REDIS_CONTAINER_NAME}
112120

113-
test-unit: start-test-redis
121+
stop-test-nsq:
122+
docker kill ${NSQ_CONTAINER_NAME}
123+
docker rm ${NSQ_CONTAINER_NAME}
124+
125+
test-unit: start-test-redis start-test-nsq
114126
docker run ${DEV_ENV_OPTS} \
115127
-it \
116128
--link ${REDIS_CONTAINER_NAME}:TEST_REDIS \
117-
${DEV_ENV_IMAGE} bash -c 'DEIS_LOGGER_REDIS_SERVICE_HOST=$$TEST_REDIS_PORT_6379_TCP_ADDR \
118-
DEIS_LOGGER_REDIS_SERVICE_PORT=$$TEST_REDIS_PORT_6379_TCP_PORT \
119-
$(GOTEST) -tags="testredis" $$(glide nv)' \
120-
|| (make stop-test-redis && false)
129+
--link ${NSQ_CONTAINER_NAME}:TEST_NSQ \
130+
${DEV_ENV_IMAGE} bash -c \
131+
'DEIS_LOGGER_REDIS_SERVICE_HOST=$$TEST_REDIS_PORT_6379_TCP_ADDR \
132+
DEIS_LOGGER_REDIS_SERVICE_PORT=$$TEST_REDIS_PORT_6379_TCP_PORT \
133+
DEIS_NSQD_SERVICE_HOST=$$TEST_NSQ_PORT_4150_TCP_ADDR \
134+
DEIS_NSQD_SERVICE_PORT_TRANSPORT=$$TEST_NSQ_PORT_4150_TCP_PORT \
135+
$(GOTEST) -tags="testredis" $$(glide nv)'
121136
make stop-test-redis
137+
make stop-test-nsq
122138

123139
kube-install:
124140
kubectl create -f manifests/deis-logger-svc.yaml

log/config_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package log
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestNsqURL(t *testing.T) {
13+
c := config{
14+
NSQHost: "somehost",
15+
NSQPort: 3333,
16+
}
17+
assert.Equal(t, c.nsqURL(), "somehost:3333")
18+
}
19+
20+
func TestStopTimeoutDuration(t *testing.T) {
21+
c := config{
22+
StopTimeoutSeconds: 60,
23+
}
24+
assert.Equal(t, c.stopTimeoutDuration(), time.Duration(c.StopTimeoutSeconds)*time.Second)
25+
}
26+
27+
func TestParseConfig(t *testing.T) {
28+
os.Setenv("NSQ_TOPIC", "topic")
29+
os.Setenv("NSQ_CHANNEL", "channel")
30+
os.Setenv("NSQ_HANDLER_COUNT", "3")
31+
os.Setenv("AGGREGATOR_STOP_TIMEOUT_SEC", "2")
32+
33+
port, err := strconv.Atoi(os.Getenv("DEIS_NSQD_SERVICE_PORT_TRANSPORT"))
34+
assert.NoError(t, err)
35+
36+
c, err := parseConfig("foo")
37+
assert.NoError(t, err)
38+
assert.Equal(t, c.NSQHost, os.Getenv("DEIS_NSQD_SERVICE_HOST"))
39+
assert.Equal(t, c.NSQPort, port)
40+
assert.Equal(t, c.NSQTopic, "topic")
41+
assert.Equal(t, c.NSQChannel, "channel")
42+
assert.Equal(t, c.NSQHandlerCount, 3)
43+
assert.Equal(t, c.StopTimeoutSeconds, 2)
44+
}

log/nsq_aggregator_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package log
2+
3+
import (
4+
"testing"
5+
6+
"github.com/deis/logger/storage"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAggregator(t *testing.T) {
11+
storageAdapter, err := storage.NewAdapter("memory", 100)
12+
assert.NoError(t, err)
13+
aggregator, err := NewAggregator("nsq", storageAdapter)
14+
assert.NoError(t, err)
15+
err = aggregator.Listen()
16+
assert.NoError(t, err)
17+
stoppedCh := aggregator.Stopped()
18+
err = aggregator.Stop()
19+
assert.NoError(t, err)
20+
stopErr := <-stoppedCh
21+
assert.NoError(t, stopErr, "Aggregator stopped with error")
22+
}

storage/redis_config_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package storage
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestParseConfig(t *testing.T) {
12+
host := os.Getenv("DEIS_LOGGER_REDIS_SERVICE_HOST")
13+
password := os.Getenv("DEIS_LOGGER_REDIS_PASSWORD")
14+
db := os.Getenv("DEIS_LOGGER_REDIS_DB")
15+
pipelineLength := os.Getenv("DEIS_LOGGER_REDIS_PIPELINE_LENGTH")
16+
pipelineTimeoutSeconds := os.Getenv("DEIS_LOGGER_REDIS_PIPELINE_TIMEOUT_SECONDS")
17+
18+
os.Setenv("DEIS_LOGGER_REDIS_PASSWORD", "password")
19+
os.Setenv("DEIS_LOGGER_REDIS_DB", "2")
20+
os.Setenv("DEIS_LOGGER_REDIS_PIPELINE_LENGTH", "1")
21+
os.Setenv("DEIS_LOGGER_REDIS_PIPELINE_TIMEOUT_SECONDS", "2")
22+
23+
p, err := strconv.Atoi(os.Getenv("DEIS_LOGGER_REDIS_SERVICE_PORT"))
24+
assert.NoError(t, err)
25+
26+
c, err := parseConfig("foo")
27+
assert.NoError(t, err, "error parsing config")
28+
assert.Equal(t, c.Host, host)
29+
assert.Equal(t, c.Port, p)
30+
assert.Equal(t, c.Password, "password")
31+
assert.Equal(t, c.PipelineLength, 1)
32+
assert.Equal(t, c.PipelineTimeoutSeconds, 2)
33+
34+
os.Setenv("DEIS_LOGGER_REDIS_PASSWORD", password)
35+
os.Setenv("DEIS_LOGGER_REDIS_DB", db)
36+
os.Setenv("DEIS_LOGGER_REDIS_PIPELINE_LENGTH", pipelineLength)
37+
os.Setenv("DEIS_LOGGER_REDIS_PIPELINE_TIMEOUT_SECONDS", pipelineTimeoutSeconds)
38+
}

0 commit comments

Comments
 (0)