|
| 1 | +package statusreaders_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/pluralsh/deployment-operator/internal/kstatus/statusreaders" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + fakecr "sigs.k8s.io/cli-utils/pkg/kstatus/polling/clusterreader/fake" |
| 15 | + "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" |
| 16 | + "sigs.k8s.io/cli-utils/pkg/kstatus/polling/testutil" |
| 17 | + "sigs.k8s.io/cli-utils/pkg/kstatus/status" |
| 18 | + "sigs.k8s.io/cli-utils/pkg/object" |
| 19 | + fakemapper "sigs.k8s.io/cli-utils/pkg/testutil" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + rsGVK = appsv1.SchemeGroupVersion.WithKind("ReplicaSet") |
| 24 | + rsGVR = appsv1.SchemeGroupVersion.WithResource("replicasets") |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + currentReplicaset = strings.TrimSpace(` |
| 29 | +apiVersion: apps/v1 |
| 30 | +kind: ReplicaSet |
| 31 | +metadata: |
| 32 | + labels: |
| 33 | + app: guestbook |
| 34 | + plural.sh/managed-by: agent |
| 35 | + tier: frontend |
| 36 | + name: frontend |
| 37 | + namespace: test-do |
| 38 | + resourceVersion: "4869207" |
| 39 | + uid: 437e2329-59e4-42b9-ae40-48da3562d17e |
| 40 | +spec: |
| 41 | + replicas: 3 |
| 42 | + selector: |
| 43 | + matchLabels: |
| 44 | + tier: frontend |
| 45 | + template: |
| 46 | + metadata: |
| 47 | + creationTimestamp: null |
| 48 | + labels: |
| 49 | + tier: frontend |
| 50 | + spec: |
| 51 | + containers: |
| 52 | + - image: us-docker.pkg.dev/google-samples/containers/gke/gb-frontend:v5 |
| 53 | + imagePullPolicy: IfNotPresent |
| 54 | + name: php-redis |
| 55 | + resources: {} |
| 56 | + terminationMessagePath: /dev/termination-log |
| 57 | + terminationMessagePolicy: File |
| 58 | + dnsPolicy: ClusterFirst |
| 59 | + restartPolicy: Always |
| 60 | + schedulerName: default-scheduler |
| 61 | + securityContext: {} |
| 62 | + terminationGracePeriodSeconds: 30 |
| 63 | +status: |
| 64 | + availableReplicas: 3 |
| 65 | + fullyLabeledReplicas: 3 |
| 66 | + observedGeneration: 1 |
| 67 | + readyReplicas: 3 |
| 68 | + replicas: 3 |
| 69 | +`) |
| 70 | +) |
| 71 | + |
| 72 | +func TestReplicaSetReadStatus(t *testing.T) { |
| 73 | + testCases := map[string]struct { |
| 74 | + identifier object.ObjMetadata |
| 75 | + readerResource *unstructured.Unstructured |
| 76 | + readerErr error |
| 77 | + expectedErr error |
| 78 | + expectedResourceStatus *event.ResourceStatus |
| 79 | + }{ |
| 80 | + "Current resource": { |
| 81 | + identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentReplicaset)), |
| 82 | + readerResource: testutil.YamlToUnstructured(t, currentReplicaset), |
| 83 | + expectedResourceStatus: &event.ResourceStatus{ |
| 84 | + Identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentReplicaset)), |
| 85 | + Status: status.CurrentStatus, |
| 86 | + Resource: testutil.YamlToUnstructured(t, currentReplicaset), |
| 87 | + Message: "ReplicaSet is available. Replicas: 3", |
| 88 | + GeneratedResources: make(event.ResourceStatuses, 0), |
| 89 | + }, |
| 90 | + }, |
| 91 | + "Resource not found": { |
| 92 | + identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentReplicaset)), |
| 93 | + readerErr: errors.NewNotFound(rsGVR.GroupResource(), "test"), |
| 94 | + expectedResourceStatus: &event.ResourceStatus{ |
| 95 | + Identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentReplicaset)), |
| 96 | + Status: status.NotFoundStatus, |
| 97 | + Message: "Resource not found", |
| 98 | + }, |
| 99 | + }, |
| 100 | + "Context cancelled": { |
| 101 | + identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentReplicaset)), |
| 102 | + readerErr: context.Canceled, |
| 103 | + expectedErr: context.Canceled, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for tn := range testCases { |
| 108 | + tc := testCases[tn] |
| 109 | + t.Run(tn, func(t *testing.T) { |
| 110 | + fakeReader := &fakecr.ClusterReader{ |
| 111 | + GetResource: tc.readerResource, |
| 112 | + GetErr: tc.readerErr, |
| 113 | + } |
| 114 | + fakeMapper := fakemapper.NewFakeRESTMapper(rsGVK) |
| 115 | + statusReader := statusreaders.NewReplicaSetStatusReader(fakeMapper) |
| 116 | + |
| 117 | + rs, err := statusReader.ReadStatus(context.Background(), fakeReader, tc.identifier) |
| 118 | + |
| 119 | + if tc.expectedErr != nil { |
| 120 | + if err == nil { |
| 121 | + t.Errorf("expected error, but didn't get one") |
| 122 | + } else { |
| 123 | + assert.EqualError(t, err, tc.expectedErr.Error()) |
| 124 | + } |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + require.NoError(t, err) |
| 129 | + assert.Equal(t, tc.expectedResourceStatus, rs) |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments