github.com/argoproj/argo-cd/v2@v2.10.9/test/e2e/fixture/app/context.go (about)

     1  package app
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
     8  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
     9  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture/certs"
    10  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture/gpgkeys"
    11  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture/repos"
    12  	"github.com/argoproj/argo-cd/v2/util/argo"
    13  	"github.com/argoproj/argo-cd/v2/util/env"
    14  	"github.com/argoproj/argo-cd/v2/util/settings"
    15  )
    16  
    17  // this implements the "given" part of given/when/then
    18  type Context struct {
    19  	t           *testing.T
    20  	path        string
    21  	chart       string
    22  	repoURLType fixture.RepoURLType
    23  	// seconds
    24  	timeout                int
    25  	name                   string
    26  	appNamespace           string
    27  	destServer             string
    28  	destName               string
    29  	env                    string
    30  	parameters             []string
    31  	namePrefix             string
    32  	nameSuffix             string
    33  	resource               string
    34  	prune                  bool
    35  	configManagementPlugin string
    36  	async                  bool
    37  	localPath              string
    38  	project                string
    39  	revision               string
    40  	force                  bool
    41  	applyOutOfSyncOnly     bool
    42  	directoryRecurse       bool
    43  	replace                bool
    44  	helmPassCredentials    bool
    45  	helmSkipCrds           bool
    46  	trackingMethod         v1alpha1.TrackingMethod
    47  	sources                []v1alpha1.ApplicationSource
    48  }
    49  
    50  type ContextArgs struct {
    51  	AppNamespace string
    52  }
    53  
    54  func Given(t *testing.T, opts ...fixture.TestOption) *Context {
    55  	fixture.EnsureCleanState(t, opts...)
    56  	return GivenWithSameState(t)
    57  }
    58  
    59  func GivenWithNamespace(t *testing.T, namespace string) *Context {
    60  	ctx := Given(t)
    61  	ctx.appNamespace = namespace
    62  	return ctx
    63  }
    64  
    65  func GivenWithSameState(t *testing.T) *Context {
    66  	// ARGOCE_E2E_DEFAULT_TIMEOUT can be used to override the default timeout
    67  	// for any context.
    68  	timeout := env.ParseNumFromEnv("ARGOCD_E2E_DEFAULT_TIMEOUT", 20, 0, 180)
    69  	return &Context{
    70  		t:              t,
    71  		destServer:     v1alpha1.KubernetesInternalAPIServerAddr,
    72  		repoURLType:    fixture.RepoURLTypeFile,
    73  		name:           fixture.Name(),
    74  		timeout:        timeout,
    75  		project:        "default",
    76  		prune:          true,
    77  		trackingMethod: argo.TrackingMethodLabel,
    78  	}
    79  }
    80  
    81  func (c *Context) AppName() string {
    82  	return c.name
    83  }
    84  
    85  func (c *Context) AppQualifiedName() string {
    86  	if c.appNamespace != "" {
    87  		return c.appNamespace + "/" + c.AppName()
    88  	} else {
    89  		return c.AppName()
    90  	}
    91  }
    92  
    93  func (c *Context) AppNamespace() string {
    94  	if c.appNamespace != "" {
    95  		return c.appNamespace
    96  	} else {
    97  		return fixture.TestNamespace()
    98  	}
    99  }
   100  
   101  func (c *Context) SetAppNamespace(namespace string) *Context {
   102  	c.appNamespace = namespace
   103  	//fixture.SetParamInSettingConfigMap("application.resourceTrackingMethod", "annotation")
   104  	return c
   105  }
   106  
   107  func (c *Context) GPGPublicKeyAdded() *Context {
   108  	gpgkeys.AddGPGPublicKey()
   109  	return c
   110  }
   111  
   112  func (c *Context) GPGPublicKeyRemoved() *Context {
   113  	gpgkeys.DeleteGPGPublicKey()
   114  	return c
   115  }
   116  
   117  func (c *Context) CustomCACertAdded() *Context {
   118  	certs.AddCustomCACert()
   119  	return c
   120  }
   121  
   122  func (c *Context) CustomSSHKnownHostsAdded() *Context {
   123  	certs.AddCustomSSHKnownHostsKeys()
   124  	return c
   125  }
   126  
   127  func (c *Context) HTTPSRepoURLAdded(withCreds bool) *Context {
   128  	repos.AddHTTPSRepo(false, withCreds, fixture.RepoURLTypeHTTPS)
   129  	return c
   130  }
   131  
   132  func (c *Context) HTTPSInsecureRepoURLAdded(withCreds bool) *Context {
   133  	repos.AddHTTPSRepo(true, withCreds, fixture.RepoURLTypeHTTPS)
   134  	return c
   135  }
   136  
   137  func (c *Context) HTTPSInsecureRepoURLWithClientCertAdded() *Context {
   138  	repos.AddHTTPSRepoClientCert(true)
   139  	return c
   140  }
   141  
   142  func (c *Context) HTTPSRepoURLWithClientCertAdded() *Context {
   143  	repos.AddHTTPSRepoClientCert(false)
   144  	return c
   145  }
   146  
   147  func (c *Context) SubmoduleHTTPSRepoURLAdded(withCreds bool) *Context {
   148  	fixture.CreateSubmoduleRepos("https")
   149  	repos.AddHTTPSRepo(false, withCreds, fixture.RepoURLTypeHTTPSSubmoduleParent)
   150  	return c
   151  }
   152  
   153  func (c *Context) SSHRepoURLAdded(withCreds bool) *Context {
   154  	repos.AddSSHRepo(false, withCreds, fixture.RepoURLTypeSSH)
   155  	return c
   156  }
   157  
   158  func (c *Context) SSHInsecureRepoURLAdded(withCreds bool) *Context {
   159  	repos.AddSSHRepo(true, withCreds, fixture.RepoURLTypeSSH)
   160  	return c
   161  }
   162  
   163  func (c *Context) SubmoduleSSHRepoURLAdded(withCreds bool) *Context {
   164  	fixture.CreateSubmoduleRepos("ssh")
   165  	repos.AddSSHRepo(false, withCreds, fixture.RepoURLTypeSSHSubmoduleParent)
   166  	return c
   167  }
   168  
   169  func (c *Context) HelmRepoAdded(name string) *Context {
   170  	repos.AddHelmRepo(name)
   171  	return c
   172  }
   173  
   174  func (c *Context) HelmOCIRepoAdded(name string) *Context {
   175  	repos.AddHelmOCIRepo(name)
   176  	return c
   177  }
   178  
   179  func (c *Context) PushChartToOCIRegistry(chartPathName, chartName, chartVersion string) *Context {
   180  	repos.PushChartToOCIRegistry(chartPathName, chartName, chartVersion)
   181  	return c
   182  }
   183  
   184  func (c *Context) HTTPSCredentialsUserPassAdded() *Context {
   185  	repos.AddHTTPSCredentialsUserPass()
   186  	return c
   187  }
   188  
   189  func (c *Context) HelmHTTPSCredentialsUserPassAdded() *Context {
   190  	repos.AddHelmHTTPSCredentialsTLSClientCert()
   191  	return c
   192  }
   193  
   194  func (c *Context) HelmoOCICredentialsWithoutUserPassAdded() *Context {
   195  	repos.AddHelmoOCICredentialsWithoutUserPass()
   196  	return c
   197  }
   198  
   199  func (c *Context) HTTPSCredentialsTLSClientCertAdded() *Context {
   200  	repos.AddHTTPSCredentialsTLSClientCert()
   201  	return c
   202  }
   203  
   204  func (c *Context) SSHCredentialsAdded() *Context {
   205  	repos.AddSSHCredentials()
   206  	return c
   207  }
   208  
   209  func (c *Context) ProjectSpec(spec v1alpha1.AppProjectSpec) *Context {
   210  	fixture.SetProjectSpec(c.project, spec)
   211  	return c
   212  }
   213  
   214  func (c *Context) Replace() *Context {
   215  	c.replace = true
   216  	return c
   217  }
   218  
   219  func (c *Context) RepoURLType(urlType fixture.RepoURLType) *Context {
   220  	c.repoURLType = urlType
   221  	return c
   222  }
   223  
   224  func (c *Context) GetName() string {
   225  	return c.name
   226  }
   227  
   228  func (c *Context) Name(name string) *Context {
   229  	c.name = name
   230  	return c
   231  }
   232  
   233  func (c *Context) Path(path string) *Context {
   234  	c.path = path
   235  	return c
   236  }
   237  
   238  func (c *Context) Recurse() *Context {
   239  	c.directoryRecurse = true
   240  	return c
   241  }
   242  
   243  func (c *Context) Chart(chart string) *Context {
   244  	c.chart = chart
   245  	return c
   246  }
   247  
   248  func (c *Context) Revision(revision string) *Context {
   249  	c.revision = revision
   250  	return c
   251  }
   252  
   253  func (c *Context) Timeout(timeout int) *Context {
   254  	c.timeout = timeout
   255  	return c
   256  }
   257  
   258  func (c *Context) DestServer(destServer string) *Context {
   259  	c.destServer = destServer
   260  	return c
   261  }
   262  
   263  func (c *Context) DestName(destName string) *Context {
   264  	c.destName = destName
   265  	return c
   266  }
   267  
   268  func (c *Context) Env(env string) *Context {
   269  	c.env = env
   270  	return c
   271  }
   272  
   273  func (c *Context) Parameter(parameter string) *Context {
   274  	c.parameters = append(c.parameters, parameter)
   275  	return c
   276  }
   277  
   278  // group:kind:name
   279  func (c *Context) SelectedResource(resource string) *Context {
   280  	c.resource = resource
   281  	return c
   282  }
   283  
   284  func (c *Context) NamePrefix(namePrefix string) *Context {
   285  	c.namePrefix = namePrefix
   286  	return c
   287  }
   288  
   289  func (c *Context) NameSuffix(nameSuffix string) *Context {
   290  	c.nameSuffix = nameSuffix
   291  	return c
   292  }
   293  
   294  func (c *Context) ResourceOverrides(overrides map[string]v1alpha1.ResourceOverride) *Context {
   295  	fixture.SetResourceOverrides(overrides)
   296  	return c
   297  }
   298  
   299  func (c *Context) ResourceFilter(filter settings.ResourcesFilter) *Context {
   300  	fixture.SetResourceFilter(filter)
   301  	return c
   302  }
   303  
   304  func (c *Context) And(block func()) *Context {
   305  	block()
   306  	return c
   307  }
   308  
   309  func (c *Context) When() *Actions {
   310  	// in case any settings have changed, pause for 1s, not great, but fine
   311  	time.Sleep(1 * time.Second)
   312  	return &Actions{context: c}
   313  }
   314  
   315  func (c *Context) Sleep(seconds time.Duration) *Context {
   316  	time.Sleep(seconds * time.Second)
   317  	return c
   318  }
   319  
   320  func (c *Context) Prune(prune bool) *Context {
   321  	c.prune = prune
   322  	return c
   323  }
   324  
   325  func (c *Context) Async(async bool) *Context {
   326  	c.async = async
   327  	return c
   328  }
   329  
   330  func (c *Context) LocalPath(localPath string) *Context {
   331  	c.localPath = localPath
   332  	return c
   333  }
   334  
   335  func (c *Context) Project(project string) *Context {
   336  	c.project = project
   337  	return c
   338  }
   339  
   340  func (c *Context) Force() *Context {
   341  	c.force = true
   342  	return c
   343  }
   344  
   345  func (c *Context) ApplyOutOfSyncOnly() *Context {
   346  	c.applyOutOfSyncOnly = true
   347  	return c
   348  }
   349  
   350  func (c *Context) HelmPassCredentials() *Context {
   351  	c.helmPassCredentials = true
   352  	return c
   353  }
   354  
   355  func (c *Context) HelmSkipCrds() *Context {
   356  	c.helmSkipCrds = true
   357  	return c
   358  }
   359  
   360  func (c *Context) SetTrackingMethod(trackingMethod string) *Context {
   361  	fixture.SetTrackingMethod(trackingMethod)
   362  	return c
   363  }
   364  
   365  func (c *Context) GetTrackingMethod() v1alpha1.TrackingMethod {
   366  	return c.trackingMethod
   367  }
   368  
   369  func (c *Context) Sources(sources []v1alpha1.ApplicationSource) *Context {
   370  	c.sources = sources
   371  	return c
   372  }