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

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