github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/remotecontext/detect_test.go (about) 1 package remotecontext // import "github.com/demonoid81/moby/builder/remotecontext" 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "log" 7 "os" 8 "sort" 9 "testing" 10 11 "github.com/demonoid81/moby/builder" 12 "github.com/demonoid81/moby/pkg/containerfs" 13 ) 14 15 const ( 16 dockerfileContents = "FROM busybox" 17 dockerignoreFilename = ".dockerignore" 18 testfileContents = "test" 19 ) 20 21 const shouldStayFilename = "should_stay" 22 23 func extractFilenames(files []os.FileInfo) []string { 24 filenames := make([]string, len(files)) 25 26 for i, file := range files { 27 filenames[i] = file.Name() 28 } 29 30 return filenames 31 } 32 33 func checkDirectory(t *testing.T, dir string, expectedFiles []string) { 34 files, err := ioutil.ReadDir(dir) 35 36 if err != nil { 37 t.Fatalf("Could not read directory: %s", err) 38 } 39 40 if len(files) != len(expectedFiles) { 41 log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files)) 42 } 43 44 filenames := extractFilenames(files) 45 sort.Strings(filenames) 46 sort.Strings(expectedFiles) 47 48 for i, filename := range filenames { 49 if filename != expectedFiles[i] { 50 t.Fatalf("File %s should be in the directory, got: %s", expectedFiles[i], filename) 51 } 52 } 53 } 54 55 func executeProcess(t *testing.T, contextDir string) { 56 modifiableCtx := &stubRemote{root: containerfs.NewLocalContainerFS(contextDir)} 57 58 err := removeDockerfile(modifiableCtx, builder.DefaultDockerfileName) 59 60 if err != nil { 61 t.Fatalf("Error when executing Process: %s", err) 62 } 63 } 64 65 func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) { 66 contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test") 67 defer cleanup() 68 69 createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777) 70 createTestTempFile(t, contextDir, dockerignoreFilename, "Dockerfile\n.dockerignore", 0777) 71 createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777) 72 73 executeProcess(t, contextDir) 74 75 checkDirectory(t, contextDir, []string{shouldStayFilename}) 76 77 } 78 79 func TestProcessNoDockerignore(t *testing.T) { 80 contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test") 81 defer cleanup() 82 83 createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777) 84 createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777) 85 86 executeProcess(t, contextDir) 87 88 checkDirectory(t, contextDir, []string{shouldStayFilename, builder.DefaultDockerfileName}) 89 90 } 91 92 func TestProcessShouldLeaveAllFiles(t *testing.T) { 93 contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test") 94 defer cleanup() 95 96 createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777) 97 createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777) 98 createTestTempFile(t, contextDir, dockerignoreFilename, "input1\ninput2", 0777) 99 100 executeProcess(t, contextDir) 101 102 checkDirectory(t, contextDir, []string{shouldStayFilename, builder.DefaultDockerfileName, dockerignoreFilename}) 103 104 } 105 106 // TODO: remove after moving to a separate pkg 107 type stubRemote struct { 108 root containerfs.ContainerFS 109 } 110 111 func (r *stubRemote) Hash(path string) (string, error) { 112 return "", errors.New("not implemented") 113 } 114 115 func (r *stubRemote) Root() containerfs.ContainerFS { 116 return r.root 117 } 118 func (r *stubRemote) Close() error { 119 return errors.New("not implemented") 120 } 121 func (r *stubRemote) Remove(p string) error { 122 return r.root.Remove(r.root.Join(r.root.Path(), p)) 123 }