@@ -106,3 +106,169 @@ func TestARSAreCreated(t *testing.T) {
106106 t .Fatalf ("APIResourceSchema does not exist: %v" , err )
107107 }
108108}
109+
110+ func TestARSAreNotUpdated (t * testing.T ) {
111+ const (
112+ apiExportName = "example.com"
113+ )
114+
115+ ctx := context .Background ()
116+ ctrlruntime .SetLogger (logr .Discard ())
117+
118+ // setup a test environment in kcp
119+ orgKubconfig := utils .CreateOrganization (t , ctx , "ars-are-not-updated" , apiExportName )
120+
121+ // start a service cluster
122+ envtestKubeconfig , envtestClient , _ := utils .RunEnvtest (t , []string {
123+ "test/crds/crontab.yaml" ,
124+ })
125+
126+ // publish Crontabs
127+ t .Logf ("Publishing CronTabs…" )
128+ pr := & syncagentv1alpha1.PublishedResource {
129+ ObjectMeta : metav1.ObjectMeta {
130+ Name : "publish-crontabs" ,
131+ },
132+ Spec : syncagentv1alpha1.PublishedResourceSpec {
133+ Resource : syncagentv1alpha1.SourceResourceDescriptor {
134+ APIGroup : "example.com" ,
135+ Version : "v1" ,
136+ Kind : "CronTab" ,
137+ },
138+ },
139+ }
140+
141+ if err := envtestClient .Create (ctx , pr ); err != nil {
142+ t .Fatalf ("Failed to create PublishedResource: %v" , err )
143+ }
144+
145+ // let the agent do its thing
146+ utils .RunAgent (ctx , t , "bob" , orgKubconfig , envtestKubeconfig , apiExportName )
147+
148+ // wait for the APIExport to be updated
149+ t .Logf ("Waiting for APIExport to be updated…" )
150+ orgClient := utils .GetClient (t , orgKubconfig )
151+ apiExportKey := types.NamespacedName {Name : apiExportName }
152+
153+ var arsName string
154+ err := wait .PollUntilContextTimeout (ctx , 500 * time .Millisecond , 1 * time .Minute , false , func (ctx context.Context ) (done bool , err error ) {
155+ apiExport := & kcpapisv1alpha1.APIExport {}
156+ err = orgClient .Get (ctx , apiExportKey , apiExport )
157+ if err != nil {
158+ return false , err
159+ }
160+
161+ if len (apiExport .Spec .LatestResourceSchemas ) == 0 {
162+ return false , nil
163+ }
164+
165+ arsName = apiExport .Spec .LatestResourceSchemas [0 ]
166+
167+ return true , nil
168+ })
169+ if err != nil {
170+ t .Fatalf ("Failed to wait for APIExport to be updated: %v" , err )
171+ }
172+
173+ // update the CRD
174+ t .Logf ("Updating CRD (same version, but new schema)…" )
175+ utils .ApplyCRD (t , ctx , envtestClient , "test/crds/crontab-improved.yaml" )
176+
177+ // give the agent some time to do nothing
178+ time .Sleep (3 * time .Second )
179+
180+ // validate that the APIExport has *not* changed
181+ apiExport := & kcpapisv1alpha1.APIExport {}
182+ err = orgClient .Get (ctx , apiExportKey , apiExport )
183+ if err != nil {
184+ t .Fatalf ("APIExport disappeared: %v" , err )
185+ }
186+
187+ if l := len (apiExport .Spec .LatestResourceSchemas ); l != 1 {
188+ t .Fatalf ("APIExport should still have 1 resource schema, but has %d." , l )
189+ }
190+
191+ if currentName := apiExport .Spec .LatestResourceSchemas [0 ]; currentName != arsName {
192+ t .Fatalf ("APIExport should still refer to the original ARS %q, but now contains %q." , arsName , currentName )
193+ }
194+ }
195+
196+ func TestARSDropsAllVersionsExceptTheSelectedOne (t * testing.T ) {
197+ const (
198+ apiExportName = "example.com"
199+ theVersion = "v1"
200+ )
201+
202+ ctx := context .Background ()
203+ ctrlruntime .SetLogger (logr .Discard ())
204+
205+ // setup a test environment in kcp
206+ orgKubconfig := utils .CreateOrganization (t , ctx , "ars-drops-crd-versions" , apiExportName )
207+
208+ // start a service cluster
209+ envtestKubeconfig , envtestClient , _ := utils .RunEnvtest (t , []string {
210+ "test/crds/crontab-multi-versions.yaml" ,
211+ })
212+
213+ // publish Crontabs
214+ t .Logf ("Publishing CronTabs…" )
215+ pr := & syncagentv1alpha1.PublishedResource {
216+ ObjectMeta : metav1.ObjectMeta {
217+ Name : "publish-crontabs" ,
218+ },
219+ Spec : syncagentv1alpha1.PublishedResourceSpec {
220+ Resource : syncagentv1alpha1.SourceResourceDescriptor {
221+ APIGroup : "example.com" ,
222+ Version : theVersion ,
223+ Kind : "CronTab" ,
224+ },
225+ },
226+ }
227+
228+ if err := envtestClient .Create (ctx , pr ); err != nil {
229+ t .Fatalf ("Failed to create PublishedResource: %v" , err )
230+ }
231+
232+ // let the agent do its thing
233+ utils .RunAgent (ctx , t , "bob" , orgKubconfig , envtestKubeconfig , apiExportName )
234+
235+ // wait for the APIExport to be updated
236+ t .Logf ("Waiting for APIExport to be updated…" )
237+ orgClient := utils .GetClient (t , orgKubconfig )
238+ apiExportKey := types.NamespacedName {Name : apiExportName }
239+
240+ var arsName string
241+ err := wait .PollUntilContextTimeout (ctx , 500 * time .Millisecond , 1 * time .Minute , false , func (ctx context.Context ) (done bool , err error ) {
242+ apiExport := & kcpapisv1alpha1.APIExport {}
243+ err = orgClient .Get (ctx , apiExportKey , apiExport )
244+ if err != nil {
245+ return false , err
246+ }
247+
248+ if len (apiExport .Spec .LatestResourceSchemas ) == 0 {
249+ return false , nil
250+ }
251+
252+ arsName = apiExport .Spec .LatestResourceSchemas [0 ]
253+
254+ return true , nil
255+ })
256+ if err != nil {
257+ t .Fatalf ("Failed to wait for APIExport to be updated: %v" , err )
258+ }
259+
260+ // check the APIResourceSchema
261+ ars := & kcpapisv1alpha1.APIResourceSchema {}
262+ err = orgClient .Get (ctx , types.NamespacedName {Name : arsName }, ars )
263+ if err != nil {
264+ t .Fatalf ("APIResourceSchema does not exist: %v" , err )
265+ }
266+
267+ if len (ars .Spec .Versions ) != 1 {
268+ t .Fatalf ("Expected only one version to remain in ARS, but found %d." , len (ars .Spec .Versions ))
269+ }
270+
271+ if name := ars .Spec .Versions [0 ].Name ; name != theVersion {
272+ t .Fatalf ("Expected ARS to contain %q, but contains %q." , theVersion , name )
273+ }
274+ }
0 commit comments