github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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:   "nothing found",
   207  			query:  "mayflower",
   208  			expect: []*Result{},
   209  		},
   210  		{
   211  			name:  "regexp, one result",
   212  			query: "th[ref]*",
   213  			expect: []*Result{
   214  				{Name: "testing/santa-maria"},
   215  			},
   216  			regexp: true,
   217  		},
   218  		{
   219  			name:    "regexp, fail compile",
   220  			query:   "th[",
   221  			expect:  []*Result{},
   222  			regexp:  true,
   223  			fail:    true,
   224  			failMsg: "error parsing regexp:",
   225  		},
   226  	}
   227  
   228  	i := loadTestIndex(t, false)
   229  
   230  	for _, tt := range tests {
   231  
   232  		charts, err := i.Search(tt.query, 100, tt.regexp)
   233  		if err != nil {
   234  			if tt.fail {
   235  				if !strings.Contains(err.Error(), tt.failMsg) {
   236  					t.Fatalf("%s: Unexpected error message: %s", tt.name, err)
   237  				}
   238  				continue
   239  			}
   240  			t.Fatalf("%s: %s", tt.name, err)
   241  		}
   242  		// Give us predictably ordered results.
   243  		SortScore(charts)
   244  
   245  		l := len(charts)
   246  		if l != len(tt.expect) {
   247  			t.Fatalf("%s: Expected %d result, got %d", tt.name, len(tt.expect), l)
   248  		}
   249  		// For empty result sets, just keep going.
   250  		if l == 0 {
   251  			continue
   252  		}
   253  
   254  		for i, got := range charts {
   255  			ex := tt.expect[i]
   256  			if got.Name != ex.Name {
   257  				t.Errorf("%s[%d]: Expected name %q, got %q", tt.name, i, ex.Name, got.Name)
   258  			}
   259  		}
   260  
   261  	}
   262  }
   263  
   264  func TestSearchByNameAll(t *testing.T) {
   265  	// Test with the All bit turned on.
   266  	i := loadTestIndex(t, true)
   267  	cs, err := i.Search("santa-maria", 100, false)
   268  	if err != nil {
   269  		t.Fatal(err)
   270  	}
   271  	if len(cs) != 2 {
   272  		t.Errorf("expected 2 charts, got %d", len(cs))
   273  	}
   274  }
   275  
   276  func TestCalcScore(t *testing.T) {
   277  	i := NewIndex()
   278  
   279  	fields := []string{"aaa", "bbb", "ccc", "ddd"}
   280  	matchline := strings.Join(fields, sep)
   281  	if r := i.calcScore(2, matchline); r != 0 {
   282  		t.Errorf("Expected 0, got %d", r)
   283  	}
   284  	if r := i.calcScore(5, matchline); r != 1 {
   285  		t.Errorf("Expected 1, got %d", r)
   286  	}
   287  	if r := i.calcScore(10, matchline); r != 2 {
   288  		t.Errorf("Expected 2, got %d", r)
   289  	}
   290  	if r := i.calcScore(14, matchline); r != 3 {
   291  		t.Errorf("Expected 3, got %d", r)
   292  	}
   293  }