@@ -2438,6 +2438,266 @@ image:
24382438 require .NoError (t , err )
24392439 assert .Equal (t , strings .TrimSpace (expected ), strings .TrimSpace (string (output )))
24402440 })
2441+
2442+ t .Run ("yaml list is correctly parsed" , func (t * testing.T ) {
2443+ expected := `
2444+ images:
2445+ - name: image-1
2446+ attributes:
2447+ name: repo-name
2448+ tag: 2.0.0
2449+ `
2450+
2451+ inputData := []byte (`
2452+ images:
2453+ - name: image-1
2454+ attributes:
2455+ name: repo-name
2456+ tag: 1.0.0
2457+ ` )
2458+ input := yaml.Node {}
2459+ err := yaml .Unmarshal (inputData , & input )
2460+ require .NoError (t , err )
2461+
2462+ key := "images[0].attributes.tag"
2463+ value := "2.0.0"
2464+
2465+ err = setHelmValue (& input , key , value )
2466+ require .NoError (t , err )
2467+
2468+ output , err := marshalWithIndent (& input , defaultIndent )
2469+ require .NoError (t , err )
2470+ assert .Equal (t , strings .TrimSpace (expected ), strings .TrimSpace (string (output )))
2471+ })
2472+
2473+ t .Run ("yaml list is correctly parsed when multiple values" , func (t * testing.T ) {
2474+ expected := `
2475+ images:
2476+ - name: image-1
2477+ attributes:
2478+ name: repo-name
2479+ tag: 1.0.0
2480+ - name: image-2
2481+ attributes:
2482+ name: repo-name
2483+ tag: 2.0.0
2484+ `
2485+
2486+ inputData := []byte (`
2487+ images:
2488+ - name: image-1
2489+ attributes:
2490+ name: repo-name
2491+ tag: 1.0.0
2492+ - name: image-2
2493+ attributes:
2494+ name: repo-name
2495+ tag: 1.0.0
2496+ ` )
2497+ input := yaml.Node {}
2498+ err := yaml .Unmarshal (inputData , & input )
2499+ require .NoError (t , err )
2500+
2501+ key := "images[1].attributes.tag"
2502+ value := "2.0.0"
2503+
2504+ err = setHelmValue (& input , key , value )
2505+ require .NoError (t , err )
2506+
2507+ output , err := marshalWithIndent (& input , defaultIndent )
2508+ require .NoError (t , err )
2509+ assert .Equal (t , strings .TrimSpace (expected ), strings .TrimSpace (string (output )))
2510+ })
2511+
2512+ t .Run ("yaml list is correctly parsed when inside map" , func (t * testing.T ) {
2513+ expected := `
2514+ extraContainers:
2515+ images:
2516+ - name: image-1
2517+ attributes:
2518+ name: repo-name
2519+ tag: 2.0.0
2520+ `
2521+
2522+ inputData := []byte (`
2523+ extraContainers:
2524+ images:
2525+ - name: image-1
2526+ attributes:
2527+ name: repo-name
2528+ tag: 1.0.0
2529+ ` )
2530+ input := yaml.Node {}
2531+ err := yaml .Unmarshal (inputData , & input )
2532+ require .NoError (t , err )
2533+
2534+ key := "extraContainers.images[0].attributes.tag"
2535+ value := "2.0.0"
2536+
2537+ err = setHelmValue (& input , key , value )
2538+ require .NoError (t , err )
2539+
2540+ output , err := marshalWithIndent (& input , defaultIndent )
2541+ require .NoError (t , err )
2542+ assert .Equal (t , strings .TrimSpace (expected ), strings .TrimSpace (string (output )))
2543+ })
2544+
2545+ t .Run ("yaml list is correctly parsed when list name contains digits" , func (t * testing.T ) {
2546+ expected := `
2547+ extraContainers:
2548+ images123:
2549+ - name: image-1
2550+ attributes:
2551+ name: repo-name
2552+ tag: 2.0.0
2553+ `
2554+
2555+ inputData := []byte (`
2556+ extraContainers:
2557+ images123:
2558+ - name: image-1
2559+ attributes:
2560+ name: repo-name
2561+ tag: 1.0.0
2562+ ` )
2563+ input := yaml.Node {}
2564+ err := yaml .Unmarshal (inputData , & input )
2565+ require .NoError (t , err )
2566+
2567+ key := "extraContainers.images123[0].attributes.tag"
2568+ value := "2.0.0"
2569+
2570+ err = setHelmValue (& input , key , value )
2571+ require .NoError (t , err )
2572+
2573+ output , err := marshalWithIndent (& input , defaultIndent )
2574+ require .NoError (t , err )
2575+ assert .Equal (t , strings .TrimSpace (expected ), strings .TrimSpace (string (output )))
2576+ })
2577+
2578+ t .Run ("id for yaml list is lower than 0" , func (t * testing.T ) {
2579+ inputData := []byte (`
2580+ images:
2581+ - name: image-1
2582+ attributes:
2583+ name: repo-name
2584+ tag: 1.0.0
2585+ ` )
2586+ input := yaml.Node {}
2587+ err := yaml .Unmarshal (inputData , & input )
2588+ require .NoError (t , err )
2589+
2590+ key := "images[-1].attributes.tag"
2591+ value := "2.0.0"
2592+
2593+ err = setHelmValue (& input , key , value )
2594+
2595+ require .Error (t , err )
2596+ assert .Equal (t , "id -1 is out of range [0, 1)" , err .Error ())
2597+ })
2598+
2599+ t .Run ("id for yaml list is greater than length of list" , func (t * testing.T ) {
2600+ inputData := []byte (`
2601+ images:
2602+ - name: image-1
2603+ attributes:
2604+ name: repo-name
2605+ tag: 1.0.0
2606+ ` )
2607+ input := yaml.Node {}
2608+ err := yaml .Unmarshal (inputData , & input )
2609+ require .NoError (t , err )
2610+
2611+ key := "images[1].attributes.tag"
2612+ value := "2.0.0"
2613+
2614+ err = setHelmValue (& input , key , value )
2615+
2616+ require .Error (t , err )
2617+ assert .Equal (t , "id 1 is out of range [0, 1)" , err .Error ())
2618+ })
2619+
2620+ t .Run ("id for YAML list is not a valid integer" , func (t * testing.T ) {
2621+ inputData := []byte (`
2622+ images:
2623+ - name: image-1
2624+ attributes:
2625+ name: repo-name
2626+ tag: 1.0.0
2627+ ` )
2628+ input := yaml.Node {}
2629+ err := yaml .Unmarshal (inputData , & input )
2630+ require .NoError (t , err )
2631+
2632+ key := "images[invalid].attributes.tag"
2633+ value := "2.0.0"
2634+
2635+ err = setHelmValue (& input , key , value )
2636+
2637+ require .Error (t , err )
2638+ assert .Equal (t , "id \" invalid\" in yaml array must match pattern ^(.*)\\ [(.*)\\ ]$" , err .Error ())
2639+ })
2640+
2641+ t .Run ("no id for yaml list given" , func (t * testing.T ) {
2642+ inputData := []byte (`
2643+ images:
2644+ - name: image-1
2645+ attributes:
2646+ name: repo-name
2647+ tag: 1.0.0
2648+ ` )
2649+ input := yaml.Node {}
2650+ err := yaml .Unmarshal (inputData , & input )
2651+ require .NoError (t , err )
2652+
2653+ key := "images.attributes.tag"
2654+ value := "2.0.0"
2655+
2656+ err = setHelmValue (& input , key , value )
2657+
2658+ require .Error (t , err )
2659+ assert .Equal (t , "no id provided for yaml array \" images\" " , err .Error ())
2660+ })
2661+
2662+ t .Run ("id given when node is not an yaml list" , func (t * testing.T ) {
2663+ inputData := []byte (`
2664+ image:
2665+ attributes:
2666+ name: repo-name
2667+ tag: 1.0.0
2668+ ` )
2669+ input := yaml.Node {}
2670+ err := yaml .Unmarshal (inputData , & input )
2671+ require .NoError (t , err )
2672+
2673+ key := "image[0].attributes.tag"
2674+ value := "2.0.0"
2675+
2676+ err = setHelmValue (& input , key , value )
2677+
2678+ require .Error (t , err )
2679+ assert .Equal (t , "id 0 provided when \" image\" is not an yaml array" , err .Error ())
2680+ })
2681+
2682+ t .Run ("invalid id given when node is not an yaml list" , func (t * testing.T ) {
2683+ inputData := []byte (`
2684+ image:
2685+ attributes:
2686+ name: repo-name
2687+ tag: 1.0.0
2688+ ` )
2689+ input := yaml.Node {}
2690+ err := yaml .Unmarshal (inputData , & input )
2691+ require .NoError (t , err )
2692+
2693+ key := "image[invalid].attributes.tag"
2694+ value := "2.0.0"
2695+
2696+ err = setHelmValue (& input , key , value )
2697+
2698+ require .Error (t , err )
2699+ assert .Equal (t , "id \" invalid\" in yaml array must match pattern ^(.*)\\ [(.*)\\ ]$" , err .Error ())
2700+ })
24412701}
24422702
24432703func Test_GetWriteBackConfig (t * testing.T ) {
0 commit comments