Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for creating controllers #52

Merged
merged 3 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions admiral/pkg/controller/admiral/dependency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package admiral

import (
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
"testing"
"time"
)

func TestNewDependencyController(t *testing.T) {
stop := make(chan struct{})
handler := test.MockDependencyHandler{}

dependencyController, err := NewDependencyController(stop, &handler, "../../test/resources/admins@fake-cluster.k8s.local", "ns", time.Duration(1000))

if err != nil {
t.Errorf("Unexpected err %v", err)
}

if dependencyController == nil {
t.Errorf("Dependency controller should never be nil without an error thrown")
}
}
27 changes: 27 additions & 0 deletions admiral/pkg/controller/admiral/globaltraffic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package admiral

import (
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
"k8s.io/client-go/tools/clientcmd"
"testing"
"time"
)

func TestNewGlobalTrafficController(t *testing.T) {
config, err := clientcmd.BuildConfigFromFlags("", "../../test/resources/admins@fake-cluster.k8s.local")
if err != nil {
t.Errorf("%v", err)
}
stop := make(chan struct{})
handler := test.MockGlobalTrafficHandler{}

globalTrafficController, err := NewGlobalTrafficController(stop, &handler, config, time.Duration(1000))

if err != nil {
t.Errorf("Unexpected err %v", err)
}

if globalTrafficController == nil {
t.Errorf("GlobalTraffic controller should never be nil without an error thrown")
}
}
26 changes: 26 additions & 0 deletions admiral/pkg/controller/admiral/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package admiral

import (
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
"k8s.io/client-go/tools/clientcmd"
"testing"
)

