Skip to content

Commit

Permalink
checkPrefixes function was divided into alpine pod wait and checkPref…
Browse files Browse the repository at this point in the history
…ixes parts

Signed-off-by: Tigran Manasyan <tigran.manasyan@xored.com>
  • Loading branch information
Tigran Manasyan committed Oct 23, 2020
1 parent 90810d5 commit fcd5ef7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
}
Expand Down
64 changes: 36 additions & 28 deletions test/excluded_prefixes_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const (

type ExcludedPrefixesSuite struct {
suite.Suite
options []*exechelper.Option
options []*exechelper.Option
alpinePodName string
}

type prefixes struct {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fcd5ef7

Please sign in to comment.