Skip to content

Commit 24d77f1

Browse files
authored
test: clean up cns state check tests (#2082)
1 parent 2dff809 commit 24d77f1

File tree

4 files changed

+90
-109
lines changed

4 files changed

+90
-109
lines changed

test/integration/load/load_test.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package load
55
import (
66
"context"
77
"flag"
8+
"fmt"
89
"testing"
910
"time"
1011

@@ -35,6 +36,13 @@ var noopDeploymentMap = map[string]string{
3536
"linux": manifestDir + "/noop-deployment-linux.yaml",
3637
}
3738

39+
// Todo: Add the validation for the data path function for the linux/windows client.
40+
type stateValidator interface {
41+
ValidateStateFile(context.Context) error
42+
ValidateRestartNetwork(context.Context) error
43+
// ValidateDataPath() error
44+
}
45+
3846
/*
3947
In order to run the scale tests, you need a k8s cluster and its kubeconfig.
4048
If no kubeconfig is passed, the test will attempt to find one in the default location for kubectl config.
@@ -131,21 +139,39 @@ func TestValidateState(t *testing.T) {
131139
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
132140
defer cancel()
133141

142+
var validator stateValidator
143+
134144
t.Log("Validating the state file")
135-
validatorClient := validate.GetValidatorClient(*osType)
136-
validator := validatorClient.CreateClient(ctx, clientset, config, namespace, *cniType, *restartCase)
145+
switch *osType {
146+
case "linux":
147+
validator, err = validate.CreateLinuxValidator(ctx, clientset, config, namespace, *cniType, *restartCase)
148+
if err != nil {
149+
t.Fatal(err)
150+
}
151+
case "windows":
152+
validator, err = validate.CreateWindowsValidator(ctx, clientset, config, namespace, *cniType, *restartCase)
153+
if err != nil {
154+
t.Fatal(err)
155+
}
156+
default:
157+
t.Fatalf("unknown os type %s", *osType)
158+
}
137159

138-
err = validator.ValidateStateFile()
160+
err = validator.ValidateStateFile(ctx)
139161
if err != nil {
140162
t.Fatal(err)
141163
}
142164

143-
//We are restarting the systmemd network and checking that the connectivity works after the restart. For more details: https://github.com/cilium/cilium/issues/18706
165+
// We are restarting the systmemd network and checking that the connectivity works after the restart. For more details: https://github.com/cilium/cilium/issues/18706
144166
t.Log("Validating the restart network scenario")
145-
err = validator.ValidateRestartNetwork()
146-
if err != nil {
147-
t.Fatal(err)
148-
}
167+
t.Run(fmt.Sprintf("validate network restart - %s", *osType), func(t *testing.T) {
168+
if *osType == "windows" {
169+
t.Skip("validate network restart not implemented on Windows")
170+
}
171+
if err := validator.ValidateRestartNetwork(ctx); err != nil {
172+
t.Fatal(err)
173+
}
174+
})
149175
}
150176

151177
// TestScaleDeployment scales the deployment up/down based on the replicas passed.

test/validate/client.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/validate/linux_validate.go

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ var (
3131

3232
type stateFileIpsFunc func([]byte) (map[string]string, error)
3333

34-
type LinuxClient struct{}
35-
3634
type LinuxValidator struct {
37-
Validator
35+
clientset *kubernetes.Clientset
36+
config *rest.Config
37+
namespace string
38+
cni string
39+
restartCase bool
3840
}
3941

4042
type CnsState struct {
@@ -62,35 +64,31 @@ type Address struct {
6264
Addr string `json:"ipv4"`
6365
}
6466

65-
func (l *LinuxClient) CreateClient(ctx context.Context, clienset *kubernetes.Clientset, config *rest.Config, namespace, cni string, restartCase bool) IValidator {
67+
func CreateLinuxValidator(ctx context.Context, clienset *kubernetes.Clientset, config *rest.Config, namespace, cni string, restartCase bool) (*LinuxValidator, error) {
6668
// deploy privileged pod
6769
privilegedDaemonSet, err := k8sutils.MustParseDaemonSet(privilegedDaemonSetPath)
6870
if err != nil {
69-
panic(err)
71+
return nil, errors.Wrap(err, "unable to parse daemonset")
7072
}
7173
daemonsetClient := clienset.AppsV1().DaemonSets(privilegedNamespace)
72-
err = k8sutils.MustCreateDaemonset(ctx, daemonsetClient, privilegedDaemonSet)
73-
if err != nil {
74-
panic(err)
74+
if err := k8sutils.MustCreateDaemonset(ctx, daemonsetClient, privilegedDaemonSet); err != nil {
75+
return nil, errors.Wrap(err, "unable to create daemonset")
7576
}
76-
err = k8sutils.WaitForPodsRunning(ctx, clienset, privilegedNamespace, privilegedLabelSelector)
77-
if err != nil {
78-
panic(err)
77+
if err := k8sutils.WaitForPodsRunning(ctx, clienset, privilegedNamespace, privilegedLabelSelector); err != nil {
78+
return nil, errors.Wrap(err, "error while waiting for pods to be running")
7979
}
80+
8081
return &LinuxValidator{
81-
Validator: Validator{
82-
ctx: ctx,
83-
clientset: clienset,
84-
config: config,
85-
namespace: namespace,
86-
cni: cni,
87-
restartCase: restartCase,
88-
},
89-
}
82+
clientset: clienset,
83+
config: config,
84+
namespace: namespace,
85+
cni: cni,
86+
restartCase: restartCase,
87+
}, nil
9088
}
9189

9290
// Todo: Based on cni version validate different state files
93-
func (v *LinuxValidator) ValidateStateFile() error {
91+
func (v *LinuxValidator) ValidateStateFile(ctx context.Context) error {
9492
checkSet := make(map[string][]check) // key is cni type, value is a list of check
9593
// TODO: add cniv1 when adding Linux related test cases
9694
checkSet["cilium"] = []check{
@@ -104,34 +102,34 @@ func (v *LinuxValidator) ValidateStateFile() error {
104102
}
105103

106104
for _, check := range checkSet[v.cni] {
107-
err := v.validateIPs(check.stateFileIps, check.cmd, check.name, check.podNamespace, check.podLabelSelector)
105+
err := v.validateIPs(ctx, check.stateFileIps, check.cmd, check.name, check.podNamespace, check.podLabelSelector)
108106
if err != nil {
109107
return err
110108
}
111109
}
112110
return nil
113111
}
114112

115-
func (v *LinuxValidator) ValidateRestartNetwork() error {
116-
nodes, err := k8sutils.GetNodeList(v.ctx, v.clientset)
113+
func (v *LinuxValidator) ValidateRestartNetwork(ctx context.Context) error {
114+
nodes, err := k8sutils.GetNodeList(ctx, v.clientset)
117115
if err != nil {
118116
return errors.Wrapf(err, "failed to get node list")
119117
}
120118

121119
for index := range nodes.Items {
122120
// get the privileged pod
123-
pod, err := k8sutils.GetPodsByNode(v.ctx, v.clientset, privilegedNamespace, privilegedLabelSelector, nodes.Items[index].Name)
121+
pod, err := k8sutils.GetPodsByNode(ctx, v.clientset, privilegedNamespace, privilegedLabelSelector, nodes.Items[index].Name)
124122
if err != nil {
125123
return errors.Wrapf(err, "failed to get privileged pod")
126124
}
127125

128126
privelegedPod := pod.Items[0]
129127
// exec into the pod to get the state file
130-
_, err = k8sutils.ExecCmdOnPod(v.ctx, v.clientset, privilegedNamespace, privelegedPod.Name, restartNetworkCmd, v.config)
128+
_, err = k8sutils.ExecCmdOnPod(ctx, v.clientset, privilegedNamespace, privelegedPod.Name, restartNetworkCmd, v.config)
131129
if err != nil {
132130
return errors.Wrapf(err, "failed to exec into privileged pod")
133131
}
134-
err = k8sutils.WaitForPodsRunning(v.ctx, v.clientset, "", "")
132+
err = k8sutils.WaitForPodsRunning(ctx, v.clientset, "", "")
135133
if err != nil {
136134
return errors.Wrapf(err, "failed to wait for pods running")
137135
}
@@ -191,22 +189,22 @@ func cnsCacheStateFileIps(result []byte) (map[string]string, error) {
191189
return cnsPodIps, nil
192190
}
193191

194-
func (v *LinuxValidator) validateIPs(stateFileIps stateFileIpsFunc, cmd []string, checkType, namespace, labelSelector string) error {
192+
func (v *LinuxValidator) validateIPs(ctx context.Context, stateFileIps stateFileIpsFunc, cmd []string, checkType, namespace, labelSelector string) error {
195193
log.Printf("Validating %s state file", checkType)
196-
nodes, err := k8sutils.GetNodeList(v.ctx, v.clientset)
194+
nodes, err := k8sutils.GetNodeList(ctx, v.clientset)
197195
if err != nil {
198196
return errors.Wrapf(err, "failed to get node list")
199197
}
200198

201199
for index := range nodes.Items {
202200
// get the privileged pod
203-
pod, err := k8sutils.GetPodsByNode(v.ctx, v.clientset, namespace, labelSelector, nodes.Items[index].Name)
201+
pod, err := k8sutils.GetPodsByNode(ctx, v.clientset, namespace, labelSelector, nodes.Items[index].Name)
204202
if err != nil {
205203
return errors.Wrapf(err, "failed to get privileged pod")
206204
}
207205
podName := pod.Items[0].Name
208206
// exec into the pod to get the state file
209-
result, err := k8sutils.ExecCmdOnPod(v.ctx, v.clientset, namespace, podName, cmd, v.config)
207+
result, err := k8sutils.ExecCmdOnPod(ctx, v.clientset, namespace, podName, cmd, v.config)
210208
if err != nil {
211209
return errors.Wrapf(err, "failed to exec into privileged pod")
212210
}
@@ -219,7 +217,7 @@ func (v *LinuxValidator) validateIPs(stateFileIps stateFileIpsFunc, cmd []string
219217
continue
220218
}
221219
// get the pod ips
222-
podIps := getPodIPsWithoutNodeIP(v.ctx, v.clientset, nodes.Items[index])
220+
podIps := getPodIPsWithoutNodeIP(ctx, v.clientset, nodes.Items[index])
223221

224222
check := compareIPs(filePodIps, podIps)
225223

test/validate/windows_validate.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ var (
2323
azureVnetIpamCmd = []string{"powershell", "-c", "cat ../../k/azure-vnet-ipam.json"}
2424
)
2525

26-
type WindowsClient struct{}
27-
2826
type WindowsValidator struct {
29-
Validator
27+
clientset *kubernetes.Clientset
28+
config *rest.Config
29+
namespace string
30+
cni string
31+
restartCase bool
3032
}
3133

3234
type HNSEndpoint struct {
@@ -86,34 +88,29 @@ type check struct {
8688
cmd []string
8789
}
8890

89-
func (w *WindowsClient) CreateClient(ctx context.Context, clienset *kubernetes.Clientset, config *rest.Config, namespace, cni string, restartCase bool) IValidator {
91+
func CreateWindowsValidator(ctx context.Context, clienset *kubernetes.Clientset, config *rest.Config, namespace, cni string, restartCase bool) (*WindowsValidator, error) {
9092
// deploy privileged pod
9193
privilegedDaemonSet, err := k8sutils.MustParseDaemonSet(privilegedWindowsDaemonSetPath)
9294
if err != nil {
93-
panic(err)
95+
return nil, errors.Wrap(err, "unable to parse daemonset")
9496
}
9597
daemonsetClient := clienset.AppsV1().DaemonSets(privilegedNamespace)
96-
err = k8sutils.MustCreateDaemonset(ctx, daemonsetClient, privilegedDaemonSet)
97-
if err != nil {
98-
panic(err)
98+
if err := k8sutils.MustCreateDaemonset(ctx, daemonsetClient, privilegedDaemonSet); err != nil {
99+
return nil, errors.Wrap(err, "unable to create daemonset")
99100
}
100-
err = k8sutils.WaitForPodsRunning(ctx, clienset, privilegedNamespace, privilegedLabelSelector)
101-
if err != nil {
102-
panic(err)
101+
if err := k8sutils.WaitForPodsRunning(ctx, clienset, privilegedNamespace, privilegedLabelSelector); err != nil {
102+
return nil, errors.Wrap(err, "error while waiting for pods to be running")
103103
}
104104
return &WindowsValidator{
105-
Validator: Validator{
106-
ctx: ctx,
107-
clientset: clienset,
108-
config: config,
109-
namespace: namespace,
110-
cni: cni,
111-
restartCase: restartCase,
112-
},
113-
}
105+
clientset: clienset,
106+
config: config,
107+
namespace: namespace,
108+
cni: cni,
109+
restartCase: restartCase,
110+
}, nil
114111
}
115112

116-
func (v *WindowsValidator) ValidateStateFile() error {
113+
func (v *WindowsValidator) ValidateStateFile(ctx context.Context) error {
117114
checkSet := make(map[string][]check) // key is cni type, value is a list of check
118115

119116
checkSet["cniv1"] = []check{
@@ -128,7 +125,7 @@ func (v *WindowsValidator) ValidateStateFile() error {
128125

129126
// this is checking all IPs of the pods with the statefile
130127
for _, check := range checkSet[v.cni] {
131-
err := v.validateIPs(check.stateFileIps, check.cmd, check.name, check.podNamespace, check.podLabelSelector)
128+
err := v.validateIPs(ctx, check.stateFileIps, check.cmd, check.name, check.podNamespace, check.podLabelSelector)
132129
if err != nil {
133130
return err
134131
}
@@ -194,21 +191,21 @@ func azureVnetIpamIps(result []byte) (map[string]string, error) {
194191
return azureVnetIpamPodIps, nil
195192
}
196193

197-
func (v *WindowsValidator) validateIPs(stateFileIps stateFileIpsFunc, cmd []string, checkType, namespace, labelSelector string) error {
194+
func (v *WindowsValidator) validateIPs(ctx context.Context, stateFileIps stateFileIpsFunc, cmd []string, checkType, namespace, labelSelector string) error {
198195
log.Println("Validating ", checkType, " state file")
199-
nodes, err := k8sutils.GetNodeListByLabelSelector(v.ctx, v.clientset, windowsNodeSelector)
196+
nodes, err := k8sutils.GetNodeListByLabelSelector(ctx, v.clientset, windowsNodeSelector)
200197
if err != nil {
201198
return errors.Wrapf(err, "failed to get node list")
202199
}
203200
for index := range nodes.Items {
204201
// get the privileged pod
205-
pod, err := k8sutils.GetPodsByNode(v.ctx, v.clientset, namespace, labelSelector, nodes.Items[index].Name)
202+
pod, err := k8sutils.GetPodsByNode(ctx, v.clientset, namespace, labelSelector, nodes.Items[index].Name)
206203
if err != nil {
207204
return errors.Wrapf(err, "failed to get privileged pod")
208205
}
209206
podName := pod.Items[0].Name
210207
// exec into the pod to get the state file
211-
result, err := k8sutils.ExecCmdOnPod(v.ctx, v.clientset, namespace, podName, cmd, v.config)
208+
result, err := k8sutils.ExecCmdOnPod(ctx, v.clientset, namespace, podName, cmd, v.config)
212209
if err != nil {
213210
return errors.Wrapf(err, "failed to exec into privileged pod")
214211
}
@@ -221,7 +218,7 @@ func (v *WindowsValidator) validateIPs(stateFileIps stateFileIpsFunc, cmd []stri
221218
continue
222219
}
223220
// get the pod ips
224-
podIps := getPodIPsWithoutNodeIP(v.ctx, v.clientset, nodes.Items[index])
221+
podIps := getPodIPsWithoutNodeIP(ctx, v.clientset, nodes.Items[index])
225222

226223
check := compareIPs(filePodIps, podIps)
227224

@@ -233,6 +230,6 @@ func (v *WindowsValidator) validateIPs(stateFileIps stateFileIpsFunc, cmd []stri
233230
return nil
234231
}
235232

236-
func (v *WindowsValidator) ValidateRestartNetwork() error {
237-
return nil
233+
func (v *WindowsValidator) ValidateRestartNetwork(context.Context) error {
234+
return errors.New("not implemented")
238235
}

0 commit comments

Comments
 (0)