@@ -24,12 +24,14 @@ import (
2424 "time"
2525
2626 "github.com/go-logr/logr"
27+ "github.com/google/go-cmp/cmp"
2728
2829 syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
2930 "github.com/kcp-dev/api-syncagent/test/utils"
3031
3132 kcpapisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
3233
34+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3335 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3436 "k8s.io/apimachinery/pkg/types"
3537 "k8s.io/apimachinery/pkg/util/wait"
@@ -272,3 +274,106 @@ func TestARSDropsAllVersionsExceptTheSelectedOne(t *testing.T) {
272274 t .Fatalf ("Expected ARS to contain %q, but contains %q." , theVersion , name )
273275 }
274276}
277+
278+ func TestProjection (t * testing.T ) {
279+ const (
280+ apiExportName = "example.com"
281+ originalVersion = "v1"
282+ )
283+
284+ ctx := context .Background ()
285+ ctrlruntime .SetLogger (logr .Discard ())
286+
287+ // setup a test environment in kcp
288+ orgKubconfig := utils .CreateOrganization (t , ctx , "ars-projections" , apiExportName )
289+
290+ // start a service cluster
291+ envtestKubeconfig , envtestClient , _ := utils .RunEnvtest (t , []string {
292+ "test/crds/crontab.yaml" ,
293+ })
294+
295+ // publish Crontabs
296+ t .Logf ("Publishing CronTabs…" )
297+ pr := & syncagentv1alpha1.PublishedResource {
298+ ObjectMeta : metav1.ObjectMeta {
299+ Name : "publish-crontabs" ,
300+ },
301+ Spec : syncagentv1alpha1.PublishedResourceSpec {
302+ Resource : syncagentv1alpha1.SourceResourceDescriptor {
303+ APIGroup : "example.com" ,
304+ Version : originalVersion ,
305+ Kind : "CronTab" ,
306+ },
307+ Projection : & syncagentv1alpha1.ResourceProjection {
308+ Version : "v6" ,
309+ Scope : syncagentv1alpha1 .ClusterScoped ,
310+ Kind : "CronusTabulatus" ,
311+ Plural : "cronustabulati" ,
312+ ShortNames : []string {"cront" },
313+ },
314+ },
315+ }
316+
317+ if err := envtestClient .Create (ctx , pr ); err != nil {
318+ t .Fatalf ("Failed to create PublishedResource: %v" , err )
319+ }
320+
321+ // let the agent do its thing
322+ utils .RunAgent (ctx , t , "bob" , orgKubconfig , envtestKubeconfig , apiExportName )
323+
324+ // wait for the APIExport to be updated
325+ t .Logf ("Waiting for APIExport to be updated…" )
326+ orgClient := utils .GetClient (t , orgKubconfig )
327+ apiExportKey := types.NamespacedName {Name : apiExportName }
328+
329+ var arsName string
330+ err := wait .PollUntilContextTimeout (ctx , 500 * time .Millisecond , 1 * time .Minute , false , func (ctx context.Context ) (done bool , err error ) {
331+ apiExport := & kcpapisv1alpha1.APIExport {}
332+ err = orgClient .Get (ctx , apiExportKey , apiExport )
333+ if err != nil {
334+ return false , err
335+ }
336+
337+ if len (apiExport .Spec .LatestResourceSchemas ) == 0 {
338+ return false , nil
339+ }
340+
341+ arsName = apiExport .Spec .LatestResourceSchemas [0 ]
342+
343+ return true , nil
344+ })
345+ if err != nil {
346+ t .Fatalf ("Failed to wait for APIExport to be updated: %v" , err )
347+ }
348+
349+ // check the APIResourceSchema
350+ ars := & kcpapisv1alpha1.APIResourceSchema {}
351+ err = orgClient .Get (ctx , types.NamespacedName {Name : arsName }, ars )
352+ if err != nil {
353+ t .Fatalf ("APIResourceSchema does not exist: %v" , err )
354+ }
355+
356+ if len (ars .Spec .Versions ) != 1 {
357+ t .Fatalf ("Expected only one version to remain in ARS, but found %d." , len (ars .Spec .Versions ))
358+ }
359+
360+ if name := ars .Spec .Versions [0 ].Name ; name != pr .Spec .Projection .Version {
361+ t .Errorf ("Expected ARS to contain version %q, but contains %q." , pr .Spec .Projection .Version , name )
362+ }
363+
364+ if ars .Spec .Scope != apiextensionsv1 .ResourceScope (pr .Spec .Projection .Scope ) {
365+ t .Errorf ("Expected ARS to be of scope %q, but is %q." , pr .Spec .Projection .Scope , ars .Spec .Scope )
366+ }
367+
368+ if ars .Spec .Names .Kind != pr .Spec .Projection .Kind {
369+ t .Errorf ("Expected ARS to be kind %q, but is %q." , pr .Spec .Projection .Kind , ars .Spec .Names .Kind )
370+ }
371+
372+ if ars .Spec .Names .Plural != pr .Spec .Projection .Plural {
373+ t .Errorf ("Expected ARS to have plural name %q, but has %q." , pr .Spec .Projection .Plural , ars .Spec .Names .Plural )
374+ }
375+
376+ if ! cmp .Equal (ars .Spec .Names .ShortNames , pr .Spec .Projection .ShortNames ) {
377+ t .Errorf ("Expected ARS to have short names %v, but has %v." , pr .Spec .Projection .ShortNames , ars .Spec .Names .ShortNames )
378+ }
379+ }
0 commit comments