github.com/argoproj/argo-cd@v1.8.7/test/e2e/fixture/app/actions.go (about)

     1  package app
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  
    10  	. "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    11  	"github.com/argoproj/argo-cd/test/e2e/fixture"
    12  	"github.com/argoproj/argo-cd/util/errors"
    13  	"github.com/argoproj/argo-cd/util/grpc"
    14  )
    15  
    16  // this implements the "when" part of given/when/then
    17  //
    18  // none of the func implement error checks, and that is complete intended, you should check for errors
    19  // using the Then()
    20  type Actions struct {
    21  	context      *Context
    22  	lastOutput   string
    23  	lastError    error
    24  	ignoreErrors bool
    25  }
    26  
    27  func (a *Actions) IgnoreErrors() *Actions {
    28  	a.ignoreErrors = true
    29  	return a
    30  }
    31  
    32  func (a *Actions) DoNotIgnoreErrors() *Actions {
    33  	a.ignoreErrors = false
    34  	return a
    35  }
    36  
    37  func (a *Actions) PatchFile(file string, jsonPath string) *Actions {
    38  	a.context.t.Helper()
    39  	fixture.Patch(a.context.path+"/"+file, jsonPath)
    40  	return a
    41  }
    42  
    43  func (a *Actions) DeleteFile(file string) *Actions {
    44  	a.context.t.Helper()
    45  	fixture.Delete(a.context.path + "/" + file)
    46  	return a
    47  }
    48  
    49  func (a *Actions) WriteFile(fileName, fileContents string) *Actions {
    50  	a.context.t.Helper()
    51  	fixture.WriteFile(a.context.path+"/"+fileName, fileContents)
    52  	return a
    53  }
    54  
    55  func (a *Actions) AddFile(fileName, fileContents string) *Actions {
    56  	a.context.t.Helper()
    57  	fixture.AddFile(a.context.path+"/"+fileName, fileContents)
    58  	return a
    59  }
    60  
    61  func (a *Actions) AddSignedFile(fileName, fileContents string) *Actions {
    62  	a.context.t.Helper()
    63  	fixture.AddSignedFile(a.context.path+"/"+fileName, fileContents)
    64  	return a
    65  }
    66  
    67  func (a *Actions) CreateFromPartialFile(data string, flags ...string) *Actions {
    68  	a.context.t.Helper()
    69  	tmpFile, err := ioutil.TempFile("", "")
    70  	errors.CheckError(err)
    71  	_, err = tmpFile.Write([]byte(data))
    72  	errors.CheckError(err)
    73  
    74  	args := append([]string{
    75  		"app", "create",
    76  		"-f", tmpFile.Name(),
    77  		"--name", a.context.name,
    78  		"--repo", fixture.RepoURL(a.context.repoURLType),
    79  		"--dest-server", a.context.destServer,
    80  		"--dest-namespace", fixture.DeploymentNamespace(),
    81  	}, flags...)
    82  
    83  	a.runCli(args...)
    84  	return a
    85  }
    86  func (a *Actions) CreateFromFile(handler func(app *Application), flags ...string) *Actions {
    87  	a.context.t.Helper()
    88  	app := &Application{
    89  		ObjectMeta: v1.ObjectMeta{
    90  			Name: a.context.name,
    91  		},
    92  		Spec: ApplicationSpec{
    93  			Project: a.context.project,
    94  			Source: ApplicationSource{
    95  				RepoURL: fixture.RepoURL(a.context.repoURLType),
    96  				Path:    a.context.path,
    97  			},
    98  			Destination: ApplicationDestination{
    99  				Server:    a.context.destServer,
   100  				Namespace: fixture.DeploymentNamespace(),
   101  			},
   102  		},
   103  	}
   104  	if a.context.env != "" {
   105  		app.Spec.Source.Ksonnet = &ApplicationSourceKsonnet{
   106  			Environment: a.context.env,
   107  		}
   108  	}
   109  	if a.context.namePrefix != "" || a.context.nameSuffix != "" {
   110  		app.Spec.Source.Kustomize = &ApplicationSourceKustomize{
   111  			NamePrefix: a.context.namePrefix,
   112  			NameSuffix: a.context.nameSuffix,
   113  		}
   114  	}
   115  	if a.context.configManagementPlugin != "" {
   116  		app.Spec.Source.Plugin = &ApplicationSourcePlugin{
   117  			Name: a.context.configManagementPlugin,
   118  		}
   119  	}
   120  
   121  	if len(a.context.parameters) > 0 {
   122  		log.Fatal("Application parameters or json tlas are not supported")
   123  	}
   124  
   125  	if a.context.directoryRecurse {
   126  		app.Spec.Source.Directory = &ApplicationSourceDirectory{Recurse: true}
   127  	}
   128  
   129  	handler(app)
   130  	data := grpc.MustMarshal(app)
   131  	tmpFile, err := ioutil.TempFile("", "")
   132  	errors.CheckError(err)
   133  	_, err = tmpFile.Write(data)
   134  	errors.CheckError(err)
   135  
   136  	args := append([]string{
   137  		"app", "create",
   138  		"-f", tmpFile.Name(),
   139  	}, flags...)
   140  
   141  	a.runCli(args...)
   142  	return a
   143  }
   144  
   145  func (a *Actions) CreateWithNoNameSpace(args ...string) *Actions {
   146  	args = a.prepareCreateArgs(args)
   147  	//  are you adding new context values? if you only use them for this func, then use args instead
   148  	a.runCli(args...)
   149  	return a
   150  }
   151  
   152  func (a *Actions) Create(args ...string) *Actions {
   153  	args = a.prepareCreateArgs(args)
   154  	args = append(args, "--dest-namespace", fixture.DeploymentNamespace())
   155  
   156  	//  are you adding new context values? if you only use them for this func, then use args instead
   157  	a.runCli(args...)
   158  
   159  	return a
   160  }
   161  
   162  func (a *Actions) prepareCreateArgs(args []string) []string {
   163  	a.context.t.Helper()
   164  	args = append([]string{
   165  		"app", "create", a.context.name,
   166  		"--repo", fixture.RepoURL(a.context.repoURLType),
   167  		"--dest-server", a.context.destServer,
   168  	}, args...)
   169  
   170  	if a.context.path != "" {
   171  		args = append(args, "--path", a.context.path)
   172  	}
   173  
   174  	if a.context.chart != "" {
   175  		args = append(args, "--helm-chart", a.context.chart)
   176  	}
   177  
   178  	if a.context.env != "" {
   179  		args = append(args, "--env", a.context.env)
   180  	}
   181  
   182  	for _, parameter := range a.context.parameters {
   183  		args = append(args, "--parameter", parameter)
   184  	}
   185  
   186  	args = append(args, "--project", a.context.project)
   187  
   188  	if a.context.namePrefix != "" {
   189  		args = append(args, "--nameprefix", a.context.namePrefix)
   190  	}
   191  
   192  	if a.context.nameSuffix != "" {
   193  		args = append(args, "--namesuffix", a.context.nameSuffix)
   194  	}
   195  
   196  	if a.context.configManagementPlugin != "" {
   197  		args = append(args, "--config-management-plugin", a.context.configManagementPlugin)
   198  	}
   199  
   200  	if a.context.revision != "" {
   201  		args = append(args, "--revision", a.context.revision)
   202  	}
   203  	return args
   204  }
   205  
   206  func (a *Actions) Declarative(filename string) *Actions {
   207  	a.context.t.Helper()
   208  	return a.DeclarativeWithCustomRepo(filename, fixture.RepoURL(a.context.repoURLType))
   209  }
   210  
   211  func (a *Actions) DeclarativeWithCustomRepo(filename string, repoURL string) *Actions {
   212  	a.context.t.Helper()
   213  	values := map[string]interface{}{
   214  		"ArgoCDNamespace":     fixture.ArgoCDNamespace,
   215  		"DeploymentNamespace": fixture.DeploymentNamespace(),
   216  		"Name":                a.context.name,
   217  		"Path":                a.context.path,
   218  		"Project":             a.context.project,
   219  		"RepoURL":             repoURL,
   220  	}
   221  	a.lastOutput, a.lastError = fixture.Declarative(filename, values)
   222  	a.verifyAction()
   223  	return a
   224  }
   225  
   226  func (a *Actions) PatchApp(patch string) *Actions {
   227  	a.context.t.Helper()
   228  	a.runCli("app", "patch", a.context.name, "--patch", patch)
   229  	return a
   230  }
   231  
   232  func (a *Actions) AppSet(flags ...string) *Actions {
   233  	a.context.t.Helper()
   234  	args := []string{"app", "set", a.context.name}
   235  	args = append(args, flags...)
   236  	a.runCli(args...)
   237  	return a
   238  }
   239  
   240  func (a *Actions) AppUnSet(flags ...string) *Actions {
   241  	a.context.t.Helper()
   242  	args := []string{"app", "unset", a.context.name}
   243  	args = append(args, flags...)
   244  	a.runCli(args...)
   245  	return a
   246  }
   247  
   248  func (a *Actions) Sync(args ...string) *Actions {
   249  	a.context.t.Helper()
   250  	args = append([]string{"app", "sync"}, args...)
   251  	if a.context.name != "" {
   252  		args = append(args, a.context.name)
   253  	}
   254  	args = append(args, "--timeout", fmt.Sprintf("%v", a.context.timeout))
   255  
   256  	if a.context.async {
   257  		args = append(args, "--async")
   258  	}
   259  
   260  	if a.context.prune {
   261  		args = append(args, "--prune")
   262  	}
   263  
   264  	if a.context.resource != "" {
   265  		args = append(args, "--resource", a.context.resource)
   266  	}
   267  
   268  	if a.context.localPath != "" {
   269  		args = append(args, "--local", a.context.localPath)
   270  	}
   271  
   272  	if a.context.force {
   273  		args = append(args, "--force")
   274  	}
   275  
   276  	//  are you adding new context values? if you only use them for this func, then use args instead
   277  
   278  	a.runCli(args...)
   279  
   280  	return a
   281  }
   282  
   283  func (a *Actions) TerminateOp() *Actions {
   284  	a.context.t.Helper()
   285  	a.runCli("app", "terminate-op", a.context.name)
   286  	return a
   287  }
   288  
   289  func (a *Actions) Refresh(refreshType RefreshType) *Actions {
   290  	a.context.t.Helper()
   291  	flag := map[RefreshType]string{
   292  		RefreshTypeNormal: "--refresh",
   293  		RefreshTypeHard:   "--hard-refresh",
   294  	}[refreshType]
   295  
   296  	a.runCli("app", "get", a.context.name, flag)
   297  
   298  	return a
   299  }
   300  
   301  func (a *Actions) Delete(cascade bool) *Actions {
   302  	a.context.t.Helper()
   303  	a.runCli("app", "delete", a.context.name, fmt.Sprintf("--cascade=%v", cascade))
   304  	return a
   305  }
   306  
   307  func (a *Actions) And(block func()) *Actions {
   308  	a.context.t.Helper()
   309  	block()
   310  	return a
   311  }
   312  
   313  func (a *Actions) Then() *Consequences {
   314  	a.context.t.Helper()
   315  	return &Consequences{a.context, a}
   316  }
   317  
   318  func (a *Actions) runCli(args ...string) {
   319  	a.context.t.Helper()
   320  	a.lastOutput, a.lastError = fixture.RunCli(args...)
   321  	a.verifyAction()
   322  }
   323  
   324  func (a *Actions) verifyAction() {
   325  	a.context.t.Helper()
   326  	if !a.ignoreErrors {
   327  		a.Then().Expect(Success(""))
   328  	}
   329  }