From f130d2c260ec6a7dad98f7fcd7c29c66a53623c6 Mon Sep 17 00:00:00 2001 From: Tigran Manasyan Date: Fri, 2 Oct 2020 11:42:45 +0700 Subject: [PATCH] Added test for incorrect env prefixes case and test with all prefix sources Signed-off-by: Tigran Manasyan --- basic_suite_test.go | 4 +- .../collector-namespace.yaml | 2 +- test/excluded_prefixes_collector_test.go | 81 ++++++++++++++++--- test/files/userConfigMap.yaml | 2 +- 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/basic_suite_test.go b/basic_suite_test.go index cc3d5aa84..ce5db5afd 100644 --- a/basic_suite_test.go +++ b/basic_suite_test.go @@ -32,6 +32,8 @@ import ( "github.com/stretchr/testify/suite" ) +const defaultNamespace = "default" + type BasicTestsSuite struct { suite.Suite options []*exechelper.Option @@ -53,7 +55,7 @@ func (s *BasicTestsSuite) TearDownSuite() { } func (s *BasicTestsSuite) TearDownTest() { - k8s.ShowLogs("default", s.options...) + k8s.ShowLogs(defaultNamespace, s.options...) s.Require().NoError(exechelper.Run("kubectl delete serviceaccounts --all")) s.Require().NoError(exechelper.Run("kubectl delete services --all")) diff --git a/deployments/prefixes-collector/collector-namespace.yaml b/deployments/prefixes-collector/collector-namespace.yaml index 57f24a90d..0ae06b13e 100644 --- a/deployments/prefixes-collector/collector-namespace.yaml +++ b/deployments/prefixes-collector/collector-namespace.yaml @@ -2,4 +2,4 @@ apiVersion: v1 kind: Namespace metadata: - name: excluded-prefixes-collector \ No newline at end of file + name: excluded-prefixes-collector diff --git a/test/excluded_prefixes_collector_test.go b/test/excluded_prefixes_collector_test.go index 1c1db7484..704ae15c7 100644 --- a/test/excluded_prefixes_collector_test.go +++ b/test/excluded_prefixes_collector_test.go @@ -14,6 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// package test contains k8s integration tests package test import ( @@ -44,12 +45,13 @@ const ( // kubeNamespace is KubeAdm ConfigMap namespace kubeNamespace = "kube-system" // kubeName is KubeAdm ConfigMap name - kubeName = "kubeadm-config" - configMapPath = "./files/userConfigMap.yaml" - prefixesFileName = "excluded_prefixes.yaml" - bufferSize = 4096 - appLabelKey = "app" - collectorNamespace = "excluded-prefixes-collector" + kubeName = "kubeadm-config" + configMapPath = "./files/userConfigMap.yaml" + prefixesFileName = "excluded_prefixes.yaml" + bufferSize = 4096 + appLabelKey = "app" + collectorNamespace = "excluded-prefixes-collector" + excludedPrefixesEnv = "EXCLUDE_PREFIXES_K8S_EXCLUDED_PREFIXES" ) type ExcludedPrefixesSuite struct { @@ -105,7 +107,7 @@ func (et *ExcludedPrefixesSuite) TestWithKubeAdmConfigPrefixes() { expectedPrefixes, err := kubeAdmPrefixes() et.Require().NoError(err) - et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*5, time.Second) + et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*10, time.Second) } func (et *ExcludedPrefixesSuite) TestWithUserConfigPrefixes() { @@ -142,12 +144,54 @@ func (et *ExcludedPrefixesSuite) TestWithUserConfigPrefixes() { et.Require().NoError(err) expectedPrefixes := []string{"0.0.0.0/0"} - et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*5, time.Second) + et.Eventually(et.checkPrefixes(expectedPrefixes), time.Second*10, time.Second) err = configMapsInterface.Delete(userConfigMap.Name, &metav1.DeleteOptions{}) et.Require().NoError(err) - et.Eventually(et.checkPrefixes(kubeAdmPrefixes), time.Second*5, time.Second) + et.Eventually(et.checkPrefixes(kubeAdmPrefixes), time.Second*10, time.Second) +} + +func (et *ExcludedPrefixesSuite) TestWithAllPrefixes() { + envPrefixes := []string{ + "127.0.0.0/8", + "134.65.0.0/16", + } + + et.deployCollectorWithEnvs(envPrefixes) + + prefixPool, err := prefixpool.New(envPrefixes...) + et.Require().NoError(err) + + userConfigMap, err := userConfigMap(configMapPath) + et.Require().NoError(err) + + configMapsInterface, err := k8s.ConfigMapInterface(userConfigMap.Namespace) + et.Require().NoError(err) + + userConfigMap, err = configMapsInterface.Create(userConfigMap) + et.Require().NoError(err) + + defer func() { + _ = configMapsInterface.Delete(userConfigMap.Name, &metav1.DeleteOptions{}) + }() + + kubeAdmPrefixes, err := kubeAdmPrefixes() + et.Require().NoError(err) + + err = prefixPool.ReleaseExcludedPrefixes(kubeAdmPrefixes) + et.Require().NoError(err) + + userPrefixes := prefixes{} + err = k8s_yaml.NewYAMLOrJSONDecoder( + strings.NewReader(userConfigMap.Data[prefixesFileName]), bufferSize, + ).Decode(&userPrefixes) + et.Require().NoError(err) + + err = prefixPool.ReleaseExcludedPrefixes(userPrefixes.Prefixes) + et.Require().NoError(err) + + et.Eventually(et.checkPrefixes(prefixPool.GetPrefixes()), time.Second*10, time.Second) } func (et *ExcludedPrefixesSuite) TestWithCorrectEnvPrefixes() { @@ -165,7 +209,22 @@ func (et *ExcludedPrefixesSuite) TestWithCorrectEnvPrefixes() { err = prefixPool.ReleaseExcludedPrefixes(kubeAdmPrefixes) et.Require().NoError(err) - et.Eventually(et.checkPrefixes(prefixPool.GetPrefixes()), time.Second*5, time.Second) + et.Eventually(et.checkPrefixes(prefixPool.GetPrefixes()), time.Second*10, time.Second) +} + +func (et *ExcludedPrefixesSuite) TestWithIncorrectEnvPrefixes() { + envPrefixes := []string{ + "256.256.256.0", + } + et.deployCollectorWithEnvs(envPrefixes) + + et.Eventually(func() bool { + podInfo, err := k8s.DescribePod(prefixesFileName, "", map[string]string{ + "app": "excluded-prefixes-collector", + }) + et.Require().NoError(err) + return podInfo == nil + }, time.Second*10, time.Second) } func TestExcludedPrefixesSuite(t *testing.T) { @@ -181,7 +240,7 @@ func (et *ExcludedPrefixesSuite) deployCollector() { func (et *ExcludedPrefixesSuite) deployCollectorWithEnvs(envPrefixes []string) { et.Require().NoError(k8s.ApplyDaemonSet("../deployments/prefixes-collector/collector.yaml", func(collector *v1.DaemonSet) { collector.Spec.Template.Spec.Containers[0].Env = append(collector.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ - Name: "EXCLUDE_PREFIXES_K8S_EXCLUDED_PREFIXES", + Name: excludedPrefixesEnv, Value: strings.Join(envPrefixes, ","), }) })) diff --git a/test/files/userConfigMap.yaml b/test/files/userConfigMap.yaml index 3b23add87..d4161b8ff 100644 --- a/test/files/userConfigMap.yaml +++ b/test/files/userConfigMap.yaml @@ -5,4 +5,4 @@ metadata: name: excluded-prefixes-config namespace: default data: - excluded_prefixes.yaml: "Prefixes:\n- 128.0.0.0/1\n- 64.0.0.0/2" + excluded_prefixes.yaml: "Prefixes:\n- 134.8.0.0/16\n- 64.5.12.0/24"