github.com/argoproj/argo-cd/v2@v2.10.5/test/e2e/fixture/applicationsets/consequences.go (about) 1 package applicationsets 2 3 import ( 4 "context" 5 "encoding/json" 6 "time" 7 8 "github.com/argoproj/argo-cd/v2/test/e2e/fixture" 9 10 "github.com/argoproj/pkg/errors" 11 log "github.com/sirupsen/logrus" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/client-go/dynamic" 14 15 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" 16 "github.com/argoproj/argo-cd/v2/test/e2e/fixture/applicationsets/utils" 17 ) 18 19 // this implements the "then" part of given/when/then 20 type Consequences struct { 21 context *Context 22 actions *Actions 23 } 24 25 func (c *Consequences) Expect(e Expectation) *Consequences { 26 return c.ExpectWithDuration(e, time.Duration(30)*time.Second) 27 } 28 29 func (c *Consequences) ExpectWithDuration(e Expectation, timeout time.Duration) *Consequences { 30 31 // this invocation makes sure this func is not reported as the cause of the failure - we are a "test helper" 32 c.context.t.Helper() 33 var message string 34 var state state 35 for start := time.Now(); time.Since(start) < timeout; time.Sleep(3 * time.Second) { 36 state, message = e(c) 37 switch state { 38 case succeeded: 39 log.Infof("expectation succeeded: %s", message) 40 return c 41 case failed: 42 c.context.t.Fatalf("failed expectation: %s", message) 43 return c 44 } 45 log.Infof("expectation pending: %s", message) 46 } 47 c.context.t.Fatal("timeout waiting for: " + message) 48 return c 49 } 50 51 func (c *Consequences) And(block func()) *Consequences { 52 c.context.t.Helper() 53 block() 54 return c 55 } 56 57 func (c *Consequences) Given() *Context { 58 return c.context 59 } 60 61 func (c *Consequences) When() *Actions { 62 return c.actions 63 } 64 65 func (c *Consequences) app(name string) *v1alpha1.Application { 66 apps := c.apps() 67 68 for index, app := range apps { 69 if app.Name == name { 70 return &apps[index] 71 } 72 } 73 74 return nil 75 } 76 77 func (c *Consequences) apps() []v1alpha1.Application { 78 79 var namespace string 80 if c.context.switchToNamespace != "" { 81 namespace = string(c.context.switchToNamespace) 82 } else { 83 namespace = fixture.TestNamespace() 84 } 85 86 fixtureClient := utils.GetE2EFixtureK8sClient() 87 list, err := fixtureClient.AppClientset.ArgoprojV1alpha1().Applications(namespace).List(context.Background(), metav1.ListOptions{}) 88 errors.CheckError(err) 89 90 if list == nil { 91 list = &v1alpha1.ApplicationList{} 92 } 93 94 return list.Items 95 } 96 97 func (c *Consequences) applicationSet(applicationSetName string) *v1alpha1.ApplicationSet { 98 99 fixtureClient := utils.GetE2EFixtureK8sClient() 100 101 var appSetClientSet dynamic.ResourceInterface 102 103 if c.context.switchToNamespace != "" { 104 appSetClientSet = fixtureClient.ExternalAppSetClientsets[c.context.switchToNamespace] 105 } else { 106 appSetClientSet = fixtureClient.AppSetClientset 107 } 108 109 list, err := appSetClientSet.Get(context.Background(), c.actions.context.name, metav1.GetOptions{}) 110 errors.CheckError(err) 111 112 var appSet v1alpha1.ApplicationSet 113 114 bytes, err := list.MarshalJSON() 115 if err != nil { 116 return &v1alpha1.ApplicationSet{} 117 } 118 119 err = json.Unmarshal(bytes, &appSet) 120 if err != nil { 121 return &v1alpha1.ApplicationSet{} 122 } 123 124 if appSet.Name == applicationSetName { 125 return &appSet 126 } 127 128 return &v1alpha1.ApplicationSet{} 129 }