github.com/argoproj/argo-cd@v1.8.7/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/pkg/apis/application/v1alpha1"
    12  	"github.com/argoproj/argo-cd/test/e2e/fixture"
    13  	"github.com/argoproj/argo-cd/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  }
    21  
    22  func (c *Consequences) Expect(e Expectation) *Consequences {
    23  	// this invocation makes sure this func is not reported as the cause of the failure - we are a "test helper"
    24  	c.context.t.Helper()
    25  	var message string
    26  	var state state
    27  	timeout := time.Duration(15) * time.Second
    28  	for start := time.Now(); time.Since(start) < timeout; time.Sleep(3 * time.Second) {
    29  		state, message = e(c)
    30  		switch state {
    31  		case succeeded:
    32  			log.Infof("expectation succeeded: %s", message)
    33  			return c
    34  		case failed:
    35  			c.context.t.Fatalf("failed expectation: %s", message)
    36  			return c
    37  		}
    38  		log.Infof("pending: %s", message)
    39  	}
    40  	c.context.t.Fatal("timeout waiting for: " + message)
    41  	return c
    42  }
    43  
    44  func (c *Consequences) And(block func(app *Application)) *Consequences {
    45  	c.context.t.Helper()
    46  	block(c.app())
    47  	return c
    48  }
    49  
    50  func (c *Consequences) Given() *Context {
    51  	return c.context
    52  }
    53  
    54  func (c *Consequences) When() *Actions {
    55  	return c.actions
    56  }
    57  
    58  func (c *Consequences) app() *Application {
    59  	app, err := c.get()
    60  	errors.CheckError(err)
    61  	return app
    62  }
    63  
    64  func (c *Consequences) get() (*Application, error) {
    65  	return fixture.AppClientset.ArgoprojV1alpha1().Applications(fixture.ArgoCDNamespace).Get(context.Background(), c.context.name, v1.GetOptions{})
    66  }
    67  
    68  func (c *Consequences) resource(kind, name, namespace string) ResourceStatus {
    69  	for _, r := range c.app().Status.Resources {
    70  		if r.Kind == kind && r.Name == name && (namespace == "" || namespace == r.Namespace) {
    71  			return r
    72  		}
    73  	}
    74  	return ResourceStatus{
    75  		Health: &HealthStatus{
    76  			Status:  health.HealthStatusMissing,
    77  			Message: "not found",
    78  		},
    79  	}
    80  }