@@ -10,107 +10,98 @@ import (
10
10
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
11
11
)
12
12
13
- func newTestGenerator () * systemdConfigGenerator {
14
- return & systemdConfigGenerator {
15
- template : templates .Lookup ("systemd.conf.tmpl" ),
16
- }
17
- }
18
-
19
- func TestGenerate (t * testing.T ) {
20
- g := NewWithT (t )
21
-
22
- content , err := newTestGenerator ().Generate (HTTPProxyVariables {
23
- HTTP : "http://example.com" ,
24
- HTTPS : "https://example.com" ,
25
- NO : []string {
26
- "https://no-proxy.example.com" ,
13
+ func TestGenerateSystemdFiles (t * testing.T ) {
14
+ t .Parallel ()
15
+
16
+ tests := []struct {
17
+ name string
18
+ vars HTTPProxyVariables
19
+ expectedContents string
20
+ }{{
21
+ name : "no proxy configuration" ,
22
+ }, {
23
+ name : "all vars set" ,
24
+ vars : HTTPProxyVariables {
25
+ HTTP : "http://example.com" ,
26
+ HTTPS : "https://example.com" ,
27
+ NO : []string {
28
+ "https://no-proxy.example.com" ,
29
+ },
27
30
},
28
- })
29
- g .Expect (err ).NotTo (HaveOccurred ())
30
- g .Expect (content ).To (And (
31
- ContainSubstring (`Environment="HTTP_PROXY=http://example.com"` ),
32
- ContainSubstring (`Environment="http_proxy=http://example.com"` ),
33
- ContainSubstring (`Environment="HTTPS_PROXY=https://example.com"` ),
34
- ContainSubstring (`Environment="https_proxy=https://example.com"` ),
35
- ContainSubstring (`Environment="NO_PROXY=https://no-proxy.example.com"` ),
36
- ContainSubstring (`Environment="no_proxy=https://no-proxy.example.com"` ),
37
- ))
38
- }
39
-
40
- func TestGenerate_OnlyHTTP (t * testing.T ) {
41
- g := NewWithT (t )
42
-
43
- content , err := newTestGenerator ().Generate (HTTPProxyVariables {
44
- HTTP : "http://example.com" ,
45
- })
46
- g .Expect (err ).NotTo (HaveOccurred ())
47
- g .Expect (content ).To (And (
48
- ContainSubstring (`Environment="HTTP_PROXY=http://example.com"` ),
49
- ContainSubstring (`Environment="http_proxy=http://example.com"` ),
50
- Not (ContainSubstring (`Environment="HTTPS_PROXY=` )),
51
- Not (ContainSubstring (`Environment="https_proxy=` )),
52
- Not (ContainSubstring (`Environment="NO_PROXY=` )),
53
- Not (ContainSubstring (`Environment="no_proxy=` )),
54
- ))
55
- }
56
-
57
- func TestGenerate_OnlyHTTPS (t * testing.T ) {
58
- g := NewWithT (t )
59
-
60
- content , err := newTestGenerator ().Generate (HTTPProxyVariables {
61
- HTTPS : "https://example.com" ,
62
- })
63
- g .Expect (err ).NotTo (HaveOccurred ())
64
- g .Expect (content ).To (And (
65
- Not (ContainSubstring (`Environment="HTTP_PROXY=http://example.com"` )),
66
- Not (ContainSubstring (`Environment="http_proxy=http://example.com"` )),
67
- ContainSubstring (`Environment="HTTPS_PROXY=https://example.com"` ),
68
- ContainSubstring (`Environment="https_proxy=https://example.com"` ),
69
- Not (ContainSubstring (`Environment="NO_PROXY=https://no-proxy.example.com"` )),
70
- Not (ContainSubstring (`Environment="no_proxy=https://no-proxy.example.com"` )),
71
- ))
72
- }
73
-
74
- func TestGenerate_OnlyNoProxy (t * testing.T ) {
75
- g := NewWithT (t )
76
-
77
- content , err := newTestGenerator ().Generate (HTTPProxyVariables {
78
- NO : []string {"https://no-proxy.example.com" },
79
- })
80
- g .Expect (err ).NotTo (HaveOccurred ())
81
- g .Expect (content ).To (And (
82
- Not (ContainSubstring (`Environment="HTTP_PROXY="` )),
83
- Not (ContainSubstring (`Environment="http_proxy="` )),
84
- Not (ContainSubstring (`Environment="HTTPS_PROXY="` )),
85
- Not (ContainSubstring (`Environment="https_proxy=` )),
86
- ContainSubstring (`Environment="NO_PROXY=https://no-proxy.example.com"` ),
87
- ContainSubstring (`Environment="no_proxy=https://no-proxy.example.com"` ),
88
- ))
89
- }
90
-
91
- func TestGenerate_NoProxyMultipleURLs (t * testing.T ) {
92
- g := NewWithT (t )
93
-
94
- content , err := newTestGenerator ().Generate (HTTPProxyVariables {
95
- NO : []string {
96
- "https://no-proxy.example.com" ,
97
- "https://no-proxy-1.example.com" ,
31
+ expectedContents : `[Service]
32
+ Environment="HTTP_PROXY=http://example.com"
33
+ Environment="http_proxy=http://example.com"
34
+ Environment="HTTPS_PROXY=https://example.com"
35
+ Environment="https_proxy=https://example.com"
36
+ Environment="NO_PROXY=https://no-proxy.example.com"
37
+ Environment="no_proxy=https://no-proxy.example.com"
38
+ ` ,
39
+ }, {
40
+ name : "http only" ,
41
+ vars : HTTPProxyVariables {
42
+ HTTP : "http://example.com" ,
98
43
},
99
- })
100
- g .Expect (err ).NotTo (HaveOccurred ())
101
- g .Expect (content ).To (And (
102
- ContainSubstring (
103
- `Environment="NO_PROXY=https://no-proxy.example.com,https://no-proxy-1.example.com"` ,
104
- ),
105
- ContainSubstring (
106
- `Environment="no_proxy=https://no-proxy.example.com,https://no-proxy-1.example.com"` ,
107
- ),
108
- ))
109
- }
110
-
111
- func TestAddSystemdFiles (t * testing.T ) {
112
- g := NewWithT (t )
113
-
114
- dst := []bootstrapv1.File {}
115
- g .Expect (newTestGenerator ().AddSystemdFiles (HTTPProxyVariables {}, dst )).To (HaveLen (2 ))
44
+ expectedContents : `[Service]
45
+ Environment="HTTP_PROXY=http://example.com"
46
+ Environment="http_proxy=http://example.com"
47
+ ` ,
48
+ }, {
49
+ name : "https only" ,
50
+ vars : HTTPProxyVariables {
51
+ HTTPS : "https://example.com" ,
52
+ },
53
+ expectedContents : `[Service]
54
+ Environment="HTTPS_PROXY=https://example.com"
55
+ Environment="https_proxy=https://example.com"
56
+ ` ,
57
+ }, {
58
+ name : "no proxy only" ,
59
+ vars : HTTPProxyVariables {
60
+ NO : []string {
61
+ "https://no-proxy.example.com" ,
62
+ },
63
+ },
64
+ expectedContents : `[Service]
65
+ Environment="NO_PROXY=https://no-proxy.example.com"
66
+ Environment="no_proxy=https://no-proxy.example.com"
67
+ ` ,
68
+ }, {
69
+ name : "multiple no proxy only" ,
70
+ vars : HTTPProxyVariables {
71
+ NO : []string {
72
+ "https://no-proxy.example.com" ,
73
+ "https://no-proxy-1.example.com" ,
74
+ },
75
+ },
76
+ expectedContents : `[Service]
77
+ Environment="NO_PROXY=https://no-proxy.example.com,https://no-proxy-1.example.com"
78
+ Environment="no_proxy=https://no-proxy.example.com,https://no-proxy-1.example.com"
79
+ ` ,
80
+ }}
81
+
82
+ for idx := range tests {
83
+ tt := tests [idx ]
84
+ t .Run (tt .name , func (t * testing.T ) {
85
+ t .Parallel ()
86
+
87
+ g := NewWithT (t )
88
+
89
+ var expectedFiles []bootstrapv1.File
90
+ if tt .expectedContents != "" {
91
+ expectedFiles = []bootstrapv1.File {{
92
+ Path : systemdUnitPaths [0 ],
93
+ Content : tt .expectedContents ,
94
+ Permissions : "0640" ,
95
+ Owner : "root" ,
96
+ }, {
97
+ Path : systemdUnitPaths [1 ],
98
+ Content : tt .expectedContents ,
99
+ Permissions : "0640" ,
100
+ Owner : "root" ,
101
+ }}
102
+ }
103
+
104
+ g .Expect (generateSystemdFiles (tt .vars )).Should (Equal (expectedFiles ))
105
+ })
106
+ }
116
107
}
0 commit comments