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