github.com/zoumo/helm@v2.5.0+incompatible/cmd/helm/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 main 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "k8s.io/helm/pkg/chartutil" 26 "k8s.io/helm/pkg/helm/environment" 27 "k8s.io/helm/pkg/helm/helmpath" 28 "k8s.io/helm/pkg/proto/hapi/chart" 29 ) 30 31 func TestCreateCmd(t *testing.T) { 32 cname := "testchart" 33 // Make a temp dir 34 tdir, err := ioutil.TempDir("", "helm-create-") 35 if err != nil { 36 t.Fatal(err) 37 } 38 defer os.Remove(tdir) 39 40 // CD into it 41 pwd, err := os.Getwd() 42 if err != nil { 43 t.Fatal(err) 44 } 45 if err := os.Chdir(tdir); err != nil { 46 t.Fatal(err) 47 } 48 defer os.Chdir(pwd) 49 50 // Run a create 51 cmd := newCreateCmd(os.Stdout) 52 if err := cmd.RunE(cmd, []string{cname}); err != nil { 53 t.Errorf("Failed to run create: %s", err) 54 return 55 } 56 57 // Test that the chart is there 58 if fi, err := os.Stat(cname); err != nil { 59 t.Fatalf("no chart directory: %s", err) 60 } else if !fi.IsDir() { 61 t.Fatalf("chart is not directory") 62 } 63 64 c, err := chartutil.LoadDir(cname) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 if c.Metadata.Name != cname { 70 t.Errorf("Expected %q name, got %q", cname, c.Metadata.Name) 71 } 72 if c.Metadata.ApiVersion != chartutil.ApiVersionV1 { 73 t.Errorf("Wrong API version: %q", c.Metadata.ApiVersion) 74 } 75 } 76 77 func TestCreateStarterCmd(t *testing.T) { 78 cname := "testchart" 79 // Make a temp dir 80 tdir, err := ioutil.TempDir("", "helm-create-") 81 if err != nil { 82 t.Fatal(err) 83 } 84 defer os.Remove(tdir) 85 86 thome, err := tempHelmHome(t) 87 if err != nil { 88 t.Fatal(err) 89 } 90 old := helmpath.Home(environment.DefaultHelmHome) 91 settings.Home = thome 92 defer func() { 93 settings.Home = old 94 os.RemoveAll(thome.String()) 95 }() 96 97 // Create a starter. 98 starterchart := filepath.Join(thome.String(), "starters") 99 os.Mkdir(starterchart, 0755) 100 if dest, err := chartutil.Create(&chart.Metadata{Name: "starterchart"}, starterchart); err != nil { 101 t.Fatalf("Could not create chart: %s", err) 102 } else { 103 t.Logf("Created %s", dest) 104 } 105 tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl") 106 if err := ioutil.WriteFile(tplpath, []byte("test"), 0755); err != nil { 107 t.Fatalf("Could not write template: %s", err) 108 } 109 110 // CD into it 111 pwd, err := os.Getwd() 112 if err != nil { 113 t.Fatal(err) 114 } 115 if err := os.Chdir(tdir); err != nil { 116 t.Fatal(err) 117 } 118 defer os.Chdir(pwd) 119 120 // Run a create 121 cmd := newCreateCmd(os.Stdout) 122 cmd.ParseFlags([]string{"--starter", "starterchart"}) 123 if err := cmd.RunE(cmd, []string{cname}); err != nil { 124 t.Errorf("Failed to run create: %s", err) 125 return 126 } 127 128 // Test that the chart is there 129 if fi, err := os.Stat(cname); err != nil { 130 t.Fatalf("no chart directory: %s", err) 131 } else if !fi.IsDir() { 132 t.Fatalf("chart is not directory") 133 } 134 135 c, err := chartutil.LoadDir(cname) 136 if err != nil { 137 t.Fatal(err) 138 } 139 140 if c.Metadata.Name != cname { 141 t.Errorf("Expected %q name, got %q", cname, c.Metadata.Name) 142 } 143 if c.Metadata.ApiVersion != chartutil.ApiVersionV1 { 144 t.Errorf("Wrong API version: %q", c.Metadata.ApiVersion) 145 } 146 147 if l := len(c.Templates); l != 6 { 148 t.Errorf("Expected 5 templates, got %d", l) 149 } 150 151 found := false 152 for _, tpl := range c.Templates { 153 if tpl.Name == "templates/foo.tpl" { 154 found = true 155 data := tpl.Data 156 if string(data) != "test" { 157 t.Errorf("Expected template 'test', got %q", string(data)) 158 } 159 } 160 } 161 if !found { 162 t.Error("Did not find foo.tpl") 163 } 164 165 }