github.imxd.top/openshift/source-to-image@v1.2.0/pkg/build/strategies/dockerfile/dockerfile_test.go (about) 1 package dockerfile 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/openshift/source-to-image/pkg/api" 11 "github.com/openshift/source-to-image/pkg/api/constants" 12 "github.com/openshift/source-to-image/pkg/util/fs" 13 ) 14 15 func TestGetImageScriptsDir(t *testing.T) { 16 type testCase struct { 17 Config *api.Config 18 ExpectedDir string 19 HasAllScripts bool 20 } 21 22 cases := []testCase{ 23 { 24 Config: &api.Config{}, 25 ExpectedDir: defaultScriptsDir, 26 }, 27 { 28 Config: &api.Config{ 29 ScriptsURL: "image:///usr/some/dir", 30 }, 31 ExpectedDir: "/usr/some/dir", 32 HasAllScripts: true, 33 }, 34 { 35 Config: &api.Config{ 36 ScriptsURL: "https://github.com/openshift/source-to-image", 37 }, 38 ExpectedDir: defaultScriptsDir, 39 }, 40 { 41 Config: &api.Config{ 42 ImageScriptsURL: "image:///usr/some/dir", 43 }, 44 ExpectedDir: "/usr/some/dir", 45 }, 46 { 47 Config: &api.Config{ 48 ImageScriptsURL: "https://github.com/openshift/source-to-image", 49 }, 50 ExpectedDir: defaultScriptsDir, 51 }, 52 { 53 Config: &api.Config{ 54 ScriptsURL: "https://github.com/openshift/source-to-image", 55 ImageScriptsURL: "image:///usr/some/dir", 56 }, 57 ExpectedDir: "/usr/some/dir", 58 }, 59 { 60 Config: &api.Config{ 61 ScriptsURL: "image:///usr/some/dir", 62 ImageScriptsURL: "image:///usr/other/dir", 63 }, 64 ExpectedDir: "/usr/some/dir", 65 HasAllScripts: true, 66 }, 67 } 68 for _, tc := range cases { 69 output, hasScripts := getImageScriptsDir(tc.Config) 70 if output != tc.ExpectedDir { 71 t.Errorf("Expected image scripts dir %s to be %s", output, tc.ExpectedDir) 72 } 73 if hasScripts != tc.HasAllScripts { 74 t.Errorf("Expected has all scripts indicator:\n%v\nto be: %v", hasScripts, tc.HasAllScripts) 75 } 76 } 77 } 78 79 func TestInstallScripts(t *testing.T) { 80 allErrs := map[string]bool{ 81 constants.Assemble: true, 82 constants.Run: true, 83 constants.SaveArtifacts: true, 84 } 85 86 tests := []struct { 87 name string 88 url string 89 createAssemble bool 90 createRun bool 91 createSaveArtifacts bool 92 scriptErrs map[string]bool 93 }{ 94 { 95 name: "empty", 96 scriptErrs: allErrs, 97 }, 98 { 99 name: "bad url", 100 url: "https://foobadbar.com", 101 scriptErrs: allErrs, 102 }, 103 { 104 // image:// URLs should always report success 105 name: "image url", 106 url: "image://path/to/scripts", 107 }, 108 { 109 name: "assemble script", 110 createAssemble: true, 111 scriptErrs: map[string]bool{ 112 constants.Assemble: false, 113 constants.Run: true, 114 constants.SaveArtifacts: true, 115 }, 116 }, 117 { 118 name: "run script", 119 createRun: true, 120 scriptErrs: map[string]bool{ 121 constants.Assemble: true, 122 constants.Run: false, 123 constants.SaveArtifacts: true, 124 }, 125 }, 126 { 127 name: "save-artifacts script", 128 createSaveArtifacts: true, 129 scriptErrs: map[string]bool{ 130 constants.Assemble: true, 131 constants.Run: true, 132 constants.SaveArtifacts: false, 133 }, 134 }, 135 { 136 name: "all scripts", 137 createAssemble: true, 138 createRun: true, 139 createSaveArtifacts: true, 140 }, 141 } 142 for _, tc := range tests { 143 t.Run(tc.name, func(t *testing.T) { 144 workDir, err := ioutil.TempDir("", "s2i-dockerfile-uploads") 145 if err != nil { 146 t.Fatalf("failed to create working dir: %v", err) 147 } 148 defer os.RemoveAll(workDir) 149 config := &api.Config{ 150 WorkingDir: workDir, 151 } 152 fileSystem := fs.NewFileSystem() 153 for _, v := range workingDirs { 154 err = fileSystem.MkdirAllWithPermissions(filepath.Join(workDir, v), 0755) 155 if err != nil { 156 t.Fatalf("failed to create working dir: %v", err) 157 } 158 } 159 160 tempDir, err := ioutil.TempDir("", "s2i-dockerfile-scripts") 161 if err != nil { 162 t.Fatalf("could not create temp dir: %v", err) 163 } 164 defer os.RemoveAll(tempDir) 165 if tc.createAssemble { 166 err := createTestScript(tempDir, constants.Assemble) 167 if err != nil { 168 t.Fatalf("failed to write %s script: %v", constants.Assemble, err) 169 } 170 tc.url = fmt.Sprintf("file://%s", filepath.ToSlash(tempDir)) 171 } 172 if tc.createRun { 173 err := createTestScript(tempDir, constants.Run) 174 if err != nil { 175 t.Fatalf("failed to write %s script: %v", constants.Run, err) 176 } 177 tc.url = fmt.Sprintf("file://%s", filepath.ToSlash(tempDir)) 178 } 179 if tc.createSaveArtifacts { 180 err := createTestScript(tempDir, constants.SaveArtifacts) 181 if err != nil { 182 t.Fatalf("failed to write %s script: %v", constants.SaveArtifacts, err) 183 } 184 tc.url = fmt.Sprintf("file://%s", filepath.ToSlash(tempDir)) 185 } 186 builder, _ := New(config, fileSystem) 187 results := builder.installScripts(tc.url, config) 188 for _, script := range results { 189 expectErr := tc.scriptErrs[script.Script] 190 if expectErr && script.Error == nil { 191 t.Errorf("expected error for %s, got nil", script.Script) 192 } 193 if script.Error != nil && !expectErr { 194 t.Errorf("received unexpected error: %v", script.Error) 195 } 196 } 197 }) 198 } 199 } 200 201 func createTestScript(dir string, name string) error { 202 script := "echo \"test script\"" 203 path := filepath.Join(dir, name) 204 err := ioutil.WriteFile(path, []byte(script), 0700) 205 return err 206 }