-
Notifications
You must be signed in to change notification settings - Fork 18
/
dnsconfig_test.go
100 lines (93 loc) · 2.46 KB
/
dnsconfig_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
package main
import (
"io/ioutil"
"log"
"os"
"path"
"strings"
"testing"
)
func createTempFileWithContents(content string) string {
file, err := ioutil.TempFile("", "*.json")
if err != nil {
log.Fatal(err)
}
_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
return file.Name()
}
func Test_updateDockerConfig(t *testing.T) {
type args struct {
configPath string
}
tmpFileName := createTempFileWithContents("{ \"cgroup-parent\": \"/actions_job\"}")
mockDockerConfigPath, err := ioutil.TempDir("", "")
if err != nil {
log.Fatal(err)
}
mockDockerConfigPath = path.Join(mockDockerConfigPath, "test.json")
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "existing file",
args: args{configPath: tmpFileName},
want: "{\"cgroup-parent\":\"/actions_job\",\"dns\":[\"172.17.0.1\"],\"live-restore\":true}",
wantErr: false},
{name: "non existent file",
args: args{configPath: mockDockerConfigPath},
want: "{\"dns\":[\"172.17.0.1\"],\"live-restore\":true}",
wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := updateDockerConfig(tt.args.configPath); (err != nil) != tt.wantErr {
t.Errorf("updateDockerConfig() error = %v, wantErr %v", err, tt.wantErr)
}
content, err := ioutil.ReadFile(tt.args.configPath)
if err != nil {
log.Fatal(err)
}
if strings.Compare(string(content), tt.want) != 0 {
t.Errorf("updateDockerConfig() = %s, want %s", string(content), tt.want)
}
defer os.Remove(tt.args.configPath)
})
}
}
func Test_writeResolveConfig(t *testing.T) {
type args struct {
configPath string
}
tmpFileName := createTempFileWithContents("Existing DNS settings")
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "overwrite file",
args: args{configPath: tmpFileName},
want: "[Resolve]\nDNS=127.0.0.1\nDomains=~.\n",
wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := writeResolveConfig(tt.args.configPath); (err != nil) != tt.wantErr {
t.Errorf("writeResolveConfig() error = %v, wantErr %v", err, tt.wantErr)
}
content, err := ioutil.ReadFile(tt.args.configPath)
if err != nil {
log.Fatal(err)
}
if strings.Compare(string(content), tt.want) != 0 {
t.Errorf("writeResolveConfig() = %s, want %s", string(content), tt.want)
}
defer os.Remove(tt.args.configPath)
})
}
}