|
| 1 | +//go:build !flaky |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/internal/defaults" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | + |
| 15 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/apimachinery/pkg/types" |
| 18 | + |
| 19 | + "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" |
| 20 | + "sigs.k8s.io/e2e-framework/klient/decoder" |
| 21 | + "sigs.k8s.io/e2e-framework/klient/k8s" |
| 22 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 23 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 24 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 25 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 26 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 27 | +) |
| 28 | + |
| 29 | +func TestHTTPRouteLabelBehavior(t *testing.T) { |
| 30 | + feature := features.New("HTTPRoute label behavior").Setup( |
| 31 | + setupEnvironment, |
| 32 | + ).Setup( |
| 33 | + setupHTTPRouteLabelEnv, |
| 34 | + ).Assess( |
| 35 | + "Label should not be present when canary weight is 0", |
| 36 | + testLabelAbsentWhenWeightZero, |
| 37 | + ).Assess( |
| 38 | + "Label should be present during canary step", |
| 39 | + testLabelPresentDuringCanary, |
| 40 | + ).Assess( |
| 41 | + "Label should be removed when rollout completes", |
| 42 | + testLabelRemovedWhenComplete, |
| 43 | + ).Teardown( |
| 44 | + teardownHTTPRouteLabelEnv, |
| 45 | + ).Feature() |
| 46 | + _ = global.Test(t, feature) |
| 47 | +} |
| 48 | + |
| 49 | +func setupHTTPRouteLabelEnv(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { |
| 50 | + var httpRoute gatewayv1.HTTPRoute |
| 51 | + var rollout v1alpha1.Rollout |
| 52 | + clusterResources := config.Client().Resources() |
| 53 | + resourcesMap := map[string]*unstructured.Unstructured{} |
| 54 | + ctx = context.WithValue(ctx, RESOURCES_MAP_KEY, resourcesMap) |
| 55 | + firstHTTPRouteFile, err := os.Open(HTTP_ROUTE_BASIC_PATH) |
| 56 | + if err != nil { |
| 57 | + logrus.Errorf("file %q opening was failed: %s", HTTP_ROUTE_BASIC_PATH, err) |
| 58 | + t.Error() |
| 59 | + return ctx |
| 60 | + } |
| 61 | + defer firstHTTPRouteFile.Close() |
| 62 | + logrus.Infof("file %q was opened", HTTP_ROUTE_BASIC_PATH) |
| 63 | + rolloutFile, err := os.Open(HTTP_ROUTE_BASIC_ROLLOUT_PATH) |
| 64 | + if err != nil { |
| 65 | + logrus.Errorf("file %q opening was failed: %s", HTTP_ROUTE_BASIC_ROLLOUT_PATH, err) |
| 66 | + t.Error() |
| 67 | + return ctx |
| 68 | + } |
| 69 | + defer rolloutFile.Close() |
| 70 | + logrus.Infof("file %q was opened", HTTP_ROUTE_BASIC_ROLLOUT_PATH) |
| 71 | + err = decoder.Decode(firstHTTPRouteFile, &httpRoute) |
| 72 | + if err != nil { |
| 73 | + logrus.Errorf("file %q decoding was failed: %s", HTTP_ROUTE_BASIC_PATH, err) |
| 74 | + t.Error() |
| 75 | + return ctx |
| 76 | + } |
| 77 | + logrus.Infof("file %q was decoded", HTTP_ROUTE_BASIC_PATH) |
| 78 | + err = decoder.Decode(rolloutFile, &rollout) |
| 79 | + if err != nil { |
| 80 | + logrus.Errorf("file %q decoding was failed: %s", HTTP_ROUTE_BASIC_ROLLOUT_PATH, err) |
| 81 | + t.Error() |
| 82 | + return ctx |
| 83 | + } |
| 84 | + logrus.Infof("file %q was decoded", HTTP_ROUTE_BASIC_ROLLOUT_PATH) |
| 85 | + httpRouteObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&httpRoute) |
| 86 | + if err != nil { |
| 87 | + logrus.Errorf("httpRoute %q converting to unstructured was failed: %s", httpRoute.GetName(), err) |
| 88 | + t.Error() |
| 89 | + return ctx |
| 90 | + } |
| 91 | + logrus.Infof("httpRoute %q was converted to unstructured", httpRoute.GetName()) |
| 92 | + resourcesMap[HTTP_ROUTE_KEY] = &unstructured.Unstructured{ |
| 93 | + Object: httpRouteObject, |
| 94 | + } |
| 95 | + rolloutObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&rollout) |
| 96 | + if err != nil { |
| 97 | + logrus.Errorf("rollout %q converting to unstructured was failed: %s", rollout.GetName(), err) |
| 98 | + t.Error() |
| 99 | + return ctx |
| 100 | + } |
| 101 | + logrus.Infof("rollout %q was converted to unstructured", rollout.GetName()) |
| 102 | + unstructured.RemoveNestedField(rolloutObject, "spec", "template", "metadata", "creationTimestamp") |
| 103 | + resourcesMap[ROLLOUT_KEY] = &unstructured.Unstructured{ |
| 104 | + Object: rolloutObject, |
| 105 | + } |
| 106 | + err = clusterResources.Create(ctx, resourcesMap[HTTP_ROUTE_KEY]) |
| 107 | + if err != nil { |
| 108 | + logrus.Errorf("httpRoute %q creation was failed: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), err) |
| 109 | + t.Error() |
| 110 | + return ctx |
| 111 | + } |
| 112 | + logrus.Infof("httpRoute %q was created", resourcesMap[HTTP_ROUTE_KEY].GetName()) |
| 113 | + err = clusterResources.Create(ctx, resourcesMap[ROLLOUT_KEY]) |
| 114 | + if err != nil { |
| 115 | + logrus.Errorf("rollout %q creation was failed: %s", resourcesMap[ROLLOUT_KEY].GetName(), err) |
| 116 | + t.Error() |
| 117 | + return ctx |
| 118 | + } |
| 119 | + logrus.Infof("rollout %q was created", resourcesMap[ROLLOUT_KEY].GetName()) |
| 120 | + waitCondition := conditions.New(clusterResources) |
| 121 | + logrus.Infof("waiting for httpRoute %q to connect with rollout %q (expecting canary weight: %d)", resourcesMap[HTTP_ROUTE_KEY].GetName(), resourcesMap[ROLLOUT_KEY].GetName(), FIRST_CANARY_ROUTE_WEIGHT) |
| 122 | + err = wait.For( |
| 123 | + waitCondition.ResourceMatch( |
| 124 | + resourcesMap[HTTP_ROUTE_KEY], |
| 125 | + getMatchHTTPRouteFetcher(t, FIRST_CANARY_ROUTE_WEIGHT), |
| 126 | + ), |
| 127 | + wait.WithTimeout(MEDIUM_PERIOD), |
| 128 | + wait.WithInterval(SHORT_PERIOD), |
| 129 | + ) |
| 130 | + if err != nil { |
| 131 | + logrus.Errorf("checking httpRoute %q connection with rollout %q was failed: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), resourcesMap[ROLLOUT_KEY].GetName(), err) |
| 132 | + t.Error() |
| 133 | + return ctx |
| 134 | + } |
| 135 | + logrus.Infof("httpRoute %q connected with rollout %q", resourcesMap[HTTP_ROUTE_KEY].GetName(), resourcesMap[ROLLOUT_KEY].GetName()) |
| 136 | + return ctx |
| 137 | +} |
| 138 | + |
| 139 | +func testLabelAbsentWhenWeightZero(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { |
| 140 | + clusterResources := config.Client().Resources() |
| 141 | + resourcesMap, ok := ctx.Value(RESOURCES_MAP_KEY).(map[string]*unstructured.Unstructured) |
| 142 | + if !ok { |
| 143 | + logrus.Errorf("%q type assertion was failed", RESOURCES_MAP_KEY) |
| 144 | + t.Error() |
| 145 | + return ctx |
| 146 | + } |
| 147 | + logrus.Infof("Checking that label is absent when canary weight is 0") |
| 148 | + err := wait.For( |
| 149 | + conditions.New(clusterResources).ResourceMatch( |
| 150 | + resourcesMap[HTTP_ROUTE_KEY], |
| 151 | + getMatchHTTPRouteLabelFetcher(t, false), |
| 152 | + ), |
| 153 | + wait.WithTimeout(MEDIUM_PERIOD), |
| 154 | + wait.WithInterval(SHORT_PERIOD), |
| 155 | + ) |
| 156 | + if err != nil { |
| 157 | + logrus.Errorf("httpRoute %q should not have label when weight is 0: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), err) |
| 158 | + t.Error() |
| 159 | + return ctx |
| 160 | + } |
| 161 | + logrus.Infof("httpRoute %q correctly has no label when weight is 0", resourcesMap[HTTP_ROUTE_KEY].GetName()) |
| 162 | + return ctx |
| 163 | +} |
| 164 | + |
| 165 | +func testLabelPresentDuringCanary(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { |
| 166 | + clusterResources := config.Client().Resources() |
| 167 | + resourcesMap, ok := ctx.Value(RESOURCES_MAP_KEY).(map[string]*unstructured.Unstructured) |
| 168 | + if !ok { |
| 169 | + logrus.Errorf("%q type assertion was failed", RESOURCES_MAP_KEY) |
| 170 | + t.Error() |
| 171 | + return ctx |
| 172 | + } |
| 173 | + logrus.Infof("%q was type asserted", RESOURCES_MAP_KEY) |
| 174 | + containersObject, isFound, err := unstructured.NestedFieldNoCopy(resourcesMap[ROLLOUT_KEY].Object, strings.Split(ROLLOUT_TEMPLATE_CONTAINERS_FIELD, ".")...) |
| 175 | + if !isFound { |
| 176 | + logrus.Errorf("rollout %q field %q was not found", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_CONTAINERS_FIELD) |
| 177 | + t.Error() |
| 178 | + return ctx |
| 179 | + } |
| 180 | + if err != nil { |
| 181 | + logrus.Errorf("getting rollout %q field %q was failed: %s", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_CONTAINERS_FIELD, err) |
| 182 | + t.Error() |
| 183 | + return ctx |
| 184 | + } |
| 185 | + logrus.Infof("rollout %q field %q was received", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_CONTAINERS_FIELD) |
| 186 | + unstructuredContainerList, ok := containersObject.([]interface{}) |
| 187 | + if !ok { |
| 188 | + logrus.Errorf("rollout %q field %q type assertion was failed", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_CONTAINERS_FIELD) |
| 189 | + t.Error() |
| 190 | + return ctx |
| 191 | + } |
| 192 | + logrus.Infof("rollout %q field %q was type asserted", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_CONTAINERS_FIELD) |
| 193 | + unstructuredContainer, ok := unstructuredContainerList[0].(map[string]interface{}) |
| 194 | + if !ok { |
| 195 | + logrus.Errorf("rollout %q field %q type assertion was failed", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_FIRST_CONTAINER_FIELD) |
| 196 | + t.Error() |
| 197 | + return ctx |
| 198 | + } |
| 199 | + logrus.Infof("rollout %q field %q was type asserted", resourcesMap[ROLLOUT_KEY].GetName(), ROLLOUT_TEMPLATE_FIRST_CONTAINER_FIELD) |
| 200 | + unstructured.RemoveNestedField(resourcesMap[ROLLOUT_KEY].Object, "metadata", "resourceVersion") |
| 201 | + unstructuredContainer["image"] = NEW_IMAGE_FIELD_VALUE |
| 202 | + serializedRollout, err := json.Marshal(resourcesMap[ROLLOUT_KEY].Object) |
| 203 | + if err != nil { |
| 204 | + logrus.Errorf("rollout %q serializing was failed: %s", resourcesMap[ROLLOUT_KEY].GetName(), err) |
| 205 | + t.Error() |
| 206 | + return ctx |
| 207 | + } |
| 208 | + logrus.Infof("rollout %q was serialized", resourcesMap[ROLLOUT_KEY].GetName()) |
| 209 | + rolloutPatch := k8s.Patch{ |
| 210 | + PatchType: types.MergePatchType, |
| 211 | + Data: serializedRollout, |
| 212 | + } |
| 213 | + err = clusterResources.Patch(ctx, resourcesMap[ROLLOUT_KEY], rolloutPatch) |
| 214 | + if err != nil { |
| 215 | + logrus.Errorf("rollout %q updating was failed: %s", resourcesMap[ROLLOUT_KEY].GetName(), err) |
| 216 | + t.Error() |
| 217 | + return ctx |
| 218 | + } |
| 219 | + logrus.Infof("rollout %q was updated", resourcesMap[ROLLOUT_KEY].GetName()) |
| 220 | + waitCondition := conditions.New(clusterResources) |
| 221 | + logrus.Infof("waiting for httpRoute %q to have label during canary step (weight: %d)", resourcesMap[HTTP_ROUTE_KEY].GetName(), LAST_CANARY_ROUTE_WEIGHT) |
| 222 | + err = wait.For( |
| 223 | + waitCondition.ResourceMatch( |
| 224 | + resourcesMap[HTTP_ROUTE_KEY], |
| 225 | + getMatchHTTPRouteLabelFetcher(t, true), |
| 226 | + ), |
| 227 | + wait.WithTimeout(LONG_PERIOD), |
| 228 | + wait.WithInterval(SHORT_PERIOD), |
| 229 | + ) |
| 230 | + if err != nil { |
| 231 | + logrus.Errorf("httpRoute %q should have label during canary: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), err) |
| 232 | + t.Error() |
| 233 | + return ctx |
| 234 | + } |
| 235 | + logrus.Infof("httpRoute %q correctly has label during canary step", resourcesMap[HTTP_ROUTE_KEY].GetName()) |
| 236 | + return ctx |
| 237 | +} |
| 238 | + |
| 239 | +func testLabelRemovedWhenComplete(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { |
| 240 | + clusterResources := config.Client().Resources() |
| 241 | + resourcesMap, ok := ctx.Value(RESOURCES_MAP_KEY).(map[string]*unstructured.Unstructured) |
| 242 | + if !ok { |
| 243 | + logrus.Errorf("%q type assertion was failed", RESOURCES_MAP_KEY) |
| 244 | + t.Error() |
| 245 | + return ctx |
| 246 | + } |
| 247 | + logrus.Infof("Waiting for rollout to complete and label to be removed") |
| 248 | + waitCondition := conditions.New(clusterResources) |
| 249 | + err := wait.For( |
| 250 | + waitCondition.ResourceMatch( |
| 251 | + resourcesMap[HTTP_ROUTE_KEY], |
| 252 | + getMatchHTTPRouteFetcher(t, FIRST_CANARY_ROUTE_WEIGHT), |
| 253 | + ), |
| 254 | + wait.WithTimeout(LONG_PERIOD), |
| 255 | + wait.WithInterval(SHORT_PERIOD), |
| 256 | + ) |
| 257 | + if err != nil { |
| 258 | + logrus.Errorf("httpRoute %q weight did not return to 0: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), err) |
| 259 | + t.Error() |
| 260 | + return ctx |
| 261 | + } |
| 262 | + logrus.Infof("httpRoute %q weight returned to 0, checking label removal", resourcesMap[HTTP_ROUTE_KEY].GetName()) |
| 263 | + err = wait.For( |
| 264 | + waitCondition.ResourceMatch( |
| 265 | + resourcesMap[HTTP_ROUTE_KEY], |
| 266 | + getMatchHTTPRouteLabelFetcher(t, false), |
| 267 | + ), |
| 268 | + wait.WithTimeout(MEDIUM_PERIOD), |
| 269 | + wait.WithInterval(SHORT_PERIOD), |
| 270 | + ) |
| 271 | + if err != nil { |
| 272 | + logrus.Errorf("httpRoute %q label should be removed when rollout completes: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), err) |
| 273 | + t.Error() |
| 274 | + return ctx |
| 275 | + } |
| 276 | + logrus.Infof("httpRoute %q correctly has no label after rollout completion", resourcesMap[HTTP_ROUTE_KEY].GetName()) |
| 277 | + return ctx |
| 278 | +} |
| 279 | + |
| 280 | +func teardownHTTPRouteLabelEnv(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { |
| 281 | + clusterResources := config.Client().Resources() |
| 282 | + resourcesMap, ok := ctx.Value(RESOURCES_MAP_KEY).(map[string]*unstructured.Unstructured) |
| 283 | + if !ok { |
| 284 | + logrus.Errorf("%q type assertion was failed", RESOURCES_MAP_KEY) |
| 285 | + t.Error() |
| 286 | + return ctx |
| 287 | + } |
| 288 | + logrus.Infof("%q was type asserted", RESOURCES_MAP_KEY) |
| 289 | + err := clusterResources.Delete(ctx, resourcesMap[ROLLOUT_KEY]) |
| 290 | + if err != nil { |
| 291 | + logrus.Errorf("deleting rollout %q was failed: %s", resourcesMap[ROLLOUT_KEY].GetName(), err) |
| 292 | + t.Error() |
| 293 | + return ctx |
| 294 | + } |
| 295 | + logrus.Infof("rollout %q was deleted", resourcesMap[ROLLOUT_KEY].GetName()) |
| 296 | + err = clusterResources.Delete(ctx, resourcesMap[HTTP_ROUTE_KEY]) |
| 297 | + if err != nil { |
| 298 | + logrus.Errorf("deleting httpRoute %q was failed: %s", resourcesMap[HTTP_ROUTE_KEY].GetName(), err) |
| 299 | + t.Error() |
| 300 | + return ctx |
| 301 | + } |
| 302 | + logrus.Infof("httpRoute %q was deleted", resourcesMap[HTTP_ROUTE_KEY].GetName()) |
| 303 | + return ctx |
| 304 | +} |
| 305 | + |
| 306 | +func getMatchHTTPRouteLabelFetcher(t *testing.T, expectLabel bool) func(k8s.Object) bool { |
| 307 | + return func(obj k8s.Object) bool { |
| 308 | + var httpRoute gatewayv1.HTTPRoute |
| 309 | + unstructuredHTTPRoute, ok := obj.(*unstructured.Unstructured) |
| 310 | + if !ok { |
| 311 | + logrus.Error("k8s object type assertion was failed") |
| 312 | + t.Error() |
| 313 | + return false |
| 314 | + } |
| 315 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredHTTPRoute.Object, &httpRoute) |
| 316 | + if err != nil { |
| 317 | + logrus.Errorf("conversion from unstructured httpRoute %q to the typed httpRoute was failed: %s", unstructuredHTTPRoute.GetName(), err) |
| 318 | + t.Error() |
| 319 | + return false |
| 320 | + } |
| 321 | + labels := httpRoute.GetLabels() |
| 322 | + value, ok := labels[defaults.InProgressLabelKey] |
| 323 | + if expectLabel { |
| 324 | + return ok && value == defaults.InProgressLabelValue |
| 325 | + } |
| 326 | + // we explicitly expect the label to be absent |
| 327 | + return !ok |
| 328 | + } |
| 329 | +} |
| 330 | + |
0 commit comments