@@ -25,32 +25,84 @@ import (
2525
2626 "github.com/go-logr/logr"
2727
28+ syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
2829 "github.com/kcp-dev/api-syncagent/test/utils"
2930
31+ kcpapisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
32+
33+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+ "k8s.io/apimachinery/pkg/types"
35+ "k8s.io/apimachinery/pkg/util/wait"
3036 ctrlruntime "sigs.k8s.io/controller-runtime"
3137)
3238
3339func TestARSAreCreated (t * testing.T ) {
40+ const (
41+ apiExportName = "example.com"
42+ )
43+
3444 ctx := context .Background ()
3545 ctrlruntime .SetLogger (logr .Discard ())
3646
3747 // setup a test environment in kcp
38- fooKubconfig := utils .CreateHomeWorkspace (t , ctx , "foo " , "my-api" )
48+ orgKubconfig := utils .CreateOrganization (t , ctx , "ars-are-created " , apiExportName )
3949
4050 // start a service cluster
41- envtestKubeconfig , _ , _ := utils .RunEnvtest (t )
42-
43- t .Run ("my subtest" , func (t * testing.T ) {
44- utils .RunAgent (
45- ctx ,
46- t ,
47- "bob" ,
48- fooKubconfig ,
49- envtestKubeconfig ,
50- "my-api" ,
51- )
52-
53- time .Sleep (5 * time .Second )
51+ envtestKubeconfig , envtestClient , _ := utils .RunEnvtest (t , []string {
52+ "test/crds/crontab.yaml" ,
53+ })
54+
55+ // publish Crontabs
56+ t .Logf ("Publishing CronTabs…" )
57+ pr := & syncagentv1alpha1.PublishedResource {
58+ ObjectMeta : metav1.ObjectMeta {
59+ Name : "publish-crontabs" ,
60+ },
61+ Spec : syncagentv1alpha1.PublishedResourceSpec {
62+ Resource : syncagentv1alpha1.SourceResourceDescriptor {
63+ APIGroup : "example.com" ,
64+ Version : "v1" ,
65+ Kind : "CronTab" ,
66+ },
67+ },
68+ }
69+
70+ if err := envtestClient .Create (ctx , pr ); err != nil {
71+ t .Fatalf ("Failed to create PublishedResource: %v" , err )
72+ }
73+
74+ // let the agent do its thing
75+ utils .RunAgent (ctx , t , "bob" , orgKubconfig , envtestKubeconfig , apiExportName )
76+
77+ // wait for the APIExport to be updated
78+ t .Logf ("Waiting for APIExport to be updated…" )
79+ orgClient := utils .GetClient (t , orgKubconfig )
80+ apiExportKey := types.NamespacedName {Name : apiExportName }
81+
82+ var arsName string
83+ err := wait .PollUntilContextTimeout (ctx , 500 * time .Millisecond , 1 * time .Minute , false , func (ctx context.Context ) (done bool , err error ) {
84+ apiExport := & kcpapisv1alpha1.APIExport {}
85+ err = orgClient .Get (ctx , apiExportKey , apiExport )
86+ if err != nil {
87+ return false , err
88+ }
89+
90+ if len (apiExport .Spec .LatestResourceSchemas ) == 0 {
91+ return false , nil
92+ }
93+
94+ arsName = apiExport .Spec .LatestResourceSchemas [0 ]
95+
96+ return true , nil
5497 })
98+ if err != nil {
99+ t .Fatalf ("Failed to wait for APIExport to be updated: %v" , err )
100+ }
55101
102+ // check the APIResourceSchema
103+ ars := & kcpapisv1alpha1.APIResourceSchema {}
104+ err = orgClient .Get (ctx , types.NamespacedName {Name : arsName }, ars )
105+ if err != nil {
106+ t .Fatalf ("APIResourceSchema does not exist: %v" , err )
107+ }
56108}
0 commit comments