GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kubernetes_test.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package kubernetes
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "github.com/go-kit/kit/log"
  7. apiv1 "k8s.io/api/core/v1"
  8. meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  9. fakekubernetes "k8s.io/client-go/kubernetes/fake"
  10. )
  11. func newNamespace(name string) *apiv1.Namespace {
  12. return &apiv1.Namespace{
  13. ObjectMeta: meta_v1.ObjectMeta{
  14. Name: name,
  15. },
  16. TypeMeta: meta_v1.TypeMeta{
  17. APIVersion: "v1",
  18. Kind: "Namespace",
  19. },
  20. }
  21. }
  22. func testGetAllowedNamespaces(t *testing.T, namespace []string, expected []string) {
  23. clientset := fakekubernetes.NewSimpleClientset(newNamespace("default"),
  24. newNamespace("kube-system"))
  25. client := ExtendedClient{coreClient: clientset}
  26. c := NewCluster(client, nil, nil, log.NewNopLogger(), namespace, []string{})
  27. namespaces, err := c.getAllowedAndExistingNamespaces(context.Background())
  28. if err != nil {
  29. t.Errorf("The error should be nil, not: %s", err)
  30. }
  31. result := []string{}
  32. for _, namespace := range namespaces {
  33. result = append(result, namespace.ObjectMeta.Name)
  34. }
  35. if reflect.DeepEqual(result, expected) != true {
  36. t.Errorf("Unexpected namespaces: %v != %v.", result, expected)
  37. }
  38. }
  39. func TestGetAllowedNamespacesDefault(t *testing.T) {
  40. testGetAllowedNamespaces(t, []string{}, []string{"default", "kube-system"})
  41. }
  42. func TestGetAllowedNamespacesNamespacesIsNil(t *testing.T) {
  43. testGetAllowedNamespaces(t, nil, []string{"default", "kube-system"})
  44. }
  45. func TestGetAllowedNamespacesNamespacesSet(t *testing.T) {
  46. testGetAllowedNamespaces(t, []string{"default"}, []string{"default"})
  47. }
  48. func TestGetAllowedNamespacesNamespacesSetDoesNotExist(t *testing.T) {
  49. testGetAllowedNamespaces(t, []string{"hello"}, []string{})
  50. }
  51. func TestGetAllowedNamespacesNamespacesMultiple(t *testing.T) {
  52. testGetAllowedNamespaces(t, []string{"default", "hello", "kube-system"}, []string{"default", "kube-system"})
  53. }