kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/configload/copy_dir_test.go (about) 1 package configload 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 // TestCopyDir_symlinks sets up a directory with two submodules, 11 // one being a symlink to the other 12 // 13 // The resultant file structure is as follows: 14 // ├── modules 15 // │ ├── symlink-module -> test-module 16 // │ └── test-module 17 // │ └── main.tf 18 // └── target 19 // ├── symlink-module -> test-module 20 // └── test-module 21 // └── main.tf 22 23 func TestCopyDir_symlinks(t *testing.T) { 24 tmpdir, err := ioutil.TempDir("", "copy-dir-test") 25 if err != nil { 26 t.Fatal(err) 27 } 28 defer os.RemoveAll(tmpdir) 29 30 moduleDir := filepath.Join(tmpdir, "modules") 31 err = os.Mkdir(moduleDir, os.ModePerm) 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 subModuleDir := filepath.Join(moduleDir, "test-module") 37 err = os.Mkdir(subModuleDir, os.ModePerm) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 err = ioutil.WriteFile(filepath.Join(subModuleDir, "main.tf"), []byte("hello"), 0644) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 err = os.Symlink("test-module", filepath.Join(moduleDir, "symlink-module")) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 targetDir := filepath.Join(tmpdir, "target") 53 os.Mkdir(targetDir, os.ModePerm) 54 55 err = copyDir(targetDir, moduleDir) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 if _, err = os.Lstat(filepath.Join(targetDir, "test-module", "main.tf")); os.IsNotExist(err) { 61 t.Fatal("target test-module/main.tf was not created") 62 } 63 64 if _, err = os.Lstat(filepath.Join(targetDir, "symlink-module", "main.tf")); os.IsNotExist(err) { 65 t.Fatal("target symlink-module/main.tf was not created") 66 } 67 } 68 69 func TestCopyDir_symlink_file(t *testing.T) { 70 tmpdir, err := ioutil.TempDir("", "copy-file-test") 71 if err != nil { 72 t.Fatal(err) 73 } 74 defer os.RemoveAll(tmpdir) 75 76 moduleDir := filepath.Join(tmpdir, "modules") 77 err = os.Mkdir(moduleDir, os.ModePerm) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 err = ioutil.WriteFile(filepath.Join(moduleDir, "main.tf"), []byte("hello"), 0644) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 err = os.Symlink("main.tf", filepath.Join(moduleDir, "symlink.tf")) 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 targetDir := filepath.Join(tmpdir, "target") 93 os.Mkdir(targetDir, os.ModePerm) 94 95 err = copyDir(targetDir, moduleDir) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 if _, err = os.Lstat(filepath.Join(targetDir, "main.tf")); os.IsNotExist(err) { 101 t.Fatal("target/main.tf was not created") 102 } 103 104 if _, err = os.Lstat(filepath.Join(targetDir, "symlink.tf")); os.IsNotExist(err) { 105 t.Fatal("target/symlink.tf was not created") 106 } 107 }