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