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

     1  package project
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  
    10  	"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    11  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
    12  )
    13  
    14  // this implements the "when" part of given/when/then
    15  //
    16  // none of the func implement error checks, and that is complete intended, you should check for errors
    17  // using the Then()
    18  type Actions struct {
    19  	context      *Context
    20  	lastOutput   string
    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) AddSource(repo string) *Actions {
    50  	a.runCli("proj", "add-source", a.context.name, repo)
    51  	return a
    52  }
    53  
    54  func (a *Actions) UpdateProject(updater func(project *v1alpha1.AppProject)) *Actions {
    55  	proj, err := fixture.AppClientset.ArgoprojV1alpha1().AppProjects(fixture.TestNamespace()).Get(context.TODO(), a.context.name, v1.GetOptions{})
    56  	require.NoError(a.context.t, err)
    57  	updater(proj)
    58  	_, err = fixture.AppClientset.ArgoprojV1alpha1().AppProjects(fixture.TestNamespace()).Update(context.TODO(), proj, v1.UpdateOptions{})
    59  	require.NoError(a.context.t, err)
    60  	return a
    61  }
    62  
    63  func (a *Actions) Name(name string) *Actions {
    64  	a.context.name = name
    65  	return a
    66  }
    67  
    68  func (a *Actions) prepareCreateArgs(args []string) []string {
    69  	a.context.t.Helper()
    70  	args = append([]string{
    71  		"proj", "create", a.context.name,
    72  	}, args...)
    73  
    74  	if a.context.destination != "" {
    75  		args = append(args, "--dest", a.context.destination)
    76  	}
    77  
    78  	if len(a.context.sourceNamespaces) > 0 {
    79  		args = append(args, "--source-namespaces", strings.Join(a.context.sourceNamespaces, ","))
    80  	}
    81  	return args
    82  }
    83  
    84  func (a *Actions) Delete() *Actions {
    85  	a.context.t.Helper()
    86  	a.runCli("proj", "delete", a.context.name)
    87  	return a
    88  }
    89  
    90  func (a *Actions) And(block func()) *Actions {
    91  	a.context.t.Helper()
    92  	block()
    93  	return a
    94  }
    95  
    96  func (a *Actions) Then() *Consequences {
    97  	a.context.t.Helper()
    98  	return &Consequences{a.context, a}
    99  }
   100  
   101  func (a *Actions) runCli(args ...string) {
   102  	a.context.t.Helper()
   103  	a.lastOutput, a.lastError = fixture.RunCli(args...)
   104  	if !a.ignoreErrors {
   105  		require.Empty(a.context.t, a.lastError)
   106  	}
   107  }