Skip to content

Commit 631554e

Browse files
authored
Make test endpoints configurable
1 parent 7ab5c57 commit 631554e

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ DOCKER_NETWORK?=test
44
DOCKER_NGINX_PLUS?=nginx-plus
55
DOCKER_NGINX_PLUS_HELPER?=nginx-plus-helper
66

7+
export TEST_API_ENDPOINT=http://127.0.0.1:8080/api
8+
export TEST_API_ENDPOINT_OF_HELPER=http://127.0.0.1:8090/api
9+
export TEST_UNAVAILABLE_STREAM_ADDRESS=127.0.0.1:8081
10+
711
test: docker-build run-nginx-plus test-run configure-no-stream-block test-run-no-stream-block clean
812

913
lint:

tests/client_no_stream_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/nginxinc/nginx-plus-go-client/client"
8+
"github.com/nginxinc/nginx-plus-go-client/tests/helpers"
89
)
910

1011
// TestStatsNoStream tests the peculiar behavior of getting Stream-related
@@ -13,7 +14,7 @@ import (
1314
// is misconfigured or of the stream block is missing.
1415
func TestStatsNoStream(t *testing.T) {
1516
httpClient := &http.Client{}
16-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
17+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
1718
if err != nil {
1819
t.Fatalf("Error connecting to nginx: %v", err)
1920
}

tests/client_test.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/nginxinc/nginx-plus-go-client/client"
11+
"github.com/nginxinc/nginx-plus-go-client/tests/helpers"
1112
)
1213

1314
const (
@@ -23,7 +24,7 @@ var defaultWeight = 1
2324

2425
func TestStreamClient(t *testing.T) {
2526
httpClient := &http.Client{}
26-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
27+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
2728

2829
if err != nil {
2930
t.Fatalf("Error when creating a client: %v", err)
@@ -168,7 +169,7 @@ func TestStreamClient(t *testing.T) {
168169

169170
func TestStreamUpstreamServer(t *testing.T) {
170171
httpClient := &http.Client{}
171-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
172+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
172173
if err != nil {
173174
t.Fatalf("Error connecting to nginx: %v", err)
174175
}
@@ -213,7 +214,7 @@ func TestStreamUpstreamServer(t *testing.T) {
213214

214215
func TestClient(t *testing.T) {
215216
httpClient := &http.Client{}
216-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
217+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
217218

218219
if err != nil {
219220
t.Fatalf("Error when creating a client: %v", err)
@@ -365,7 +366,7 @@ func TestClient(t *testing.T) {
365366

366367
func TestUpstreamServer(t *testing.T) {
367368
httpClient := &http.Client{}
368-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
369+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
369370
if err != nil {
370371
t.Fatalf("Error connecting to nginx: %v", err)
371372
}
@@ -410,7 +411,7 @@ func TestUpstreamServer(t *testing.T) {
410411

411412
func TestStats(t *testing.T) {
412413
httpClient := &http.Client{}
413-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
414+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
414415
if err != nil {
415416
t.Fatalf("Error connecting to nginx: %v", err)
416417
}
@@ -509,7 +510,7 @@ func TestStats(t *testing.T) {
509510

510511
func TestUpstreamServerDefaultParameters(t *testing.T) {
511512
httpClient := &http.Client{}
512-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
513+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
513514
if err != nil {
514515
t.Fatalf("Error connecting to nginx: %v", err)
515516
}
@@ -552,7 +553,7 @@ func TestUpstreamServerDefaultParameters(t *testing.T) {
552553

553554
func TestStreamStats(t *testing.T) {
554555
httpClient := &http.Client{}
555-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
556+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
556557
if err != nil {
557558
t.Fatalf("Error connecting to nginx: %v", err)
558559
}
@@ -566,7 +567,7 @@ func TestStreamStats(t *testing.T) {
566567
}
567568

568569
// make connection so we have stream server zone stats - ignore response
569-
_, err = net.Dial("tcp", "127.0.0.1:8081")
570+
_, err = net.Dial("tcp", helpers.GetStreamAddress())
570571
if err != nil {
571572
t.Errorf("Error making tcp connection: %v", err)
572573
}
@@ -622,7 +623,7 @@ func TestStreamStats(t *testing.T) {
622623

623624
func TestStreamUpstreamServerDefaultParameters(t *testing.T) {
624625
httpClient := &http.Client{}
625-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
626+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
626627
if err != nil {
627628
t.Fatalf("Error connecting to nginx: %v", err)
628629
}
@@ -666,7 +667,7 @@ func TestStreamUpstreamServerDefaultParameters(t *testing.T) {
666667
func TestKeyValue(t *testing.T) {
667668
zoneName := "zone_one"
668669
httpClient := &http.Client{}
669-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
670+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
670671
if err != nil {
671672
t.Fatalf("Error connecting to nginx: %v", err)
672673
}
@@ -764,7 +765,7 @@ func TestKeyValue(t *testing.T) {
764765
func TestKeyValueStream(t *testing.T) {
765766
zoneName := "zone_one_stream"
766767
httpClient := &http.Client{}
767-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
768+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
768769
if err != nil {
769770
t.Fatalf("Error connecting to nginx: %v", err)
770771
}
@@ -860,12 +861,12 @@ func TestKeyValueStream(t *testing.T) {
860861
}
861862

862863
func TestStreamZoneSync(t *testing.T) {
863-
c1, err := client.NewNginxClient(&http.Client{}, "http://127.0.0.1:8080/api")
864+
c1, err := client.NewNginxClient(&http.Client{}, helpers.GetAPIEndpoint())
864865
if err != nil {
865866
t.Fatalf("Error connecting to nginx: %v", err)
866867
}
867868

868-
c2, err := client.NewNginxClient(&http.Client{}, "http://127.0.0.1:8090/api")
869+
c2, err := client.NewNginxClient(&http.Client{}, helpers.GetAPIEndpointOfHelper())
869870
if err != nil {
870871
t.Fatalf("Error connecting to nginx: %v", err)
871872
}
@@ -987,7 +988,7 @@ func compareStreamUpstreamServers(x []client.StreamUpstreamServer, y []client.St
987988

988989
func TestUpstreamServerWithDrain(t *testing.T) {
989990
httpClient := &http.Client{}
990-
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
991+
c, err := client.NewNginxClient(httpClient, helpers.GetAPIEndpoint())
991992
if err != nil {
992993
t.Fatalf("Error connecting to nginx: %v", err)
993994
}

tests/helpers/env_variables.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package helpers
2+
3+
import "os"
4+
5+
// GetAPIEndpoint returns the api endpoint.
6+
// For testing purposes only. The endpoint is set in the Makefile.
7+
func GetAPIEndpoint() string {
8+
ep := os.Getenv("TEST_API_ENDPOINT")
9+
if ep == "" {
10+
panic("TEST_API_ENDPOINT env variable is not set or empty")
11+
}
12+
return ep
13+
}
14+
15+
// GetAPIEndpointOfHelper returns the api endpoint of the helper.
16+
// For testing purposes only. The endpoint is set in the Makefile.
17+
func GetAPIEndpointOfHelper() string {
18+
ep := os.Getenv("TEST_API_ENDPOINT_OF_HELPER")
19+
if ep == "" {
20+
panic("TEST_API_ENDPOINT_OF_HELPER env variable is not set or empty")
21+
}
22+
return ep
23+
}
24+
25+
// GetStreamAddress returns the address of the unavailable stream server.
26+
// For testing purposes only. The address is set in the Makefile.
27+
func GetStreamAddress() string {
28+
addr := os.Getenv("TEST_UNAVAILABLE_STREAM_ADDRESS")
29+
if addr == "" {
30+
panic("TEST_UNAVAILABLE_STREAM_ADDRESS env variable is not set or empty")
31+
}
32+
return addr
33+
}

0 commit comments

Comments
 (0)