func TestNewNodeController(t *testing.T) {
config, err := clientcmd.BuildConfigFromFlags("", "../../test/resources/admins@fake-cluster.k8s.local")
if err != nil {
t.Errorf("%v", err)
}
stop := make(chan struct{})
handler := test.MockNodeHandler{}

nodeController, err := NewNodeController(stop, &handler, config)

if err != nil {
t.Errorf("Unexpected err %v", err)
}

if nodeController == nil {
t.Errorf("Node controller should never be nil without an error thrown")
}
}
18 changes: 13 additions & 5 deletions admiral/pkg/controller/admiral/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (d *PodController) GetPods() ([]*k8sV1.Pod, error) {

ns := d.K8sClient.CoreV1().Namespaces()

sidecarInjectionNamespaceFilter := d.labelSet.NamespaceSidecarInjectionLabel+"="+d.labelSet.NamespaceSidecarInjectionLabelValue
istioEnabledNs, err := ns.List(meta_v1.ListOptions{LabelSelector: sidecarInjectionNamespaceFilter})
namespaceSidecarInjectionLabelFilter := d.labelSet.NamespaceSidecarInjectionLabel+"="+d.labelSet.NamespaceSidecarInjectionLabelValue
istioEnabledNs, err := ns.List(meta_v1.ListOptions{LabelSelector: namespaceSidecarInjectionLabelFilter})

if err != nil {
return nil, fmt.Errorf("error getting istio labled namespaces: %v", err)
Expand All @@ -104,14 +104,22 @@ func (d *PodController) GetPods() ([]*k8sV1.Pod, error) {
for _, v := range istioEnabledNs.Items {

pods := d.K8sClient.CoreV1().Pods(v.Name)
admiralEnabledLabelFilter := d.labelSet.DeploymentAnnotation +"=true"
podsList, err := pods.List(meta_v1.ListOptions{LabelSelector: admiralEnabledLabelFilter})
podsList, err := pods.List(meta_v1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing pods: %v", err)
}
var admiralDeployments []k8sV1.Pod
for _, pod := range podsList.Items {
if pod.Annotations[d.labelSet.DeploymentAnnotation] == "true" {
admiralDeployments = append(admiralDeployments, pod)
}
}

if err != nil {
return nil, fmt.Errorf("error getting istio labled namespaces: %v", err)
}

for _, pi := range podsList.Items {
for _, pi := range admiralDeployments {
res = append(res, &pi)
}
}
Expand Down
125 changes: 125 additions & 0 deletions admiral/pkg/controller/admiral/pod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package admiral

import (
"github.com/google/go-cmp/cmp"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/clientcmd"
"sync"
"testing"
"time"
)

func TestNewPodController(t *testing.T) {
config, err := clientcmd.BuildConfigFromFlags("", "../../test/resources/admins@fake-cluster.k8s.local")
if err != nil {
t.Errorf("%v", err)
}
stop := make(chan struct{})
handler := test.MockPodHandler{}

podController, err := NewPodController(stop, &handler, config, time.Duration(1000))

if err != nil {
t.Errorf("Unexpected err %v", err)
}

if podController == nil {
t.Errorf("Pod controller should never be nil without an error thrown")
}
}

func TestPodController_GetPods(t *testing.T) {
controller := PodController{
labelSet: common.LabelSet{
DeploymentAnnotation: "sidecar.istio.io/inject",
NamespaceSidecarInjectionLabel: "istio-injection",
NamespaceSidecarInjectionLabelValue: "enabled",
AdmiralIgnoreLabel: "admiral-ignore",
},
}

client := fake.NewSimpleClientset()
ns := v1.Namespace{}
ns.Labels = map[string]string{"istio-injection": "enabled"}
ns.Name = "test-ns"

_, err := client.CoreV1().Namespaces().Create(&ns)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

pod := v1.Pod{}
pod.Namespace = "test-ns"
pod.Name="pod"
pod.Labels = map[string]string{"identity": "id", "istio-injected": "true"}
pod.Annotations = map[string]string{"sidecar.istio.io/inject": "true"}
podWithBadLabels := v1.Pod{}
podWithBadLabels.Namespace = "test-ns"
podWithBadLabels.Name="podWithBadLabels"
podWithBadLabels.Labels = map[string]string{"identity": "id", "random-label": "true"}
podWithBadLabels.Annotations = map[string]string{"woo": "yay"}
_, err = client.CoreV1().Pods("test-ns").Create(&pod)
_, err = client.CoreV1().Pods("test-ns").Create(&podWithBadLabels)

if err != nil {
t.Errorf("Unexpected err %v", err)
}

controller.K8sClient = client

podsList, err := controller.GetPods()

if err != nil {
t.Errorf("Unexpected err %v", err)
}
if !cmp.Equal(podsList[0].Name, pod.Name) || !cmp.Equal(podsList[0].Annotations, pod.Annotations) || !cmp.Equal(podsList[0].Labels, pod.Labels) {
t.Errorf("Incorrect pod found. Mismatch: %v", cmp.Diff(podsList[0], pod))
}
if len(podsList) != 1 {
t.Errorf("Too many pods found. Expected 1, found %v", podsList)
}

}

func TestPodCache_AppendPodToCluster(t *testing.T) {
podCache := podCache{}
podCache.cache = make(map[string]*PodClusterEntry)
podCache.mutex = &sync.Mutex{}

pod := &v1.Pod{}
pod.Name="foobar"
pod.Namespace = "ns"
pod.Labels = map[string]string{"identity":"my-first-pod"}

podCache.AppendPodToCluster("ns", pod)

if podCache.getKey(pod) != "my-first-pod" {
t.Errorf("Incorrect key. Got %v, expected ns", podCache.getKey(pod))
}
if !cmp.Equal(podCache.Get("ns").Pods["ns"][0], pod) {
t.Errorf("Incorrect pod fount. Diff: %v", cmp.Diff(podCache.Get("ns").Pods["ns"], pod))
}

length := len(podCache.Get("ns").Pods["ns"])

podCache.AppendPodToCluster("ns", pod)

if podCache.getKey(pod) != "my-first-pod" {
t.Errorf("Incorrect key. Got %v, expected ns", podCache.getKey(pod))
}
if !cmp.Equal(podCache.Get("ns").Pods["ns"][0], pod) {
t.Errorf("Incorrect pod fount. Diff: %v", cmp.Diff(podCache.Get("ns").Pods["ns"], pod))
}
if (length+1) != len(podCache.Get("ns").Pods["ns"]) {
t.Errorf("Didn't add a second pod, expected %v, got %v", length+1, len(podCache.Get("ns").Pods["ns"]))
}

podCache.Delete(podCache.Get("ns"))

if podCache.Get("ns") != nil {
t.Errorf("Didn't delete successfully, expected nil, got %v", podCache.Get("ns"))
}
}
22 changes: 22 additions & 0 deletions admiral/pkg/test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func (m *MockServiceHandler) Deleted(obj *k8sCoreV1.Service) {

}

type MockPodHandler struct {
}

func (m MockPodHandler) Added (obj *k8sCoreV1.Pod) {

}

func (m MockPodHandler) Deleted (obj *k8sCoreV1.Pod) {

}

type MockNodeHandler struct {
}

Expand All @@ -94,3 +105,14 @@ func (m *MockDependencyHandler) Added(obj *v1.Dependency) {
func (m *MockDependencyHandler) Deleted(obj *v1.Dependency) {

}

type MockGlobalTrafficHandler struct {
}

func (m *MockGlobalTrafficHandler) Added(obj *v1.GlobalTrafficPolicy) {

}

func (m *MockGlobalTrafficHandler) Deleted(obj *v1.GlobalTrafficPolicy) {

}