github.com/umeshredd/helm@v3.0.0-alpha.1+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  
    63  	if c2.Name() != c.Name() {
    64  		t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name())
    65  	}
    66  	// FIXME
    67  	// if !bytes.Equal(c2.RawValues, c.RawValues) {
    68  	// 	t.Fatal("Values data did not match")
    69  	// }
    70  	if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" {
    71  		t.Fatal("Files data did not match")
    72  	}
    73  }
    74  
    75  func TestSaveDir(t *testing.T) {
    76  	tmp, err := ioutil.TempDir("", "helm-")
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	defer os.RemoveAll(tmp)
    81  
    82  	c := &chart.Chart{
    83  		Metadata: &chart.Metadata{
    84  			APIVersion: chart.APIVersionV1,
    85  			Name:       "ahab",
    86  			Version:    "1.2.3",
    87  		},
    88  		Files: []*chart.File{
    89  			{Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")},
    90  		},
    91  	}
    92  
    93  	if err := SaveDir(c, tmp); err != nil {
    94  		t.Fatalf("Failed to save: %s", err)
    95  	}
    96  
    97  	c2, err := loader.LoadDir(tmp + "/ahab")
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	if c2.Name() != c.Name() {
   103  		t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name())
   104  	}
   105  	// FIXME
   106  	// if !bytes.Equal(c2.RawValues, c.RawValues) {
   107  	// 	t.Fatal("Values data did not match")
   108  	// }
   109  	if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" {
   110  		t.Fatal("Files data did not match")
   111  	}
   112  }