main_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  4. "testing"
  5. )
  6. func TestGetClusterDomain(t *testing.T) {
  7. c := Config{}
  8. if c.GetClusterDomain() != "cluster.local" {
  9. t.Errorf("cluster domain should default to cluster.local, not: %s", c.GetClusterDomain())
  10. }
  11. c.ClusterDomain = "mydomain.com"
  12. if c.GetClusterDomain() != "mydomain.com" {
  13. t.Errorf("cluster domain should default to cluster.local, not: %s", c.GetClusterDomain())
  14. }
  15. }
  16. func TestShouldMutate(t *testing.T) {
  17. testCases := []struct {
  18. description string
  19. subject string
  20. namespace string
  21. expected bool
  22. }{
  23. {"full cluster domain", "test.default.svc.cluster.local", "default", true},
  24. {"full cluster domain wrong ns", "test.default.svc.cluster.local", "kube-system", false},
  25. {"left dots get stripped", ".test.default.svc.cluster.local", "default", true},
  26. {"left dots get stripped wrong ns", ".test.default.svc.cluster.local", "kube-system", false},
  27. {"right dots get stripped", "test.default.svc.cluster.local.", "default", true},
  28. {"right dots get stripped wrong ns", "test.default.svc.cluster.local.", "kube-system", false},
  29. {"dots get stripped", ".test.default.svc.cluster.local.", "default", true},
  30. {"dots get stripped wrong ns", ".test.default.svc.cluster.local.", "kube-system", false},
  31. {"partial cluster domain", "test.default.svc.cluster", "default", true},
  32. {"partial cluster domain wrong ns is still allowed because not valid hostname", "test.default.svc.cluster", "kube-system", true},
  33. {"service domain", "test.default.svc", "default", true},
  34. {"service domain wrong ns", "test.default.svc", "kube-system", false},
  35. {"two part domain", "test.default", "default", true},
  36. {"two part domain different ns", "test.default", "kube-system", true},
  37. {"one hostname", "test", "default", true},
  38. {"no subject specified", "", "default", false},
  39. {"three part not cluster", "test.default.com", "kube-system", true},
  40. {"four part not cluster", "test.default.svc.com", "kube-system", true},
  41. {"five part not cluster", "test.default.svc.cluster.com", "kube-system", true},
  42. {"six part not cluster", "test.default.svc.cluster.local.com", "kube-system", true},
  43. }
  44. for _, testCase := range testCases {
  45. t.Run(testCase.description, func(t *testing.T) {
  46. mutationAllowed, validationErr := shouldMutate(&metav1.ObjectMeta{
  47. Annotations: map[string]string{
  48. admissionWebhookAnnotationKey: testCase.subject,
  49. },
  50. }, testCase.namespace, "cluster.local", true)
  51. if mutationAllowed != testCase.expected {
  52. t.Errorf("shouldMutate did not return %t for %s", testCase.expected, testCase.description)
  53. }
  54. if testCase.subject != "" && mutationAllowed == false && validationErr == nil {
  55. t.Errorf("shouldMutate should return validation error for invalid hostname")
  56. }
  57. })
  58. }
  59. }
  60. func TestShouldMutateNotRestrictToNamespace(t *testing.T) {
  61. mutationAllowed, _ := shouldMutate(&metav1.ObjectMeta{
  62. Annotations: map[string]string{
  63. admissionWebhookAnnotationKey: "test.default.svc.cluster.local",
  64. },
  65. }, "kube-system", "cluster.local", false)
  66. if mutationAllowed == false {
  67. t.Errorf("shouldMutate should return true even with a wrong namespace if restrictToNamespace is false.")
  68. }
  69. }