From 0131dfa979936d49b1683d5b38ead0a3310998f4 Mon Sep 17 00:00:00 2001 From: Tigran Manasyan Date: Fri, 23 Oct 2020 13:42:13 +0700 Subject: [PATCH] checkPrefixes function was divided into alpine pod wait and checkPrefixes parts Signed-off-by: Tigran Manasyan --- k8s/k8s.go | 2 +- test/excluded_prefixes_collector_test.go | 64 +++++++++++++----------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/k8s/k8s.go b/k8s/k8s.go index 6f7b0ae40..c599080be 100644 --- a/k8s/k8s.go +++ b/k8s/k8s.go @@ -103,7 +103,7 @@ func ShowLogs(namespace string, options ...*exechelper.Option) { pod := &pods.Items[i] for j := 0; j < len(pod.Spec.Containers); j++ { container := &pod.Spec.Containers[j] - _ = exechelper.Run(fmt.Sprintf("kubectl logs %v -c %v ", pod.Name, container.Name), options...) + _ = exechelper.Run(fmt.Sprintf("kubectl logs -n %v %v -c %v ", namespace, pod.Name, container.Name), options...) } } } diff --git a/test/excluded_prefixes_collector_test.go b/test/excluded_prefixes_collector_test.go index a24745c11..3588000e8 100644 --- a/test/excluded_prefixes_collector_test.go +++ b/test/excluded_prefixes_collector_test.go @@ -42,7 +42,8 @@ const ( type ExcludedPrefixesSuite struct { suite.Suite - options []*exechelper.Option + options []*exechelper.Option + alpinePodName string } type prefixes struct { @@ -72,25 +73,7 @@ func (et *ExcludedPrefixesSuite) SetupSuite() { et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector-cluster-role.yaml", et.options...)) et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/config-map.yaml", et.options...)) - et.Require().NoError(k8s.ApplyDeployment("../deployments/alpine.yaml", func(alpine *v1.Deployment) { - alpine.Namespace = collectorNamespace - alpine.Spec.Template.Spec.Volumes = append(alpine.Spec.Template.Spec.Volumes, corev1.Volume{ - Name: "config-volume", - VolumeSource: corev1.VolumeSource{ - ConfigMap: &corev1.ConfigMapVolumeSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "nsm-config", - }, - }, - }, - }) - - alpine.Spec.Template.Spec.Containers[0].VolumeMounts = append(alpine.Spec.Template.Spec.Containers[0].VolumeMounts, - corev1.VolumeMount{ - Name: "config-volume", - MountPath: "/var/lib/networkservicemesh/config", - }) - })) + et.setupAlpine() } func (et *ExcludedPrefixesSuite) TearDownTest() { @@ -193,26 +176,51 @@ func TestExcludedPrefixesSuite(t *testing.T) { suite.Run(t, &ExcludedPrefixesSuite{}) } -func (et *ExcludedPrefixesSuite) checkPrefixes(expectedPrefixes []string) func() bool { +func (et *ExcludedPrefixesSuite) setupAlpine() { + et.Require().NoError(k8s.ApplyDeployment("../deployments/alpine.yaml", func(alpine *v1.Deployment) { + alpine.Namespace = collectorNamespace + alpine.Spec.Template.Spec.Volumes = append(alpine.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: "config-volume", + VolumeSource: corev1.VolumeSource{ + ConfigMap: &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: "nsm-config", + }, + }, + }, + }) + + alpine.Spec.Template.Spec.Containers[0].VolumeMounts = append(alpine.Spec.Template.Spec.Containers[0].VolumeMounts, + corev1.VolumeMount{ + Name: "config-volume", + MountPath: "/var/lib/networkservicemesh/config", + }) + })) + var podInfo *corev1.Pod labels := map[string]string{ "app": "alpine", } - expectedPrefixesYaml, err := yaml.Marshal(&prefixes{expectedPrefixes}) - et.Require().NoError(err) - return func() bool { + et.Eventually(func() bool { + var err error if podInfo == nil { podInfo, err = k8s.GetPod(collectorNamespace, "", labels) et.Require().NoError(err) } + return podInfo != nil + }, time.Second*60, time.Second*5) - if podInfo == nil { - return false - } + et.alpinePodName = podInfo.Name +} + +func (et *ExcludedPrefixesSuite) checkPrefixes(expectedPrefixes []string) func() bool { + expectedPrefixesYaml, err := yaml.Marshal(&prefixes{expectedPrefixes}) + et.Require().NoError(err) + return func() bool { var sb strings.Builder - cmd := fmt.Sprintf("kubectl exec -n excluded-prefixes-collector -ti %v -- cat %v", podInfo.Name, prefixesFilePath) + cmd := fmt.Sprintf("kubectl exec -n excluded-prefixes-collector -ti %v -- cat %v", et.alpinePodName, prefixesFilePath) err := exechelper.Run(cmd, exechelper.WithStdout(&sb)) logrus.Infof("Actual: %v Expected: %v", sb.String(), string(expectedPrefixesYaml)) return err == nil && sb.String() == string(expectedPrefixesYaml)