@@ -17,11 +17,15 @@ limitations under the License.
17
17
package controller
18
18
19
19
import (
20
+ "context"
21
+ "os"
22
+ "path/filepath"
20
23
"testing"
21
24
22
25
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
23
26
"github.com/k0sproject/k0s/pkg/config"
24
27
28
+ "github.com/stretchr/testify/assert"
25
29
"github.com/stretchr/testify/require"
26
30
"sigs.k8s.io/yaml"
27
31
)
@@ -34,54 +38,61 @@ func (i inMemorySaver) Save(dst string, content []byte) error {
34
38
}
35
39
36
40
func TestCalicoManifests (t * testing.T ) {
37
- k0sVars , err := config .NewCfgVars (nil , t .TempDir ())
38
- require .NoError (t , err )
41
+ newTestInstance := func (t * testing.T ) * Calico {
42
+ k0sVars , err := config .NewCfgVars (nil , t .TempDir ())
43
+ require .NoError (t , err )
44
+ calico := NewCalico (k0sVars )
45
+ require .NoError (t , calico .Init (context .TODO ()))
46
+ require .NoError (t , calico .Start (context .TODO ()))
47
+ t .Cleanup (func () { assert .NoError (t , calico .Stop ()) })
48
+ return calico
49
+ }
50
+
39
51
clusterConfig := v1beta1 .DefaultClusterConfig ()
40
52
clusterConfig .Spec .Network .Calico = v1beta1 .DefaultCalico ()
41
53
clusterConfig .Spec .Network .Provider = "calico"
42
54
clusterConfig .Spec .Network .KubeRouter = nil
43
55
44
56
t .Run ("must_write_only_non_crd_on_change" , func (t * testing.T ) {
45
- saver := inMemorySaver {}
46
- crdSaver := inMemorySaver {}
47
- calico := NewCalico (k0sVars , crdSaver , saver )
57
+ calico := newTestInstance (t )
48
58
49
- _ = calico .processConfigChanges (calicoConfig {})
59
+ assert . NoError ( t , calico .processConfigChanges (calicoConfig {}) )
50
60
51
- for k := range saver {
52
- require .NotContains (t , k , "calico-crd" )
61
+ if entries , err := os .ReadDir (filepath .Join (calico .k0sVars .ManifestsDir , "calico" )); assert .NoError (t , err ) {
62
+ assert .NotEmpty (t , entries )
63
+ for _ , entry := range entries {
64
+ assert .NotContains (t , entry .Name (), "calico-crd" )
65
+ }
66
+ }
67
+ if entries , err := os .ReadDir (filepath .Join (calico .k0sVars .ManifestsDir , "calico_init" )); assert .NoError (t , err ) {
68
+ assert .Empty (t , entries )
53
69
}
54
- require .Len (t , crdSaver , 0 )
55
70
})
56
71
57
72
t .Run ("must_have_wireguard_enabled_if_config_has" , func (t * testing.T ) {
58
73
clusterConfig .Spec .Network .Calico .EnableWireguard = true
59
- saver := inMemorySaver {}
60
- crdSaver := inMemorySaver {}
61
- calico := NewCalico (k0sVars , crdSaver , saver )
74
+ calico := newTestInstance (t )
62
75
cfg , err := calico .getConfig (clusterConfig )
63
76
require .NoError (t , err )
64
77
require .NoError (t , calico .processConfigChanges (cfg ))
65
78
66
- daemonSetManifestRaw , foundRaw := saver [ "calico-DaemonSet-calico-node.yaml" ]
67
- require .True (t , foundRaw , "must have daemon set for calico" )
79
+ daemonSetManifestRaw , err := os . ReadFile ( filepath . Join ( calico . k0sVars . ManifestsDir , "calico" , "calico -DaemonSet-calico-node.yaml"))
80
+ require .NoError (t , err , "must have daemon set for calico" )
68
81
spec := daemonSetContainersEnv {}
69
82
require .NoError (t , yaml .Unmarshal (daemonSetManifestRaw , & spec ))
70
83
spec .RequireContainerHasEnvVariable (t , "calico-node" , "FELIX_WIREGUARDENABLED" , "true" )
71
84
})
72
85
73
86
t .Run ("must_not_have_wireguard_enabled_if_config_has_no" , func (t * testing.T ) {
74
87
clusterConfig .Spec .Network .Calico .EnableWireguard = false
75
- saver := inMemorySaver {}
76
- crdSaver := inMemorySaver {}
77
- calico := NewCalico (k0sVars , crdSaver , saver )
88
+ calico := newTestInstance (t )
78
89
79
90
cfg , err := calico .getConfig (clusterConfig )
80
91
require .NoError (t , err )
81
92
_ = calico .processConfigChanges (cfg )
82
93
83
- daemonSetManifestRaw , foundRaw := saver [ "calico-DaemonSet-calico-node.yaml" ]
84
- require .True (t , foundRaw , "must have daemon set for calico" )
94
+ daemonSetManifestRaw , err := os . ReadFile ( filepath . Join ( calico . k0sVars . ManifestsDir , "calico" , "calico -DaemonSet-calico-node.yaml"))
95
+ require .NoError (t , err , "must have daemon set for calico" )
85
96
spec := daemonSetContainersEnv {}
86
97
require .NoError (t , yaml .Unmarshal (daemonSetManifestRaw , & spec ))
87
98
spec .RequireContainerHasNoEnvVariable (t , "calico-node" , "FELIX_WIREGUARDENABLED" )
@@ -90,18 +101,16 @@ func TestCalicoManifests(t *testing.T) {
90
101
t .Run ("ip_autodetection" , func (t * testing.T ) {
91
102
t .Run ("use_IPAutodetectionMethod_for_both_families_by_default" , func (t * testing.T ) {
92
103
clusterConfig .Spec .Network .Calico .IPAutodetectionMethod = "somemethod"
93
- saver := inMemorySaver {}
94
- crdSaver := inMemorySaver {}
95
- calico := NewCalico (k0sVars , crdSaver , saver )
104
+ calico := newTestInstance (t )
96
105
templateContext , err := calico .getConfig (clusterConfig )
97
106
require .NoError (t , err )
98
107
require .Equal (t , clusterConfig .Spec .Network .Calico .IPAutodetectionMethod , templateContext .IPAutodetectionMethod )
99
108
require .Equal (t , templateContext .IPV6AutodetectionMethod , templateContext .IPV6AutodetectionMethod )
100
109
cfg , err := calico .getConfig (clusterConfig )
101
110
require .NoError (t , err )
102
111
_ = calico .processConfigChanges (cfg )
103
- daemonSetManifestRaw , foundRaw := saver [ "calico-DaemonSet-calico-node.yaml" ]
104
- require .True (t , foundRaw , "must have daemon set for calico" )
112
+ daemonSetManifestRaw , err := os . ReadFile ( filepath . Join ( calico . k0sVars . ManifestsDir , "calico" , "calico -DaemonSet-calico-node.yaml"))
113
+ require .NoError (t , err , "must have daemon set for calico" )
105
114
106
115
spec := daemonSetContainersEnv {}
107
116
require .NoError (t , yaml .Unmarshal (daemonSetManifestRaw , & spec ))
@@ -111,19 +120,17 @@ func TestCalicoManifests(t *testing.T) {
111
120
t .Run ("use_IPV6AutodetectionMethod_for_ipv6_if_specified" , func (t * testing.T ) {
112
121
clusterConfig .Spec .Network .Calico .IPAutodetectionMethod = "somemethod"
113
122
clusterConfig .Spec .Network .Calico .IPv6AutodetectionMethod = "anothermethod"
114
- saver := inMemorySaver {}
115
- crdSaver := inMemorySaver {}
116
- calico := NewCalico (k0sVars , crdSaver , saver )
123
+ calico := newTestInstance (t )
117
124
templateContext , err := calico .getConfig (clusterConfig )
118
125
require .NoError (t , err )
119
126
require .Equal (t , clusterConfig .Spec .Network .Calico .IPAutodetectionMethod , templateContext .IPAutodetectionMethod )
120
127
require .Equal (t , clusterConfig .Spec .Network .Calico .IPv6AutodetectionMethod , templateContext .IPV6AutodetectionMethod )
121
128
cfg , err := calico .getConfig (clusterConfig )
122
129
require .NoError (t , err )
123
130
_ = calico .processConfigChanges (cfg )
124
- daemonSetManifestRaw , foundRaw := saver ["calico-DaemonSet-calico-node.yaml" ]
131
+ daemonSetManifestRaw , err := os .ReadFile (filepath .Join (calico .k0sVars .ManifestsDir , "calico" , "calico-DaemonSet-calico-node.yaml" ))
132
+ require .NoError (t , err , "must have daemon set for calico" )
125
133
126
- require .True (t , foundRaw , "must have daemon set for calico" )
127
134
spec := daemonSetContainersEnv {}
128
135
require .NoError (t , yaml .Unmarshal (daemonSetManifestRaw , & spec ))
129
136
spec .RequireContainerHasEnvVariable (t , "calico-node" , "IP6_AUTODETECTION_METHOD" , templateContext .IPV6AutodetectionMethod )
0 commit comments