| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package runfn |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"os/user" |
| 11 |
"path/filepath" |
| 12 |
"runtime" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"sigs.k8s.io/kustomize/kyaml/copyutil" |
| 18 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 19 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 20 |
"sigs.k8s.io/kustomize/kyaml/fn/runtime/container" |
| 21 |
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil" |
| 22 |
"sigs.k8s.io/kustomize/kyaml/kio" |
| 23 |
"sigs.k8s.io/kustomize/kyaml/kio/filters" |
| 24 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
ValueReplacerYAMLData = `apiVersion: v1 |
| 29 |
kind: ValueReplacer |
| 30 |
metadata: |
| 31 |
annotations: |
| 32 |
config.kubernetes.io/function: | |
| 33 |
container: |
| 34 |
image: gcr.io/example.com/image:version |
| 35 |
config.kubernetes.io/local-config: "true" |
| 36 |
stringMatch: Deployment |
| 37 |
replace: StatefulSet |
| 38 |
` |
| 39 |
) |
| 40 |
|
| 41 |
func currentUser() (*user.User, error) { |
| 42 |
return &user.User{ |
| 43 |
Uid: "1", |
| 44 |
Gid: "2", |
| 45 |
}, nil |
| 46 |
} |
| 47 |
|
| 48 |
func TestRunFns_init(t *testing.T) { |
| 49 |
instance := RunFns{} |
| 50 |
instance.init() |
| 51 |
if !assert.Equal(t, instance.Input, os.Stdin) { |
| 52 |
t.FailNow() |
| 53 |
} |
| 54 |
if !assert.Equal(t, instance.Output, os.Stdout) { |
| 55 |
t.FailNow() |
| 56 |
} |
| 57 |
|
| 58 |
api, err := yaml.Parse(`apiVersion: apps/v1 |
| 59 |
kind: |
| 60 |
`) |
| 61 |
spec := runtimeutil.FunctionSpec{ |
| 62 |
Container: runtimeutil.ContainerSpec{ |
| 63 |
Image: "example.com:version", |
| 64 |
}, |
| 65 |
} |
| 66 |
if !assert.NoError(t, err) { |
| 67 |
return |
| 68 |
} |
| 69 |
filter, _ := instance.functionFilterProvider(spec, api, currentUser) |
| 70 |
c := container.NewContainer(runtimeutil.ContainerSpec{Image: "example.com:version"}, "nobody") |
| 71 |
cf := &c |
| 72 |
cf.Exec.FunctionConfig = api |
| 73 |
assert.Equal(t, cf, filter) |
| 74 |
} |
| 75 |
|
| 76 |
func TestRunFns_initAsCurrentUser(t *testing.T) { |
| 77 |
instance := RunFns{ |
| 78 |
AsCurrentUser: true, |
| 79 |
} |
| 80 |
instance.init() |
| 81 |
if !assert.Equal(t, instance.Input, os.Stdin) { |
| 82 |
t.FailNow() |
| 83 |
} |
| 84 |
if !assert.Equal(t, instance.Output, os.Stdout) { |
| 85 |
t.FailNow() |
| 86 |
} |
| 87 |
|
| 88 |
api, err := yaml.Parse(`apiVersion: apps/v1 |
| 89 |
kind: |
| 90 |
`) |
| 91 |
spec := runtimeutil.FunctionSpec{ |
| 92 |
Container: runtimeutil.ContainerSpec{ |
| 93 |
Image: "example.com:version", |
| 94 |
}, |
| 95 |
} |
| 96 |
if !assert.NoError(t, err) { |
| 97 |
return |
| 98 |
} |
| 99 |
filter, _ := instance.functionFilterProvider(spec, api, currentUser) |
| 100 |
c := container.NewContainer(runtimeutil.ContainerSpec{Image: "example.com:version"}, "1:2") |
| 101 |
cf := &c |
| 102 |
cf.Exec.FunctionConfig = api |
| 103 |
assert.Equal(t, cf, filter) |
| 104 |
} |
| 105 |
|
| 106 |
func TestRunFns_Execute__initGlobalScope(t *testing.T) { |
| 107 |
instance := RunFns{GlobalScope: true} |
| 108 |
instance.init() |
| 109 |
if !assert.Equal(t, instance.Input, os.Stdin) { |
| 110 |
t.FailNow() |
| 111 |
} |
| 112 |
if !assert.Equal(t, instance.Output, os.Stdout) { |
| 113 |
t.FailNow() |
| 114 |
} |
| 115 |
api, err := yaml.Parse(`apiVersion: apps/v1 |
| 116 |
kind: |
| 117 |
`) |
| 118 |
if !assert.NoError(t, err) { |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
spec := runtimeutil.FunctionSpec{ |
| 123 |
Container: runtimeutil.ContainerSpec{ |
| 124 |
Image: "example.com:version", |
| 125 |
}, |
| 126 |
} |
| 127 |
if !assert.NoError(t, err) { |
| 128 |
return |
| 129 |
} |
| 130 |
filter, _ := instance.functionFilterProvider(spec, api, currentUser) |
| 131 |
c := container.NewContainer(runtimeutil.ContainerSpec{Image: "example.com:version"}, "nobody") |
| 132 |
cf := &c |
| 133 |
cf.Exec.FunctionConfig = api |
| 134 |
cf.Exec.GlobalScope = true |
| 135 |
assert.Equal(t, cf, filter) |
| 136 |
} |
| 137 |
|
| 138 |
func TestRunFns_Execute__initDefault(t *testing.T) { |
| 139 |
b := &bytes.Buffer{} |
| 140 |
var tests = []struct { |
| 141 |
instance RunFns |
| 142 |
expected RunFns |
| 143 |
name string |
| 144 |
}{ |
| 145 |
{ |
| 146 |
instance: RunFns{}, |
| 147 |
name: "empty", |
| 148 |
expected: RunFns{Output: os.Stdout, Input: os.Stdin, NoFunctionsFromInput: getFalse()}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
name: "explicit output", |
| 152 |
instance: RunFns{Output: b}, |
| 153 |
expected: RunFns{Output: b, Input: os.Stdin, NoFunctionsFromInput: getFalse()}, |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "explicit input", |
| 157 |
instance: RunFns{Input: b}, |
| 158 |
expected: RunFns{Output: os.Stdout, Input: b, NoFunctionsFromInput: getFalse()}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "explicit functions -- no functions from input", |
| 162 |
instance: RunFns{Functions: []*yaml.RNode{{}}}, |
| 163 |
expected: RunFns{Output: os.Stdout, Input: os.Stdin, NoFunctionsFromInput: getTrue(), Functions: []*yaml.RNode{{}}}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "explicit functions -- yes functions from input", |
| 167 |
instance: RunFns{Functions: []*yaml.RNode{{}}, NoFunctionsFromInput: getFalse()}, |
| 168 |
expected: RunFns{Output: os.Stdout, Input: os.Stdin, NoFunctionsFromInput: getFalse(), Functions: []*yaml.RNode{{}}}, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "explicit functions in paths -- no functions from input", |
| 172 |
instance: RunFns{FunctionPaths: []string{"foo"}}, |
| 173 |
expected: RunFns{ |
| 174 |
Output: os.Stdout, |
| 175 |
Input: os.Stdin, |
| 176 |
NoFunctionsFromInput: getTrue(), |
| 177 |
FunctionPaths: []string{"foo"}, |
| 178 |
}, |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: "functions in paths -- yes functions from input", |
| 182 |
instance: RunFns{FunctionPaths: []string{"foo"}, NoFunctionsFromInput: getFalse()}, |
| 183 |
expected: RunFns{ |
| 184 |
Output: os.Stdout, |
| 185 |
Input: os.Stdin, |
| 186 |
NoFunctionsFromInput: getFalse(), |
| 187 |
FunctionPaths: []string{"foo"}, |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "explicit directories in mounts", |
| 192 |
instance: RunFns{StorageMounts: []runtimeutil.StorageMount{{MountType: "volume", Src: "myvol", DstPath: "/local/"}}}, |
| 193 |
expected: RunFns{ |
| 194 |
Output: os.Stdout, |
| 195 |
Input: os.Stdin, |
| 196 |
NoFunctionsFromInput: getFalse(), |
| 197 |
StorageMounts: []runtimeutil.StorageMount{{MountType: "volume", Src: "myvol", DstPath: "/local/"}}, |
| 198 |
}, |
| 199 |
}, |
| 200 |
} |
| 201 |
for i := range tests { |
| 202 |
tt := tests[i] |
| 203 |
t.Run(tt.name, func(t *testing.T) { |
| 204 |
(&tt.instance).init() |
| 205 |
(&tt.instance).functionFilterProvider = nil |
| 206 |
if !assert.Equal(t, tt.expected, tt.instance) { |
| 207 |
t.FailNow() |
| 208 |
} |
| 209 |
}) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func getTrue() *bool { |
| 214 |
t := true |
| 215 |
return &t |
| 216 |
} |
| 217 |
|
| 218 |
func getFalse() *bool { |
| 219 |
f := false |
| 220 |
return &f |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
func TestRunFns_getFilters(t *testing.T) { |
| 225 |
type f struct { |
| 226 |
|
| 227 |
path, value string |
| 228 |
|
| 229 |
|
| 230 |
outOfPackage bool |
| 231 |
|
| 232 |
|
| 233 |
explicitFunction bool |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
newFnPath bool |
| 240 |
} |
| 241 |
var tests = []struct { |
| 242 |
|
| 243 |
in []f |
| 244 |
|
| 245 |
out []string |
| 246 |
|
| 247 |
|
| 248 |
outFn func(string) []string |
| 249 |
|
| 250 |
|
| 251 |
error string |
| 252 |
|
| 253 |
|
| 254 |
name string |
| 255 |
|
| 256 |
noFunctionsFromInput *bool |
| 257 |
|
| 258 |
disableContainers bool |
| 259 |
}{ |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
{name: "single implicit function", |
| 264 |
in: []f{ |
| 265 |
{ |
| 266 |
path: filepath.Join("foo", "bar.yaml"), |
| 267 |
value: ` |
| 268 |
apiVersion: example.com/v1alpha1 |
| 269 |
kind: ExampleFunction |
| 270 |
metadata: |
| 271 |
annotations: |
| 272 |
config.kubernetes.io/function: | |
| 273 |
container: |
| 274 |
image: gcr.io/example.com/image:v1.0.0 |
| 275 |
config.kubernetes.io/local-config: "true" |
| 276 |
`, |
| 277 |
}, |
| 278 |
}, |
| 279 |
out: []string{"gcr.io/example.com/image:v1.0.0"}, |
| 280 |
}, |
| 281 |
|
| 282 |
{ |
| 283 |
name: "no function spec", |
| 284 |
in: []f{ |
| 285 |
{ |
| 286 |
explicitFunction: true, |
| 287 |
value: ` |
| 288 |
apiVersion: example.com/v1alpha1 |
| 289 |
kind: ExampleFunction |
| 290 |
metadata: |
| 291 |
annotations: |
| 292 |
foo: bar |
| 293 |
`, |
| 294 |
}, |
| 295 |
}, |
| 296 |
}, |
| 297 |
{ |
| 298 |
name: "invalid input object", |
| 299 |
in: []f{ |
| 300 |
{ |
| 301 |
explicitFunction: true, |
| 302 |
value: ` |
| 303 |
foo: bar |
| 304 |
`, |
| 305 |
}, |
| 306 |
}, |
| 307 |
error: "failed to get FunctionSpec: failed to get ResourceMeta: missing Resource metadata", |
| 308 |
}, |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
{name: "defer_failure", |
| 314 |
in: []f{ |
| 315 |
{ |
| 316 |
path: filepath.Join("foo", "bar.yaml"), |
| 317 |
value: ` |
| 318 |
apiVersion: example.com/v1alpha1 |
| 319 |
kind: ExampleFunction |
| 320 |
metadata: |
| 321 |
annotations: |
| 322 |
config.kubernetes.io/function: | |
| 323 |
deferFailure: true |
| 324 |
container: |
| 325 |
image: gcr.io/example.com/image:v1.0.0 |
| 326 |
config.kubernetes.io/local-config: "true" |
| 327 |
`, |
| 328 |
}, |
| 329 |
}, |
| 330 |
out: []string{"gcr.io/example.com/image:v1.0.0 deferFailure: true"}, |
| 331 |
}, |
| 332 |
{ |
| 333 |
name: "parse_failure", |
| 334 |
in: []f{ |
| 335 |
{ |
| 336 |
path: filepath.Join("foo", "bar.yaml"), |
| 337 |
value: ` |
| 338 |
apiVersion: example.com/v1alpha1 |
| 339 |
kind: ExampleFunction |
| 340 |
metadata: |
| 341 |
annotations: |
| 342 |
config.kubernetes.io/function: | |
| 343 |
containeeer: |
| 344 |
image: gcr.io/example.com/image:v1.0.0 |
| 345 |
`, |
| 346 |
}, |
| 347 |
}, |
| 348 |
error: "config.kubernetes.io/function unmarshal error: error unmarshaling JSON: while decoding JSON: json: unknown field \"containeeer\"", |
| 349 |
}, |
| 350 |
|
| 351 |
{name: "disable containers", |
| 352 |
in: []f{ |
| 353 |
{ |
| 354 |
path: filepath.Join("foo", "bar.yaml"), |
| 355 |
value: ` |
| 356 |
apiVersion: example.com/v1alpha1 |
| 357 |
kind: ExampleFunction |
| 358 |
metadata: |
| 359 |
annotations: |
| 360 |
config.kubernetes.io/function: | |
| 361 |
container: |
| 362 |
image: gcr.io/example.com/image:v1.0.0 |
| 363 |
config.kubernetes.io/local-config: "true" |
| 364 |
`, |
| 365 |
}, |
| 366 |
}, |
| 367 |
out: nil, |
| 368 |
disableContainers: true, |
| 369 |
}, |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
{name: "sort functions -- deepest first", |
| 375 |
in: []f{ |
| 376 |
{ |
| 377 |
path: "a.yaml", |
| 378 |
value: ` |
| 379 |
metadata: |
| 380 |
annotations: |
| 381 |
config.kubernetes.io/function: | |
| 382 |
container: |
| 383 |
image: a |
| 384 |
`, |
| 385 |
}, |
| 386 |
{ |
| 387 |
path: filepath.Join("foo", "b.yaml"), |
| 388 |
value: ` |
| 389 |
metadata: |
| 390 |
annotations: |
| 391 |
config.kubernetes.io/function: | |
| 392 |
container: |
| 393 |
image: b |
| 394 |
`, |
| 395 |
}, |
| 396 |
}, |
| 397 |
out: []string{"b", "a"}, |
| 398 |
}, |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
{name: "sort functions -- skip implicit with output of package", |
| 404 |
in: []f{ |
| 405 |
{ |
| 406 |
path: filepath.Join("foo", "a.yaml"), |
| 407 |
outOfPackage: true, |
| 408 |
value: ` |
| 409 |
metadata: |
| 410 |
annotations: |
| 411 |
config.kubernetes.io/function: | |
| 412 |
container: |
| 413 |
image: a |
| 414 |
`, |
| 415 |
}, |
| 416 |
{ |
| 417 |
path: "b.yaml", |
| 418 |
value: ` |
| 419 |
metadata: |
| 420 |
annotations: |
| 421 |
config.kubernetes.io/function: | |
| 422 |
container: |
| 423 |
image: b |
| 424 |
`, |
| 425 |
}, |
| 426 |
}, |
| 427 |
out: []string{"a"}, |
| 428 |
}, |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
{name: "sort functions -- skip implicit", |
| 434 |
noFunctionsFromInput: getTrue(), |
| 435 |
in: []f{ |
| 436 |
{ |
| 437 |
path: filepath.Join("foo", "a.yaml"), |
| 438 |
value: ` |
| 439 |
metadata: |
| 440 |
annotations: |
| 441 |
config.kubernetes.io/function: | |
| 442 |
container: |
| 443 |
image: a |
| 444 |
`, |
| 445 |
}, |
| 446 |
{ |
| 447 |
path: "b.yaml", |
| 448 |
value: ` |
| 449 |
metadata: |
| 450 |
annotations: |
| 451 |
config.kubernetes.io/function: | |
| 452 |
container: |
| 453 |
image: b |
| 454 |
`, |
| 455 |
}, |
| 456 |
}, |
| 457 |
out: nil, |
| 458 |
}, |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
{name: "sort functions -- include implicit", |
| 464 |
noFunctionsFromInput: getFalse(), |
| 465 |
in: []f{ |
| 466 |
{ |
| 467 |
path: filepath.Join("foo", "a.yaml"), |
| 468 |
value: ` |
| 469 |
metadata: |
| 470 |
annotations: |
| 471 |
config.kubernetes.io/function: | |
| 472 |
container: |
| 473 |
image: a |
| 474 |
`, |
| 475 |
}, |
| 476 |
{ |
| 477 |
path: "b.yaml", |
| 478 |
value: ` |
| 479 |
metadata: |
| 480 |
annotations: |
| 481 |
config.kubernetes.io/function: | |
| 482 |
container: |
| 483 |
image: b |
| 484 |
`, |
| 485 |
}, |
| 486 |
}, |
| 487 |
out: []string{"a", "b"}, |
| 488 |
}, |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
{name: "sort functions -- implicit first", |
| 494 |
noFunctionsFromInput: getFalse(), |
| 495 |
in: []f{ |
| 496 |
{ |
| 497 |
path: filepath.Join("foo", "a.yaml"), |
| 498 |
outOfPackage: true, |
| 499 |
value: ` |
| 500 |
metadata: |
| 501 |
annotations: |
| 502 |
config.kubernetes.io/function: | |
| 503 |
container: |
| 504 |
image: a |
| 505 |
`, |
| 506 |
}, |
| 507 |
{ |
| 508 |
path: "b.yaml", |
| 509 |
value: ` |
| 510 |
metadata: |
| 511 |
annotations: |
| 512 |
config.kubernetes.io/function: | |
| 513 |
container: |
| 514 |
image: b |
| 515 |
`, |
| 516 |
}, |
| 517 |
}, |
| 518 |
out: []string{"b", "a"}, |
| 519 |
}, |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
{name: "explicit functions", |
| 525 |
in: []f{ |
| 526 |
{ |
| 527 |
explicitFunction: true, |
| 528 |
value: ` |
| 529 |
metadata: |
| 530 |
annotations: |
| 531 |
config.kubernetes.io/function: | |
| 532 |
container: |
| 533 |
image: c |
| 534 |
`, |
| 535 |
}, |
| 536 |
{ |
| 537 |
path: "b.yaml", |
| 538 |
value: ` |
| 539 |
metadata: |
| 540 |
annotations: |
| 541 |
config.kubernetes.io/function: | |
| 542 |
container: |
| 543 |
image: b |
| 544 |
`, |
| 545 |
}, |
| 546 |
}, |
| 547 |
out: []string{"c"}, |
| 548 |
}, |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
{name: "sort functions -- implicit first", |
| 554 |
noFunctionsFromInput: getFalse(), |
| 555 |
in: []f{ |
| 556 |
{ |
| 557 |
explicitFunction: true, |
| 558 |
value: ` |
| 559 |
metadata: |
| 560 |
annotations: |
| 561 |
config.kubernetes.io/function: | |
| 562 |
container: |
| 563 |
image: c |
| 564 |
`, |
| 565 |
}, |
| 566 |
{ |
| 567 |
path: filepath.Join("foo", "a.yaml"), |
| 568 |
outOfPackage: true, |
| 569 |
value: ` |
| 570 |
metadata: |
| 571 |
annotations: |
| 572 |
config.kubernetes.io/function: | |
| 573 |
container: |
| 574 |
image: a |
| 575 |
`, |
| 576 |
}, |
| 577 |
{ |
| 578 |
path: "b.yaml", |
| 579 |
value: ` |
| 580 |
metadata: |
| 581 |
annotations: |
| 582 |
config.kubernetes.io/function: | |
| 583 |
container: |
| 584 |
image: b |
| 585 |
`, |
| 586 |
}, |
| 587 |
}, |
| 588 |
out: []string{"b", "a", "c"}, |
| 589 |
}, |
| 590 |
} |
| 591 |
|
| 592 |
for i := range tests { |
| 593 |
tt := tests[i] |
| 594 |
t.Run(tt.name, func(t *testing.T) { |
| 595 |
|
| 596 |
d := setupTest(t) |
| 597 |
|
| 598 |
|
| 599 |
var fnPaths []string |
| 600 |
var parsedFns []*yaml.RNode |
| 601 |
var fnPath string |
| 602 |
var err error |
| 603 |
for _, f := range tt.in { |
| 604 |
|
| 605 |
var dir string |
| 606 |
switch { |
| 607 |
case f.outOfPackage: |
| 608 |
|
| 609 |
if f.newFnPath || fnPath == "" { |
| 610 |
|
| 611 |
fnPath = t.TempDir() |
| 612 |
fnPaths = append(fnPaths, fnPath) |
| 613 |
} |
| 614 |
dir = fnPath |
| 615 |
case f.explicitFunction: |
| 616 |
parsedFns = append(parsedFns, yaml.MustParse(f.value)) |
| 617 |
default: |
| 618 |
|
| 619 |
dir = d |
| 620 |
} |
| 621 |
|
| 622 |
if !f.explicitFunction { |
| 623 |
|
| 624 |
err = os.MkdirAll(filepath.Join(dir, filepath.Dir(f.path)), 0700) |
| 625 |
if !assert.NoError(t, err) { |
| 626 |
t.FailNow() |
| 627 |
} |
| 628 |
err := os.WriteFile(filepath.Join(dir, f.path), []byte(f.value), 0600) |
| 629 |
if !assert.NoError(t, err) { |
| 630 |
t.FailNow() |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
r := &RunFns{ |
| 637 |
DisableContainers: tt.disableContainers, |
| 638 |
FunctionPaths: fnPaths, |
| 639 |
Functions: parsedFns, |
| 640 |
Path: d, |
| 641 |
NoFunctionsFromInput: tt.noFunctionsFromInput, |
| 642 |
} |
| 643 |
r.init() |
| 644 |
|
| 645 |
|
| 646 |
var results []string |
| 647 |
_, fltrs, _, err := r.getNodesAndFilters() |
| 648 |
|
| 649 |
if tt.error != "" { |
| 650 |
if !assert.EqualError(t, err, tt.error) { |
| 651 |
t.FailNow() |
| 652 |
} |
| 653 |
return |
| 654 |
} |
| 655 |
|
| 656 |
if !assert.NoError(t, err) { |
| 657 |
t.FailNow() |
| 658 |
} |
| 659 |
for _, f := range fltrs { |
| 660 |
results = append(results, strings.TrimSpace(fmt.Sprintf("%v", f))) |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
if tt.outFn != nil { |
| 665 |
if !assert.Equal(t, tt.outFn(d), results) { |
| 666 |
t.FailNow() |
| 667 |
} |
| 668 |
} else { |
| 669 |
if !assert.Equal(t, tt.out, results) { |
| 670 |
t.FailNow() |
| 671 |
} |
| 672 |
} |
| 673 |
}) |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
func TestRunFns_sortFns(t *testing.T) { |
| 678 |
testCases := []struct { |
| 679 |
name string |
| 680 |
nodes []*yaml.RNode |
| 681 |
expectedImages []string |
| 682 |
expectedErrMsg string |
| 683 |
}{ |
| 684 |
{ |
| 685 |
name: "multiple functions in the same file are ordered by index", |
| 686 |
nodes: []*yaml.RNode{ |
| 687 |
yaml.MustParse(` |
| 688 |
metadata: |
| 689 |
annotations: |
| 690 |
config.kubernetes.io/path: functions.yaml |
| 691 |
config.kubernetes.io/index: 1 |
| 692 |
config.kubernetes.io/function: | |
| 693 |
container: |
| 694 |
image: a |
| 695 |
`), |
| 696 |
yaml.MustParse(` |
| 697 |
metadata: |
| 698 |
annotations: |
| 699 |
config.kubernetes.io/path: functions.yaml |
| 700 |
config.kubernetes.io/index: 0 |
| 701 |
config.kubernetes.io/function: | |
| 702 |
container: |
| 703 |
image: b |
| 704 |
`), |
| 705 |
}, |
| 706 |
expectedImages: []string{"b", "a"}, |
| 707 |
}, |
| 708 |
{ |
| 709 |
name: "non-integer value in index annotation is an error", |
| 710 |
nodes: []*yaml.RNode{ |
| 711 |
yaml.MustParse(` |
| 712 |
metadata: |
| 713 |
annotations: |
| 714 |
config.kubernetes.io/path: functions.yaml |
| 715 |
config.kubernetes.io/index: 0 |
| 716 |
config.kubernetes.io/function: | |
| 717 |
container: |
| 718 |
image: a |
| 719 |
`), |
| 720 |
yaml.MustParse(` |
| 721 |
metadata: |
| 722 |
annotations: |
| 723 |
config.kubernetes.io/path: functions.yaml |
| 724 |
config.kubernetes.io/index: abc |
| 725 |
config.kubernetes.io/function: | |
| 726 |
container: |
| 727 |
image: b |
| 728 |
`), |
| 729 |
}, |
| 730 |
expectedErrMsg: "strconv.Atoi: parsing \"abc\": invalid syntax", |
| 731 |
}, |
| 732 |
} |
| 733 |
|
| 734 |
for i := range testCases { |
| 735 |
test := testCases[i] |
| 736 |
t.Run(test.name, func(t *testing.T) { |
| 737 |
packageBuff := &kio.PackageBuffer{ |
| 738 |
Nodes: test.nodes, |
| 739 |
} |
| 740 |
|
| 741 |
err := sortFns(packageBuff) |
| 742 |
if test.expectedErrMsg != "" { |
| 743 |
if !assert.Error(t, err) { |
| 744 |
t.FailNow() |
| 745 |
} |
| 746 |
assert.Equal(t, test.expectedErrMsg, err.Error()) |
| 747 |
return |
| 748 |
} |
| 749 |
|
| 750 |
if !assert.NoError(t, err) { |
| 751 |
t.FailNow() |
| 752 |
} |
| 753 |
|
| 754 |
var images []string |
| 755 |
for _, n := range packageBuff.Nodes { |
| 756 |
spec, err := runtimeutil.GetFunctionSpec(n) |
| 757 |
if !assert.NoError(t, err) { |
| 758 |
t.FailNow() |
| 759 |
} |
| 760 |
images = append(images, spec.Container.Image) |
| 761 |
} |
| 762 |
|
| 763 |
assert.Equal(t, test.expectedImages, images) |
| 764 |
}) |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
func TestRunFns_network(t *testing.T) { |
| 769 |
tests := []struct { |
| 770 |
name string |
| 771 |
input string |
| 772 |
network bool |
| 773 |
expectNetwork bool |
| 774 |
error string |
| 775 |
}{ |
| 776 |
{ |
| 777 |
name: "imperative false, declarative false", |
| 778 |
input: ` |
| 779 |
metadata: |
| 780 |
annotations: |
| 781 |
config.kubernetes.io/function: | |
| 782 |
container: |
| 783 |
image: a |
| 784 |
network: false |
| 785 |
`, |
| 786 |
network: false, |
| 787 |
expectNetwork: false, |
| 788 |
}, |
| 789 |
{ |
| 790 |
name: "imperative true, declarative false", |
| 791 |
input: ` |
| 792 |
metadata: |
| 793 |
annotations: |
| 794 |
config.kubernetes.io/function: | |
| 795 |
container: |
| 796 |
image: a |
| 797 |
network: false |
| 798 |
`, |
| 799 |
network: true, |
| 800 |
expectNetwork: false, |
| 801 |
}, |
| 802 |
{ |
| 803 |
name: "imperative true, declarative true", |
| 804 |
input: ` |
| 805 |
metadata: |
| 806 |
annotations: |
| 807 |
config.kubernetes.io/function: | |
| 808 |
container: |
| 809 |
image: a |
| 810 |
network: true |
| 811 |
`, |
| 812 |
network: true, |
| 813 |
expectNetwork: true, |
| 814 |
}, |
| 815 |
{ |
| 816 |
name: "imperative false, declarative true", |
| 817 |
input: ` |
| 818 |
metadata: |
| 819 |
annotations: |
| 820 |
config.kubernetes.io/function: | |
| 821 |
container: |
| 822 |
image: a |
| 823 |
network: true |
| 824 |
`, |
| 825 |
network: false, |
| 826 |
error: "network required but not enabled with --network", |
| 827 |
}, |
| 828 |
} |
| 829 |
|
| 830 |
for i := range tests { |
| 831 |
tt := tests[i] |
| 832 |
fn := yaml.MustParse(tt.input) |
| 833 |
t.Run(tt.name, func(t *testing.T) { |
| 834 |
|
| 835 |
r := &RunFns{ |
| 836 |
Functions: []*yaml.RNode{fn}, |
| 837 |
Network: tt.network, |
| 838 |
} |
| 839 |
r.init() |
| 840 |
|
| 841 |
_, fltrs, _, err := r.getNodesAndFilters() |
| 842 |
if tt.error != "" { |
| 843 |
if !assert.EqualError(t, err, tt.error) { |
| 844 |
t.FailNow() |
| 845 |
} |
| 846 |
return |
| 847 |
} |
| 848 |
if !assert.NoError(t, err) { |
| 849 |
t.FailNow() |
| 850 |
} |
| 851 |
|
| 852 |
fltr := fltrs[0].(*container.Filter) |
| 853 |
if !assert.Equal(t, tt.expectNetwork, fltr.Network) { |
| 854 |
t.FailNow() |
| 855 |
} |
| 856 |
}) |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
func TestCmd_Execute(t *testing.T) { |
| 861 |
dir := setupTest(t) |
| 862 |
|
| 863 |
|
| 864 |
if !assert.NoError(t, os.WriteFile( |
| 865 |
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) { |
| 866 |
return |
| 867 |
} |
| 868 |
|
| 869 |
instance := RunFns{Path: dir, functionFilterProvider: getFilterProvider(t)} |
| 870 |
if !assert.NoError(t, instance.Execute()) { |
| 871 |
t.FailNow() |
| 872 |
} |
| 873 |
b, err := os.ReadFile( |
| 874 |
filepath.Join(dir, "java", "java-deployment.resource.yaml")) |
| 875 |
if !assert.NoError(t, err) { |
| 876 |
t.FailNow() |
| 877 |
} |
| 878 |
assert.Contains(t, string(b), "kind: StatefulSet") |
| 879 |
} |
| 880 |
|
| 881 |
type TestFilter struct { |
| 882 |
invoked bool |
| 883 |
Exit error |
| 884 |
} |
| 885 |
|
| 886 |
func (f *TestFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) { |
| 887 |
f.invoked = true |
| 888 |
return input, nil |
| 889 |
} |
| 890 |
|
| 891 |
func (f *TestFilter) GetExit() error { |
| 892 |
return f.Exit |
| 893 |
} |
| 894 |
|
| 895 |
func TestCmd_Execute_deferFailure(t *testing.T) { |
| 896 |
dir := setupTest(t) |
| 897 |
|
| 898 |
|
| 899 |
if !assert.NoError(t, os.WriteFile( |
| 900 |
filepath.Join(dir, "filter1.yaml"), []byte(`apiVersion: v1 |
| 901 |
kind: ValueReplacer |
| 902 |
metadata: |
| 903 |
annotations: |
| 904 |
config.kubernetes.io/function: | |
| 905 |
container: |
| 906 |
image: 1 |
| 907 |
config.kubernetes.io/local-config: "true" |
| 908 |
stringMatch: Deployment |
| 909 |
replace: StatefulSet |
| 910 |
`), 0600)) { |
| 911 |
t.FailNow() |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
if !assert.NoError(t, os.WriteFile( |
| 916 |
filepath.Join(dir, "filter2.yaml"), []byte(`apiVersion: v1 |
| 917 |
kind: ValueReplacer |
| 918 |
metadata: |
| 919 |
annotations: |
| 920 |
config.kubernetes.io/function: | |
| 921 |
container: |
| 922 |
image: 2 |
| 923 |
config.kubernetes.io/local-config: "true" |
| 924 |
stringMatch: Deployment |
| 925 |
replace: StatefulSet |
| 926 |
`), 0600)) { |
| 927 |
t.FailNow() |
| 928 |
} |
| 929 |
|
| 930 |
var fltrs []*TestFilter |
| 931 |
instance := RunFns{ |
| 932 |
Path: dir, |
| 933 |
functionFilterProvider: func(f runtimeutil.FunctionSpec, node *yaml.RNode, currentUser currentUserFunc) (kio.Filter, error) { |
| 934 |
tf := &TestFilter{ |
| 935 |
Exit: errors.Errorf("message: %s", f.Container.Image), |
| 936 |
} |
| 937 |
fltrs = append(fltrs, tf) |
| 938 |
return tf, nil |
| 939 |
}, |
| 940 |
} |
| 941 |
instance.init() |
| 942 |
|
| 943 |
err := instance.Execute() |
| 944 |
|
| 945 |
|
| 946 |
if !assert.Equal(t, 2, len(fltrs)) { |
| 947 |
t.FailNow() |
| 948 |
} |
| 949 |
for i := range fltrs { |
| 950 |
if !assert.True(t, fltrs[i].invoked) { |
| 951 |
t.FailNow() |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
if !assert.EqualError(t, err, "message: 1\n---\nmessage: 2") { |
| 956 |
t.FailNow() |
| 957 |
} |
| 958 |
b, err := os.ReadFile( |
| 959 |
filepath.Join(dir, "java", "java-deployment.resource.yaml")) |
| 960 |
if !assert.NoError(t, err) { |
| 961 |
t.FailNow() |
| 962 |
} |
| 963 |
|
| 964 |
assert.Contains(t, string(b), "kind: Deployment") |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
func TestCmd_Execute_setFunctionPaths(t *testing.T) { |
| 969 |
dir := setupTest(t) |
| 970 |
|
| 971 |
|
| 972 |
tmpF, err := os.CreateTemp("", "filter*.yaml") |
| 973 |
if !assert.NoError(t, err) { |
| 974 |
return |
| 975 |
} |
| 976 |
os.RemoveAll(tmpF.Name()) |
| 977 |
if !assert.NoError(t, os.WriteFile(tmpF.Name(), []byte(ValueReplacerYAMLData), 0600)) { |
| 978 |
return |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
instance := RunFns{ |
| 983 |
FunctionPaths: []string{tmpF.Name()}, |
| 984 |
Path: dir, |
| 985 |
functionFilterProvider: getFilterProvider(t), |
| 986 |
} |
| 987 |
|
| 988 |
instance.init() |
| 989 |
|
| 990 |
err = instance.Execute() |
| 991 |
if !assert.NoError(t, err) { |
| 992 |
return |
| 993 |
} |
| 994 |
b, err := os.ReadFile( |
| 995 |
filepath.Join(dir, "java", "java-deployment.resource.yaml")) |
| 996 |
if !assert.NoError(t, err) { |
| 997 |
return |
| 998 |
} |
| 999 |
assert.Contains(t, string(b), "kind: StatefulSet") |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
func TestCmd_Execute_setOutput(t *testing.T) { |
| 1004 |
dir := setupTest(t) |
| 1005 |
|
| 1006 |
|
| 1007 |
if !assert.NoError(t, os.WriteFile( |
| 1008 |
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) { |
| 1009 |
return |
| 1010 |
} |
| 1011 |
|
| 1012 |
out := &bytes.Buffer{} |
| 1013 |
instance := RunFns{ |
| 1014 |
Output: out, |
| 1015 |
Path: dir, |
| 1016 |
functionFilterProvider: getFilterProvider(t), |
| 1017 |
} |
| 1018 |
|
| 1019 |
instance.init() |
| 1020 |
|
| 1021 |
if !assert.NoError(t, instance.Execute()) { |
| 1022 |
return |
| 1023 |
} |
| 1024 |
b, err := os.ReadFile( |
| 1025 |
filepath.Join(dir, "java", "java-deployment.resource.yaml")) |
| 1026 |
if !assert.NoError(t, err) { |
| 1027 |
return |
| 1028 |
} |
| 1029 |
assert.NotContains(t, string(b), "kind: StatefulSet") |
| 1030 |
assert.Contains(t, out.String(), "kind: StatefulSet") |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
func TestCmd_Execute_setInput(t *testing.T) { |
| 1035 |
dir := setupTest(t) |
| 1036 |
if !assert.NoError(t, os.WriteFile( |
| 1037 |
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) { |
| 1038 |
return |
| 1039 |
} |
| 1040 |
|
| 1041 |
read, err := kio.LocalPackageReader{PackagePath: dir}.Read() |
| 1042 |
if !assert.NoError(t, err) { |
| 1043 |
t.FailNow() |
| 1044 |
} |
| 1045 |
input := &bytes.Buffer{} |
| 1046 |
if !assert.NoError(t, kio.ByteWriter{Writer: input}.Write(read)) { |
| 1047 |
t.FailNow() |
| 1048 |
} |
| 1049 |
|
| 1050 |
outDir := t.TempDir() |
| 1051 |
|
| 1052 |
if !assert.NoError(t, os.WriteFile( |
| 1053 |
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) { |
| 1054 |
return |
| 1055 |
} |
| 1056 |
|
| 1057 |
instance := RunFns{ |
| 1058 |
Input: input, |
| 1059 |
Path: outDir, |
| 1060 |
functionFilterProvider: getFilterProvider(t), |
| 1061 |
} |
| 1062 |
|
| 1063 |
instance.init() |
| 1064 |
|
| 1065 |
if !assert.NoError(t, instance.Execute()) { |
| 1066 |
return |
| 1067 |
} |
| 1068 |
b, err := os.ReadFile( |
| 1069 |
filepath.Join(outDir, "java", "java-deployment.resource.yaml")) |
| 1070 |
if !assert.NoError(t, err) { |
| 1071 |
t.FailNow() |
| 1072 |
} |
| 1073 |
assert.Contains(t, string(b), "kind: StatefulSet") |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
func TestCmd_Execute_enableLogSteps(t *testing.T) { |
| 1078 |
dir := setupTest(t) |
| 1079 |
|
| 1080 |
|
| 1081 |
if !assert.NoError(t, os.WriteFile( |
| 1082 |
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) { |
| 1083 |
return |
| 1084 |
} |
| 1085 |
|
| 1086 |
logs := &bytes.Buffer{} |
| 1087 |
instance := RunFns{ |
| 1088 |
Path: dir, |
| 1089 |
functionFilterProvider: getFilterProvider(t), |
| 1090 |
LogSteps: true, |
| 1091 |
LogWriter: logs, |
| 1092 |
} |
| 1093 |
if !assert.NoError(t, instance.Execute()) { |
| 1094 |
t.FailNow() |
| 1095 |
} |
| 1096 |
b, err := os.ReadFile( |
| 1097 |
filepath.Join(dir, "java", "java-deployment.resource.yaml")) |
| 1098 |
if !assert.NoError(t, err) { |
| 1099 |
t.FailNow() |
| 1100 |
} |
| 1101 |
assert.Contains(t, string(b), "kind: StatefulSet") |
| 1102 |
assert.Equal(t, "Running unknown-type function\n", logs.String()) |
| 1103 |
} |
| 1104 |
|
| 1105 |
func getGeneratorFilterProvider(t *testing.T) func(runtimeutil.FunctionSpec, *yaml.RNode, currentUserFunc) (kio.Filter, error) { |
| 1106 |
t.Helper() |
| 1107 |
return func(f runtimeutil.FunctionSpec, node *yaml.RNode, currentUser currentUserFunc) (kio.Filter, error) { |
| 1108 |
return kio.FilterFunc(func(items []*yaml.RNode) ([]*yaml.RNode, error) { |
| 1109 |
if f.Container.Image == "generate" { |
| 1110 |
node, err := yaml.Parse("kind: generated") |
| 1111 |
if !assert.NoError(t, err) { |
| 1112 |
t.FailNow() |
| 1113 |
} |
| 1114 |
return append(items, node), nil |
| 1115 |
} |
| 1116 |
return items, nil |
| 1117 |
}), nil |
| 1118 |
} |
| 1119 |
} |
| 1120 |
func TestRunFns_ContinueOnEmptyResult(t *testing.T) { |
| 1121 |
fn1, err := yaml.Parse(` |
| 1122 |
kind: fakefn |
| 1123 |
metadata: |
| 1124 |
annotations: |
| 1125 |
config.kubernetes.io/function: | |
| 1126 |
container: |
| 1127 |
image: pass |
| 1128 |
`) |
| 1129 |
if !assert.NoError(t, err) { |
| 1130 |
t.FailNow() |
| 1131 |
} |
| 1132 |
fn2, err := yaml.Parse(` |
| 1133 |
kind: fakefn |
| 1134 |
metadata: |
| 1135 |
annotations: |
| 1136 |
config.kubernetes.io/function: | |
| 1137 |
container: |
| 1138 |
image: generate |
| 1139 |
`) |
| 1140 |
if !assert.NoError(t, err) { |
| 1141 |
t.FailNow() |
| 1142 |
} |
| 1143 |
|
| 1144 |
var test = []struct { |
| 1145 |
ContinueOnEmptyResult bool |
| 1146 |
ExpectedOutput string |
| 1147 |
}{ |
| 1148 |
{ |
| 1149 |
ContinueOnEmptyResult: false, |
| 1150 |
ExpectedOutput: "", |
| 1151 |
}, |
| 1152 |
{ |
| 1153 |
ContinueOnEmptyResult: true, |
| 1154 |
ExpectedOutput: "kind: generated\n", |
| 1155 |
}, |
| 1156 |
} |
| 1157 |
for i := range test { |
| 1158 |
ouputBuffer := bytes.Buffer{} |
| 1159 |
instance := RunFns{ |
| 1160 |
Input: bytes.NewReader([]byte{}), |
| 1161 |
Output: &ouputBuffer, |
| 1162 |
Functions: []*yaml.RNode{fn1, fn2}, |
| 1163 |
functionFilterProvider: getGeneratorFilterProvider(t), |
| 1164 |
ContinueOnEmptyResult: test[i].ContinueOnEmptyResult, |
| 1165 |
} |
| 1166 |
if !assert.NoError(t, instance.Execute()) { |
| 1167 |
t.FailNow() |
| 1168 |
} |
| 1169 |
assert.Equal(t, test[i].ExpectedOutput, ouputBuffer.String()) |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
func setupTest(t *testing.T) string { |
| 1175 |
t.Helper() |
| 1176 |
dir := t.TempDir() |
| 1177 |
|
| 1178 |
_, filename, _, ok := runtime.Caller(0) |
| 1179 |
if !assert.True(t, ok) { |
| 1180 |
t.FailNow() |
| 1181 |
} |
| 1182 |
ds, err := filepath.Abs(filepath.Join(filepath.Dir(filename), "test", "testdata")) |
| 1183 |
if !assert.NoError(t, err) { |
| 1184 |
t.FailNow() |
| 1185 |
} |
| 1186 |
if !assert.NoError(t, copyutil.CopyDir(filesys.MakeFsOnDisk(), ds, dir)) { |
| 1187 |
t.FailNow() |
| 1188 |
} |
| 1189 |
|
| 1190 |
cwd, err := os.Getwd() |
| 1191 |
if !assert.NoError(t, err) { |
| 1192 |
t.FailNow() |
| 1193 |
} |
| 1194 |
|
| 1195 |
if !assert.NoError(t, os.Chdir(filepath.Dir(dir))) { |
| 1196 |
t.FailNow() |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
t.Cleanup(func() { |
| 1201 |
if !assert.NoError(t, os.Chdir(cwd)) { |
| 1202 |
t.FailNow() |
| 1203 |
} |
| 1204 |
}) |
| 1205 |
|
| 1206 |
return dir |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
func getFilterProvider(t *testing.T) func(runtimeutil.FunctionSpec, *yaml.RNode, currentUserFunc) (kio.Filter, error) { |
| 1213 |
t.Helper() |
| 1214 |
return func(f runtimeutil.FunctionSpec, node *yaml.RNode, currentUser currentUserFunc) (kio.Filter, error) { |
| 1215 |
|
| 1216 |
filter := yaml.YFilter{} |
| 1217 |
b := &bytes.Buffer{} |
| 1218 |
e := yaml.NewEncoder(b) |
| 1219 |
if !assert.NoError(t, e.Encode(node.YNode())) { |
| 1220 |
t.FailNow() |
| 1221 |
} |
| 1222 |
e.Close() |
| 1223 |
d := yaml.NewDecoder(b) |
| 1224 |
if !assert.NoError(t, d.Decode(&filter)) { |
| 1225 |
t.FailNow() |
| 1226 |
} |
| 1227 |
|
| 1228 |
return filters.Modifier{ |
| 1229 |
Filters: []yaml.YFilter{{Filter: yaml.Lookup("kind")}, filter}, |
| 1230 |
}, nil |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
func TestRunFns_mergeContainerEnv(t *testing.T) { |
| 1235 |
testcases := []struct { |
| 1236 |
name string |
| 1237 |
instance RunFns |
| 1238 |
inputEnvs []string |
| 1239 |
expect runtimeutil.ContainerEnv |
| 1240 |
}{ |
| 1241 |
{ |
| 1242 |
name: "all empty", |
| 1243 |
instance: RunFns{}, |
| 1244 |
expect: *runtimeutil.NewContainerEnv(), |
| 1245 |
}, |
| 1246 |
{ |
| 1247 |
name: "empty command line envs", |
| 1248 |
instance: RunFns{}, |
| 1249 |
inputEnvs: []string{"foo=bar"}, |
| 1250 |
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}), |
| 1251 |
}, |
| 1252 |
{ |
| 1253 |
name: "empty declarative envs", |
| 1254 |
instance: RunFns{ |
| 1255 |
Env: []string{"foo=bar"}, |
| 1256 |
}, |
| 1257 |
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}), |
| 1258 |
}, |
| 1259 |
{ |
| 1260 |
name: "same key", |
| 1261 |
instance: RunFns{ |
| 1262 |
Env: []string{"foo=bar", "foo"}, |
| 1263 |
}, |
| 1264 |
inputEnvs: []string{"foo=bar1", "bar"}, |
| 1265 |
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "bar", "foo"}), |
| 1266 |
}, |
| 1267 |
{ |
| 1268 |
name: "same exported key", |
| 1269 |
instance: RunFns{ |
| 1270 |
Env: []string{"foo=bar", "foo"}, |
| 1271 |
}, |
| 1272 |
inputEnvs: []string{"foo1=bar1", "foo"}, |
| 1273 |
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "foo1=bar1", "foo"}), |
| 1274 |
}, |
| 1275 |
} |
| 1276 |
|
| 1277 |
for i := range testcases { |
| 1278 |
tc := testcases[i] |
| 1279 |
t.Run(tc.name, func(t *testing.T) { |
| 1280 |
envs := tc.instance.mergeContainerEnv(tc.inputEnvs) |
| 1281 |
assert.Equal(t, tc.expect.GetDockerFlags(), runtimeutil.NewContainerEnvFromStringSlice(envs).GetDockerFlags()) |
| 1282 |
}) |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
func TestRunFns_mergeExecEnv(t *testing.T) { |
| 1287 |
testcases := []struct { |
| 1288 |
name string |
| 1289 |
instance RunFns |
| 1290 |
inputEnvs []string |
| 1291 |
expect []string |
| 1292 |
}{ |
| 1293 |
{ |
| 1294 |
name: "all empty", |
| 1295 |
instance: RunFns{}, |
| 1296 |
expect: []string{}, |
| 1297 |
}, |
| 1298 |
{ |
| 1299 |
name: "empty command line envs", |
| 1300 |
instance: RunFns{}, |
| 1301 |
inputEnvs: []string{"foo=bar"}, |
| 1302 |
expect: []string{"foo=bar"}, |
| 1303 |
}, |
| 1304 |
{ |
| 1305 |
name: "empty declarative envs", |
| 1306 |
instance: RunFns{ |
| 1307 |
Env: []string{"foo=bar"}, |
| 1308 |
}, |
| 1309 |
expect: []string{"foo=bar"}, |
| 1310 |
}, |
| 1311 |
{ |
| 1312 |
name: "same key", |
| 1313 |
instance: RunFns{ |
| 1314 |
Env: []string{"foo=bar"}, |
| 1315 |
}, |
| 1316 |
inputEnvs: []string{"foo=bar1"}, |
| 1317 |
expect: []string{"foo=bar"}, |
| 1318 |
}, |
| 1319 |
{ |
| 1320 |
name: "same exported key", |
| 1321 |
instance: RunFns{ |
| 1322 |
Env: []string{"foo=bar"}, |
| 1323 |
}, |
| 1324 |
inputEnvs: []string{"foo1=bar1"}, |
| 1325 |
expect: []string{"foo1=bar1", "foo=bar"}, |
| 1326 |
}, |
| 1327 |
} |
| 1328 |
|
| 1329 |
for i := range testcases { |
| 1330 |
tc := testcases[i] |
| 1331 |
t.Run(tc.name, func(t *testing.T) { |
| 1332 |
envs := tc.instance.mergeExecEnv(tc.inputEnvs) |
| 1333 |
assert.Equal(t, tc.expect, envs) |
| 1334 |
}) |
| 1335 |
} |
| 1336 |
} |