forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
95 lines (87 loc) · 2.37 KB
/
config_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
package config
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestWriteDefaultConf(t *testing.T) {
conf := &Config{}
os.Unsetenv("TYK_GW_LISTENPORT")
defer os.Unsetenv("TYK_GW_LISTENPORT")
if err := WriteDefault("", conf); err != nil {
t.Fatal(err)
}
if conf.ListenPort != 8080 {
t.Fatalf("Expected ListenPort to be set to its default")
}
*conf = Config{}
os.Setenv("TYK_GW_LISTENPORT", "9090")
if err := WriteDefault("", conf); err != nil {
t.Fatal(err)
}
if conf.ListenPort != 9090 {
t.Fatalf("Expected ListenPort to be set to 9090")
}
}
func TestConfigFiles(t *testing.T) {
dir, err := ioutil.TempDir("", "tyk")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
conf := &Config{}
path1 := filepath.Join(dir, "tyk1.conf")
path2 := filepath.Join(dir, "tyk2.conf")
if err := WriteDefault(path1, conf); err != nil {
t.Fatal(err)
}
if conf.ListenPort != 8080 {
t.Fatalf("Expected ListenPort to be set to its default")
}
bs, _ := ioutil.ReadFile(path1)
if !strings.Contains(string(bs), "8080") {
t.Fatalf("Expected 8080 to be in the written conf file")
}
os.Remove(path1)
paths := []string{path1, path2}
// should write default config to path1 and return nil
if err := Load(paths, conf); err != nil {
t.Fatalf("Load with no existing configs errored")
}
if _, err := os.Stat(path1); err != nil {
t.Fatalf("Load with no configs did not write a default config file")
}
if _, err := os.Stat(path2); err == nil {
t.Fatalf("Load with no configs wrote too many default config files")
}
if conf.OriginalPath != path1 {
t.Fatalf("OriginalPath was not set properly")
}
// both exist, we use path1
os.Link(path1, path2)
if err := Load(paths, conf); err != nil {
t.Fatalf("Load with an existing config errored")
}
if conf.OriginalPath != path1 {
t.Fatalf("OriginalPath was not set properly")
}
// path2 exists but path1 doesn't
os.Remove(path1)
if err := Load(paths, conf); err != nil {
t.Fatalf("Load with an existing config errored")
}
if _, err := os.Stat(path1); err == nil {
t.Fatalf("Load with a config wrote a default config file")
}
if conf.OriginalPath != path2 {
t.Fatalf("OriginalPath was not set properly")
}
// path1 exists but is invalid
os.Remove(path2)
ioutil.WriteFile(path1, []byte("{"), 0644)
if err := Load(paths, conf); err == nil {
t.Fatalf("Load with an invalid config did not error")
}
}