diff --git a/cmd/main.go b/cmd/main.go index 6c7fc34..cf7ab1b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,6 +35,8 @@ func main() { "Timeout could be overridden per resource using \"uptest.upbound.io/timeout\" annotation.").Default("1200").Int() defaultConditions = e2e.Flag("default-conditions", "Comma seperated list of default conditions to wait for a successful test.\n"+ "Conditions could be overridden per resource using \"uptest.upbound.io/conditions\" annotation.").Default("Ready").String() + + testDir = e2e.Flag("test-directory", "Directory where kuttl test case will be generated and executed.").Envar("UPTEST_TEST_DIR").Default(filepath.Join(os.TempDir(), "uptest-e2e")).String() ) kingpin.MustParse(app.Parse(os.Args[1:])) @@ -72,6 +74,7 @@ func main() { TeardownScriptPath: teardownPath, DefaultConditions: strings.Split(*defaultConditions, ","), DefaultTimeout: *defaultTimeout, + Directory: *testDir, } kingpin.FatalIfError(internal.RunTest(o), "cannot run e2e tests successfully") diff --git a/internal/config/config.go b/internal/config/config.go index 0a4106e..1b8ce28 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,6 +10,8 @@ const ( ) type AutomatedTest struct { + Directory string + ManifestPaths []string DataSourcePath string diff --git a/internal/prepare.go b/internal/prepare.go index 5916791..bd36b2f 100644 --- a/internal/prepare.go +++ b/internal/prepare.go @@ -20,29 +20,33 @@ import ( "github.com/upbound/uptest/internal/config" ) -var ( - testDirectory = filepath.Join(os.TempDir(), "uptest-e2e") - caseDirectory = filepath.Join(testDirectory, "case") -) - var ( charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") dataSourceRegex = regexp.MustCompile("\\${data\\.(.*?)}") randomStrRegex = regexp.MustCompile("\\${Rand\\.(.*?)}") + + caseDirectory = "case" ) +type PreparerOption func(*Preparer) + func WithDataSource(path string) PreparerOption { return func(p *Preparer) { p.dataSourcePath = path } } -type PreparerOption func(*Preparer) +func WithTestDirectory(path string) PreparerOption { + return func(p *Preparer) { + p.testDirectory = path + } +} func NewPreparer(testFilePaths []string, opts ...PreparerOption) *Preparer { p := &Preparer{ testFilePaths: testFilePaths, + testDirectory: os.TempDir(), } for _, f := range opts { f(p) @@ -53,9 +57,11 @@ func NewPreparer(testFilePaths []string, opts ...PreparerOption) *Preparer { type Preparer struct { testFilePaths []string dataSourcePath string + testDirectory string } func (p *Preparer) PrepareManifests() ([]config.Manifest, error) { + caseDirectory := filepath.Join(p.testDirectory, caseDirectory) if err := os.MkdirAll(caseDirectory, os.ModePerm); err != nil { return nil, errors.Wrapf(err, "cannot create directory %s", caseDirectory) } diff --git a/internal/runner.go b/internal/runner.go index ab5d5d4..4969297 100644 --- a/internal/runner.go +++ b/internal/runner.go @@ -8,7 +8,7 @@ import ( func RunTest(o *config.AutomatedTest) error { // Read examples and inject data source values to manifests - manifests, err := NewPreparer(o.ManifestPaths, WithDataSource(o.DataSourcePath)).PrepareManifests() + manifests, err := NewPreparer(o.ManifestPaths, WithDataSource(o.DataSourcePath), WithTestDirectory(o.Directory)).PrepareManifests() if err != nil { return errors.Wrap(err, "cannot prepare manifests") } diff --git a/internal/tester.go b/internal/tester.go index 273a3e0..3a03c5e 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -32,8 +32,8 @@ func (t *Tester) ExecuteTests() error { if err := t.writeKuttlFiles(); err != nil { return errors.Wrap(err, "cannot write kuttl test files") } - fmt.Println("Running kuttl tests at " + testDirectory) - cmd := exec.Command("bash", "-c", fmt.Sprintf(`"${KUTTL}" test --start-kind=false --skip-cluster-delete %s --timeout %d 2>&1`, testDirectory, t.options.DefaultTimeout)) + fmt.Println("Running kuttl tests at " + t.options.Directory) + cmd := exec.Command("bash", "-c", fmt.Sprintf(`"${KUTTL}" test --start-kind=false --skip-cluster-delete %s --timeout %d 2>&1`, t.options.Directory, t.options.DefaultTimeout)) stdout, _ := cmd.StdoutPipe() if err := cmd.Start(); err != nil { return errors.Wrap(err, "cannot start kuttl") @@ -119,7 +119,7 @@ func (t *Tester) writeKuttlFiles() error { } for k, v := range files { - if err := os.WriteFile(filepath.Join(caseDirectory, k), []byte(v), fs.ModePerm); err != nil { + if err := os.WriteFile(filepath.Join(filepath.Join(t.options.Directory, caseDirectory), k), []byte(v), fs.ModePerm); err != nil { return errors.Wrapf(err, "cannot write file %q", k) } }