github.com/amundsenjunior/helm@v2.8.0-rc.1.0.20180119233529-2b92431476e1+incompatible/pkg/chartutil/create_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  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"k8s.io/helm/pkg/proto/hapi/chart"
    27  )
    28  
    29  func TestCreate(t *testing.T) {
    30  	tdir, err := ioutil.TempDir("", "helm-")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer os.RemoveAll(tdir)
    35  
    36  	cf := &chart.Metadata{Name: "foo"}
    37  
    38  	c, err := Create(cf, tdir)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	dir := filepath.Join(tdir, "foo")
    44  
    45  	mychart, err := LoadDir(c)
    46  	if err != nil {
    47  		t.Fatalf("Failed to load newly created chart %q: %s", c, err)
    48  	}
    49  
    50  	if mychart.Metadata.Name != "foo" {
    51  		t.Errorf("Expected name to be 'foo', got %q", mychart.Metadata.Name)
    52  	}
    53  
    54  	for _, d := range []string{TemplatesDir, ChartsDir} {
    55  		if fi, err := os.Stat(filepath.Join(dir, d)); err != nil {
    56  			t.Errorf("Expected %s dir: %s", d, err)
    57  		} else if !fi.IsDir() {
    58  			t.Errorf("Expected %s to be a directory.", d)
    59  		}
    60  	}
    61  
    62  	for _, f := range []string{ChartfileName, ValuesfileName, IgnorefileName} {
    63  		if fi, err := os.Stat(filepath.Join(dir, f)); err != nil {
    64  			t.Errorf("Expected %s file: %s", f, err)
    65  		} else if fi.IsDir() {
    66  			t.Errorf("Expected %s to be a file.", f)
    67  		}
    68  	}
    69  
    70  	for _, f := range []string{NotesName, DeploymentName, ServiceName, HelpersName} {
    71  		if fi, err := os.Stat(filepath.Join(dir, TemplatesDir, f)); err != nil {
    72  			t.Errorf("Expected %s file: %s", f, err)
    73  		} else if fi.IsDir() {
    74  			t.Errorf("Expected %s to be a file.", f)
    75  		}
    76  	}
    77  
    78  }
    79  
    80  func TestCreateFrom(t *testing.T) {
    81  	tdir, err := ioutil.TempDir("", "helm-")
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	defer os.RemoveAll(tdir)
    86  
    87  	cf := &chart.Metadata{Name: "foo"}
    88  	srcdir := "./testdata/mariner"
    89  
    90  	if err := CreateFrom(cf, tdir, srcdir); err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	dir := filepath.Join(tdir, "foo")
    95  
    96  	c := filepath.Join(tdir, cf.Name)
    97  	mychart, err := LoadDir(c)
    98  	if err != nil {
    99  		t.Fatalf("Failed to load newly created chart %q: %s", c, err)
   100  	}
   101  
   102  	if mychart.Metadata.Name != "foo" {
   103  		t.Errorf("Expected name to be 'foo', got %q", mychart.Metadata.Name)
   104  	}
   105  
   106  	for _, d := range []string{TemplatesDir, ChartsDir} {
   107  		if fi, err := os.Stat(filepath.Join(dir, d)); err != nil {
   108  			t.Errorf("Expected %s dir: %s", d, err)
   109  		} else if !fi.IsDir() {
   110  			t.Errorf("Expected %s to be a directory.", d)
   111  		}
   112  	}
   113  
   114  	for _, f := range []string{ChartfileName, ValuesfileName, "requirements.yaml"} {
   115  		if fi, err := os.Stat(filepath.Join(dir, f)); err != nil {
   116  			t.Errorf("Expected %s file: %s", f, err)
   117  		} else if fi.IsDir() {
   118  			t.Errorf("Expected %s to be a file.", f)
   119  		}
   120  	}
   121  
   122  	for _, f := range []string{"placeholder.tpl"} {
   123  		if fi, err := os.Stat(filepath.Join(dir, TemplatesDir, f)); err != nil {
   124  			t.Errorf("Expected %s file: %s", f, err)
   125  		} else if fi.IsDir() {
   126  			t.Errorf("Expected %s to be a file.", f)
   127  		}
   128  	}
   129  
   130  	// Ensure we replace `<CHARTNAME>`
   131  	if strings.Contains(mychart.Values.Raw, "<CHARTNAME>") {
   132  		t.Errorf("Did not expect %s to be present in %s", "<CHARTNAME>", mychart.Values.Raw)
   133  	}
   134  }