1 package agent
2
3 import (
4 context "context"
5 alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
6 "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/typed/rollouts/v1alpha1"
7 "github.com/datawire/dlib/dlog"
8 "github.com/stretchr/testify/assert"
9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10 "k8s.io/apimachinery/pkg/types"
11 "k8s.io/apimachinery/pkg/watch"
12 "testing"
13 )
14
15 type mockRolloutsGetter struct {
16 mockRolloutInterface *mockRolloutInterface
17 latestNamespace string
18 }
19
20 var _ v1alpha1.RolloutsGetter = &mockRolloutsGetter{}
21
22 func (m *mockRolloutsGetter) Rollouts(namespace string) v1alpha1.RolloutInterface {
23 m.latestNamespace = namespace
24 return m.mockRolloutInterface
25 }
26
27 type mockRolloutInterface struct {
28 latestName string
29 latestPatchType types.PatchType
30 latestOptions metav1.PatchOptions
31 patches []string
32 subresources []string
33 }
34
35 var _ v1alpha1.RolloutInterface = &mockRolloutInterface{}
36
37 func (m *mockRolloutInterface) Create(ctx context.Context, rollout *alpha1.Rollout, opts metav1.CreateOptions) (*alpha1.Rollout, error) {
38 panic("implement me")
39 }
40
41 func (m *mockRolloutInterface) Update(ctx context.Context, rollout *alpha1.Rollout, opts metav1.UpdateOptions) (*alpha1.Rollout, error) {
42 panic("implement me")
43 }
44
45 func (m *mockRolloutInterface) UpdateStatus(ctx context.Context, rollout *alpha1.Rollout, opts metav1.UpdateOptions) (*alpha1.Rollout, error) {
46 panic("implement me")
47 }
48
49 func (m *mockRolloutInterface) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
50 panic("implement me")
51 }
52
53 func (m *mockRolloutInterface) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
54 panic("implement me")
55 }
56
57 func (m *mockRolloutInterface) Get(ctx context.Context, name string, opts metav1.GetOptions) (*alpha1.Rollout, error) {
58 panic("implement me")
59 }
60
61 func (m *mockRolloutInterface) List(ctx context.Context, opts metav1.ListOptions) (*alpha1.RolloutList, error) {
62 panic("implement me")
63 }
64
65 func (m *mockRolloutInterface) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
66 panic("implement me")
67 }
68
69 func (m *mockRolloutInterface) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *alpha1.Rollout, err error) {
70 m.latestName = name
71 m.latestPatchType = pt
72 m.patches = append(m.patches, string(data))
73 m.subresources = append(m.subresources, subresources...)
74 m.latestOptions = opts
75 return nil, nil
76 }
77
78 func TestRolloutCommand_RunWithClient(t *testing.T) {
79 type fields struct {
80 namespace string
81 rolloutName string
82 action rolloutAction
83 }
84 tests := []struct {
85 name string
86 fields fields
87 wantPatches []string
88 wantSubresources []string
89 wantErr assert.ErrorAssertionFunc
90 }{
91 {
92 name: "Pausing a rollout",
93 fields: fields{
94 namespace: "default",
95 rolloutName: "my-rollout",
96 action: rolloutActionPause,
97 },
98 wantPatches: []string{`{"spec":{"paused":true}}`},
99 wantErr: nil,
100 },
101 {
102 name: "Aborting a rollout",
103 fields: fields{
104 namespace: "default",
105 rolloutName: "my-rollout",
106 action: rolloutActionAbort,
107 },
108 wantPatches: []string{`{"status":{"abort":true}}`},
109 wantSubresources: []string{"status"},
110 wantErr: nil,
111 },
112 {
113 name: "Resume a rollout",
114 fields: fields{
115 namespace: "default",
116 rolloutName: "my-rollout",
117 action: rolloutActionResume,
118 },
119 wantPatches: []string{`{"spec":{"paused":false}}`, `{"status":{"abort":false}}`},
120 wantSubresources: []string{"status"},
121 wantErr: nil,
122 },
123 }
124
125 for _, tt := range tests {
126 t.Run(tt.name, func(t *testing.T) {
127 mockRolloutInterface := &mockRolloutInterface{}
128 mockRolloutsGetter := &mockRolloutsGetter{mockRolloutInterface: mockRolloutInterface}
129
130 mockRolloutsFactory := rolloutsGetterFactory(func() (v1alpha1.RolloutsGetter, error) {
131 return mockRolloutsGetter, nil
132 })
133
134 r := &rolloutCommand{
135 namespace: tt.fields.namespace,
136 rolloutName: tt.fields.rolloutName,
137 action: tt.fields.action,
138 }
139 ctx := dlog.NewTestContext(t, true)
140 err := r.RunWithClientFactory(ctx, mockRolloutsFactory)
141
142 assert.NoError(t, err)
143 assert.Equal(t, tt.fields.namespace, mockRolloutsGetter.latestNamespace)
144 assert.Equal(t, tt.fields.rolloutName, mockRolloutInterface.latestName)
145 assert.Equal(t, types.MergePatchType, mockRolloutInterface.latestPatchType)
146 assert.Equal(t, tt.wantPatches, mockRolloutInterface.patches)
147 assert.Equal(t, tt.wantSubresources, mockRolloutInterface.subresources)
148 assert.Equal(t, metav1.PatchOptions{}, mockRolloutInterface.latestOptions)
149 })
150 }
151 }
152
View as plain text