-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkswp_test.go
57 lines (50 loc) · 1.29 KB
/
kswp_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
package kswp
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestSwap(t *testing.T) {
dir, err := ioutil.TempDir("", "TestSwap")
if err != nil {
t.Fatalf("Could not create temp dir: %s", err)
}
defer os.RemoveAll(dir) // clean up
k := Kswp{
KubeConf: filepath.Join(dir, "conf"),
Configs: []KubeConfig{
{Name: "foo"},
{Name: "bar"},
{Name: "buzz"},
{Name: "none", Path: "/tmp/does/not/exist"},
},
}
for i, c := range k.Configs {
if c.Path == "" {
tmpfn := filepath.Join(dir, c.Name)
if err := ioutil.WriteFile(tmpfn, []byte(c.Name), 0666); err != nil {
t.Fatalf("Could not create temp file: %s", err)
}
k.Configs[i].Path = tmpfn
}
}
for _, c := range k.Configs[0:3] {
if err := k.Swap(c.Name); err != nil {
t.Errorf("Swap for %s failed with \n%s", c.Name, err.Error())
} else {
file, err := ioutil.ReadFile(k.KubeConf)
if err != nil {
t.Errorf("Could not read config file: %s", err)
} else if string(file) != c.Name {
t.Errorf("Config not written. Expected %s got %s", c.Name, string(file))
}
}
}
if err := k.Swap(k.Configs[3].Name); err == nil {
t.Errorf("Swap for %s should have failed", k.Configs[3].Name)
}
if err := k.Swap("wrong name"); err == nil {
t.Errorf("Swap for %s should have failed", "wrong name")
}
}