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