github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/chartutil/save_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  	"io/ioutil"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"helm.sh/helm/pkg/chart"
    26  	"helm.sh/helm/pkg/chart/loader"
    27  )
    28  
    29  func TestSave(t *testing.T) {
    30  	tmp, err := ioutil.TempDir("", "helm-")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer os.RemoveAll(tmp)
    35  
    36  	c := &chart.Chart{
    37  		Metadata: &chart.Metadata{
    38  			APIVersion: chart.APIVersionV1,
    39  			Name:       "ahab",
    40  			Version:    "1.2.3",
    41  		},
    42  		Files: []*chart.File{
    43  			{Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")},
    44  		},
    45  	}
    46  
    47  	where, err := Save(c, tmp)
    48  	if err != nil {
    49  		t.Fatalf("Failed to save: %s", err)
    50  	}
    51  	if !strings.HasPrefix(where, tmp) {
    52  		t.Fatalf("Expected %q to start with %q", where, tmp)
    53  	}
    54  	if !strings.HasSuffix(where, ".tgz") {
    55  		t.Fatalf("Expected %q to end with .tgz", where)
    56  	}
    57  
    58  	c2, err := loader.LoadFile(where)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if c2.Name() != c.Name() {
    63  		t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name())
    64  	}
    65  	if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" {
    66  		t.Fatal("Files data did not match")
    67  	}
    68  }
    69  
    70  func TestSaveDir(t *testing.T) {
    71  	tmp, err := ioutil.TempDir("", "helm-")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	defer os.RemoveAll(tmp)
    76  
    77  	c := &chart.Chart{
    78  		Metadata: &chart.Metadata{
    79  			APIVersion: chart.APIVersionV1,
    80  			Name:       "ahab",
    81  			Version:    "1.2.3",
    82  		},
    83  		Files: []*chart.File{
    84  			{Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")},
    85  		},
    86  		Templates: []*chart.File{
    87  			{Name: "templates/nested/dir/thing.yaml", Data: []byte("abc: {{ .Values.abc }}")},
    88  		},
    89  	}
    90  
    91  	if err := SaveDir(c, tmp); err != nil {
    92  		t.Fatalf("Failed to save: %s", err)
    93  	}
    94  
    95  	c2, err := loader.LoadDir(tmp + "/ahab")
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	if c2.Name() != c.Name() {
   101  		t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name())
   102  	}
   103  
   104  	if len(c2.Templates) != 1 || c2.Templates[0].Name != "templates/nested/dir/thing.yaml" {
   105  		t.Fatal("Templates data did not match")
   106  	}
   107  
   108  	if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" {
   109  		t.Fatal("Files data did not match")
   110  	}
   111  }