github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/jib/jib_test.go (about) 1 /* 2 Copyright 2019 The Skaffold 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 package jib 18 19 import ( 20 "context" 21 "fmt" 22 "os/exec" 23 "path/filepath" 24 "testing" 25 26 "github.com/docker/docker/client" 27 28 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" 29 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 30 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 31 "github.com/GoogleContainerTools/skaffold/testutil" 32 ) 33 34 func TestGetDependencies(t *testing.T) { 35 tmpDir := testutil.NewTempDir(t) 36 37 tmpDir.Touch("dep1", "dep2", "dep3/fileA", "dep3/sub/path/fileB") 38 dep1 := tmpDir.Path("dep1") 39 dep2 := tmpDir.Path("dep2") 40 dep3 := tmpDir.Path("dep3") 41 42 tests := []struct { 43 description string 44 stdout string 45 shouldErr bool 46 expectedDeps []string 47 }{ 48 { 49 description: "empty", 50 stdout: "", 51 shouldErr: true, 52 expectedDeps: nil, 53 }, 54 { 55 description: "base case", 56 stdout: "BEGIN JIB JSON\n{\"build\":[],\"inputs\":[],\"ignore\":[]}", 57 shouldErr: false, 58 expectedDeps: nil, 59 }, 60 { 61 description: "file in build and file in input", 62 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[\"%s\"],\"inputs\":[\"%s\"],\"ignore\":[]}\n", dep1, dep2), 63 shouldErr: false, 64 expectedDeps: []string{"dep1", "dep2"}, 65 }, 66 { 67 description: "dir in input should be expanded", 68 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[],\"inputs\":[\"%s\"],\"ignore\":[]}\n", dep3), 69 shouldErr: false, 70 expectedDeps: []string{filepath.FromSlash("dep3/fileA"), filepath.FromSlash("dep3/sub/path/fileB")}, 71 }, 72 { 73 description: "files and dir in input should be expanded", 74 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[],\"inputs\":[\"%s\",\"%s\",\"%s\"],\"ignore\":[]}\n", dep1, dep2, dep3), 75 shouldErr: false, 76 expectedDeps: []string{"dep1", "dep2", filepath.FromSlash("dep3/fileA"), filepath.FromSlash("dep3/sub/path/fileB")}, 77 }, 78 { 79 description: "non-existing files should be ignored", 80 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[],\"inputs\":[\"%s\",\"%s\",\"nonexistent\",\"%s\"],\"ignore\":[]}\n", dep1, dep2, dep3), 81 shouldErr: false, 82 expectedDeps: []string{"dep1", "dep2", filepath.FromSlash("dep3/fileA"), filepath.FromSlash("dep3/sub/path/fileB")}, 83 }, 84 { 85 description: "ignored files should not be reported", 86 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[],\"inputs\":[\"%s\",\"%s\"],\"ignore\":[\"%s\"]}\n", dep1, dep2, dep2), 87 shouldErr: false, 88 expectedDeps: []string{"dep1"}, 89 }, 90 { 91 description: "ignored directories should not be reported", 92 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[\"%s\"],\"inputs\":[\"%s\"],\"ignore\":[\"%s\",\"%s\"]}\n", dep1, dep3, dep1, dep3), 93 shouldErr: false, 94 expectedDeps: nil, 95 }, 96 { 97 description: "partial subpaths should be ignored", 98 stdout: fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[\"%s\",\"%s\",\"%s\"],\"inputs\":[],\"ignore\":[\"%s\"]}\n", dep1, dep2, dep3, tmpDir.Path("dep3/sub/path")), 99 shouldErr: false, 100 expectedDeps: []string{"dep1", "dep2", filepath.FromSlash("dep3/fileA")}, 101 }, 102 } 103 for _, test := range tests { 104 testutil.Run(t, test.description, func(t *testutil.T) { 105 t.Override(&util.DefaultExecCommand, testutil.CmdRunOut( 106 "ignored", 107 test.stdout, 108 )) 109 110 results, err := getDependencies(context.Background(), tmpDir.Root(), exec.Cmd{Args: []string{"ignored"}, Dir: tmpDir.Root()}, &latest.JibArtifact{Project: util.RandomID()}) 111 112 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedDeps, results) 113 }) 114 } 115 } 116 117 func TestGetUpdatedDependencies(t *testing.T) { 118 testutil.Run(t, "Both build definitions are created at the same time", func(t *testutil.T) { 119 tmpDir := t.NewTempDir() 120 121 stdout := fmt.Sprintf("BEGIN JIB JSON\n{\"build\":[\"%s\",\"%s\"],\"inputs\":[],\"ignore\":[]}\n", tmpDir.Path("build.gradle"), tmpDir.Path("settings.gradle")) 122 t.Override(&util.DefaultExecCommand, testutil. 123 CmdRunOut("ignored", stdout). 124 AndRunOut("ignored", stdout). 125 AndRunOut("ignored", stdout), 126 ) 127 128 listCmd := exec.Cmd{Args: []string{"ignored"}, Dir: tmpDir.Root()} 129 artifact := &latest.JibArtifact{Project: util.RandomID()} 130 131 // List dependencies 132 _, err := getDependencies(context.Background(), tmpDir.Root(), listCmd, artifact) 133 t.CheckNoError(err) 134 135 // Create new build definition files 136 tmpDir. 137 Write("build.gradle", ""). 138 Write("settings.gradle", "") 139 140 // Update dependencies 141 _, err = getDependencies(context.Background(), tmpDir.Root(), listCmd, artifact) 142 t.CheckNoError(err) 143 }) 144 } 145 146 func TestPluginName(t *testing.T) { 147 testutil.CheckDeepEqual(t, "Jib Maven Plugin", PluginName(JibMaven)) 148 testutil.CheckDeepEqual(t, "Jib Gradle Plugin", PluginName(JibGradle)) 149 } 150 151 func TestPluginType_IsKnown(t *testing.T) { 152 tests := []struct { 153 value PluginType 154 known bool 155 }{ 156 {JibMaven, true}, 157 {JibGradle, true}, 158 {PluginType("ant"), false}, 159 {PluginType("make"), false}, 160 {PluginType(""), false}, 161 } 162 for _, test := range tests { 163 testutil.Run(t, string(test.value), func(t *testutil.T) { 164 t.CheckDeepEqual(test.known, test.value.IsKnown()) 165 }) 166 } 167 } 168 169 func TestDeterminePluginType(t *testing.T) { 170 tests := []struct { 171 description string 172 files []string 173 artifact *latest.JibArtifact 174 shouldErr bool 175 PluginType PluginType 176 }{ 177 {"empty", []string{}, nil, true, PluginType("")}, 178 {"gradle-2", []string{"gradle.properties"}, nil, false, JibGradle}, 179 {"gradle-3", []string{"gradlew"}, nil, false, JibGradle}, 180 {"gradle-4", []string{"gradlew.bat"}, nil, false, JibGradle}, 181 {"gradle-5", []string{"gradlew.cmd"}, nil, false, JibGradle}, 182 {"gradle-6", []string{"settings.gradle"}, nil, false, JibGradle}, 183 {"gradle-kotlin-1", []string{"build.gradle.kts"}, nil, false, JibGradle}, 184 {"maven-1", []string{"pom.xml"}, nil, false, JibMaven}, 185 {"maven-2", []string{".mvn/maven.config"}, nil, false, JibMaven}, 186 {"maven-3", []string{".mvn/extensions.xml"}, nil, false, JibMaven}, 187 {"gradle override", []string{"pom.xml"}, &latest.JibArtifact{Type: string(JibGradle)}, false, JibGradle}, 188 {"maven override", []string{"build.gradle"}, &latest.JibArtifact{Type: string(JibMaven)}, false, JibMaven}, 189 } 190 for _, test := range tests { 191 testutil.Run(t, test.description, func(t *testutil.T) { 192 buildDir := t.NewTempDir() 193 buildDir.Touch(test.files...) 194 PluginType, err := DeterminePluginType(context.Background(), buildDir.Root(), test.artifact) 195 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.PluginType, PluginType) 196 }) 197 } 198 } 199 200 func TestGetProjectKey(t *testing.T) { 201 tests := []struct { 202 description string 203 artifact *latest.JibArtifact 204 workspace string 205 expected projectKey 206 }{ 207 { 208 "empty project", 209 &latest.JibArtifact{}, 210 "dir", 211 projectKey("dir+"), 212 }, 213 { 214 "non-empty project", 215 &latest.JibArtifact{Project: "project"}, 216 "dir", 217 projectKey("dir+project"), 218 }, 219 } 220 for _, test := range tests { 221 projectKey := getProjectKey(test.workspace, test.artifact) 222 testutil.CheckDeepEqual(t, test.expected, projectKey) 223 } 224 } 225 226 func fakeLocalDaemon(api client.CommonAPIClient) docker.LocalDaemon { 227 return docker.NewLocalDaemon(api, nil, false, nil) 228 }