github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/repo/repo_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 repo 18 19 import ( 20 "os" 21 "path/filepath" 22 "reflect" 23 "testing" 24 "time" 25 ) 26 27 const testRepositoriesFile = "testdata/repositories.yaml" 28 const testRepository = "testdata/repository" 29 const testURL = "http://example-charts.com" 30 31 func TestLoadChartRepository(t *testing.T) { 32 cr, err := LoadChartRepository(testRepository, testURL) 33 if err != nil { 34 t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 35 } 36 37 paths := []string{filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), filepath.Join(testRepository, "sprocket-1.2.0.tgz")} 38 39 if cr.RootPath != testRepository { 40 t.Errorf("Expected %s as RootPath but got %s", testRepository, cr.RootPath) 41 } 42 43 if !reflect.DeepEqual(cr.ChartPaths, paths) { 44 t.Errorf("Expected %#v but got %#v\n", paths, cr.ChartPaths) 45 } 46 47 if cr.URL != testURL { 48 t.Errorf("Expected url for chart repository to be %s but got %s", testURL, cr.URL) 49 } 50 } 51 52 func TestIndex(t *testing.T) { 53 cr, err := LoadChartRepository(testRepository, testURL) 54 if err != nil { 55 t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 56 } 57 58 err = cr.Index() 59 if err != nil { 60 t.Errorf("Error performing index: %v\n", err) 61 } 62 63 tempIndexPath := filepath.Join(testRepository, indexPath) 64 actual, err := LoadIndexFile(tempIndexPath) 65 defer os.Remove(tempIndexPath) // clean up 66 if err != nil { 67 t.Errorf("Error loading index file %v", err) 68 } 69 70 entries := actual.Entries 71 numEntries := len(entries) 72 if numEntries != 2 { 73 t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries) 74 } 75 76 timestamps := make(map[string]string) 77 var empty time.Time 78 for chartName, details := range entries { 79 if details == nil { 80 t.Errorf("Chart Entry is not filled out for %s", chartName) 81 } 82 83 if details.Created == empty.String() { 84 t.Errorf("Created timestamp under %s chart entry is nil", chartName) 85 } 86 timestamps[chartName] = details.Created 87 88 if details.Checksum == "" { 89 t.Errorf("Checksum was not set for %s", chartName) 90 } 91 } 92 93 if err = cr.Index(); err != nil { 94 t.Errorf("Error performing index the second time: %v\n", err) 95 } 96 second, err := LoadIndexFile(tempIndexPath) 97 if err != nil { 98 t.Errorf("Error loading index file second time: %#v\n", err) 99 } 100 101 for chart, created := range timestamps { 102 v, ok := second.Entries[chart] 103 if !ok { 104 t.Errorf("Expected %s chart entry in index file but did not find it", chart) 105 } 106 if v.Created != created { 107 t.Errorf("Expected Created timestamp to be %s, but got %s for chart %s", created, v.Created, chart) 108 } 109 // Created manually since we control the input of the test 110 expectedURL := testURL + "/" + chart + ".tgz" 111 if v.URL != expectedURL { 112 t.Errorf("Expected url in entry to be %s but got %s for chart: %s", expectedURL, v.URL, chart) 113 } 114 } 115 } 116 117 func TestLoadRepositoriesFile(t *testing.T) { 118 rf, err := LoadRepositoriesFile(testRepositoriesFile) 119 if err != nil { 120 t.Errorf(testRepositoriesFile + " could not be loaded: " + err.Error()) 121 } 122 expected := map[string]string{"best-charts-ever": "http://best-charts-ever.com", 123 "okay-charts": "http://okay-charts.org", "example123": "http://examplecharts.net/charts/123"} 124 125 numOfRepositories := len(rf.Repositories) 126 expectedNumOfRepositories := 3 127 if numOfRepositories != expectedNumOfRepositories { 128 t.Errorf("Expected %v repositories but only got %v", expectedNumOfRepositories, numOfRepositories) 129 } 130 131 for expectedRepo, expectedURL := range expected { 132 actual, ok := rf.Repositories[expectedRepo] 133 if !ok { 134 t.Errorf("Expected repository: %v but was not found", expectedRepo) 135 } 136 137 if expectedURL != actual { 138 t.Errorf("Expected url %s for the %s repository but got %s ", expectedURL, expectedRepo, actual) 139 } 140 } 141 }