github.com/y-taka-23/helm@v2.8.0+incompatible/cmd/helm/repo_index_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 "bytes" 21 "io" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 "testing" 26 27 "k8s.io/helm/pkg/repo" 28 ) 29 30 func TestRepoIndexCmd(t *testing.T) { 31 32 dir, err := ioutil.TempDir("", "helm-") 33 if err != nil { 34 t.Fatal(err) 35 } 36 defer os.RemoveAll(dir) 37 38 comp := filepath.Join(dir, "compressedchart-0.1.0.tgz") 39 if err := linkOrCopy("testdata/testcharts/compressedchart-0.1.0.tgz", comp); err != nil { 40 t.Fatal(err) 41 } 42 comp2 := filepath.Join(dir, "compressedchart-0.2.0.tgz") 43 if err := linkOrCopy("testdata/testcharts/compressedchart-0.2.0.tgz", comp2); err != nil { 44 t.Fatal(err) 45 } 46 47 buf := bytes.NewBuffer(nil) 48 c := newRepoIndexCmd(buf) 49 50 if err := c.RunE(c, []string{dir}); err != nil { 51 t.Error(err) 52 } 53 54 destIndex := filepath.Join(dir, "index.yaml") 55 56 index, err := repo.LoadIndexFile(destIndex) 57 if err != nil { 58 t.Fatal(err) 59 } 60 61 if len(index.Entries) != 1 { 62 t.Errorf("expected 1 entry, got %d: %#v", len(index.Entries), index.Entries) 63 } 64 65 vs := index.Entries["compressedchart"] 66 if len(vs) != 2 { 67 t.Errorf("expected 2 versions, got %d: %#v", len(vs), vs) 68 } 69 70 expectedVersion := "0.2.0" 71 if vs[0].Version != expectedVersion { 72 t.Errorf("expected %q, got %q", expectedVersion, vs[0].Version) 73 } 74 75 // Test with `--merge` 76 77 // Remove first two charts. 78 if err := os.Remove(comp); err != nil { 79 t.Fatal(err) 80 } 81 if err := os.Remove(comp2); err != nil { 82 t.Fatal(err) 83 } 84 // Add a new chart and a new version of an existing chart 85 if err := linkOrCopy("testdata/testcharts/reqtest-0.1.0.tgz", filepath.Join(dir, "reqtest-0.1.0.tgz")); err != nil { 86 t.Fatal(err) 87 } 88 if err := linkOrCopy("testdata/testcharts/compressedchart-0.3.0.tgz", filepath.Join(dir, "compressedchart-0.3.0.tgz")); err != nil { 89 t.Fatal(err) 90 } 91 92 c.ParseFlags([]string{"--merge", destIndex}) 93 if err := c.RunE(c, []string{dir}); err != nil { 94 t.Error(err) 95 } 96 97 index, err = repo.LoadIndexFile(destIndex) 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 if len(index.Entries) != 2 { 103 t.Errorf("expected 2 entries, got %d: %#v", len(index.Entries), index.Entries) 104 } 105 106 vs = index.Entries["compressedchart"] 107 if len(vs) != 3 { 108 t.Errorf("expected 3 versions, got %d: %#v", len(vs), vs) 109 } 110 111 expectedVersion = "0.3.0" 112 if vs[0].Version != expectedVersion { 113 t.Errorf("expected %q, got %q", expectedVersion, vs[0].Version) 114 } 115 } 116 117 func linkOrCopy(old, new string) error { 118 if err := os.Link(old, new); err != nil { 119 return copyFile(old, new) 120 } 121 122 return nil 123 } 124 125 func copyFile(dst, src string) error { 126 i, err := os.Open(dst) 127 if err != nil { 128 return err 129 } 130 defer i.Close() 131 132 o, err := os.Create(src) 133 if err != nil { 134 return err 135 } 136 defer o.Close() 137 138 _, err = io.Copy(o, i) 139 140 return err 141 }