Skip to content

Commit

Permalink
Merge pull request #30 from turkenh/backport-test-dir
Browse files Browse the repository at this point in the history
[Backport to release-0.1] option to pass test directory instead of using os.TmpDir
  • Loading branch information
turkenh authored Oct 18, 2022
2 parents 49be0b7 + 327ef4c commit 340331a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]))

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (
)

type AutomatedTest struct {
Directory string

ManifestPaths []string
DataSourcePath string

Expand Down
18 changes: 12 additions & 6 deletions internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 340331a

Please sign in to comment.