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

     1  package project
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    12  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    13  )
    14  
    15  // this implements the "when" part of given/when/then
    16  //
    17  // none of the func implement error checks, and that is complete intended, you should check for errors
    18  // using the Then()
    19  type Actions struct {
    20  	context      *Context
    21  	lastError    error
    22  	ignoreErrors bool
    23  }
    24  
    25  func (a *Actions) IgnoreErrors() *Actions {
    26  	a.ignoreErrors = true
    27  	return a
    28  }
    29  
    30  func (a *Actions) DoNotIgnoreErrors() *Actions {
    31  	a.ignoreErrors = false
    32  	return a
    33  }
    34  
    35  func (a *Actions) Create(args ...string) *Actions {
    36  	args = a.prepareCreateArgs(args)
    37  
    38  	//  are you adding new context values? if you only use them for this func, then use args instead
    39  	a.runCli(args...)
    40  
    41  	return a
    42  }
    43  
    44  func (a *Actions) AddDestination(cluster string, namespace string) *Actions {
    45  	a.runCli("proj", "add-destination", a.context.name, cluster, namespace)
    46  	return a
    47  }
    48  
    49  func (a *Actions) AddDestinationServiceAccount(cluster string, namespace string) *Actions {
    50  	a.runCli("proj", "add-destination-service-account", a.context.name, cluster, namespace)
    51  	return a
    52  }
    53  
    54  func (a *Actions) AddSource(repo string) *Actions {
    55  	a.runCli("proj", "add-source", a.context.name, repo)
    56  	return a
    57  }
    58  
    59  func (a *Actions) UpdateProject(updater func(project *v1alpha1.AppProject)) *Actions {
    60  	proj, err := fixture.AppClientset.ArgoprojV1alpha1().AppProjects(fixture.TestNamespace()).Get(context.TODO(), a.context.name, metav1.GetOptions{})
    61  	require.NoError(a.context.t, err)
    62  	updater(proj)
    63  	_, err = fixture.AppClientset.ArgoprojV1alpha1().AppProjects(fixture.TestNamespace()).Update(context.TODO(), proj, metav1.UpdateOptions{})
    64  	require.NoError(a.context.t, err)
    65  	return a
    66  }
    67  
    68  func (a *Actions) Name(name string) *Actions {
    69  	a.context.name = name
    70  	return a
    71  }
    72  
    73  func (a *Actions) prepareCreateArgs(args []string) []string {
    74  	a.context.t.Helper()
    75  	args = append([]string{
    76  		"proj", "create", a.context.name,
    77  	}, args...)
    78  
    79  	if a.context.destination != "" {
    80  		args = append(args, "--dest", a.context.destination)
    81  	}
    82  
    83  	if len(a.context.sourceNamespaces) > 0 {
    84  		args = append(args, "--source-namespaces", strings.Join(a.context.sourceNamespaces, ","))
    85  	}
    86  
    87  	if len(a.context.repos) > 0 {
    88  		for _, repo := range a.context.repos {
    89  			args = append(args, "--src", repo)
    90  		}
    91  	}
    92  
    93  	if len(a.context.destinationServiceAccounts) != 0 {
    94  		for _, destinationServiceAccount := range a.context.destinationServiceAccounts {
    95  			args = append(args, "--dest-service-accounts", destinationServiceAccount)
    96  		}
    97  	}
    98  	return args
    99  }
   100  
   101  func (a *Actions) Delete() *Actions {
   102  	a.context.t.Helper()
   103  	a.runCli("proj", "delete", a.context.name)
   104  	return a
   105  }
   106  
   107  func (a *Actions) And(block func()) *Actions {
   108  	a.context.t.Helper()
   109  	block()
   110  	return a
   111  }
   112  
   113  func (a *Actions) Then() *Consequences {
   114  	a.context.t.Helper()
   115  	time.Sleep(fixture.WhenThenSleepInterval)
   116  	return &Consequences{a.context, a}
   117  }
   118  
   119  func (a *Actions) runCli(args ...string) {
   120  	a.context.t.Helper()
   121  	_, a.lastError = fixture.RunCli(args...)
   122  	if !a.ignoreErrors {
   123  		require.NoError(a.context.t, a.lastError)
   124  	}
   125  }