Skip to content

Commit

Permalink
Added test for nsm-config external change catch
Browse files Browse the repository at this point in the history
Signed-off-by: Tigran Manasyan <tigran.manasyan@xored.com>
  • Loading branch information
Tigran Manasyan committed Oct 28, 2020
1 parent 0131dfa commit f86fb3c
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions test/excluded_prefixes_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const (
prefixesFilePath = "/var/lib/networkservicemesh/config/excluded_prefixes.yaml"
collectorNamespace = "excluded-prefixes-collector"
excludedPrefixesEnv = "EXCLUDE_PREFIXES_K8S_EXCLUDED_PREFIXES"
excludedPrefixesKey = "excluded_prefixes.yaml"
// why defaultTimeout is 120 sec:
// https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically
defaultTimeout = time.Second * 120
defaultTick = time.Second * 5
)

type ExcludedPrefixesSuite struct {
Expand Down Expand Up @@ -94,22 +99,22 @@ func (et *ExcludedPrefixesSuite) TearDownSuite() {
func (et *ExcludedPrefixesSuite) TestWithKubeAdmConfigPrefixes() {
et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector.yaml", et.options...))

et.Eventually(et.checkPrefixes(kubeAdmPrefixes), time.Second*120, time.Second*5)
et.Eventually(et.checkPrefixes(kubeAdmPrefixes), defaultTimeout, defaultTick)
}

func (et *ExcludedPrefixesSuite) TestWithUserConfigPrefixes() {
et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector.yaml", et.options...))
et.Require().NoError(exechelper.Run("kubectl apply -f ./files/userConfigMap.yaml", et.options...))

expectedPrefixes := append(kubeAdmPrefixes, userConfigPrefixes...)
et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*120, time.Second*5)
et.Eventually(et.checkPrefixes(expectedPrefixes), defaultTimeout, defaultTick)

expectedPrefixes = []string{"0.0.0.0/0"}
et.Require().NoError(exechelper.Run("kubectl replace -f ./files/updatedUserConfigMap.yaml", et.options...))
et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*120, time.Second*5)
et.Eventually(et.checkPrefixes(expectedPrefixes), defaultTimeout, defaultTick)

et.Require().NoError(exechelper.Run("kubectl delete configmaps excluded-prefixes-config", et.options...))
et.Eventually(et.checkPrefixes(kubeAdmPrefixes), time.Second*120, time.Second*5)
et.Eventually(et.checkPrefixes(kubeAdmPrefixes), defaultTimeout, defaultTick)
}

func (et *ExcludedPrefixesSuite) TestWithAllPrefixes() {
Expand All @@ -131,7 +136,7 @@ func (et *ExcludedPrefixesSuite) TestWithAllPrefixes() {
}()

expectedPrefixes := append(append(kubeAdmPrefixes, envPrefixes...), userConfigPrefixes...)
et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*120, time.Second*5)
et.Eventually(et.checkPrefixes(expectedPrefixes), defaultTimeout, defaultTick)
}

func (et *ExcludedPrefixesSuite) TestWithCorrectEnvPrefixes() {
Expand All @@ -148,7 +153,7 @@ func (et *ExcludedPrefixesSuite) TestWithCorrectEnvPrefixes() {
}))

expectedPrefixes := append(kubeAdmPrefixes, envPrefixes...)
et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*120, time.Second*5)
et.Eventually(et.checkPrefixes(expectedPrefixes), defaultTimeout, defaultTick)
}

func (et *ExcludedPrefixesSuite) TestWithIncorrectEnvPrefixes() {
Expand All @@ -172,6 +177,26 @@ func (et *ExcludedPrefixesSuite) TestWithIncorrectEnvPrefixes() {
}, time.Second*30, time.Second)
}

func (et *ExcludedPrefixesSuite) TestCatchNsmConfigExternalChange() {
et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector.yaml", et.options...))
et.Eventually(et.checkPrefixes(kubeAdmPrefixes), defaultTimeout, defaultTick)

et.Require().NoError(exechelper.Run("kubectl replace -f ../deployments/prefixes-collector/config-map.yaml", et.options...))

expectedPrefixesYaml, err := yaml.Marshal(&prefixes{kubeAdmPrefixes})
et.Require().NoError(err)

client, err := k8s.Client()
et.Require().NoError(err)
configMapInterface := client.CoreV1().ConfigMaps(collectorNamespace)

et.Eventually(func() bool {
configMap, err := configMapInterface.Get("nsm-config", metav1.GetOptions{})
et.Require().NoError(err)
return configMap.Data[excludedPrefixesKey] == string(expectedPrefixesYaml)
}, time.Second*15, time.Second)
}

func TestExcludedPrefixesSuite(t *testing.T) {
suite.Run(t, &ExcludedPrefixesSuite{})
}
Expand Down

0 comments on commit f86fb3c

Please sign in to comment.