github.com/koderover/helm@v2.17.0+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  	"archive/tar"
    21  	"compress/gzip"
    22  	"io"
    23  	"io/ioutil"
    24  	"os"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/golang/protobuf/ptypes/any"
    30  	"k8s.io/helm/pkg/proto/hapi/chart"
    31  )
    32  
    33  func TestSave(t *testing.T) {
    34  	tmp, err := ioutil.TempDir("", "helm-")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer os.RemoveAll(tmp)
    39  
    40  	c := &chart.Chart{
    41  		Metadata: &chart.Metadata{
    42  			Name:    "ahab",
    43  			Version: "1.2.3.4",
    44  		},
    45  		Values: &chart.Config{
    46  			Raw: "ship: Pequod",
    47  		},
    48  		Files: []*any.Any{
    49  			{TypeUrl: "scheherazade/shahryar.txt", Value: []byte("1,001 Nights")},
    50  		},
    51  		Templates: []*chart.Template{
    52  			{Name: "templates/scheherazade/shahryar.txt.tmpl", Data: []byte("{{ \"1,001 Nights\" }}")},
    53  		},
    54  	}
    55  
    56  	where, err := Save(c, tmp)
    57  	if err != nil {
    58  		t.Fatalf("Failed to save: %s", err)
    59  	}
    60  	if !strings.HasPrefix(where, tmp) {
    61  		t.Fatalf("Expected %q to start with %q", where, tmp)
    62  	}
    63  	if !strings.HasSuffix(where, ".tgz") {
    64  		t.Fatalf("Expected %q to end with .tgz", where)
    65  	}
    66  
    67  	c2, err := LoadFile(where)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	if c2.Metadata.Name != c.Metadata.Name {
    73  		t.Fatalf("Expected chart archive to have %q, got %q", c.Metadata.Name, c2.Metadata.Name)
    74  	}
    75  	if c2.Values.Raw != c.Values.Raw {
    76  		t.Fatal("Values data did not match")
    77  	}
    78  	if len(c2.Files) != 1 || c2.Files[0].TypeUrl != "scheherazade/shahryar.txt" {
    79  		t.Fatal("Files data did not match")
    80  	}
    81  	if len(c2.Templates) != 1 || c2.Templates[0].Name != "templates/scheherazade/shahryar.txt.tmpl" {
    82  		t.Fatal("Templates data did not match")
    83  	}
    84  }
    85  
    86  func TestSavePreservesTimestamps(t *testing.T) {
    87  	// Test executes so quickly that if we don't subtract a second, the
    88  	// check will fail because `initialCreateTime` will be identical to the
    89  	// written timestamp for the files.
    90  	initialCreateTime := time.Now().Add(-1 * time.Second)
    91  
    92  	tmp, err := ioutil.TempDir("", "helm-")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	defer os.RemoveAll(tmp)
    97  
    98  	c := &chart.Chart{
    99  		Metadata: &chart.Metadata{
   100  			Name:    "ahab",
   101  			Version: "1.2.3.4",
   102  		},
   103  		Values: &chart.Config{
   104  			Raw: "ship: Pequod",
   105  		},
   106  		Files: []*any.Any{
   107  			{TypeUrl: "scheherazade/shahryar.txt", Value: []byte("1,001 Nights")},
   108  		},
   109  		Templates: []*chart.Template{
   110  			{Name: "templates/scheherazade/shahryar.txt.tmpl", Data: []byte("{{ \"1,001 Nights\" }}")},
   111  		},
   112  	}
   113  
   114  	where, err := Save(c, tmp)
   115  	if err != nil {
   116  		t.Fatalf("Failed to save: %s", err)
   117  	}
   118  
   119  	allHeaders, err := retrieveAllHeadersFromTar(where)
   120  	if err != nil {
   121  		t.Fatalf("Failed to parse tar: %v", err)
   122  	}
   123  
   124  	for _, header := range allHeaders {
   125  		if header.ModTime.Before(initialCreateTime) {
   126  			t.Fatalf("File timestamp not preserved: %v", header.ModTime)
   127  		}
   128  	}
   129  }
   130  
   131  // We could refactor `load.go` to use this `retrieveAllHeadersFromTar` function
   132  // as well, so we are not duplicating components of the code which iterate
   133  // through the tar.
   134  func retrieveAllHeadersFromTar(path string) ([]*tar.Header, error) {
   135  	raw, err := os.Open(path)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  	defer raw.Close()
   140  
   141  	unzipped, err := gzip.NewReader(raw)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	defer unzipped.Close()
   146  
   147  	tr := tar.NewReader(unzipped)
   148  	headers := []*tar.Header{}
   149  	for {
   150  		hd, err := tr.Next()
   151  		if err == io.EOF {
   152  			break
   153  		}
   154  
   155  		if err != nil {
   156  			return nil, err
   157  		}
   158  
   159  		headers = append(headers, hd)
   160  	}
   161  
   162  	return headers, nil
   163  }
   164  
   165  func TestSaveDir(t *testing.T) {
   166  	tmp, err := ioutil.TempDir("", "helm-")
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  	defer os.RemoveAll(tmp)
   171  
   172  	c := &chart.Chart{
   173  		Metadata: &chart.Metadata{
   174  			Name:    "ahab",
   175  			Version: "1.2.3.4",
   176  		},
   177  		Values: &chart.Config{
   178  			Raw: "ship: Pequod",
   179  		},
   180  		Files: []*any.Any{
   181  			{TypeUrl: "scheherazade/shahryar.txt", Value: []byte("1,001 Nights")},
   182  		},
   183  		Templates: []*chart.Template{
   184  			{Name: "templates/scheherazade/shahryar.txt.tmpl", Data: []byte("{{ \"1,001 Nights\" }}")},
   185  		},
   186  	}
   187  
   188  	if err := SaveDir(c, tmp); err != nil {
   189  		t.Fatalf("Failed to save: %s", err)
   190  	}
   191  
   192  	c2, err := LoadDir(tmp + "/ahab")
   193  	if err != nil {
   194  		t.Fatal(err)
   195  	}
   196  
   197  	if c2.Metadata.Name != c.Metadata.Name {
   198  		t.Fatalf("Expected chart archive to have %q, got %q", c.Metadata.Name, c2.Metadata.Name)
   199  	}
   200  	if c2.Values.Raw != c.Values.Raw {
   201  		t.Fatal("Values data did not match")
   202  	}
   203  	if len(c2.Files) != 1 || c2.Files[0].TypeUrl != "scheherazade/shahryar.txt" {
   204  		t.Fatal("Files data did not match")
   205  	}
   206  	if len(c2.Templates) != 1 || c2.Templates[0].Name != "templates/scheherazade/shahryar.txt.tmpl" {
   207  		t.Fatal("Templates data did not match")
   208  	}
   209  }