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

     1  package app
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    10  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    11  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture/certs"
    12  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture/gpgkeys"
    13  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture/repos"
    14  	"github.com/argoproj/argo-cd/v3/util/env"
    15  	"github.com/argoproj/argo-cd/v3/util/settings"
    16  )
    17  
    18  // Context implements the "given" part of given/when/then
    19  type Context struct {
    20  	t               *testing.T
    21  	path            string
    22  	chart           string
    23  	ociRegistry     string
    24  	ociRegistryPath string
    25  	repoURLType     fixture.RepoURLType
    26  	// seconds
    27  	timeout                  int
    28  	name                     string
    29  	appNamespace             string
    30  	destServer               string
    31  	destName                 string
    32  	isDestServerInferred     bool
    33  	env                      string
    34  	parameters               []string
    35  	namePrefix               string
    36  	nameSuffix               string
    37  	resource                 string
    38  	prune                    bool
    39  	configManagementPlugin   string
    40  	async                    bool
    41  	localPath                string
    42  	project                  string
    43  	revision                 string
    44  	force                    bool
    45  	applyOutOfSyncOnly       bool
    46  	directoryRecurse         bool
    47  	replace                  bool
    48  	helmPassCredentials      bool
    49  	helmSkipCrds             bool
    50  	helmSkipSchemaValidation bool
    51  	helmSkipTests            bool
    52  	trackingMethod           v1alpha1.TrackingMethod
    53  	sources                  []v1alpha1.ApplicationSource
    54  	drySourceRevision        string
    55  	drySourcePath            string
    56  	syncSourceBranch         string
    57  	syncSourcePath           string
    58  	hydrateToBranch          string
    59  }
    60  
    61  type ContextArgs struct {
    62  	AppNamespace string
    63  }
    64  
    65  func Given(t *testing.T, opts ...fixture.TestOption) *Context {
    66  	t.Helper()
    67  	fixture.EnsureCleanState(t, opts...)
    68  	return GivenWithSameState(t)
    69  }
    70  
    71  func GivenWithNamespace(t *testing.T, namespace string) *Context {
    72  	t.Helper()
    73  	ctx := Given(t)
    74  	ctx.appNamespace = namespace
    75  	return ctx
    76  }
    77  
    78  func GivenWithSameState(t *testing.T) *Context {
    79  	t.Helper()
    80  	// ARGOCD_E2E_DEFAULT_TIMEOUT can be used to override the default timeout
    81  	// for any context.
    82  	timeout := env.ParseNumFromEnv("ARGOCD_E2E_DEFAULT_TIMEOUT", 20, 0, 180)
    83  	return &Context{
    84  		t:              t,
    85  		destServer:     v1alpha1.KubernetesInternalAPIServerAddr,
    86  		destName:       "in-cluster",
    87  		repoURLType:    fixture.RepoURLTypeFile,
    88  		name:           fixture.Name(),
    89  		timeout:        timeout,
    90  		project:        "default",
    91  		prune:          true,
    92  		trackingMethod: v1alpha1.TrackingMethodLabel,
    93  	}
    94  }
    95  
    96  func (c *Context) AppName() string {
    97  	return c.name
    98  }
    99  
   100  func (c *Context) AppQualifiedName() string {
   101  	if c.appNamespace != "" {
   102  		return c.appNamespace + "/" + c.AppName()
   103  	}
   104  	return c.AppName()
   105  }
   106  
   107  func (c *Context) AppNamespace() string {
   108  	if c.appNamespace != "" {
   109  		return c.appNamespace
   110  	}
   111  	return fixture.TestNamespace()
   112  }
   113  
   114  func (c *Context) SetAppNamespace(namespace string) *Context {
   115  	c.appNamespace = namespace
   116  	// errors.CheckError(fixture.SetParamInSettingConfigMap("application.resourceTrackingMethod", "annotation"))
   117  	return c
   118  }
   119  
   120  func (c *Context) GPGPublicKeyAdded() *Context {
   121  	gpgkeys.AddGPGPublicKey(c.t)
   122  	return c
   123  }
   124  
   125  func (c *Context) GPGPublicKeyRemoved() *Context {
   126  	gpgkeys.DeleteGPGPublicKey(c.t)
   127  	return c
   128  }
   129  
   130  func (c *Context) CustomCACertAdded() *Context {
   131  	certs.AddCustomCACert(c.t)
   132  	return c
   133  }
   134  
   135  func (c *Context) CustomSSHKnownHostsAdded() *Context {
   136  	certs.AddCustomSSHKnownHostsKeys(c.t)
   137  	return c
   138  }
   139  
   140  func (c *Context) HTTPSRepoURLAdded(withCreds bool) *Context {
   141  	repos.AddHTTPSRepo(c.t, false, withCreds, "", fixture.RepoURLTypeHTTPS)
   142  	return c
   143  }
   144  
   145  func (c *Context) HTTPSInsecureRepoURLAdded(withCreds bool) *Context {
   146  	repos.AddHTTPSRepo(c.t, true, withCreds, "", fixture.RepoURLTypeHTTPS)
   147  	return c
   148  }
   149  
   150  func (c *Context) HTTPSInsecureRepoURLWithClientCertAdded() *Context {
   151  	repos.AddHTTPSRepoClientCert(c.t, true)
   152  	return c
   153  }
   154  
   155  func (c *Context) HTTPSRepoURLWithClientCertAdded() *Context {
   156  	repos.AddHTTPSRepoClientCert(c.t, false)
   157  	return c
   158  }
   159  
   160  func (c *Context) SubmoduleHTTPSRepoURLAdded(withCreds bool) *Context {
   161  	fixture.CreateSubmoduleRepos(c.t, "https")
   162  	repos.AddHTTPSRepo(c.t, false, withCreds, "", fixture.RepoURLTypeHTTPSSubmoduleParent)
   163  	return c
   164  }
   165  
   166  func (c *Context) SSHRepoURLAdded(withCreds bool) *Context {
   167  	repos.AddSSHRepo(c.t, false, withCreds, fixture.RepoURLTypeSSH)
   168  	return c
   169  }
   170  
   171  func (c *Context) SSHInsecureRepoURLAdded(withCreds bool) *Context {
   172  	repos.AddSSHRepo(c.t, true, withCreds, fixture.RepoURLTypeSSH)
   173  	return c
   174  }
   175  
   176  func (c *Context) SubmoduleSSHRepoURLAdded(withCreds bool) *Context {
   177  	fixture.CreateSubmoduleRepos(c.t, "ssh")
   178  	repos.AddSSHRepo(c.t, false, withCreds, fixture.RepoURLTypeSSHSubmoduleParent)
   179  	return c
   180  }
   181  
   182  func (c *Context) HelmRepoAdded(name string) *Context {
   183  	repos.AddHelmRepo(c.t, name)
   184  	return c
   185  }
   186  
   187  func (c *Context) HelmOCIRepoAdded(name string) *Context {
   188  	repos.AddHelmOCIRepo(c.t, name)
   189  	return c
   190  }
   191  
   192  func (c *Context) PushImageToOCIRegistry(pathName, tag string) *Context {
   193  	repos.PushImageToOCIRegistry(c.t, pathName, tag)
   194  	return c
   195  }
   196  
   197  func (c *Context) PushImageToAuthenticatedOCIRegistry(pathName, tag string) *Context {
   198  	repos.PushImageToAuthenticatedOCIRegistry(c.t, pathName, tag)
   199  	return c
   200  }
   201  
   202  func (c *Context) PushChartToOCIRegistry(chartPathName, chartName, chartVersion string) *Context {
   203  	repos.PushChartToOCIRegistry(c.t, chartPathName, chartName, chartVersion)
   204  	return c
   205  }
   206  
   207  func (c *Context) PushChartToAuthenticatedOCIRegistry(chartPathName, chartName, chartVersion string) *Context {
   208  	repos.PushChartToAuthenticatedOCIRegistry(c.t, chartPathName, chartName, chartVersion)
   209  	return c
   210  }
   211  
   212  func (c *Context) HTTPSCredentialsUserPassAdded() *Context {
   213  	repos.AddHTTPSCredentialsUserPass(c.t)
   214  	return c
   215  }
   216  
   217  func (c *Context) HelmHTTPSCredentialsUserPassAdded() *Context {
   218  	repos.AddHelmHTTPSCredentialsTLSClientCert(c.t)
   219  	return c
   220  }
   221  
   222  func (c *Context) HelmoOCICredentialsWithoutUserPassAdded() *Context {
   223  	repos.AddHelmoOCICredentialsWithoutUserPass(c.t)
   224  	return c
   225  }
   226  
   227  func (c *Context) HTTPSCredentialsTLSClientCertAdded() *Context {
   228  	repos.AddHTTPSCredentialsTLSClientCert(c.t)
   229  	return c
   230  }
   231  
   232  func (c *Context) SSHCredentialsAdded() *Context {
   233  	repos.AddSSHCredentials(c.t)
   234  	return c
   235  }
   236  
   237  func (c *Context) OCIRepoAdded(name, imagePath string) *Context {
   238  	repos.AddOCIRepo(c.t, name, imagePath)
   239  	return c
   240  }
   241  
   242  func (c *Context) AuthenticatedOCIRepoAdded(name, imagePath string) *Context {
   243  	repos.AddAuthenticatedOCIRepo(c.t, name, imagePath)
   244  	return c
   245  }
   246  
   247  func (c *Context) OCIRegistry(registry string) *Context {
   248  	c.ociRegistry = registry
   249  	return c
   250  }
   251  
   252  func (c *Context) ProjectSpec(spec v1alpha1.AppProjectSpec) *Context {
   253  	c.t.Helper()
   254  	require.NoError(c.t, fixture.SetProjectSpec(c.project, spec))
   255  	return c
   256  }
   257  
   258  func (c *Context) Replace() *Context {
   259  	c.replace = true
   260  	return c
   261  }
   262  
   263  func (c *Context) RepoURLType(urlType fixture.RepoURLType) *Context {
   264  	c.repoURLType = urlType
   265  	return c
   266  }
   267  
   268  func (c *Context) GetName() string {
   269  	return c.name
   270  }
   271  
   272  func (c *Context) Name(name string) *Context {
   273  	c.name = name
   274  	return c
   275  }
   276  
   277  func (c *Context) Path(path string) *Context {
   278  	c.path = path
   279  	return c
   280  }
   281  
   282  func (c *Context) DrySourceRevision(revision string) *Context {
   283  	c.drySourceRevision = revision
   284  	return c
   285  }
   286  
   287  func (c *Context) DrySourcePath(path string) *Context {
   288  	c.drySourcePath = path
   289  	return c
   290  }
   291  
   292  func (c *Context) SyncSourceBranch(branch string) *Context {
   293  	c.syncSourceBranch = branch
   294  	return c
   295  }
   296  
   297  func (c *Context) SyncSourcePath(path string) *Context {
   298  	c.syncSourcePath = path
   299  	return c
   300  }
   301  
   302  func (c *Context) HydrateToBranch(branch string) *Context {
   303  	c.hydrateToBranch = branch
   304  	return c
   305  }
   306  
   307  func (c *Context) Recurse() *Context {
   308  	c.directoryRecurse = true
   309  	return c
   310  }
   311  
   312  func (c *Context) Chart(chart string) *Context {
   313  	c.chart = chart
   314  	return c
   315  }
   316  
   317  func (c *Context) OCIRegistryPath(ociPath string) *Context {
   318  	c.ociRegistryPath = ociPath
   319  	return c
   320  }
   321  
   322  func (c *Context) Revision(revision string) *Context {
   323  	c.revision = revision
   324  	return c
   325  }
   326  
   327  func (c *Context) Timeout(timeout int) *Context {
   328  	c.timeout = timeout
   329  	return c
   330  }
   331  
   332  func (c *Context) DestServer(destServer string) *Context {
   333  	c.destServer = destServer
   334  	c.isDestServerInferred = false
   335  	return c
   336  }
   337  
   338  func (c *Context) DestName(destName string) *Context {
   339  	c.destName = destName
   340  	c.isDestServerInferred = true
   341  	return c
   342  }
   343  
   344  func (c *Context) Env(env string) *Context {
   345  	c.env = env
   346  	return c
   347  }
   348  
   349  func (c *Context) Parameter(parameter string) *Context {
   350  	c.parameters = append(c.parameters, parameter)
   351  	return c
   352  }
   353  
   354  // group:kind:name
   355  func (c *Context) SelectedResource(resource string) *Context {
   356  	c.resource = resource
   357  	return c
   358  }
   359  
   360  func (c *Context) NamePrefix(namePrefix string) *Context {
   361  	c.namePrefix = namePrefix
   362  	return c
   363  }
   364  
   365  func (c *Context) NameSuffix(nameSuffix string) *Context {
   366  	c.nameSuffix = nameSuffix
   367  	return c
   368  }
   369  
   370  func (c *Context) ResourceOverrides(overrides map[string]v1alpha1.ResourceOverride) *Context {
   371  	c.t.Helper()
   372  	require.NoError(c.t, fixture.SetResourceOverrides(overrides))
   373  	return c
   374  }
   375  
   376  func (c *Context) ResourceFilter(filter settings.ResourcesFilter) *Context {
   377  	c.t.Helper()
   378  	require.NoError(c.t, fixture.SetResourceFilter(filter))
   379  	return c
   380  }
   381  
   382  func (c *Context) And(block func()) *Context {
   383  	block()
   384  	return c
   385  }
   386  
   387  func (c *Context) When() *Actions {
   388  	time.Sleep(fixture.WhenThenSleepInterval)
   389  	return &Actions{context: c}
   390  }
   391  
   392  func (c *Context) Sleep(seconds time.Duration) *Context {
   393  	time.Sleep(seconds * time.Second)
   394  	return c
   395  }
   396  
   397  func (c *Context) Prune(prune bool) *Context {
   398  	c.prune = prune
   399  	return c
   400  }
   401  
   402  func (c *Context) Async(async bool) *Context {
   403  	c.async = async
   404  	return c
   405  }
   406  
   407  func (c *Context) LocalPath(localPath string) *Context {
   408  	c.localPath = localPath
   409  	return c
   410  }
   411  
   412  func (c *Context) Project(project string) *Context {
   413  	c.project = project
   414  	return c
   415  }
   416  
   417  func (c *Context) Force() *Context {
   418  	c.force = true
   419  	return c
   420  }
   421  
   422  func (c *Context) ApplyOutOfSyncOnly() *Context {
   423  	c.applyOutOfSyncOnly = true
   424  	return c
   425  }
   426  
   427  func (c *Context) HelmPassCredentials() *Context {
   428  	c.helmPassCredentials = true
   429  	return c
   430  }
   431  
   432  func (c *Context) HelmSkipCrds() *Context {
   433  	c.helmSkipCrds = true
   434  	return c
   435  }
   436  
   437  func (c *Context) HelmSkipSchemaValidation() *Context {
   438  	c.helmSkipSchemaValidation = true
   439  	return c
   440  }
   441  
   442  func (c *Context) HelmSkipTests() *Context {
   443  	c.helmSkipTests = true
   444  	return c
   445  }
   446  
   447  func (c *Context) SetTrackingMethod(trackingMethod string) *Context {
   448  	c.t.Helper()
   449  	require.NoError(c.t, fixture.SetTrackingMethod(trackingMethod))
   450  	return c
   451  }
   452  
   453  func (c *Context) SetInstallationID(installationID string) *Context {
   454  	c.t.Helper()
   455  	require.NoError(c.t, fixture.SetInstallationID(installationID))
   456  	return c
   457  }
   458  
   459  func (c *Context) GetTrackingMethod() v1alpha1.TrackingMethod {
   460  	return c.trackingMethod
   461  }
   462  
   463  func (c *Context) Sources(sources []v1alpha1.ApplicationSource) *Context {
   464  	c.sources = sources
   465  	return c
   466  }
   467  
   468  func (c *Context) RegisterKustomizeVersion(version, path string) *Context {
   469  	c.t.Helper()
   470  	require.NoError(c.t, fixture.RegisterKustomizeVersion(version, path))
   471  	return c
   472  }