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