github.com/rakanixu/helm@v2.8.2+incompatible/cmd/helm/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 main
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func TestSearchCmd(t *testing.T) {
    26  	tests := []struct {
    27  		name   string
    28  		args   []string
    29  		flags  []string
    30  		expect string
    31  		regexp bool
    32  		fail   bool
    33  	}{
    34  		{
    35  			name:   "search for 'maria', expect one match",
    36  			args:   []string{"maria"},
    37  			expect: "NAME           \tCHART VERSION\tAPP VERSION\tDESCRIPTION      \ntesting/mariadb\t0.3.0        \t           \tChart for MariaDB",
    38  		},
    39  		{
    40  			name:   "search for 'alpine', expect two matches",
    41  			args:   []string{"alpine"},
    42  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod",
    43  		},
    44  		{
    45  			name:   "search for 'alpine' with versions, expect three matches",
    46  			args:   []string{"alpine"},
    47  			flags:  []string{"--versions"},
    48  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod\ntesting/alpine\t0.1.0        \t1.2.3      \tDeploy a basic Alpine Linux pod",
    49  		},
    50  		{
    51  			name:   "search for 'alpine' with version constraint, expect one match with version 0.1.0",
    52  			args:   []string{"alpine"},
    53  			flags:  []string{"--version", ">= 0.1, < 0.2"},
    54  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.1.0        \t1.2.3      \tDeploy a basic Alpine Linux pod",
    55  		},
    56  		{
    57  			name:   "search for 'alpine' with version constraint, expect one match with version 0.1.0",
    58  			args:   []string{"alpine"},
    59  			flags:  []string{"--versions", "--version", ">= 0.1, < 0.2"},
    60  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.1.0        \t1.2.3      \tDeploy a basic Alpine Linux pod",
    61  		},
    62  		{
    63  			name:   "search for 'alpine' with version constraint, expect one match with version 0.2.0",
    64  			args:   []string{"alpine"},
    65  			flags:  []string{"--version", ">= 0.1"},
    66  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod",
    67  		},
    68  		{
    69  			name:   "search for 'alpine' with version constraint and --versions, expect two matches",
    70  			args:   []string{"alpine"},
    71  			flags:  []string{"--versions", "--version", ">= 0.1"},
    72  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod\ntesting/alpine\t0.1.0        \t1.2.3      \tDeploy a basic Alpine Linux pod",
    73  		},
    74  		{
    75  			name:   "search for 'syzygy', expect no matches",
    76  			args:   []string{"syzygy"},
    77  			expect: "No results found",
    78  		},
    79  		{
    80  			name:   "search for 'alp[a-z]+', expect two matches",
    81  			args:   []string{"alp[a-z]+"},
    82  			flags:  []string{"--regexp"},
    83  			expect: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod",
    84  			regexp: true,
    85  		},
    86  		{
    87  			name:   "search for 'alp[', expect failure to compile regexp",
    88  			args:   []string{"alp["},
    89  			flags:  []string{"--regexp"},
    90  			regexp: true,
    91  			fail:   true,
    92  		},
    93  	}
    94  
    95  	cleanup := resetEnv()
    96  	defer cleanup()
    97  
    98  	settings.Home = "testdata/helmhome"
    99  
   100  	for _, tt := range tests {
   101  		buf := bytes.NewBuffer(nil)
   102  		cmd := newSearchCmd(buf)
   103  		cmd.ParseFlags(tt.flags)
   104  		if err := cmd.RunE(cmd, tt.args); err != nil {
   105  			if tt.fail {
   106  				continue
   107  			}
   108  			t.Fatalf("%s: unexpected error %s", tt.name, err)
   109  		}
   110  		got := strings.TrimSpace(buf.String())
   111  		if got != tt.expect {
   112  			t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got)
   113  		}
   114  	}
   115  }