|
| 1 | +package watcher_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + apiappsv1 "k8s.io/api/apps/v1" |
| 10 | + apicorev1 "k8s.io/api/core/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + |
| 13 | + "github.com/kyma-project/lifecycle-manager/pkg/watcher" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAssertDeploymentReady_ReturnsNoError_WhenDeploymentReady(t *testing.T) { |
| 17 | + readyDeployment := &apiappsv1.Deployment{ |
| 18 | + Status: apiappsv1.DeploymentStatus{ |
| 19 | + ReadyReplicas: 1, |
| 20 | + }, |
| 21 | + } |
| 22 | + getFunc := func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 23 | + deployment, _ := obj.(*apiappsv1.Deployment) |
| 24 | + *deployment = *readyDeployment |
| 25 | + return nil |
| 26 | + } |
| 27 | + listFunc := func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 28 | + return nil |
| 29 | + } |
| 30 | + mockClnt := &mockClient{getFunc: getFunc, listFunc: listFunc} |
| 31 | + ctx := t.Context() |
| 32 | + |
| 33 | + err := watcher.AssertDeploymentReady(ctx, mockClnt) |
| 34 | + require.NoError(t, err) |
| 35 | +} |
| 36 | + |
| 37 | +func TestAssertDeploymentReady_ReturnsError_WhenDeploymentNotReady(t *testing.T) { |
| 38 | + notReadyDeployment := &apiappsv1.Deployment{ |
| 39 | + Status: apiappsv1.DeploymentStatus{ |
| 40 | + ReadyReplicas: 0, |
| 41 | + }, |
| 42 | + } |
| 43 | + getFunc := func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 44 | + deployment, _ := obj.(*apiappsv1.Deployment) |
| 45 | + *deployment = *notReadyDeployment |
| 46 | + return nil |
| 47 | + } |
| 48 | + listFunc := func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 49 | + return nil |
| 50 | + } |
| 51 | + mockClnt := &mockClient{getFunc: getFunc, listFunc: listFunc} |
| 52 | + ctx := t.Context() |
| 53 | + |
| 54 | + err := watcher.AssertDeploymentReady(ctx, mockClnt) |
| 55 | + require.Error(t, err) |
| 56 | + require.ErrorIs(t, err, watcher.ErrSkrWebhookDeploymentNotReady) |
| 57 | +} |
| 58 | + |
| 59 | +func TestAssertDeploymentReady_ReturnsError_WhenClientReturnsError(t *testing.T) { |
| 60 | + unexpectedError := errors.New("unexpected error") |
| 61 | + notFoundFunc := func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 62 | + return unexpectedError |
| 63 | + } |
| 64 | + listFunc := func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 65 | + return nil |
| 66 | + } |
| 67 | + mockClnt := &mockClient{getFunc: notFoundFunc, listFunc: listFunc} |
| 68 | + ctx := t.Context() |
| 69 | + |
| 70 | + err := watcher.AssertDeploymentReady(ctx, mockClnt) |
| 71 | + require.Error(t, err) |
| 72 | + require.ErrorIs(t, err, unexpectedError) |
| 73 | +} |
| 74 | + |
| 75 | +func TestAssertDeploymentReady_ReturnsError_WhenDeploymentInBackoff(t *testing.T) { |
| 76 | + deployment := &apiappsv1.Deployment{ |
| 77 | + Status: apiappsv1.DeploymentStatus{ |
| 78 | + ReadyReplicas: 0, |
| 79 | + }, |
| 80 | + } |
| 81 | + podList := &apicorev1.PodList{ |
| 82 | + Items: []apicorev1.Pod{{ |
| 83 | + Status: apicorev1.PodStatus{ |
| 84 | + ContainerStatuses: []apicorev1.ContainerStatus{{ |
| 85 | + State: apicorev1.ContainerState{ |
| 86 | + Waiting: &apicorev1.ContainerStateWaiting{ |
| 87 | + Reason: "CrashLoopBackOff", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }}, |
| 91 | + }, |
| 92 | + }}, |
| 93 | + } |
| 94 | + getFunc := func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 95 | + deploymentObj, ok := obj.(*apiappsv1.Deployment) |
| 96 | + if ok { |
| 97 | + *deploymentObj = *deployment |
| 98 | + } |
| 99 | + return nil |
| 100 | + } |
| 101 | + listFunc := func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 102 | + podListObj, ok := list.(*apicorev1.PodList) |
| 103 | + if ok { |
| 104 | + *podListObj = *podList |
| 105 | + } |
| 106 | + return nil |
| 107 | + } |
| 108 | + mockClnt := &mockClient{getFunc: getFunc, listFunc: listFunc} |
| 109 | + ctx := t.Context() |
| 110 | + |
| 111 | + err := watcher.AssertDeploymentReady(ctx, mockClnt) |
| 112 | + require.Error(t, err) |
| 113 | + require.ErrorIs(t, err, watcher.ErrSkrWebhookDeploymentInBackoff) |
| 114 | +} |
| 115 | + |
| 116 | +// Stub for tests |
| 117 | + |
| 118 | +type mockClient struct { |
| 119 | + getFunc func(context.Context, client.ObjectKey, client.Object, ...client.GetOption) error |
| 120 | + listFunc func(context.Context, client.ObjectList, ...client.ListOption) error |
| 121 | +} |
| 122 | + |
| 123 | +func (m *mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 124 | + return m.getFunc(ctx, key, obj, opts...) |
| 125 | +} |
| 126 | + |
| 127 | +func (m *mockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { |
| 128 | + return m.listFunc(ctx, list, opts...) |
| 129 | +} |
0 commit comments