Skip to content

Commit

Permalink
Added test for incorrect env prefixes case and test with all prefix s…
Browse files Browse the repository at this point in the history
…ources

Signed-off-by: Tigran Manasyan <tigran.manasyan@xored.com>
  • Loading branch information
Tigran Manasyan committed Oct 2, 2020
1 parent 88d96a4 commit f130d2c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
4 changes: 3 additions & 1 deletion basic_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/stretchr/testify/suite"
)

const defaultNamespace = "default"

type BasicTestsSuite struct {
suite.Suite
options []*exechelper.Option
Expand All @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion deployments/prefixes-collector/collector-namespace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: excluded-prefixes-collector
name: excluded-prefixes-collector
81 changes: 70 additions & 11 deletions test/excluded_prefixes_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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, ","),
})
}))
Expand Down
2 changes: 1 addition & 1 deletion test/files/userConfigMap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit f130d2c

Please sign in to comment.