|
| 1 | +package leadership |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + "github.com/stakater/Reloader/internal/pkg/constants" |
| 14 | + "github.com/stakater/Reloader/internal/pkg/controller" |
| 15 | + "github.com/stakater/Reloader/internal/pkg/handler" |
| 16 | + "github.com/stakater/Reloader/internal/pkg/metrics" |
| 17 | + "github.com/stakater/Reloader/internal/pkg/options" |
| 18 | + "github.com/stakater/Reloader/internal/pkg/testutil" |
| 19 | + "github.com/stakater/Reloader/internal/pkg/util" |
| 20 | + "github.com/stakater/Reloader/pkg/kube" |
| 21 | +) |
| 22 | + |
| 23 | +func TestMain(m *testing.M) { |
| 24 | + |
| 25 | + testutil.CreateNamespace(testutil.Namespace, testutil.Clients.KubernetesClient) |
| 26 | + |
| 27 | + logrus.Infof("Running Testcases") |
| 28 | + retCode := m.Run() |
| 29 | + |
| 30 | + testutil.DeleteNamespace(testutil.Namespace, testutil.Clients.KubernetesClient) |
| 31 | + |
| 32 | + os.Exit(retCode) |
| 33 | +} |
| 34 | + |
| 35 | +func TestHealthz(t *testing.T) { |
| 36 | + request, err := http.NewRequest(http.MethodGet, "/live", nil) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf(("failed to create request")) |
| 39 | + } |
| 40 | + |
| 41 | + response := httptest.NewRecorder() |
| 42 | + |
| 43 | + healthz(response, request) |
| 44 | + got := response.Code |
| 45 | + want := 200 |
| 46 | + |
| 47 | + if got != want { |
| 48 | + t.Fatalf("got: %q, want: %q", got, want) |
| 49 | + } |
| 50 | + |
| 51 | + // Have the liveness probe serve a 500 |
| 52 | + healthy = false |
| 53 | + |
| 54 | + request, err = http.NewRequest(http.MethodGet, "/live", nil) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf(("failed to create request")) |
| 57 | + } |
| 58 | + |
| 59 | + response = httptest.NewRecorder() |
| 60 | + |
| 61 | + healthz(response, request) |
| 62 | + got = response.Code |
| 63 | + want = 500 |
| 64 | + |
| 65 | + if got != want { |
| 66 | + t.Fatalf("got: %q, want: %q", got, want) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestRunLeaderElection validates that the liveness endpoint serves 500 when |
| 71 | +// leadership election fails |
| 72 | +func TestRunLeaderElection(t *testing.T) { |
| 73 | + ctx, cancel := context.WithCancel(context.TODO()) |
| 74 | + |
| 75 | + lock := GetNewLock(testutil.Clients.KubernetesClient.CoordinationV1(), constants.LockName, testutil.Pod, testutil.Namespace) |
| 76 | + |
| 77 | + go RunLeaderElection(lock, ctx, cancel, testutil.Pod, []*controller.Controller{}) |
| 78 | + |
| 79 | + // Liveness probe should be serving OK |
| 80 | + request, err := http.NewRequest(http.MethodGet, "/live", nil) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf(("failed to create request")) |
| 83 | + } |
| 84 | + |
| 85 | + response := httptest.NewRecorder() |
| 86 | + |
| 87 | + healthz(response, request) |
| 88 | + got := response.Code |
| 89 | + want := 500 |
| 90 | + |
| 91 | + if got != want { |
| 92 | + t.Fatalf("got: %q, want: %q", got, want) |
| 93 | + } |
| 94 | + |
| 95 | + // Cancel the leader election context, so leadership is released and |
| 96 | + // live endpoint serves 500 |
| 97 | + cancel() |
| 98 | + |
| 99 | + request, err = http.NewRequest(http.MethodGet, "/live", nil) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf(("failed to create request")) |
| 102 | + } |
| 103 | + |
| 104 | + response = httptest.NewRecorder() |
| 105 | + |
| 106 | + healthz(response, request) |
| 107 | + got = response.Code |
| 108 | + want = 500 |
| 109 | + |
| 110 | + if got != want { |
| 111 | + t.Fatalf("got: %q, want: %q", got, want) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// TestRunLeaderElectionWithControllers tests that leadership election works |
| 116 | +// wiht real controllers and that on context cancellation the controllers stop |
| 117 | +// running. |
| 118 | +func TestRunLeaderElectionWithControllers(t *testing.T) { |
| 119 | + t.Logf("Creating controller") |
| 120 | + var controllers []*controller.Controller |
| 121 | + for k := range kube.ResourceMap { |
| 122 | + c, err := controller.NewController(testutil.Clients.KubernetesClient, k, testutil.Namespace, []string{}, metrics.NewCollectors()) |
| 123 | + if err != nil { |
| 124 | + logrus.Fatalf("%s", err) |
| 125 | + } |
| 126 | + |
| 127 | + controllers = append(controllers, c) |
| 128 | + } |
| 129 | + time.Sleep(3 * time.Second) |
| 130 | + |
| 131 | + lock := GetNewLock(testutil.Clients.KubernetesClient.CoordinationV1(), fmt.Sprintf("%s-%d", constants.LockName, 1), testutil.Pod, testutil.Namespace) |
| 132 | + |
| 133 | + ctx, cancel := context.WithCancel(context.TODO()) |
| 134 | + |
| 135 | + // Start running leadership election, this also starts the controllers |
| 136 | + go RunLeaderElection(lock, ctx, cancel, testutil.Pod, controllers) |
| 137 | + time.Sleep(3 * time.Second) |
| 138 | + |
| 139 | + // Create some stuff and do a thing |
| 140 | + configmapName := testutil.ConfigmapNamePrefix + "-update-" + testutil.RandSeq(5) |
| 141 | + configmapClient, err := testutil.CreateConfigMap(testutil.Clients.KubernetesClient, testutil.Namespace, configmapName, "www.google.com") |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("Error while creating the configmap %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + // Creating deployment |
| 147 | + _, err = testutil.CreateDeployment(testutil.Clients.KubernetesClient, configmapName, testutil.Namespace, true) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("Error in deployment creation: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + // Updating configmap for first time |
| 153 | + updateErr := testutil.UpdateConfigMap(configmapClient, testutil.Namespace, configmapName, "", "www.stakater.com") |
| 154 | + if updateErr != nil { |
| 155 | + t.Fatalf("Configmap was not updated") |
| 156 | + } |
| 157 | + time.Sleep(3 * time.Second) |
| 158 | + |
| 159 | + // Verifying deployment update |
| 160 | + logrus.Infof("Verifying pod envvars has been created") |
| 161 | + shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, testutil.Namespace, configmapName, "www.stakater.com") |
| 162 | + config := util.Config{ |
| 163 | + Namespace: testutil.Namespace, |
| 164 | + ResourceName: configmapName, |
| 165 | + SHAValue: shaData, |
| 166 | + Annotation: options.ConfigmapUpdateOnChangeAnnotation, |
| 167 | + } |
| 168 | + deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() |
| 169 | + updated := testutil.VerifyResourceEnvVarUpdate(testutil.Clients, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs) |
| 170 | + if !updated { |
| 171 | + t.Fatalf("Deployment was not updated") |
| 172 | + } |
| 173 | + time.Sleep(testutil.SleepDuration) |
| 174 | + |
| 175 | + // Cancel the leader election context, so leadership is released |
| 176 | + logrus.Info("shutting down controller from test") |
| 177 | + cancel() |
| 178 | + time.Sleep(5 * time.Second) |
| 179 | + |
| 180 | + // Updating configmap again |
| 181 | + updateErr = testutil.UpdateConfigMap(configmapClient, testutil.Namespace, configmapName, "", "www.stakater.com/new") |
| 182 | + if updateErr != nil { |
| 183 | + t.Fatalf("Configmap was not updated") |
| 184 | + } |
| 185 | + |
| 186 | + // Verifying that the deployment was not updated as leadership has been lost |
| 187 | + logrus.Infof("Verifying pod envvars has not been updated") |
| 188 | + shaData = testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, testutil.Namespace, configmapName, "www.stakater.com/new") |
| 189 | + config = util.Config{ |
| 190 | + Namespace: testutil.Namespace, |
| 191 | + ResourceName: configmapName, |
| 192 | + SHAValue: shaData, |
| 193 | + Annotation: options.ConfigmapUpdateOnChangeAnnotation, |
| 194 | + } |
| 195 | + deploymentFuncs = handler.GetDeploymentRollingUpgradeFuncs() |
| 196 | + updated = testutil.VerifyResourceEnvVarUpdate(testutil.Clients, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs) |
| 197 | + if updated { |
| 198 | + t.Fatalf("Deployment was updated") |
| 199 | + } |
| 200 | + |
| 201 | + // Deleting deployment |
| 202 | + err = testutil.DeleteDeployment(testutil.Clients.KubernetesClient, testutil.Namespace, configmapName) |
| 203 | + if err != nil { |
| 204 | + logrus.Errorf("Error while deleting the deployment %v", err) |
| 205 | + } |
| 206 | + |
| 207 | + // Deleting configmap |
| 208 | + err = testutil.DeleteConfigMap(testutil.Clients.KubernetesClient, testutil.Namespace, configmapName) |
| 209 | + if err != nil { |
| 210 | + logrus.Errorf("Error while deleting the configmap %v", err) |
| 211 | + } |
| 212 | + time.Sleep(testutil.SleepDuration) |
| 213 | +} |
0 commit comments