Skip to content

Commit a28ae92

Browse files
authored
add tests for custom status readers (#316)
1 parent 0c0539a commit a28ae92

File tree

3 files changed

+409
-0
lines changed

3 files changed

+409
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
deploymentGVK = appsv1.SchemeGroupVersion.WithKind("Deployment")
24+
deploymentGVR = appsv1.SchemeGroupVersion.WithResource("deployments")
25+
)
26+
27+
var (
28+
currentDeployment = strings.TrimSpace(`
29+
apiVersion: apps/v1
30+
kind: Deployment
31+
metadata:
32+
name: test
33+
generation: 1
34+
namespace: qual
35+
spec:
36+
selector:
37+
matchLabels:
38+
app: app
39+
status:
40+
observedGeneration: 1
41+
updatedReplicas: 1
42+
readyReplicas: 1
43+
availableReplicas: 1
44+
replicas: 1
45+
conditions:
46+
- type: Progressing
47+
status: "True"
48+
reason: NewReplicaSetAvailable
49+
- type: Available
50+
status: "True"
51+
`)
52+
)
53+
54+
func TestDeploymentReadStatus(t *testing.T) {
55+
testCases := map[string]struct {
56+
identifier object.ObjMetadata
57+
readerResource *unstructured.Unstructured
58+
readerErr error
59+
expectedErr error
60+
expectedResourceStatus *event.ResourceStatus
61+
}{
62+
"Current resource": {
63+
identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentDeployment)),
64+
readerResource: testutil.YamlToUnstructured(t, currentDeployment),
65+
expectedResourceStatus: &event.ResourceStatus{
66+
Identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentDeployment)),
67+
Status: status.CurrentStatus,
68+
Resource: testutil.YamlToUnstructured(t, currentDeployment),
69+
Message: "Deployment is available. Replicas: 1",
70+
GeneratedResources: make(event.ResourceStatuses, 0),
71+
},
72+
},
73+
"Resource not found": {
74+
identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentDeployment)),
75+
readerErr: errors.NewNotFound(deploymentGVR.GroupResource(), "test"),
76+
expectedResourceStatus: &event.ResourceStatus{
77+
Identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentDeployment)),
78+
Status: status.NotFoundStatus,
79+
Message: "Resource not found",
80+
},
81+
},
82+
"Context cancelled": {
83+
identifier: object.UnstructuredToObjMetadata(testutil.YamlToUnstructured(t, currentDeployment)),
84+
readerErr: context.Canceled,
85+
expectedErr: context.Canceled,
86+
},
87+
}
88+
89+
for tn := range testCases {
90+
tc := testCases[tn]
91+
t.Run(tn, func(t *testing.T) {
92+
fakeReader := &fakecr.ClusterReader{
93+
GetResource: tc.readerResource,
94+
GetErr: tc.readerErr,
95+
}
96+
fakeMapper := fakemapper.NewFakeRESTMapper(deploymentGVK)
97+
statusReader := statusreaders.NewDeploymentResourceReader(fakeMapper)
98+
99+
rs, err := statusReader.ReadStatus(context.Background(), fakeReader, tc.identifier)
100+
101+
if tc.expectedErr != nil {
102+
if err == nil {
103+
t.Errorf("expected error, but didn't get one")
104+
} else {
105+
assert.EqualError(t, err, tc.expectedErr.Error())
106+
}
107+
return
108+
}
109+
110+
require.NoError(t, err)
111+
assert.Equal(t, tc.expectedResourceStatus, rs)
112+
})
113+
}
114+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)