@@ -35,6 +35,7 @@ import (
35
35
artifactv1 "github.com/openfluxcd/artifact/api/v1alpha1"
36
36
corev1 "k8s.io/api/core/v1"
37
37
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
38
+ "k8s.io/apimachinery/pkg/api/errors"
38
39
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39
40
. "ocm.software/ocm/api/helper/builder"
40
41
environment "ocm.software/ocm/api/helper/env"
@@ -65,11 +66,10 @@ const (
65
66
66
67
var _ = Describe ("Resource Controller" , func () {
67
68
var (
68
- ctx context.Context
69
- cancel context.CancelFunc
70
69
env * Builder
71
70
ctfPath string
72
71
)
72
+
73
73
BeforeEach (func () {
74
74
ctfPath = Must (os .MkdirTemp ("" , CTFPath ))
75
75
DeferCleanup (func () error {
@@ -78,31 +78,29 @@ var _ = Describe("Resource Controller", func() {
78
78
79
79
env = NewBuilder (environment .FileSystem (osfs .OsFs ))
80
80
DeferCleanup (env .Cleanup )
81
-
82
- ctx , cancel = context .WithCancel (context .Background ())
83
- DeferCleanup (cancel )
84
81
})
85
82
86
83
Context ("resource controller" , func () {
87
84
var (
88
85
componentName string
86
+ resourceName string
89
87
testNumber int
90
- //componentObj *v1alpha1.Component
91
88
)
92
89
93
- BeforeEach (func () {
90
+ BeforeEach (func (ctx SpecContext ) {
94
91
By ("mocking the component controller" )
95
92
componentName = fmt .Sprintf ("%s-%d" , ComponentObj , testNumber )
93
+ resourceName = fmt .Sprintf ("%s-%d" , ResourceObj , testNumber )
96
94
mockComponentReconciler (ctx , env , ctfPath , componentName )
95
+ testNumber ++
97
96
})
98
97
99
- It ("can reconcile a resource" , func () {
100
-
98
+ FIt ("can reconcile a resource" , func (ctx SpecContext ) {
101
99
By ("creating a resource object" )
102
100
resource := & v1alpha1.Resource {
103
101
ObjectMeta : metav1.ObjectMeta {
104
102
Namespace : Namespace ,
105
- Name : ResourceObj ,
103
+ Name : resourceName ,
106
104
},
107
105
Spec : v1alpha1.ResourceSpec {
108
106
ComponentRef : corev1.LocalObjectReference {
@@ -117,26 +115,23 @@ var _ = Describe("Resource Controller", func() {
117
115
},
118
116
}
119
117
Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
120
- DeferCleanup (func (ctx SpecContext ) {
121
- Expect (k8sClient .Delete (ctx , resource , client .PropagationPolicy (metav1 .DeletePropagationForeground ))).To (Succeed ())
122
- })
123
118
124
119
By ("checking that the resource has been reconciled successfully" )
125
- Eventually (komega .Object (resource ), "5m " ).Should (
120
+ Eventually (komega .Object (resource ), "15s" , "100ms " ).Should (
126
121
HaveField ("Status.ObservedGeneration" , Equal (int64 (1 ))))
127
122
Expect (resource ).To (HaveField ("Status.ArtifactRef.Name" , Not (BeEmpty ())))
128
123
Expect (resource ).To (HaveField ("Status.Resource.Name" , Equal (ResourceObj )))
129
124
Expect (resource ).To (HaveField ("Status.Resource.Type" , Equal (artifacttypes .PLAIN_TEXT )))
130
125
Expect (resource ).To (HaveField ("Status.Resource.Version" , Equal (ResourceVersion )))
131
126
132
- By ("checking that the artifact has been created successfully " )
127
+ By ("checking that the resource artifact was created" )
133
128
artifact := & artifactv1.Artifact {
134
129
ObjectMeta : metav1.ObjectMeta {
135
130
Namespace : resource .Namespace ,
136
131
Name : resource .Status .ArtifactRef .Name ,
137
132
},
138
133
}
139
- Eventually (komega .Get (artifact )).Should (Succeed ())
134
+ Eventually (komega .Get (artifact ), "15s" , "100ms" ).Should (Succeed ())
140
135
141
136
By ("checking that the artifact server provides the resource" )
142
137
r := Must (http .Get (artifact .Spec .URL ))
@@ -152,11 +147,59 @@ var _ = Describe("Resource Controller", func() {
152
147
resourceContent := Must (io .ReadAll (reader ))
153
148
154
149
Expect (string (resourceContent )).To (Equal (ResourceContent ))
150
+
151
+ By ("checking that the resource is deleted correctly" )
152
+ Expect (k8sClient .Delete (ctx , resource )).To (Succeed ())
153
+ Eventually (komega .Object (resource ), "15s" ).Should (HaveField ("Status.DeletionTimestamp" , Not (BeNil ())))
154
+ Expect (k8sClient .Get (ctx , client .ObjectKeyFromObject (resource ), resource )).To (Succeed ())
155
+
156
+ // Simulate ownerreference deletion
157
+ Expect (k8sClient .Delete (ctx , artifact )).To (Succeed ())
158
+
159
+ Eventually (func (ctx SpecContext ) error {
160
+ return k8sClient .Get (ctx , client .ObjectKeyFromObject (resource ), resource )
161
+ }).WithContext (ctx ).Should (Satisfy (errors .IsNotFound ))
162
+ })
163
+
164
+ It ("stops when requirements are not met: component not found" , func (ctx SpecContext ) {
165
+ By ("creating a resource object" )
166
+ resource := & v1alpha1.Resource {
167
+ ObjectMeta : metav1.ObjectMeta {
168
+ Namespace : Namespace ,
169
+ Name : resourceName ,
170
+ },
171
+ Spec : v1alpha1.ResourceSpec {
172
+ ComponentRef : corev1.LocalObjectReference {
173
+ Name : "anotherName" ,
174
+ },
175
+ Resource : v1alpha1.ResourceID {
176
+ ByReference : v1alpha1.ResourceReference {
177
+ Resource : v1 .NewIdentity (ResourceObj ),
178
+ },
179
+ },
180
+ Interval : metav1.Duration {Duration : time .Minute * 5 },
181
+ },
182
+ }
183
+ Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
184
+ DeferCleanup (func (ctx SpecContext ) {
185
+ Expect (k8sClient .Delete (ctx , resource , client .PropagationPolicy (metav1 .DeletePropagationForeground ))).To (Succeed ())
186
+ })
187
+
188
+ By ("checking that the resource was not reconciled successfully" )
189
+ Eventually (komega .Object (resource ), "5m" ).Should (
190
+ HaveField ("Status.ObservedGeneration" , Equal (int64 (1 ))))
191
+ Expect (resource ).To (HaveField ("Status.ArtifactRef.Name" , Not (BeEmpty ())))
192
+ Expect (resource ).To (HaveField ("Status.Resource.Name" , Equal (ResourceObj )))
193
+ Expect (resource ).To (HaveField ("Status.Resource.Type" , Equal (artifacttypes .PLAIN_TEXT )))
194
+ Expect (resource ).To (HaveField ("Status.Resource.Version" , Equal (ResourceVersion )))
195
+
155
196
})
156
197
})
157
198
})
158
199
159
200
func mockComponentReconciler (ctx context.Context , env * Builder , ctfPath , name string ) {
201
+ GinkgoHelper ()
202
+
160
203
// Create a ctf storing a component-version with a blob data as content
161
204
env .OCMCommonTransport (ctfPath , accessio .FormatDirectory , func () {
162
205
env .Component (Component , func () {
0 commit comments