github.com/qsis/helm@v3.0.0-beta.3+incompatible/pkg/chartutil/values_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  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  	"text/template"
    24  
    25  	"helm.sh/helm/pkg/chart"
    26  )
    27  
    28  func TestReadValues(t *testing.T) {
    29  	doc := `# Test YAML parse
    30  poet: "Coleridge"
    31  title: "Rime of the Ancient Mariner"
    32  stanza:
    33    - "at"
    34    - "length"
    35    - "did"
    36    - cross
    37    - an
    38    - Albatross
    39  
    40  mariner:
    41    with: "crossbow"
    42    shot: "ALBATROSS"
    43  
    44  water:
    45    water:
    46      where: "everywhere"
    47      nor: "any drop to drink"
    48      temperature: 1234567890
    49  `
    50  
    51  	data, err := ReadValues([]byte(doc))
    52  	if err != nil {
    53  		t.Fatalf("Error parsing bytes: %s", err)
    54  	}
    55  	matchValues(t, data)
    56  
    57  	tests := []string{`poet: "Coleridge"`, "# Just a comment", ""}
    58  
    59  	for _, tt := range tests {
    60  		data, err = ReadValues([]byte(tt))
    61  		if err != nil {
    62  			t.Fatalf("Error parsing bytes (%s): %s", tt, err)
    63  		}
    64  		if data == nil {
    65  			t.Errorf(`YAML string "%s" gave a nil map`, tt)
    66  		}
    67  	}
    68  }
    69  
    70  func TestToRenderValues(t *testing.T) {
    71  
    72  	chartValues := map[string]interface{}{
    73  		"name": "al Rashid",
    74  		"where": map[string]interface{}{
    75  			"city":  "Basrah",
    76  			"title": "caliph",
    77  		},
    78  	}
    79  
    80  	overideValues := map[string]interface{}{
    81  		"name": "Haroun",
    82  		"where": map[string]interface{}{
    83  			"city": "Baghdad",
    84  			"date": "809 CE",
    85  		},
    86  	}
    87  
    88  	c := &chart.Chart{
    89  		Metadata:  &chart.Metadata{Name: "test"},
    90  		Templates: []*chart.File{},
    91  		Values:    chartValues,
    92  		Files: []*chart.File{
    93  			{Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")},
    94  		},
    95  	}
    96  	c.AddDependency(&chart.Chart{
    97  		Metadata: &chart.Metadata{Name: "where"},
    98  	})
    99  
   100  	o := ReleaseOptions{
   101  		Name:      "Seven Voyages",
   102  		IsInstall: true,
   103  	}
   104  
   105  	res, err := ToRenderValues(c, overideValues, o, nil)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	// Ensure that the top-level values are all set.
   111  	if name := res["Chart"].(*chart.Metadata).Name; name != "test" {
   112  		t.Errorf("Expected chart name 'test', got %q", name)
   113  	}
   114  	relmap := res["Release"].(map[string]interface{})
   115  	if name := relmap["Name"]; name.(string) != "Seven Voyages" {
   116  		t.Errorf("Expected release name 'Seven Voyages', got %q", name)
   117  	}
   118  	if relmap["IsUpgrade"].(bool) {
   119  		t.Error("Expected upgrade to be false.")
   120  	}
   121  	if !relmap["IsInstall"].(bool) {
   122  		t.Errorf("Expected install to be true.")
   123  	}
   124  	if !res["Capabilities"].(*Capabilities).APIVersions.Has("v1") {
   125  		t.Error("Expected Capabilities to have v1 as an API")
   126  	}
   127  	if res["Capabilities"].(*Capabilities).KubeVersion.Major != "1" {
   128  		t.Error("Expected Capabilities to have a Kube version")
   129  	}
   130  
   131  	vals := res["Values"].(Values)
   132  	if vals["name"] != "Haroun" {
   133  		t.Errorf("Expected 'Haroun', got %q (%v)", vals["name"], vals)
   134  	}
   135  	where := vals["where"].(map[string]interface{})
   136  	expects := map[string]string{
   137  		"city":  "Baghdad",
   138  		"date":  "809 CE",
   139  		"title": "caliph",
   140  	}
   141  	for field, expect := range expects {
   142  		if got := where[field]; got != expect {
   143  			t.Errorf("Expected %q, got %q (%v)", expect, got, where)
   144  		}
   145  	}
   146  }
   147  
   148  func TestReadValuesFile(t *testing.T) {
   149  	data, err := ReadValuesFile("./testdata/coleridge.yaml")
   150  	if err != nil {
   151  		t.Fatalf("Error reading YAML file: %s", err)
   152  	}
   153  	matchValues(t, data)
   154  }
   155  
   156  func ExampleValues() {
   157  	doc := `
   158  title: "Moby Dick"
   159  chapter:
   160    one:
   161      title: "Loomings"
   162    two:
   163      title: "The Carpet-Bag"
   164    three:
   165      title: "The Spouter Inn"
   166  `
   167  	d, err := ReadValues([]byte(doc))
   168  	if err != nil {
   169  		panic(err)
   170  	}
   171  	ch1, err := d.Table("chapter.one")
   172  	if err != nil {
   173  		panic("could not find chapter one")
   174  	}
   175  	fmt.Print(ch1["title"])
   176  	// Output:
   177  	// Loomings
   178  }
   179  
   180  func TestTable(t *testing.T) {
   181  	doc := `
   182  title: "Moby Dick"
   183  chapter:
   184    one:
   185      title: "Loomings"
   186    two:
   187      title: "The Carpet-Bag"
   188    three:
   189      title: "The Spouter Inn"
   190  `
   191  	d, err := ReadValues([]byte(doc))
   192  	if err != nil {
   193  		t.Fatalf("Failed to parse the White Whale: %s", err)
   194  	}
   195  
   196  	if _, err := d.Table("title"); err == nil {
   197  		t.Fatalf("Title is not a table.")
   198  	}
   199  
   200  	if _, err := d.Table("chapter"); err != nil {
   201  		t.Fatalf("Failed to get the chapter table: %s\n%v", err, d)
   202  	}
   203  
   204  	if v, err := d.Table("chapter.one"); err != nil {
   205  		t.Errorf("Failed to get chapter.one: %s", err)
   206  	} else if v["title"] != "Loomings" {
   207  		t.Errorf("Unexpected title: %s", v["title"])
   208  	}
   209  
   210  	if _, err := d.Table("chapter.three"); err != nil {
   211  		t.Errorf("Chapter three is missing: %s\n%v", err, d)
   212  	}
   213  
   214  	if _, err := d.Table("chapter.OneHundredThirtySix"); err == nil {
   215  		t.Errorf("I think you mean 'Epilogue'")
   216  	}
   217  }
   218  
   219  func matchValues(t *testing.T, data map[string]interface{}) {
   220  	if data["poet"] != "Coleridge" {
   221  		t.Errorf("Unexpected poet: %s", data["poet"])
   222  	}
   223  
   224  	if o, err := ttpl("{{len .stanza}}", data); err != nil {
   225  		t.Errorf("len stanza: %s", err)
   226  	} else if o != "6" {
   227  		t.Errorf("Expected 6, got %s", o)
   228  	}
   229  
   230  	if o, err := ttpl("{{.mariner.shot}}", data); err != nil {
   231  		t.Errorf(".mariner.shot: %s", err)
   232  	} else if o != "ALBATROSS" {
   233  		t.Errorf("Expected that mariner shot ALBATROSS")
   234  	}
   235  
   236  	if o, err := ttpl("{{.water.water.where}}", data); err != nil {
   237  		t.Errorf(".water.water.where: %s", err)
   238  	} else if o != "everywhere" {
   239  		t.Errorf("Expected water water everywhere")
   240  	}
   241  
   242  	if o, err := ttpl("{{.water.water.temperature}}", data); err != nil {
   243  		t.Errorf(".water.water.temperature: %s", err)
   244  	} else if o != "1234567890" {
   245  		t.Errorf("Expected water water temperature: 1234567890, got: %s", o)
   246  	}
   247  }
   248  
   249  func ttpl(tpl string, v map[string]interface{}) (string, error) {
   250  	var b bytes.Buffer
   251  	tt := template.Must(template.New("t").Parse(tpl))
   252  	err := tt.Execute(&b, v)
   253  	return b.String(), err
   254  }
   255  
   256  func TestPathValue(t *testing.T) {
   257  	doc := `
   258  title: "Moby Dick"
   259  chapter:
   260    one:
   261      title: "Loomings"
   262    two:
   263      title: "The Carpet-Bag"
   264    three:
   265      title: "The Spouter Inn"
   266  `
   267  	d, err := ReadValues([]byte(doc))
   268  	if err != nil {
   269  		t.Fatalf("Failed to parse the White Whale: %s", err)
   270  	}
   271  
   272  	if v, err := d.PathValue("chapter.one.title"); err != nil {
   273  		t.Errorf("Got error instead of title: %s\n%v", err, d)
   274  	} else if v != "Loomings" {
   275  		t.Errorf("No error but got wrong value for title: %s\n%v", err, d)
   276  	}
   277  	if _, err := d.PathValue("chapter.one.doesntexist"); err == nil {
   278  		t.Errorf("Non-existent key should return error: %s\n%v", err, d)
   279  	}
   280  	if _, err := d.PathValue("chapter.doesntexist.one"); err == nil {
   281  		t.Errorf("Non-existent key in middle of path should return error: %s\n%v", err, d)
   282  	}
   283  	if _, err := d.PathValue(""); err == nil {
   284  		t.Error("Asking for the value from an empty path should yield an error")
   285  	}
   286  	if v, err := d.PathValue("title"); err == nil {
   287  		if v != "Moby Dick" {
   288  			t.Errorf("Failed to return values for root key title")
   289  		}
   290  	}
   291  }