github.com/opsramp/moby@v1.13.1/builder/context_test.go (about) 1 package builder 2 3 import ( 4 "archive/tar" 5 "bytes" 6 "io" 7 "io/ioutil" 8 "path/filepath" 9 "runtime" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/pkg/archive" 14 ) 15 16 var prepareEmpty = func(t *testing.T) (string, func()) { 17 return "", func() {} 18 } 19 20 var prepareNoFiles = func(t *testing.T) (string, func()) { 21 return createTestTempDir(t, "", "builder-context-test") 22 } 23 24 var prepareOneFile = func(t *testing.T) (string, func()) { 25 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 26 createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) 27 return contextDir, cleanup 28 } 29 30 func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) (string, func()), excludes []string) { 31 contextDir, cleanup := prepare(t) 32 defer cleanup() 33 34 err := ValidateContextDirectory(contextDir, excludes) 35 36 if err != nil { 37 t.Fatalf("Error should be nil, got: %s", err) 38 } 39 } 40 41 func TestGetContextFromLocalDirNoDockerfile(t *testing.T) { 42 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 43 defer cleanup() 44 45 absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") 46 47 if err == nil { 48 t.Fatalf("Error should not be nil") 49 } 50 51 if absContextDir != "" { 52 t.Fatalf("Absolute directory path should be empty, got: %s", absContextDir) 53 } 54 55 if relDockerfile != "" { 56 t.Fatalf("Relative path to Dockerfile should be empty, got: %s", relDockerfile) 57 } 58 } 59 60 func TestGetContextFromLocalDirNotExistingDir(t *testing.T) { 61 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 62 defer cleanup() 63 64 fakePath := filepath.Join(contextDir, "fake") 65 66 absContextDir, relDockerfile, err := GetContextFromLocalDir(fakePath, "") 67 68 if err == nil { 69 t.Fatalf("Error should not be nil") 70 } 71 72 if absContextDir != "" { 73 t.Fatalf("Absolute directory path should be empty, got: %s", absContextDir) 74 } 75 76 if relDockerfile != "" { 77 t.Fatalf("Relative path to Dockerfile should be empty, got: %s", relDockerfile) 78 } 79 } 80 81 func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) { 82 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 83 defer cleanup() 84 85 fakePath := filepath.Join(contextDir, "fake") 86 87 absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, fakePath) 88 89 if err == nil { 90 t.Fatalf("Error should not be nil") 91 } 92 93 if absContextDir != "" { 94 t.Fatalf("Absolute directory path should be empty, got: %s", absContextDir) 95 } 96 97 if relDockerfile != "" { 98 t.Fatalf("Relative path to Dockerfile should be empty, got: %s", relDockerfile) 99 } 100 } 101 102 func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) { 103 contextDir, dirCleanup := createTestTempDir(t, "", "builder-context-test") 104 defer dirCleanup() 105 106 createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) 107 108 chdirCleanup := chdir(t, contextDir) 109 defer chdirCleanup() 110 111 absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") 112 113 if err != nil { 114 t.Fatalf("Error when getting context from local dir: %s", err) 115 } 116 117 if absContextDir != contextDir { 118 t.Fatalf("Absolute directory path should be equal to %s, got: %s", contextDir, absContextDir) 119 } 120 121 if relDockerfile != DefaultDockerfileName { 122 t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", DefaultDockerfileName, relDockerfile) 123 } 124 } 125 126 func TestGetContextFromLocalDirWithDockerfile(t *testing.T) { 127 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 128 defer cleanup() 129 130 createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) 131 132 absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") 133 134 if err != nil { 135 t.Fatalf("Error when getting context from local dir: %s", err) 136 } 137 138 if absContextDir != contextDir { 139 t.Fatalf("Absolute directory path should be equal to %s, got: %s", contextDir, absContextDir) 140 } 141 142 if relDockerfile != DefaultDockerfileName { 143 t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", DefaultDockerfileName, relDockerfile) 144 } 145 } 146 147 func TestGetContextFromLocalDirLocalFile(t *testing.T) { 148 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 149 defer cleanup() 150 151 createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) 152 testFilename := createTestTempFile(t, contextDir, "tmpTest", "test", 0777) 153 154 absContextDir, relDockerfile, err := GetContextFromLocalDir(testFilename, "") 155 156 if err == nil { 157 t.Fatalf("Error should not be nil") 158 } 159 160 if absContextDir != "" { 161 t.Fatalf("Absolute directory path should be empty, got: %s", absContextDir) 162 } 163 164 if relDockerfile != "" { 165 t.Fatalf("Relative path to Dockerfile should be empty, got: %s", relDockerfile) 166 } 167 } 168 169 func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) { 170 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 171 defer cleanup() 172 173 chdirCleanup := chdir(t, contextDir) 174 defer chdirCleanup() 175 176 createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) 177 178 absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, DefaultDockerfileName) 179 180 if err != nil { 181 t.Fatalf("Error when getting context from local dir: %s", err) 182 } 183 184 if absContextDir != contextDir { 185 t.Fatalf("Absolute directory path should be equal to %s, got: %s", contextDir, absContextDir) 186 } 187 188 if relDockerfile != DefaultDockerfileName { 189 t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", DefaultDockerfileName, relDockerfile) 190 } 191 192 } 193 194 func TestGetContextFromReaderString(t *testing.T) { 195 tarArchive, relDockerfile, err := GetContextFromReader(ioutil.NopCloser(strings.NewReader(dockerfileContents)), "") 196 197 if err != nil { 198 t.Fatalf("Error when executing GetContextFromReader: %s", err) 199 } 200 201 tarReader := tar.NewReader(tarArchive) 202 203 _, err = tarReader.Next() 204 205 if err != nil { 206 t.Fatalf("Error when reading tar archive: %s", err) 207 } 208 209 buff := new(bytes.Buffer) 210 buff.ReadFrom(tarReader) 211 contents := buff.String() 212 213 _, err = tarReader.Next() 214 215 if err != io.EOF { 216 t.Fatalf("Tar stream too long: %s", err) 217 } 218 219 if err = tarArchive.Close(); err != nil { 220 t.Fatalf("Error when closing tar stream: %s", err) 221 } 222 223 if dockerfileContents != contents { 224 t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents) 225 } 226 227 if relDockerfile != DefaultDockerfileName { 228 t.Fatalf("Relative path not equals %s, got: %s", DefaultDockerfileName, relDockerfile) 229 } 230 } 231 232 func TestGetContextFromReaderTar(t *testing.T) { 233 contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") 234 defer cleanup() 235 236 createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) 237 238 tarStream, err := archive.Tar(contextDir, archive.Uncompressed) 239 240 if err != nil { 241 t.Fatalf("Error when creating tar: %s", err) 242 } 243 244 tarArchive, relDockerfile, err := GetContextFromReader(tarStream, DefaultDockerfileName) 245 246 if err != nil { 247 t.Fatalf("Error when executing GetContextFromReader: %s", err) 248 } 249 250 tarReader := tar.NewReader(tarArchive) 251 252 header, err := tarReader.Next() 253 254 if err != nil { 255 t.Fatalf("Error when reading tar archive: %s", err) 256 } 257 258 if header.Name != DefaultDockerfileName { 259 t.Fatalf("Dockerfile name should be: %s, got: %s", DefaultDockerfileName, header.Name) 260 } 261 262 buff := new(bytes.Buffer) 263 buff.ReadFrom(tarReader) 264 contents := buff.String() 265 266 _, err = tarReader.Next() 267 268 if err != io.EOF { 269 t.Fatalf("Tar stream too long: %s", err) 270 } 271 272 if err = tarArchive.Close(); err != nil { 273 t.Fatalf("Error when closing tar stream: %s", err) 274 } 275 276 if dockerfileContents != contents { 277 t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents) 278 } 279 280 if relDockerfile != DefaultDockerfileName { 281 t.Fatalf("Relative path not equals %s, got: %s", DefaultDockerfileName, relDockerfile) 282 } 283 } 284 285 func TestValidateContextDirectoryEmptyContext(t *testing.T) { 286 // This isn't a valid test on Windows. See https://play.golang.org/p/RR6z6jxR81. 287 // The test will ultimately end up calling filepath.Abs(""). On Windows, 288 // golang will error. On Linux, golang will return /. Due to there being 289 // drive letters on Windows, this is probably the correct behaviour for 290 // Windows. 291 if runtime.GOOS == "windows" { 292 t.Skip("Invalid test on Windows") 293 } 294 testValidateContextDirectory(t, prepareEmpty, []string{}) 295 } 296 297 func TestValidateContextDirectoryContextWithNoFiles(t *testing.T) { 298 testValidateContextDirectory(t, prepareNoFiles, []string{}) 299 } 300 301 func TestValidateContextDirectoryWithOneFile(t *testing.T) { 302 testValidateContextDirectory(t, prepareOneFile, []string{}) 303 } 304 305 func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) { 306 testValidateContextDirectory(t, prepareOneFile, []string{DefaultDockerfileName}) 307 }