github.com/argoproj/argo-cd/v2@v2.10.5/test/e2e/fixture/app/consequences.go (about)

     1  package app
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/argoproj/gitops-engine/pkg/health"
     8  	log "github.com/sirupsen/logrus"
     9  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	. "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    12  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
    13  	"github.com/argoproj/argo-cd/v2/util/errors"
    14  )
    15  
    16  // this implements the "then" part of given/when/then
    17  type Consequences struct {
    18  	context *Context
    19  	actions *Actions
    20  	timeout int
    21  }
    22  
    23  func (c *Consequences) Expect(e Expectation) *Consequences {
    24  	// this invocation makes sure this func is not reported as the cause of the failure - we are a "test helper"
    25  	c.context.t.Helper()
    26  	var message string
    27  	var state state
    28  	timeout := time.Duration(c.timeout) * time.Second
    29  	for start := time.Now(); time.Since(start) < timeout; time.Sleep(3 * time.Second) {
    30  		state, message = e(c)
    31  		switch state {
    32  		case succeeded:
    33  			log.Infof("expectation succeeded: %s", message)
    34  			return c
    35  		case failed:
    36  			c.context.t.Fatalf("failed expectation: %s", message)
    37  			return c
    38  		}
    39  		log.Infof("pending: %s", message)
    40  	}
    41  	c.context.t.Fatal("timeout waiting for: " + message)
    42  	return c
    43  }
    44  
    45  func (c *Consequences) And(block func(app *Application)) *Consequences {
    46  	c.context.t.Helper()
    47  	block(c.app())
    48  	return c
    49  }
    50  
    51  func (c *Consequences) AndAction(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() *Application {
    66  	app, err := c.get()
    67  	errors.CheckError(err)
    68  	return app
    69  }
    70  
    71  func (c *Consequences) get() (*Application, error) {
    72  	return fixture.AppClientset.ArgoprojV1alpha1().Applications(c.context.AppNamespace()).Get(context.Background(), c.context.AppName(), v1.GetOptions{})
    73  }
    74  
    75  func (c *Consequences) resource(kind, name, namespace string) ResourceStatus {
    76  	for _, r := range c.app().Status.Resources {
    77  		if r.Kind == kind && r.Name == name && (namespace == "" || namespace == r.Namespace) {
    78  			return r
    79  		}
    80  	}
    81  	return ResourceStatus{
    82  		Health: &HealthStatus{
    83  			Status:  health.HealthStatusMissing,
    84  			Message: "not found",
    85  		},
    86  	}
    87  }
    88  
    89  func (c *Consequences) AndCLIOutput(block func(output string, err error)) *Consequences {
    90  	c.context.t.Helper()
    91  	block(c.actions.lastOutput, c.actions.lastError)
    92  	return c
    93  }