github.com/valdemarpavesi/helm@v2.9.1+incompatible/pkg/chartutil/save_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"github.com/golang/protobuf/ptypes/any"
    26  	"k8s.io/helm/pkg/proto/hapi/chart"
    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  			Name:    "ahab",
    39  			Version: "1.2.3.4",
    40  		},
    41  		Values: &chart.Config{
    42  			Raw: "ship: Pequod",
    43  		},
    44  		Files: []*any.Any{
    45  			{TypeUrl: "scheherazade/shahryar.txt", Value: []byte("1,001 Nights")},
    46  		},
    47  	}
    48  
    49  	where, err := Save(c, tmp)
    50  	if err != nil {
    51  		t.Fatalf("Failed to save: %s", err)
    52  	}
    53  	if !strings.HasPrefix(where, tmp) {
    54  		t.Fatalf("Expected %q to start with %q", where, tmp)
    55  	}
    56  	if !strings.HasSuffix(where, ".tgz") {
    57  		t.Fatalf("Expected %q to end with .tgz", where)
    58  	}
    59  
    60  	c2, err := LoadFile(where)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	if c2.Metadata.Name != c.Metadata.Name {
    66  		t.Fatalf("Expected chart archive to have %q, got %q", c.Metadata.Name, c2.Metadata.Name)
    67  	}
    68  	if c2.Values.Raw != c.Values.Raw {
    69  		t.Fatal("Values data did not match")
    70  	}
    71  	if len(c2.Files) != 1 || c2.Files[0].TypeUrl != "scheherazade/shahryar.txt" {
    72  		t.Fatal("Files data did not match")
    73  	}
    74  }
    75  
    76  func TestSaveDir(t *testing.T) {
    77  	tmp, err := ioutil.TempDir("", "helm-")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	defer os.RemoveAll(tmp)
    82  
    83  	c := &chart.Chart{
    84  		Metadata: &chart.Metadata{
    85  			Name:    "ahab",
    86  			Version: "1.2.3.4",
    87  		},
    88  		Values: &chart.Config{
    89  			Raw: "ship: Pequod",
    90  		},
    91  		Files: []*any.Any{
    92  			{TypeUrl: "scheherazade/shahryar.txt", Value: []byte("1,001 Nights")},
    93  		},
    94  	}
    95  
    96  	if err := SaveDir(c, tmp); err != nil {
    97  		t.Fatalf("Failed to save: %s", err)
    98  	}
    99  
   100  	c2, err := LoadDir(tmp + "/ahab")
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	if c2.Metadata.Name != c.Metadata.Name {
   106  		t.Fatalf("Expected chart archive to have %q, got %q", c.Metadata.Name, c2.Metadata.Name)
   107  	}
   108  	if c2.Values.Raw != c.Values.Raw {
   109  		t.Fatal("Values data did not match")
   110  	}
   111  	if len(c2.Files) != 1 || c2.Files[0].TypeUrl != "scheherazade/shahryar.txt" {
   112  		t.Fatal("Files data did not match")
   113  	}
   114  }