|
| 1 | +// Copyright 2020 The Kubernetes Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + appsv1 "k8s.io/api/apps/v1" |
| 11 | + v1 "k8s.io/api/core/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "sigs.k8s.io/cli-utils/pkg/apply/event" |
| 16 | + "sigs.k8s.io/cli-utils/pkg/common" |
| 17 | + "sigs.k8s.io/cli-utils/pkg/object" |
| 18 | +) |
| 19 | + |
| 20 | +func randomString(prefix string) string { |
| 21 | + seed := time.Now().UTC().UnixNano() |
| 22 | + randomSuffix := common.RandomStr(seed) |
| 23 | + return fmt.Sprintf("%s%s", prefix, randomSuffix) |
| 24 | +} |
| 25 | + |
| 26 | +func cmInventoryManifest(name, namespace, id string) *unstructured.Unstructured { |
| 27 | + cm := &v1.ConfigMap{ |
| 28 | + TypeMeta: metav1.TypeMeta{ |
| 29 | + APIVersion: v1.SchemeGroupVersion.String(), |
| 30 | + Kind: "ConfigMap", |
| 31 | + }, |
| 32 | + ObjectMeta: metav1.ObjectMeta{ |
| 33 | + Name: name, |
| 34 | + Namespace: namespace, |
| 35 | + Labels: map[string]string{ |
| 36 | + common.InventoryLabel: id, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm) |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | + return &unstructured.Unstructured{ |
| 45 | + Object: u, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func deploymentManifest(namespace string) *unstructured.Unstructured { |
| 50 | + dep := &appsv1.Deployment{ |
| 51 | + TypeMeta: metav1.TypeMeta{ |
| 52 | + APIVersion: appsv1.SchemeGroupVersion.String(), |
| 53 | + Kind: "Deployment", |
| 54 | + }, |
| 55 | + ObjectMeta: metav1.ObjectMeta{ |
| 56 | + Name: "nginx-deployment", |
| 57 | + Namespace: namespace, |
| 58 | + }, |
| 59 | + Spec: appsv1.DeploymentSpec{ |
| 60 | + Replicas: func() *int32 { r := int32(4); return &r }(), |
| 61 | + Selector: &metav1.LabelSelector{ |
| 62 | + MatchLabels: map[string]string{ |
| 63 | + "app": "nginx", |
| 64 | + }, |
| 65 | + }, |
| 66 | + Template: v1.PodTemplateSpec{ |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Labels: map[string]string{ |
| 69 | + "app": "nginx", |
| 70 | + }, |
| 71 | + }, |
| 72 | + Spec: v1.PodSpec{ |
| 73 | + Containers: []v1.Container{ |
| 74 | + { |
| 75 | + Name: "nginx", |
| 76 | + Image: "nginx:1.19.6", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(dep) |
| 84 | + if err != nil { |
| 85 | + panic(err) |
| 86 | + } |
| 87 | + return &unstructured.Unstructured{ |
| 88 | + Object: u, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func updateReplicas(u *unstructured.Unstructured, replicas int) *unstructured.Unstructured { |
| 93 | + err := unstructured.SetNestedField(u.Object, int64(replicas), "spec", "replicas") |
| 94 | + if err != nil { |
| 95 | + panic(err) |
| 96 | + } |
| 97 | + return u |
| 98 | +} |
| 99 | + |
| 100 | +type expEvent struct { |
| 101 | + eventType event.Type |
| 102 | + |
| 103 | + applyEvent *expApplyEvent |
| 104 | +} |
| 105 | + |
| 106 | +type expApplyEvent struct { |
| 107 | + applyEventType event.ApplyEventType |
| 108 | + operation event.ApplyEventOperation |
| 109 | + identifier object.ObjMetadata |
| 110 | + error error |
| 111 | +} |
| 112 | + |
| 113 | +func verifyEvents(expEvents []expEvent, events []event.Event) error { |
| 114 | + expEventIndex := 0 |
| 115 | + for i := range events { |
| 116 | + e := events[i] |
| 117 | + ee := expEvents[expEventIndex] |
| 118 | + if isMatch(ee, e) { |
| 119 | + expEventIndex += 1 |
| 120 | + if expEventIndex >= len(expEvents) { |
| 121 | + return nil |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return fmt.Errorf("event %s not found", expEvents[expEventIndex].eventType) |
| 126 | +} |
| 127 | + |
| 128 | +var nilIdentifier = object.ObjMetadata{} |
| 129 | + |
| 130 | +func isMatch(ee expEvent, e event.Event) bool { |
| 131 | + if ee.eventType != e.Type { |
| 132 | + return false |
| 133 | + } |
| 134 | + |
| 135 | + // nolint:gocritic |
| 136 | + switch e.Type { |
| 137 | + case event.ApplyType: |
| 138 | + aee := ee.applyEvent |
| 139 | + if aee == nil { |
| 140 | + return true |
| 141 | + } |
| 142 | + ae := e.ApplyEvent |
| 143 | + |
| 144 | + if aee.applyEventType != ae.Type { |
| 145 | + return false |
| 146 | + } |
| 147 | + |
| 148 | + if aee.applyEventType == event.ApplyEventResourceUpdate { |
| 149 | + if aee.operation != ae.Operation { |
| 150 | + return false |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if aee.identifier != nilIdentifier { |
| 155 | + if aee.identifier != ae.Identifier { |
| 156 | + return false |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if aee.error != nil { |
| 161 | + return ae.Error != nil |
| 162 | + } |
| 163 | + return ae.Error == nil |
| 164 | + } |
| 165 | + return true |
| 166 | +} |
0 commit comments