github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/chartutil/coalesce_test.go (about)

     1  /*
     2  Copyright The Helm 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 chartutil
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  )
    23  
    24  // ref: http://www.yaml.org/spec/1.2/spec.html#id2803362
    25  var testCoalesceValuesYaml = []byte(`
    26  top: yup
    27  bottom: null
    28  right: Null
    29  left: NULL
    30  front: ~
    31  back: ""
    32  
    33  global:
    34    name: Ishmael
    35    subject: Queequeg
    36    nested:
    37      boat: true
    38  
    39  pequod:
    40    global:
    41      name: Stinky
    42      harpooner: Tashtego
    43      nested:
    44        boat: false
    45        sail: true
    46    ahab:
    47      scope: whale
    48  `)
    49  
    50  func TestCoalesceValues(t *testing.T) {
    51  	c := loadChart(t, "testdata/moby")
    52  
    53  	vals, err := ReadValues(testCoalesceValuesYaml)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	v, err := CoalesceValues(c, vals)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	j, _ := json.MarshalIndent(v, "", "  ")
    63  	t.Logf("Coalesced Values: %s", string(j))
    64  
    65  	tests := []struct {
    66  		tpl    string
    67  		expect string
    68  	}{
    69  		{"{{.top}}", "yup"},
    70  		{"{{.back}}", ""},
    71  		{"{{.name}}", "moby"},
    72  		{"{{.global.name}}", "Ishmael"},
    73  		{"{{.global.subject}}", "Queequeg"},
    74  		{"{{.global.harpooner}}", "<no value>"},
    75  		{"{{.pequod.name}}", "pequod"},
    76  		{"{{.pequod.ahab.name}}", "ahab"},
    77  		{"{{.pequod.ahab.scope}}", "whale"},
    78  		{"{{.pequod.ahab.global.name}}", "Ishmael"},
    79  		{"{{.pequod.ahab.global.subject}}", "Queequeg"},
    80  		{"{{.pequod.ahab.global.harpooner}}", "Tashtego"},
    81  		{"{{.pequod.global.name}}", "Ishmael"},
    82  		{"{{.pequod.global.subject}}", "Queequeg"},
    83  		{"{{.spouter.global.name}}", "Ishmael"},
    84  		{"{{.spouter.global.harpooner}}", "<no value>"},
    85  
    86  		{"{{.global.nested.boat}}", "true"},
    87  		{"{{.pequod.global.nested.boat}}", "true"},
    88  		{"{{.spouter.global.nested.boat}}", "true"},
    89  		{"{{.pequod.global.nested.sail}}", "true"},
    90  		{"{{.spouter.global.nested.sail}}", "<no value>"},
    91  	}
    92  
    93  	for _, tt := range tests {
    94  		if o, err := ttpl(tt.tpl, v); err != nil || o != tt.expect {
    95  			t.Errorf("Expected %q to expand to %q, got %q", tt.tpl, tt.expect, o)
    96  		}
    97  	}
    98  
    99  	nullKeys := []string{"bottom", "right", "left", "front"}
   100  	for _, nullKey := range nullKeys {
   101  		if _, ok := v[nullKey]; ok {
   102  			t.Errorf("Expected key %q to be removed, still present", nullKey)
   103  		}
   104  	}
   105  }
   106  
   107  func TestCoalesceTables(t *testing.T) {
   108  	dst := map[string]interface{}{
   109  		"name": "Ishmael",
   110  		"address": map[string]interface{}{
   111  			"street": "123 Spouter Inn Ct.",
   112  			"city":   "Nantucket",
   113  		},
   114  		"details": map[string]interface{}{
   115  			"friends": []string{"Tashtego"},
   116  		},
   117  		"boat": "pequod",
   118  	}
   119  	src := map[string]interface{}{
   120  		"occupation": "whaler",
   121  		"address": map[string]interface{}{
   122  			"state":  "MA",
   123  			"street": "234 Spouter Inn Ct.",
   124  		},
   125  		"details": "empty",
   126  		"boat": map[string]interface{}{
   127  			"mast": true,
   128  		},
   129  	}
   130  
   131  	// What we expect is that anything in dst overrides anything in src, but that
   132  	// otherwise the values are coalesced.
   133  	CoalesceTables(dst, src)
   134  
   135  	if dst["name"] != "Ishmael" {
   136  		t.Errorf("Unexpected name: %s", dst["name"])
   137  	}
   138  	if dst["occupation"] != "whaler" {
   139  		t.Errorf("Unexpected occupation: %s", dst["occupation"])
   140  	}
   141  
   142  	addr, ok := dst["address"].(map[string]interface{})
   143  	if !ok {
   144  		t.Fatal("Address went away.")
   145  	}
   146  
   147  	if addr["street"].(string) != "123 Spouter Inn Ct." {
   148  		t.Errorf("Unexpected address: %v", addr["street"])
   149  	}
   150  
   151  	if addr["city"].(string) != "Nantucket" {
   152  		t.Errorf("Unexpected city: %v", addr["city"])
   153  	}
   154  
   155  	if addr["state"].(string) != "MA" {
   156  		t.Errorf("Unexpected state: %v", addr["state"])
   157  	}
   158  
   159  	if det, ok := dst["details"].(map[string]interface{}); !ok {
   160  		t.Fatalf("Details is the wrong type: %v", dst["details"])
   161  	} else if _, ok := det["friends"]; !ok {
   162  		t.Error("Could not find your friends. Maybe you don't have any. :-(")
   163  	}
   164  
   165  	if dst["boat"].(string) != "pequod" {
   166  		t.Errorf("Expected boat string, got %v", dst["boat"])
   167  	}
   168  }