github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/thirdparty/cmdconfig/commands/cmdtree/cmdtree_test.go (about) 1 // Copyright 2019 The Kubernetes Authors. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package cmdtree 5 6 import ( 7 "bytes" 8 "fmt" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/GoogleContainerTools/kpt/internal/printer/fake" 14 "github.com/GoogleContainerTools/kpt/internal/testutil" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestTreeCommandDefaultCurDir_files(t *testing.T) { 19 d, err := os.MkdirTemp("", "tree-test") 20 defer os.RemoveAll(d) 21 if !assert.NoError(t, err) { 22 return 23 } 24 revert := testutil.Chdir(t, d) 25 defer revert() 26 27 err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(` 28 apiVersion: v1 29 kind: Abstraction 30 metadata: 31 name: foo 32 configFn: 33 container: 34 image: gcr.io/example/reconciler:v1 35 annotations: 36 config.kubernetes.io/local-config: "true" 37 spec: 38 replicas: 1 39 --- 40 kind: Deployment 41 metadata: 42 labels: 43 app: nginx2 44 name: foo 45 annotations: 46 app: nginx2 47 spec: 48 replicas: 1 49 --- 50 kind: Service 51 metadata: 52 name: foo 53 annotations: 54 app: nginx 55 spec: 56 selector: 57 app: nginx 58 `), 0600) 59 if !assert.NoError(t, err) { 60 return 61 } 62 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment 63 metadata: 64 labels: 65 app: nginx 66 name: bar 67 annotations: 68 app: nginx 69 spec: 70 replicas: 3 71 `), 0600) 72 if !assert.NoError(t, err) { 73 return 74 } 75 76 // fmt the files 77 b := &bytes.Buffer{} 78 r := GetTreeRunner(fake.CtxWithPrinter(b, b), "") 79 r.Command.SetArgs([]string{}) 80 r.Command.SetOut(b) 81 if !assert.NoError(t, r.Command.Execute()) { 82 return 83 } 84 85 if !assert.Equal(t, fmt.Sprintf(`%s 86 ├── [f1.yaml] Abstraction foo 87 ├── [f1.yaml] Deployment foo 88 ├── [f1.yaml] Service foo 89 └── [f2.yaml] Deployment bar 90 `, filepath.Base(d)), b.String()) { 91 return 92 } 93 } 94 95 func TestTreeCommand_files(t *testing.T) { 96 d, err := os.MkdirTemp("", "tree-test") 97 defer os.RemoveAll(d) 98 if !assert.NoError(t, err) { 99 return 100 } 101 102 err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(` 103 apiVersion: v1 104 kind: Abstraction 105 metadata: 106 name: foo 107 configFn: 108 container: 109 image: gcr.io/example/reconciler:v1 110 annotations: 111 config.kubernetes.io/local-config: "true" 112 spec: 113 replicas: 1 114 --- 115 kind: Deployment 116 metadata: 117 labels: 118 app: nginx2 119 name: foo 120 annotations: 121 app: nginx2 122 spec: 123 replicas: 1 124 --- 125 kind: Service 126 metadata: 127 name: foo 128 annotations: 129 app: nginx 130 spec: 131 selector: 132 app: nginx 133 `), 0600) 134 if !assert.NoError(t, err) { 135 return 136 } 137 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment 138 metadata: 139 labels: 140 app: nginx 141 name: bar 142 annotations: 143 app: nginx 144 spec: 145 replicas: 3 146 `), 0600) 147 if !assert.NoError(t, err) { 148 return 149 } 150 151 // fmt the files 152 b := &bytes.Buffer{} 153 r := GetTreeRunner(fake.CtxWithPrinter(b, nil), "") 154 r.Command.SetArgs([]string{d}) 155 r.Command.SetOut(b) 156 if !assert.NoError(t, r.Command.Execute()) { 157 return 158 } 159 160 if !assert.Equal(t, fmt.Sprintf(`%s 161 ├── [f1.yaml] Abstraction foo 162 ├── [f1.yaml] Deployment foo 163 ├── [f1.yaml] Service foo 164 └── [f2.yaml] Deployment bar 165 `, filepath.Base(d)), b.String()) { 166 return 167 } 168 } 169 170 func TestTreeCommand_Kustomization(t *testing.T) { 171 d, err := os.MkdirTemp("", "tree-test") 172 defer os.RemoveAll(d) 173 if !assert.NoError(t, err) { 174 return 175 } 176 177 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment 178 metadata: 179 labels: 180 app: nginx 181 name: bar 182 annotations: 183 app: nginx 184 spec: 185 replicas: 3 186 `), 0600) 187 if !assert.NoError(t, err) { 188 return 189 } 190 191 err = os.WriteFile(filepath.Join(d, "Kustomization"), []byte(`apiVersion: kustomize.config.k8s.io/v1beta1 192 kind: Kustomization 193 resources: 194 - f2.yaml 195 `), 0600) 196 if !assert.NoError(t, err) { 197 return 198 } 199 200 // fmt the files 201 b := &bytes.Buffer{} 202 r := GetTreeRunner(fake.CtxWithPrinter(b, nil), "") 203 r.Command.SetArgs([]string{d}) 204 r.Command.SetOut(b) 205 if !assert.NoError(t, r.Command.Execute()) { 206 return 207 } 208 209 if !assert.Equal(t, fmt.Sprintf(`%s 210 └── [f2.yaml] Deployment bar 211 `, filepath.Base(d)), b.String()) { 212 return 213 } 214 } 215 216 func TestTreeCommand_subpkgs(t *testing.T) { 217 d, err := os.MkdirTemp("", "tree-test") 218 defer os.RemoveAll(d) 219 if !assert.NoError(t, err) { 220 t.FailNow() 221 } 222 223 err = os.MkdirAll(filepath.Join(d, "subpkg"), 0700) 224 if !assert.NoError(t, err) { 225 t.FailNow() 226 } 227 228 err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(` 229 apiVersion: v1 230 kind: Abstraction 231 metadata: 232 name: foo 233 configFn: 234 container: 235 image: gcr.io/example/reconciler:v1 236 annotations: 237 config.kubernetes.io/local-config: "true" 238 spec: 239 replicas: 1 240 --- 241 kind: Deployment 242 metadata: 243 labels: 244 app: nginx2 245 name: foo 246 annotations: 247 app: nginx2 248 spec: 249 replicas: 1 250 --- 251 kind: Service 252 metadata: 253 name: foo 254 annotations: 255 app: nginx 256 spec: 257 selector: 258 app: nginx 259 `), 0600) 260 if !assert.NoError(t, err) { 261 return 262 } 263 err = os.WriteFile(filepath.Join(d, "subpkg", "f2.yaml"), []byte(`kind: Deployment 264 metadata: 265 labels: 266 app: nginx 267 name: bar 268 annotations: 269 app: nginx 270 spec: 271 replicas: 3 272 `), 0600) 273 if !assert.NoError(t, err) { 274 return 275 } 276 277 err = os.WriteFile(filepath.Join(d, "Kptfile"), []byte(`apiVersion: kpt.dev/v1 278 kind: Kptfile 279 metadata: 280 name: mainpkg 281 openAPI: 282 definitions: 283 `), 0600) 284 if !assert.NoError(t, err) { 285 return 286 } 287 err = os.WriteFile(filepath.Join(d, "subpkg", "Kptfile"), []byte(`apiVersion: kpt.dev/v1 288 kind: Kptfile 289 metadata: 290 name: subpkg 291 openAPI: 292 definitions: 293 `), 0600) 294 if !assert.NoError(t, err) { 295 return 296 } 297 298 // fmt the files 299 b := &bytes.Buffer{} 300 r := GetTreeRunner(fake.CtxWithPrinter(b, nil), "") 301 r.Command.SetArgs([]string{d}) 302 r.Command.SetOut(b) 303 if !assert.NoError(t, r.Command.Execute()) { 304 return 305 } 306 307 if !assert.Equal(t, fmt.Sprintf(`Package %q 308 ├── [Kptfile] Kptfile mainpkg 309 ├── [f1.yaml] Abstraction foo 310 ├── [f1.yaml] Deployment foo 311 ├── [f1.yaml] Service foo 312 └── Package "subpkg" 313 ├── [Kptfile] Kptfile subpkg 314 └── [f2.yaml] Deployment bar 315 `, filepath.Base(d)), b.String()) { 316 return 317 } 318 } 319 320 func TestTreeCommand_CurDirInput(t *testing.T) { 321 d, err := os.MkdirTemp("", "tree-test") 322 defer os.RemoveAll(d) 323 if !assert.NoError(t, err) { 324 t.FailNow() 325 } 326 327 err = os.MkdirAll(filepath.Join(d, "Mainpkg", "Subpkg"), 0700) 328 if !assert.NoError(t, err) { 329 t.FailNow() 330 } 331 332 revert := testutil.Chdir(t, filepath.Join(d, "Mainpkg")) 333 defer revert() 334 335 err = os.WriteFile(filepath.Join(d, "Mainpkg", "f1.yaml"), []byte(` 336 kind: Deployment 337 metadata: 338 labels: 339 app: nginx2 340 name: foo 341 annotations: 342 app: nginx2 343 spec: 344 replicas: 1 345 `), 0600) 346 if !assert.NoError(t, err) { 347 return 348 } 349 err = os.WriteFile(filepath.Join(d, "Mainpkg", "Subpkg", "f2.yaml"), []byte(`kind: Deployment 350 metadata: 351 labels: 352 app: nginx 353 name: bar 354 annotations: 355 app: nginx 356 spec: 357 replicas: 3 358 `), 0600) 359 if !assert.NoError(t, err) { 360 return 361 } 362 363 err = os.WriteFile(filepath.Join(d, "Mainpkg", "Kptfile"), []byte(`apiVersion: kpt.dev/v1 364 kind: Kptfile 365 metadata: 366 name: Mainpkg 367 `), 0600) 368 if !assert.NoError(t, err) { 369 return 370 } 371 err = os.WriteFile(filepath.Join(d, "Mainpkg", "Subpkg", "Kptfile"), []byte(`apiVersion: kpt.dev/v1 372 kind: Kptfile 373 metadata: 374 name: Subpkg 375 `), 0600) 376 if !assert.NoError(t, err) { 377 return 378 } 379 380 // fmt the files 381 b := &bytes.Buffer{} 382 r := GetTreeRunner(fake.CtxWithPrinter(b, nil), "") 383 r.Command.SetArgs([]string{}) 384 r.Command.SetOut(b) 385 if !assert.NoError(t, r.Command.Execute()) { 386 return 387 } 388 389 if !assert.Equal(t, `Package "Mainpkg" 390 ├── [Kptfile] Kptfile Mainpkg 391 ├── [f1.yaml] Deployment foo 392 └── Package "Subpkg" 393 ├── [Kptfile] Kptfile Subpkg 394 └── [f2.yaml] Deployment bar 395 `, b.String()) { 396 return 397 } 398 } 399 400 func TestTreeCommand_symlink(t *testing.T) { 401 d, err := os.MkdirTemp("", "tree-test") 402 if !assert.NoError(t, err) { 403 return 404 } 405 revert := testutil.Chdir(t, d) 406 defer revert() 407 err = os.MkdirAll(filepath.Join(d, "foo"), 0700) 408 assert.NoError(t, err) 409 err = os.Symlink("foo", "foo-link") 410 if !assert.NoError(t, err) { 411 return 412 } 413 defer os.RemoveAll(d) 414 err = os.WriteFile(filepath.Join(d, "foo", "f1.yaml"), []byte(` 415 apiVersion: v1 416 kind: Abstraction 417 metadata: 418 name: foo 419 configFn: 420 container: 421 image: gcr.io/example/reconciler:v1 422 annotations: 423 config.kubernetes.io/local-config: "true" 424 spec: 425 replicas: 1 426 --- 427 kind: Deployment 428 metadata: 429 labels: 430 app: nginx2 431 name: foo 432 annotations: 433 app: nginx2 434 spec: 435 replicas: 1 436 --- 437 kind: Service 438 metadata: 439 name: foo 440 annotations: 441 app: nginx 442 spec: 443 selector: 444 app: nginx 445 `), 0600) 446 if !assert.NoError(t, err) { 447 return 448 } 449 err = os.WriteFile(filepath.Join(d, "foo", "f2.yaml"), []byte(`kind: Deployment 450 metadata: 451 labels: 452 app: nginx 453 name: bar 454 annotations: 455 app: nginx 456 spec: 457 replicas: 3 458 `), 0600) 459 if !assert.NoError(t, err) { 460 return 461 } 462 463 // fmt the files 464 b := &bytes.Buffer{} 465 stderr := &bytes.Buffer{} 466 r := GetTreeRunner(fake.CtxWithPrinter(b, stderr), "") 467 r.Command.SetArgs([]string{filepath.Join(d, "foo-link")}) 468 r.Command.SetOut(b) 469 if !assert.NoError(t, r.Command.Execute()) { 470 return 471 } 472 473 if !assert.Equal(t, `foo-link 474 ├── [f1.yaml] Abstraction foo 475 ├── [f1.yaml] Deployment foo 476 ├── [f1.yaml] Service foo 477 └── [f2.yaml] Deployment bar 478 `, b.String()) { 479 return 480 } 481 assert.Contains(t, stderr.String(), "please note that the symlinks within the package are ignored") 482 }