github.com/qsis/helm@v3.0.0-beta.3+incompatible/cmd/helm/search/search_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  	"helm.sh/helm/pkg/chart"
    24  	"helm.sh/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-rc-1.tgz"},
    95  			Metadata: &chart.Metadata{
    96  				Name:        "santa-maria",
    97  				Version:     "1.2.2-RC-1",
    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  		t.Run(tt.name, func(t *testing.T) {
   240  
   241  			charts, err := i.Search(tt.query, 100, tt.regexp)
   242  			if err != nil {
   243  				if tt.fail {
   244  					if !strings.Contains(err.Error(), tt.failMsg) {
   245  						t.Fatalf("Unexpected error message: %s", err)
   246  					}
   247  					return
   248  				}
   249  				t.Fatalf("%s: %s", tt.name, err)
   250  			}
   251  			// Give us predictably ordered results.
   252  			SortScore(charts)
   253  
   254  			l := len(charts)
   255  			if l != len(tt.expect) {
   256  				t.Fatalf("Expected %d result, got %d", len(tt.expect), l)
   257  			}
   258  			// For empty result sets, just keep going.
   259  			if l == 0 {
   260  				return
   261  			}
   262  
   263  			for i, got := range charts {
   264  				ex := tt.expect[i]
   265  				if got.Name != ex.Name {
   266  					t.Errorf("[%d]: Expected name %q, got %q", i, ex.Name, got.Name)
   267  				}
   268  			}
   269  
   270  		})
   271  	}
   272  }
   273  
   274  func TestSearchByNameAll(t *testing.T) {
   275  	// Test with the All bit turned on.
   276  	i := loadTestIndex(t, true)
   277  	cs, err := i.Search("santa-maria", 100, false)
   278  	if err != nil {
   279  		t.Fatal(err)
   280  	}
   281  	if len(cs) != 2 {
   282  		t.Errorf("expected 2 charts, got %d", len(cs))
   283  	}
   284  }
   285  
   286  func TestCalcScore(t *testing.T) {
   287  	i := NewIndex()
   288  
   289  	fields := []string{"aaa", "bbb", "ccc", "ddd"}
   290  	matchline := strings.Join(fields, sep)
   291  	if r := i.calcScore(2, matchline); r != 0 {
   292  		t.Errorf("Expected 0, got %d", r)
   293  	}
   294  	if r := i.calcScore(5, matchline); r != 1 {
   295  		t.Errorf("Expected 1, got %d", r)
   296  	}
   297  	if r := i.calcScore(10, matchline); r != 2 {
   298  		t.Errorf("Expected 2, got %d", r)
   299  	}
   300  	if r := i.calcScore(14, matchline); r != 3 {
   301  		t.Errorf("Expected 3, got %d", r)
   302  	}
   303  }