|
| 1 | +package appliance |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "time" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + "sigs.k8s.io/yaml" |
| 12 | +) |
| 13 | + |
| 14 | +// Test helpers |
| 15 | + |
| 16 | +// creationTimestamp and uid need to be normalized |
| 17 | +var magicTime = metav1.NewTime(time.Date(2024, time.April, 19, 0, 0, 0, 0, time.UTC)) |
| 18 | + |
| 19 | +type goldenFile struct { |
| 20 | + Resources []client.Object `json:"resources"` |
| 21 | +} |
| 22 | + |
| 23 | +func (suite *ApplianceTestSuite) makeGoldenAssertions(namespace, goldenFileName string) { |
| 24 | + require := suite.Require() |
| 25 | + |
| 26 | + goldenFilePath := filepath.Join("testdata", "golden-fixtures", goldenFileName+".yaml") |
| 27 | + obtainedResources := goldenFile{Resources: suite.gatherResources(namespace)} |
| 28 | + obtainedBytes, err := yaml.Marshal(obtainedResources) |
| 29 | + require.NoError(err) |
| 30 | + if len(os.Args) > 0 && os.Args[len(os.Args)-1] == "appliance-update-golden-files" { |
| 31 | + err := os.WriteFile(goldenFilePath, obtainedBytes, 0600) |
| 32 | + require.NoError(err) |
| 33 | + } |
| 34 | + |
| 35 | + goldenBytes, err := os.ReadFile(goldenFilePath) |
| 36 | + require.NoError(err) |
| 37 | + |
| 38 | + // testify prints a readable yaml diff |
| 39 | + require.Equal(string(goldenBytes), string(obtainedBytes)) |
| 40 | +} |
| 41 | + |
| 42 | +// When new owned types are declared in SetupWithManager() in reconcile.go, we |
| 43 | +// must gather them here for golden testing to be reliable. |
| 44 | +func (suite *ApplianceTestSuite) gatherResources(namespace string) []client.Object { |
| 45 | + var objs []client.Object |
| 46 | + |
| 47 | + // We set the GVK ourselves, as this is missing from the List response: |
| 48 | + // https://github.com/kubernetes/client-go/issues/861 |
| 49 | + // This makes eyeballing golden file diffs a little easier, as we can see |
| 50 | + // which object is being changed. |
| 51 | + // |
| 52 | + // Certain common fields must be normalized in order to make golden testing |
| 53 | + // work, such as the creationTimestamp and UID, which would differ every |
| 54 | + // test run. Some resource-specific normalizations are also performed. |
| 55 | + deps, err := suite.k8sClient.AppsV1().Deployments(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 56 | + suite.Require().NoError(err) |
| 57 | + for _, obj := range deps.Items { |
| 58 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}) |
| 59 | + normalizeObj(&obj) |
| 60 | + objs = append(objs, &obj) |
| 61 | + } |
| 62 | + ssets, err := suite.k8sClient.AppsV1().StatefulSets(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 63 | + suite.Require().NoError(err) |
| 64 | + for _, obj := range ssets.Items { |
| 65 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}) |
| 66 | + normalizeObj(&obj) |
| 67 | + objs = append(objs, &obj) |
| 68 | + } |
| 69 | + cmaps, err := suite.k8sClient.CoreV1().ConfigMaps(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 70 | + suite.Require().NoError(err) |
| 71 | + for _, obj := range cmaps.Items { |
| 72 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ConfigMap"}) |
| 73 | + normalizeObj(&obj) |
| 74 | + objs = append(objs, &obj) |
| 75 | + } |
| 76 | + pvcs, err := suite.k8sClient.CoreV1().PersistentVolumeClaims(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 77 | + suite.Require().NoError(err) |
| 78 | + for _, obj := range pvcs.Items { |
| 79 | + if obj.DeletionTimestamp != nil { |
| 80 | + obj.DeletionTimestamp = &magicTime |
| 81 | + } |
| 82 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "PersistentVolumeClaim"}) |
| 83 | + normalizeObj(&obj) |
| 84 | + objs = append(objs, &obj) |
| 85 | + } |
| 86 | + pods, err := suite.k8sClient.CoreV1().Pods(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 87 | + suite.Require().NoError(err) |
| 88 | + for _, obj := range pods.Items { |
| 89 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}) |
| 90 | + normalizeObj(&obj) |
| 91 | + objs = append(objs, &obj) |
| 92 | + } |
| 93 | + |
| 94 | + // These are just test secrets, nothing truly sensitive should end up in the |
| 95 | + // golden files. |
| 96 | + secrets, err := suite.k8sClient.CoreV1().Secrets(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 97 | + suite.Require().NoError(err) |
| 98 | + for _, obj := range secrets.Items { |
| 99 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Secret"}) |
| 100 | + normalizeObj(&obj) |
| 101 | + objs = append(objs, &obj) |
| 102 | + } |
| 103 | + |
| 104 | + sas, err := suite.k8sClient.CoreV1().ServiceAccounts(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 105 | + suite.Require().NoError(err) |
| 106 | + for _, obj := range sas.Items { |
| 107 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ServiceAccount"}) |
| 108 | + normalizeObj(&obj) |
| 109 | + objs = append(objs, &obj) |
| 110 | + } |
| 111 | + svcs, err := suite.k8sClient.CoreV1().Services(namespace).List(suite.ctx, metav1.ListOptions{}) |
| 112 | + suite.Require().NoError(err) |
| 113 | + for _, obj := range svcs.Items { |
| 114 | + obj.Spec.ClusterIP = "NORMALIZED_FOR_TESTING" |
| 115 | + obj.Spec.ClusterIPs = []string{"NORMALIZED_FOR_TESTING"} |
| 116 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Service"}) |
| 117 | + normalizeObj(&obj) |
| 118 | + objs = append(objs, &obj) |
| 119 | + } |
| 120 | + |
| 121 | + return objs |
| 122 | +} |
| 123 | + |
| 124 | +func normalizeObj(obj client.Object) { |
| 125 | + obj.SetUID("NORMALIZED_FOR_TESTING") |
| 126 | + obj.SetCreationTimestamp(magicTime) |
| 127 | + obj.SetManagedFields(nil) |
| 128 | + obj.SetNamespace("NORMALIZED_FOR_TESTING") |
| 129 | + obj.SetResourceVersion("NORMALIZED_FOR_TESTING") |
| 130 | +} |
0 commit comments