15
15
package approval
16
16
17
17
import (
18
+ context "context"
19
+ "fmt"
18
20
"testing"
19
21
"time"
20
22
23
+ "github.com/stretchr/testify/mock"
24
+
21
25
porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
22
26
"github.com/stretchr/testify/require"
23
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
+ "github.com/nephio-project/nephio/controllers/pkg/mocks/external/client"
24
29
)
25
30
26
31
func TestShouldProcess (t * testing.T ) {
@@ -117,7 +122,7 @@ func TestManageDelay(t *testing.T) {
117
122
"not old enough" : {
118
123
pr : porchapi.PackageRevision {
119
124
ObjectMeta : metav1.ObjectMeta {
120
- CreationTimestamp : metav1.Time {now },
125
+ CreationTimestamp : metav1.Time {Time : now },
121
126
Annotations : map [string ]string {
122
127
"approval.nephio.org/delay" : "1h" ,
123
128
},
@@ -129,7 +134,7 @@ func TestManageDelay(t *testing.T) {
129
134
"old enough" : {
130
135
pr : porchapi.PackageRevision {
131
136
ObjectMeta : metav1.ObjectMeta {
132
- CreationTimestamp : metav1.Time {now .AddDate (- 1 , 0 , 0 )},
137
+ CreationTimestamp : metav1.Time {Time : now .AddDate (- 1 , 0 , 0 )},
133
138
Annotations : map [string ]string {
134
139
"approval.nephio.org/delay" : "1h" ,
135
140
},
@@ -147,3 +152,86 @@ func TestManageDelay(t *testing.T) {
147
152
})
148
153
}
149
154
}
155
+
156
+
157
+
158
+ func TestPolicyInitial (t * testing.T ) {
159
+
160
+ testCases := map [string ]struct {
161
+ pr porchapi.PackageRevision
162
+ prl * porchapi.PackageRevisionList
163
+ expectedApprove bool
164
+ expectedError error
165
+ mockReturnErr error
166
+ }{
167
+ "Draft with proposed lifecycle" : {
168
+ pr : porchapi.PackageRevision {
169
+ ObjectMeta : metav1.ObjectMeta {
170
+ Annotations : map [string ]string {
171
+ "approval.nephio.org/policy" : "initial" ,
172
+ },
173
+ },
174
+ },
175
+ prl : & porchapi.PackageRevisionList {
176
+ TypeMeta : metav1.TypeMeta {
177
+ APIVersion : "Blah" ,
178
+ Kind : "Blah" ,
179
+ },
180
+ Items : []porchapi.PackageRevision {
181
+ {
182
+ Spec : porchapi.PackageRevisionSpec {
183
+ Lifecycle : porchapi .PackageRevisionLifecycleProposed ,
184
+ },
185
+ },
186
+ },
187
+ },
188
+ expectedApprove : true ,
189
+ expectedError : nil ,
190
+ mockReturnErr : nil ,
191
+ },
192
+ "Draft with existing version" : {
193
+ pr : porchapi.PackageRevision {
194
+ Spec : porchapi.PackageRevisionSpec {
195
+ RepositoryName : "MyRepo" ,
196
+ PackageName : "MyPackage" ,
197
+ },
198
+ },
199
+ prl : & porchapi.PackageRevisionList {
200
+ Items : []porchapi.PackageRevision {
201
+ {
202
+ Spec : porchapi.PackageRevisionSpec {
203
+ Lifecycle : porchapi .PackageRevisionLifecyclePublished ,
204
+ RepositoryName : "MyRepo" ,
205
+ PackageName : "MyPackage" ,
206
+ },
207
+ },
208
+ },
209
+ },
210
+ expectedApprove : false ,
211
+ expectedError : nil ,
212
+ mockReturnErr : nil ,
213
+ },
214
+ "runtime client list failure" : {
215
+ pr : porchapi.PackageRevision {},
216
+ prl : & porchapi.PackageRevisionList {},
217
+ expectedApprove : false ,
218
+ expectedError : fmt .Errorf ("Failed to list items" ),
219
+ mockReturnErr : fmt .Errorf ("Failed to list items" ),
220
+ },
221
+ }
222
+ for tn , tc := range testCases {
223
+ // Create a new instance of the mock object
224
+ clientMock := new (mocks.MockClient )
225
+ clientMock .On ("List" , context .TODO (), mock .AnythingOfType ("*v1alpha1.PackageRevisionList" )).Return (tc .mockReturnErr ).Run (func (args mock.Arguments ) {
226
+ packRevList := args .Get (1 ).(* porchapi.PackageRevisionList )
227
+ * packRevList = * tc .prl // tc.prl is what r.Get will store in 2nd Argument
228
+ })
229
+ // Create an instance of the component under test
230
+ r := reconciler {baseClient : clientMock }
231
+ t .Run (tn , func (t * testing.T ) {
232
+ actualApproval , actualError := r .policyInitial (context .TODO (), & tc .pr )
233
+ require .Equal (t , tc .expectedApprove , actualApproval )
234
+ require .Equal (t , tc .expectedError , actualError )
235
+ })
236
+ }
237
+ }
0 commit comments