github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/jib/init_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 "path/filepath" 22 "testing" 23 24 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 25 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 26 "github.com/GoogleContainerTools/skaffold/testutil" 27 ) 28 29 func TestValidate(t *testing.T) { 30 var tests = []struct { 31 description string 32 path string 33 enableGradle bool 34 fileContents string 35 command string 36 stdout string 37 expectedConfig []ArtifactConfig 38 }{ 39 { 40 description: "not a jib file", 41 path: "path/to/something.txt", 42 enableGradle: true, 43 expectedConfig: nil, 44 }, 45 { 46 description: "jib string not found", 47 path: "path/to/build.gradle", 48 enableGradle: true, 49 fileContents: "not a useful string", 50 expectedConfig: nil, 51 }, 52 { 53 description: "jib string found but not configured", 54 path: "path/to/build.gradle", 55 enableGradle: true, 56 fileContents: "com.google.cloud.tools.jib", 57 command: "gradle _jibSkaffoldInit -q --console=plain", 58 stdout: "error", 59 expectedConfig: nil, 60 }, 61 { 62 description: "jib gradle single project", 63 path: "path/to/build.gradle", 64 enableGradle: true, 65 fileContents: "com.google.cloud.tools.jib", 66 command: "gradle _jibSkaffoldInit -q --console=plain", 67 stdout: `BEGIN JIB JSON 68 {"image":"image","project":"project"} 69 `, 70 expectedConfig: []ArtifactConfig{ 71 {BuilderName: PluginName(JibGradle), File: "path/to/build.gradle", Image: "image", Project: "project"}, 72 }, 73 }, 74 { 75 description: "jib gradle-kotlin single project", 76 path: "path/to/build.gradle.kts", 77 enableGradle: true, 78 fileContents: "com.google.cloud.tools.jib", 79 command: "gradle _jibSkaffoldInit -q --console=plain", 80 stdout: `BEGIN JIB JSON 81 {"image":"image","project":"project"} 82 `, 83 expectedConfig: []ArtifactConfig{ 84 {BuilderName: PluginName(JibGradle), File: "path/to/build.gradle.kts", Image: "image", Project: "project"}, 85 }, 86 }, 87 { 88 description: "jib gradle multi-project", 89 enableGradle: true, 90 path: "path/to/build.gradle", 91 fileContents: "com.google.cloud.tools.jib", 92 command: "gradle _jibSkaffoldInit -q --console=plain", 93 stdout: `BEGIN JIB JSON 94 {"image":"image","project":"project1"} 95 96 BEGIN JIB JSON 97 {"project":"project2"} 98 `, 99 expectedConfig: []ArtifactConfig{ 100 {BuilderName: PluginName(JibGradle), File: "path/to/build.gradle", Image: "image", Project: "project1"}, 101 {BuilderName: PluginName(JibGradle), File: "path/to/build.gradle", Project: "project2"}, 102 }, 103 }, 104 { 105 description: "jib gradle disabled", 106 path: "path/to/build.gradle", 107 enableGradle: false, 108 fileContents: "com.google.cloud.tools.jib", 109 command: "", 110 stdout: ``, 111 expectedConfig: nil, 112 }, 113 { 114 description: "jib maven single module", 115 path: "path/to/pom.xml", 116 enableGradle: true, 117 fileContents: "<artifactId>jib-maven-plugin</artifactId>", 118 command: "mvn jib:_skaffold-init -q --batch-mode", 119 stdout: `BEGIN JIB JSON 120 {"image":"image","project":"project"}`, 121 expectedConfig: []ArtifactConfig{ 122 {BuilderName: PluginName(JibMaven), File: "path/to/pom.xml", Image: "image", Project: "project"}, 123 }, 124 }, 125 { 126 description: "jib maven multi-module", 127 path: "path/to/pom.xml", 128 fileContents: "<artifactId>jib-maven-plugin</artifactId>", 129 command: "mvn jib:_skaffold-init -q --batch-mode", 130 stdout: `BEGIN JIB JSON 131 {"image":"image","project":"project1"} 132 133 BEGIN JIB JSON 134 {"project":"project2"} 135 `, 136 expectedConfig: []ArtifactConfig{ 137 {BuilderName: PluginName(JibMaven), File: "path/to/pom.xml", Image: "image", Project: "project1"}, 138 {BuilderName: PluginName(JibMaven), File: "path/to/pom.xml", Project: "project2"}, 139 }, 140 }, 141 { 142 description: "jib maven pom.atom single module", 143 path: "path/to/pom.atom", 144 fileContents: "com.google.cloud.tools:jib-maven-plugin", 145 command: "mvn jib:_skaffold-init -q --batch-mode", 146 stdout: `BEGIN JIB JSON 147 {"image":"image","project":"project"}`, 148 expectedConfig: []ArtifactConfig{ 149 {BuilderName: PluginName(JibMaven), File: "path/to/pom.atom", Image: "image", Project: "project"}, 150 }, 151 }, 152 { 153 description: "jib maven pom.scala single module", 154 path: "path/to/pom.scala", 155 fileContents: `"com.google.cloud.tools" % "jib-maven-plugin"`, 156 command: "mvn jib:_skaffold-init -q --batch-mode", 157 stdout: `BEGIN JIB JSON 158 {"image":"image","project":"project"}`, 159 expectedConfig: []ArtifactConfig{ 160 {BuilderName: PluginName(JibMaven), File: "path/to/pom.scala", Image: "image", Project: "project"}, 161 }, 162 }, 163 } 164 for _, test := range tests { 165 testutil.Run(t, test.description, func(t *testutil.T) { 166 tmpDir := t.NewTempDir().Write(test.path, test.fileContents) 167 for i := range test.expectedConfig { 168 test.expectedConfig[i].File = tmpDir.Path(test.expectedConfig[i].File) 169 } 170 171 t.Override(&util.DefaultExecCommand, testutil.CmdRunOut( 172 test.command, 173 test.stdout, 174 )) 175 176 validated := Validate(context.Background(), tmpDir.Path(test.path), test.enableGradle) 177 178 t.CheckDeepEqual(test.expectedConfig, validated) 179 }) 180 } 181 } 182 183 func TestDescribe(t *testing.T) { 184 var tests = []struct { 185 description string 186 config ArtifactConfig 187 expectedPrompt string 188 }{ 189 { 190 description: "gradle without project", 191 config: ArtifactConfig{BuilderName: PluginName(JibGradle), File: "path/to/build.gradle"}, 192 expectedPrompt: "Jib Gradle Plugin (path/to/build.gradle)", 193 }, 194 { 195 description: "gradle with project", 196 config: ArtifactConfig{BuilderName: PluginName(JibGradle), Project: "project", File: "path/to/build.gradle"}, 197 expectedPrompt: "Jib Gradle Plugin (project, path/to/build.gradle)", 198 }, 199 { 200 description: "maven without project", 201 config: ArtifactConfig{BuilderName: PluginName(JibMaven), File: "path/to/pom.xml"}, 202 expectedPrompt: "Jib Maven Plugin (path/to/pom.xml)", 203 }, 204 { 205 description: "maven with project", 206 config: ArtifactConfig{BuilderName: PluginName(JibMaven), Project: "project", File: "path/to/pom.xml"}, 207 expectedPrompt: "Jib Maven Plugin (project, path/to/pom.xml)", 208 }, 209 } 210 for _, test := range tests { 211 testutil.Run(t, test.description, func(t *testutil.T) { 212 t.CheckDeepEqual(test.expectedPrompt, test.config.Describe()) 213 }) 214 } 215 } 216 217 func TestArtifactType(t *testing.T) { 218 var tests = []struct { 219 description string 220 config ArtifactConfig 221 expectedType latest.ArtifactType 222 }{ 223 { 224 description: "jib gradle", 225 config: ArtifactConfig{BuilderName: "Jib Gradle Plugin", File: filepath.Join("path", "to", "build.gradle"), Project: "project"}, 226 expectedType: latest.ArtifactType{JibArtifact: &latest.JibArtifact{Project: "project"}}, 227 }, 228 { 229 description: "jib gradle without project", 230 config: ArtifactConfig{BuilderName: "Jib Gradle Plugin", File: filepath.Join("path", "to", "build.gradle")}, 231 expectedType: latest.ArtifactType{JibArtifact: &latest.JibArtifact{}}, 232 }, 233 { 234 description: "jib maven", 235 config: ArtifactConfig{BuilderName: "Jib Maven Plugin", File: filepath.Join("path", "to", "pom.xml"), Project: "project"}, 236 expectedType: latest.ArtifactType{JibArtifact: &latest.JibArtifact{Project: "project"}}, 237 }, 238 { 239 description: "jib maven without project", 240 config: ArtifactConfig{BuilderName: "Jib Maven Plugin", File: filepath.Join("path", "to", "pom.xml")}, 241 expectedType: latest.ArtifactType{JibArtifact: &latest.JibArtifact{}}, 242 }, 243 } 244 for _, test := range tests { 245 testutil.Run(t, test.description, func(t *testutil.T) { 246 at := test.config.ArtifactType("ignored") // jib doesn't include file references in its artifacts 247 248 t.CheckDeepEqual(test.expectedType, at) 249 }) 250 } 251 }