github.com/felipejfc/helm@v2.1.2+incompatible/cmd/helm/search/search_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 search 18 19 import ( 20 "strings" 21 "testing" 22 23 "k8s.io/helm/pkg/proto/hapi/chart" 24 "k8s.io/helm/pkg/repo" 25 ) 26 27 func TestSortScore(t *testing.T) { 28 in := []*Result{ 29 {Name: "bbb", Score: 0}, 30 {Name: "aaa", Score: 5}, 31 {Name: "abb", Score: 5}, 32 {Name: "aab", Score: 0}, 33 {Name: "bab", Score: 5}, 34 } 35 expect := []string{"aab", "bbb", "aaa", "abb", "bab"} 36 expectScore := []int{0, 0, 5, 5, 5} 37 SortScore(in) 38 39 // Test Score 40 for i := 0; i < len(expectScore); i++ { 41 if expectScore[i] != in[i].Score { 42 t.Errorf("Sort error on index %d: expected %d, got %d", i, expectScore[i], in[i].Score) 43 } 44 } 45 // Test Name 46 for i := 0; i < len(expect); i++ { 47 if expect[i] != in[i].Name { 48 t.Errorf("Sort error: expected %s, got %s", expect[i], in[i].Name) 49 } 50 } 51 } 52 53 var testCacheDir = "../testdata/" 54 55 var indexfileEntries = map[string]repo.ChartVersions{ 56 "niña": { 57 { 58 URLs: []string{"http://example.com/charts/nina-0.1.0.tgz"}, 59 Metadata: &chart.Metadata{ 60 Name: "niña", 61 Version: "0.1.0", 62 Description: "One boat", 63 }, 64 }, 65 }, 66 "pinta": { 67 { 68 URLs: []string{"http://example.com/charts/pinta-0.1.0.tgz"}, 69 Metadata: &chart.Metadata{ 70 Name: "pinta", 71 Version: "0.1.0", 72 Description: "Two ship", 73 }, 74 }, 75 }, 76 "santa-maria": { 77 { 78 URLs: []string{"http://example.com/charts/santa-maria-1.2.3.tgz"}, 79 Metadata: &chart.Metadata{ 80 Name: "santa-maria", 81 Version: "1.2.3", 82 Description: "Three boat", 83 }, 84 }, 85 { 86 URLs: []string{"http://example.com/charts/santa-maria-1.2.2.tgz"}, 87 Metadata: &chart.Metadata{ 88 Name: "santa-maria", 89 Version: "1.2.2", 90 Description: "Three boat", 91 }, 92 }, 93 }, 94 } 95 96 func loadTestIndex(t *testing.T, all bool) *Index { 97 i := NewIndex() 98 i.AddRepo("testing", &repo.IndexFile{Entries: indexfileEntries}, all) 99 i.AddRepo("ztesting", &repo.IndexFile{Entries: map[string]repo.ChartVersions{ 100 "pinta": { 101 { 102 URLs: []string{"http://example.com/charts/pinta-2.0.0.tgz"}, 103 Metadata: &chart.Metadata{ 104 Name: "pinta", 105 Version: "2.0.0", 106 Description: "Two ship, version two", 107 }, 108 }, 109 }, 110 }}, all) 111 return i 112 } 113 114 func TestAll(t *testing.T) { 115 i := loadTestIndex(t, false) 116 all := i.All() 117 if len(all) != 4 { 118 t.Errorf("Expected 4 entries, got %d", len(all)) 119 } 120 121 i = loadTestIndex(t, true) 122 all = i.All() 123 if len(all) != 5 { 124 t.Errorf("Expected 5 entries, got %d", len(all)) 125 } 126 } 127 128 func TestSearchByName(t *testing.T) { 129 130 tests := []struct { 131 name string 132 query string 133 expect []*Result 134 regexp bool 135 fail bool 136 failMsg string 137 }{ 138 { 139 name: "basic search for one result", 140 query: "santa-maria", 141 expect: []*Result{ 142 {Name: "testing/santa-maria"}, 143 }, 144 }, 145 { 146 name: "basic search for two results", 147 query: "pinta", 148 expect: []*Result{ 149 {Name: "testing/pinta"}, 150 {Name: "ztesting/pinta"}, 151 }, 152 }, 153 { 154 name: "repo-specific search for one result", 155 query: "ztesting/pinta", 156 expect: []*Result{ 157 {Name: "ztesting/pinta"}, 158 }, 159 }, 160 { 161 name: "partial name search", 162 query: "santa", 163 expect: []*Result{ 164 {Name: "testing/santa-maria"}, 165 }, 166 }, 167 { 168 name: "description search, one result", 169 query: "Three", 170 expect: []*Result{ 171 {Name: "testing/santa-maria"}, 172 }, 173 }, 174 { 175 name: "description search, two results", 176 query: "two", 177 expect: []*Result{ 178 {Name: "testing/pinta"}, 179 {Name: "ztesting/pinta"}, 180 }, 181 }, 182 { 183 name: "nothing found", 184 query: "mayflower", 185 expect: []*Result{}, 186 }, 187 { 188 name: "regexp, one result", 189 query: "th[ref]*", 190 expect: []*Result{ 191 {Name: "testing/santa-maria"}, 192 }, 193 regexp: true, 194 }, 195 { 196 name: "regexp, fail compile", 197 query: "th[", 198 expect: []*Result{}, 199 regexp: true, 200 fail: true, 201 failMsg: "error parsing regexp:", 202 }, 203 } 204 205 i := loadTestIndex(t, false) 206 207 for _, tt := range tests { 208 209 charts, err := i.Search(tt.query, 100, tt.regexp) 210 if err != nil { 211 if tt.fail { 212 if !strings.Contains(err.Error(), tt.failMsg) { 213 t.Fatalf("%s: Unexpected error message: %s", tt.name, err) 214 } 215 continue 216 } 217 t.Fatalf("%s: %s", tt.name, err) 218 } 219 // Give us predictably ordered results. 220 SortScore(charts) 221 222 l := len(charts) 223 if l != len(tt.expect) { 224 t.Fatalf("%s: Expected %d result, got %d", tt.name, len(tt.expect), l) 225 } 226 // For empty result sets, just keep going. 227 if l == 0 { 228 continue 229 } 230 231 for i, got := range charts { 232 ex := tt.expect[i] 233 if got.Name != ex.Name { 234 t.Errorf("%s[%d]: Expected name %q, got %q", tt.name, i, ex.Name, got.Name) 235 } 236 } 237 238 } 239 } 240 241 func TestSearchByNameAll(t *testing.T) { 242 // Test with the All bit turned on. 243 i := loadTestIndex(t, true) 244 cs, err := i.Search("santa-maria", 100, false) 245 if err != nil { 246 t.Fatal(err) 247 } 248 if len(cs) != 2 { 249 t.Errorf("expected 2 charts, got %d", len(cs)) 250 } 251 } 252 253 func TestCalcScore(t *testing.T) { 254 i := NewIndex() 255 256 fields := []string{"aaa", "bbb", "ccc", "ddd"} 257 matchline := strings.Join(fields, sep) 258 if r := i.calcScore(2, matchline); r != 0 { 259 t.Errorf("Expected 0, got %d", r) 260 } 261 if r := i.calcScore(5, matchline); r != 1 { 262 t.Errorf("Expected 1, got %d", r) 263 } 264 if r := i.calcScore(10, matchline); r != 2 { 265 t.Errorf("Expected 2, got %d", r) 266 } 267 if r := i.calcScore(14, matchline); r != 3 { 268 t.Errorf("Expected 3, got %d", r) 269 } 270 }