github.com/Beeketing/helm@v2.12.1+incompatible/pkg/chartutil/create_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  	"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  	for _, f := range []string{TestConnectionName} {
    79  		if fi, err := os.Stat(filepath.Join(dir, TemplatesTestsDir, f)); err != nil {
    80  			t.Errorf("Expected %s file: %s", f, err)
    81  		} else if fi.IsDir() {
    82  			t.Errorf("Expected %s to be a file.", f)
    83  		}
    84  	}
    85  
    86  }
    87  
    88  func TestCreateFrom(t *testing.T) {
    89  	tdir, err := ioutil.TempDir("", "helm-")
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	defer os.RemoveAll(tdir)
    94  
    95  	cf := &chart.Metadata{Name: "foo"}
    96  	srcdir := "./testdata/mariner"
    97  
    98  	if err := CreateFrom(cf, tdir, srcdir); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	dir := filepath.Join(tdir, "foo")
   103  
   104  	c := filepath.Join(tdir, cf.Name)
   105  	mychart, err := LoadDir(c)
   106  	if err != nil {
   107  		t.Fatalf("Failed to load newly created chart %q: %s", c, err)
   108  	}
   109  
   110  	if mychart.Metadata.Name != "foo" {
   111  		t.Errorf("Expected name to be 'foo', got %q", mychart.Metadata.Name)
   112  	}
   113  
   114  	for _, d := range []string{TemplatesDir, ChartsDir} {
   115  		if fi, err := os.Stat(filepath.Join(dir, d)); err != nil {
   116  			t.Errorf("Expected %s dir: %s", d, err)
   117  		} else if !fi.IsDir() {
   118  			t.Errorf("Expected %s to be a directory.", d)
   119  		}
   120  	}
   121  
   122  	for _, f := range []string{ChartfileName, ValuesfileName, "requirements.yaml"} {
   123  		if fi, err := os.Stat(filepath.Join(dir, 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  	for _, f := range []string{"placeholder.tpl"} {
   131  		if fi, err := os.Stat(filepath.Join(dir, TemplatesDir, f)); err != nil {
   132  			t.Errorf("Expected %s file: %s", f, err)
   133  		} else if fi.IsDir() {
   134  			t.Errorf("Expected %s to be a file.", f)
   135  		}
   136  	}
   137  
   138  	// Ensure we replace `<CHARTNAME>`
   139  	if strings.Contains(mychart.Values.Raw, "<CHARTNAME>") {
   140  		t.Errorf("Did not expect %s to be present in %s", "<CHARTNAME>", mychart.Values.Raw)
   141  	}
   142  }