github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/buildkitutil/buildkitutil_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 Portions from https://github.com/docker/cli/blob/v20.10.9/cli/command/image/build/context.go 19 Copyright (C) Docker authors. 20 Licensed under the Apache License, Version 2.0 21 NOTICE: https://github.com/docker/cli/blob/v20.10.9/NOTICE 22 */ 23 24 package buildkitutil 25 26 import ( 27 "os" 28 "path/filepath" 29 "testing" 30 31 "gotest.tools/v3/assert" 32 ) 33 34 func TestBuildKitFile(t *testing.T) { 35 var tmp = t.TempDir() 36 var wd, err = os.Getwd() 37 assert.NilError(t, err) 38 err = os.Chdir(tmp) 39 assert.NilError(t, err) 40 defer os.Chdir(wd) 41 type args struct { 42 dir string 43 inputfile string 44 } 45 tests := []struct { 46 name string 47 args args 48 prepare func(t *testing.T) error 49 wantAbsDir string 50 wantFile string 51 wantErr bool 52 }{ 53 { 54 name: "only Dockerfile is present", 55 prepare: func(t *testing.T) error { 56 return os.WriteFile(filepath.Join(tmp, DefaultDockerfileName), []byte{}, 0644) 57 }, 58 args: args{".", ""}, 59 wantAbsDir: tmp, 60 wantFile: DefaultDockerfileName, 61 wantErr: false, 62 }, 63 { 64 name: "only Containerfile is present", 65 prepare: func(t *testing.T) error { 66 return os.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{}, 0644) 67 }, 68 args: args{".", ""}, 69 wantAbsDir: tmp, 70 wantFile: ContainerfileName, 71 wantErr: false, 72 }, 73 { 74 name: "both Dockerfile and Containerfile are present", 75 prepare: func(t *testing.T) error { 76 var err = os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, 0644) 77 if err != nil { 78 return err 79 } 80 return os.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{}, 0644) 81 }, 82 args: args{".", ""}, 83 wantAbsDir: tmp, 84 wantFile: DefaultDockerfileName, 85 wantErr: false, 86 }, 87 { 88 name: "Dockerfile and Containerfile have different contents", 89 prepare: func(t *testing.T) error { 90 var err = os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{'d'}, 0644) 91 if err != nil { 92 return err 93 } 94 return os.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{'c'}, 0644) 95 }, 96 args: args{".", ""}, 97 wantAbsDir: tmp, 98 wantFile: DefaultDockerfileName, 99 wantErr: false, 100 }, 101 { 102 name: "Custom file is specfied", 103 prepare: func(t *testing.T) error { 104 return os.WriteFile(filepath.Join(tmp, "CustomFile"), []byte{}, 0644) 105 }, 106 args: args{".", "CustomFile"}, 107 wantAbsDir: tmp, 108 wantFile: "CustomFile", 109 wantErr: false, 110 }, 111 { 112 name: "Absolute path is specified along with custom file", 113 prepare: func(t *testing.T) error { 114 return os.WriteFile(filepath.Join(tmp, "CustomFile"), []byte{}, 0644) 115 }, 116 args: args{tmp, "CustomFile"}, 117 wantAbsDir: tmp, 118 wantFile: "CustomFile", 119 wantErr: false, 120 }, 121 { 122 name: "Absolute path is specified along with Docker file", 123 prepare: func(t *testing.T) error { 124 return os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, 0644) 125 }, 126 args: args{tmp, "."}, 127 wantAbsDir: tmp, 128 wantFile: DefaultDockerfileName, 129 wantErr: false, 130 }, 131 { 132 name: "Absolute path is specified with Container file in the path", 133 prepare: func(t *testing.T) error { 134 return os.WriteFile(filepath.Join(tmp, ContainerfileName), []byte{}, 0644) 135 }, 136 args: args{tmp, "."}, 137 wantAbsDir: tmp, 138 wantFile: ContainerfileName, 139 wantErr: false, 140 }, 141 } 142 for _, tt := range tests { 143 t.Run(tt.name, func(t *testing.T) { 144 tt.prepare(t) 145 gotAbsDir, gotFile, err := BuildKitFile(tt.args.dir, tt.args.inputfile) 146 if (err != nil) != tt.wantErr { 147 t.Errorf("BuildKitFile() error = %v, wantErr %v", err, tt.wantErr) 148 return 149 } 150 if gotAbsDir != tt.wantAbsDir { 151 t.Errorf("BuildKitFile() gotAbsDir = %v, want %v", gotAbsDir, tt.wantAbsDir) 152 } 153 if gotFile != tt.wantFile { 154 t.Errorf("BuildKitFile() gotFile = %v, want %v", gotFile, tt.wantFile) 155 } 156 157 entry, err := os.ReadDir(tmp) 158 assert.NilError(t, err) 159 for _, f := range entry { 160 err = os.Remove(f.Name()) 161 assert.NilError(t, err) 162 } 163 }) 164 } 165 }