Skip to content

Commit 7dd6ef0

Browse files
authored
Copy additional stack resources inside tmpDir before validating stacks with odov3 (#230)
* Add base path of stack to struct Signed-off-by: thepetk <thepetk@gmail.com> * Copy all contents of stack before running odo dev Signed-off-by: thepetk <thepetk@gmail.com> --------- Signed-off-by: thepetk <thepetk@gmail.com>
1 parent 87c1917 commit 7dd6ef0

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

tests/odov3/odo_test.go

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func TestOdo(t *testing.T) {
6262
}
6363

6464
for _, dir := range dirs {
65-
path := filepath.Join(stacksPath, dir, "devfile.yaml")
66-
65+
base := filepath.Join(stacksPath, dir)
66+
path := filepath.Join(base, "devfile.yaml")
6767
parserArgs := parser.ParserArgs{
6868
Path: path,
6969
}
@@ -76,7 +76,7 @@ func TestOdo(t *testing.T) {
7676
starterProjects, err := devfile.Data.GetStarterProjects(common.DevfileOptions{})
7777
g.Expect(err).To(BeNil())
7878

79-
stack := Stack{name: name, version: version, path: path, starterProjects: starterProjects}
79+
stack := Stack{name: name, version: version, path: path, starterProjects: starterProjects, base: base}
8080

8181
stacks = append(stacks, stack)
8282
}
@@ -161,6 +161,10 @@ var _ = Describe("test starter projects from devfile stacks", func() {
161161
_, _, err := runOdo("init", "--devfile-path", stack.path, "--name", cmpName)
162162
Expect(err).To(BeNil())
163163

164+
// Copy all additional resources found inside the stack directory.
165+
err = copyDir(stack.base, filepath.Join(tmpDir, "app"))
166+
Expect(err).To(BeNil())
167+
164168
devStdout, devStderr, devProcess, err := runOdoDev("--no-commands")
165169
Expect(err).To(BeNil())
166170

@@ -220,6 +224,10 @@ var _ = Describe("test starter projects from devfile stacks", func() {
220224
_, _, err := runOdo("init", "--devfile-path", stack.path, "--starter", starterProject.Name, "--name", starterProject.Name)
221225
Expect(err).To(BeNil())
222226

227+
// Copy all additional resources found inside the stack directory.
228+
err = copyDir(stack.base, filepath.Join(tmpDir, "app"))
229+
Expect(err).To(BeNil())
230+
223231
devStdout, devStderr, devProcess, err := runOdoDev()
224232
Expect(err).To(BeNil())
225233

@@ -451,7 +459,6 @@ func runOdo(args ...string) ([]byte, []byte, error) {
451459
}
452460

453461
return dataStdout, dataStderr, nil
454-
455462
}
456463

457464
const letters = "abcdefghijklmnopqrstuvwxyz"
@@ -464,8 +471,51 @@ func randomString(n int) string {
464471
return string(b)
465472
}
466473

474+
// copyDir: Copies all items inside given stack's directory to the given destination.
475+
// If the item exists it skips it.
476+
func copyDir(src string, dst string) error {
477+
entries, err := os.ReadDir(src)
478+
if err != nil {
479+
return err
480+
}
481+
for _, entry := range entries {
482+
sourceEntryPath := filepath.Join(src, entry.Name())
483+
destEntryPath := filepath.Join(dst, entry.Name())
484+
sourceEntryInfo, err := os.Stat(sourceEntryPath)
485+
if err != nil {
486+
return err
487+
}
488+
_, err = os.Stat(destEntryPath)
489+
// check if destination exists
490+
if err == nil {
491+
// if it exists continue
492+
continue
493+
} else if !os.IsNotExist(err) {
494+
// for every other err return it
495+
return err
496+
}
497+
498+
// if entry is a directory, create a dir in destination and go one level down.
499+
if sourceEntryInfo.IsDir() {
500+
os.Mkdir(destEntryPath, 0755)
501+
err = copyDir(sourceEntryPath, destEntryPath)
502+
if err != nil {
503+
return err
504+
}
505+
continue
506+
}
507+
// if is not a dir copy file
508+
err = copyFile(sourceEntryPath, destEntryPath)
509+
if err != nil {
510+
return err
511+
}
512+
}
513+
return nil
514+
}
515+
467516
// this is not the best way to copy files
468517
func copyFile(src string, dst string) error {
518+
GinkgoWriter.Printf("Copying file %s to %s\n", src, dst)
469519
input, err := os.ReadFile(src)
470520
if err != nil {
471521
return err

tests/odov3/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Stack struct {
3333
name string
3434
version string
3535
path string
36+
base string
3637
starterProjects []v1alpha2.StarterProject
3738
}
3839

0 commit comments

Comments
 (0)