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

     1  package project
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/argoproj/argo-cd/v2/pkg/apiclient/session"
     8  
     9  	"github.com/argoproj/argo-cd/v2/pkg/apiclient/account"
    10  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
    11  	. "github.com/argoproj/argo-cd/v2/util/errors"
    12  	"github.com/argoproj/argo-cd/v2/util/io"
    13  )
    14  
    15  // this implements the "then" part of given/when/then
    16  type Consequences struct {
    17  	context *Context
    18  	actions *Actions
    19  }
    20  
    21  func (c *Consequences) And(block func(account *account.Account, err error)) *Consequences {
    22  	c.context.t.Helper()
    23  	block(c.get())
    24  	return c
    25  }
    26  
    27  func (c *Consequences) AndCLIOutput(block func(output string, err error)) *Consequences {
    28  	c.context.t.Helper()
    29  	block(c.actions.lastOutput, c.actions.lastError)
    30  	return c
    31  }
    32  
    33  func (c *Consequences) CurrentUser(block func(user *session.GetUserInfoResponse, err error)) *Consequences {
    34  	c.context.t.Helper()
    35  	block(c.getCurrentUser())
    36  	return c
    37  }
    38  
    39  func (c *Consequences) get() (*account.Account, error) {
    40  	_, accountClient, _ := fixture.ArgoCDClientset.NewAccountClient()
    41  	accList, err := accountClient.ListAccounts(context.Background(), &account.ListAccountRequest{})
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	for _, acc := range accList.Items {
    46  		if acc.Name == c.context.name {
    47  			return acc, nil
    48  		}
    49  	}
    50  	return nil, errors.New("account not found")
    51  }
    52  
    53  func (c *Consequences) getCurrentUser() (*session.GetUserInfoResponse, error) {
    54  	closer, client, err := fixture.ArgoCDClientset.NewSessionClient()
    55  	CheckError(err)
    56  	defer io.Close(closer)
    57  	return client.GetUserInfo(context.Background(), &session.GetUserInfoRequest{})
    58  }
    59  
    60  func (c *Consequences) Given() *Context {
    61  	return c.context
    62  }
    63  
    64  func (c *Consequences) When() *Actions {
    65  	return c.actions
    66  }