forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopentracing_custom_env_loader_test.go
124 lines (117 loc) · 2.82 KB
/
opentracing_custom_env_loader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package config
import (
"fmt"
"os"
"testing"
jaeger "github.com/uber/jaeger-client-go/config"
)
func TestLoadZipkin(t *testing.T) {
base := ZipkinConfig{
Reporter: Reporter{
URL: "repoturl",
BatchSize: 10,
MaxBacklog: 20,
},
Sampler: Sampler{
Name: "boundary",
Rate: 10.1,
Salt: 10,
Mod: 12,
},
}
sample := []struct {
env string
value string
}{
{"TYK_GW_TRACER_OPTIONS_REPORTER_URL", base.Reporter.URL},
{"TYK_GW_TRACER_OPTIONS_REPORTER_BATCHSIZE", fmt.Sprint(base.Reporter.BatchSize)},
{"TYK_GW_TRACER_OPTIONS_REPORTER_MAXBACKLOG", fmt.Sprint(base.Reporter.MaxBacklog)},
{"TYK_GW_TRACER_OPTIONS_SAMPLER_NAME", base.Sampler.Name},
{"TYK_GW_TRACER_OPTIONS_SAMPLER_SALT", fmt.Sprint(base.Sampler.Salt)},
{"TYK_GW_TRACER_OPTIONS_SAMPLER_MOD", fmt.Sprint(base.Sampler.Mod)},
}
t.Run("Returns nil when it is not zipkin config", func(t *testing.T) {
conf := &Config{}
err := loadZipkin(envPrefix, conf)
if err != nil {
t.Fatal(err)
}
if conf.Tracer.Options != nil {
t.Error("expected options to be nil")
}
})
t.Run("loads env vars", func(t *testing.T) {
for _, v := range sample {
err := os.Setenv(v.env, v.value)
if err != nil {
t.Fatal(err)
}
}
defer func() {
for _, v := range sample {
os.Unsetenv(v.env)
}
}()
conf := &Config{Tracer: Tracer{Name: "zipkin"}}
err := loadZipkin(envPrefix, conf)
if err != nil {
t.Fatal(err)
}
var got ZipkinConfig
err = DecodeJSON(&got, conf.Tracer.Options)
if err != nil {
t.Fatal(err)
}
if base.Reporter.URL != got.Reporter.URL {
t.Errorf("expected %#v got %#v", base.Reporter.URL, got.Reporter.URL)
}
if base.Sampler.Name != got.Sampler.Name {
t.Errorf("expected %#v got %#v", base.Sampler.Name, got.Sampler.Name)
}
})
}
func TestLoadJaeger(t *testing.T) {
base := &jaeger.Configuration{ServiceName: "jaeger-test-service"}
sample := []struct {
env string
value string
}{
{"TYK_GW_TRACER_OPTIONS_SERVICENAME", base.ServiceName},
}
t.Run("Returns nil when it is not jaeger config", func(t *testing.T) {
conf := &Config{}
err := loadJaeger(envPrefix, conf)
if err != nil {
t.Fatal(err)
}
if conf.Tracer.Options != nil {
t.Error("expected options to be nil")
}
})
t.Run("Loads env vars", func(t *testing.T) {
for _, v := range sample {
err := os.Setenv(v.env, v.value)
if err != nil {
t.Fatal(err)
}
}
defer func() {
for _, v := range sample {
os.Unsetenv(v.env)
}
}()
conf := &Config{Tracer: Tracer{Name: "jaeger"}}
err := loadJaeger(envPrefix, conf)
if err != nil {
t.Fatal(err)
}
var got jaeger.Configuration
err = DecodeJSON(&got, conf.Tracer.Options)
if err != nil {
t.Fatal(err)
}
if base.ServiceName != got.ServiceName {
t.Errorf("expected %#v got %#v", base.ServiceName, got.ServiceName)
}
})
}