github.com/sdbaiguanghe/helm@v2.16.7+incompatible/cmd/helm/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 main
    18  
    19  import (
    20  	"io"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/helm"
    27  )
    28  
    29  func TestSearchCmd(t *testing.T) {
    30  	tests := []releaseCase{
    31  		{
    32  			name:     "search for 'maria', expect one match",
    33  			args:     []string{"maria"},
    34  			expected: "NAME           \tCHART VERSION\tAPP VERSION\tDESCRIPTION      \ntesting/mariadb\t0.3.0        \t           \tChart for MariaDB",
    35  		},
    36  		{
    37  			name:     "search for 'alpine' with --devel, expect one match with newest development version",
    38  			args:     []string{"alpine"},
    39  			flags:    []string{"--devel"},
    40  			expected: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.3.0-rc.1   \t3.0.0      \tDeploy a basic Alpine Linux pod\n",
    41  		},
    42  		{
    43  			name:     "search for 'alpine', expect two matches",
    44  			args:     []string{"alpine"},
    45  			expected: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod",
    46  		},
    47  		{
    48  			name:     "search for 'alpine' with versions, expect three matches",
    49  			args:     []string{"alpine"},
    50  			flags:    []string{"--versions"},
    51  			expected: "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",
    52  		},
    53  		{
    54  			name:     "search for 'alpine' with version constraint, expect one match with version 0.1.0",
    55  			args:     []string{"alpine"},
    56  			flags:    []string{"--version", ">= 0.1, < 0.2"},
    57  			expected: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.1.0        \t1.2.3      \tDeploy a basic Alpine Linux pod",
    58  		},
    59  		{
    60  			name:     "search for 'alpine' with version constraint, expect one match with version 0.1.0",
    61  			args:     []string{"alpine"},
    62  			flags:    []string{"--versions", "--version", ">= 0.1, < 0.2"},
    63  			expected: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.1.0        \t1.2.3      \tDeploy a basic Alpine Linux pod",
    64  		},
    65  		{
    66  			name:     "search for 'alpine' with version constraint, expect one match with version 0.2.0",
    67  			args:     []string{"alpine"},
    68  			flags:    []string{"--version", ">= 0.1"},
    69  			expected: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod",
    70  		},
    71  		{
    72  			name:     "search for 'alpine' with version constraint and --versions, expect two matches",
    73  			args:     []string{"alpine"},
    74  			flags:    []string{"--versions", "--version", ">= 0.1"},
    75  			expected: "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",
    76  		},
    77  		{
    78  			name:     "search for 'syzygy', expect no matches",
    79  			args:     []string{"syzygy"},
    80  			expected: "No results found",
    81  		},
    82  		{
    83  			name:     "search for 'alp[a-z]+', expect two matches",
    84  			args:     []string{"alp[a-z]+"},
    85  			flags:    []string{"--regexp"},
    86  			expected: "NAME          \tCHART VERSION\tAPP VERSION\tDESCRIPTION                    \ntesting/alpine\t0.2.0        \t2.3.4      \tDeploy a basic Alpine Linux pod",
    87  		},
    88  		{
    89  			name:  "search for 'alp[', expect failure to compile regexp",
    90  			args:  []string{"alp["},
    91  			flags: []string{"--regexp"},
    92  			err:   true,
    93  		},
    94  		{
    95  			name:     "search for 'maria', expect one match output json",
    96  			args:     []string{"maria"},
    97  			flags:    strings.Split("--output json", " "),
    98  			expected: `[{"Name":"testing/mariadb","Version":"0.3.0","Appversion":"","Description":"Chart for MariaDB"}]`,
    99  		},
   100  		{
   101  			name:     "search for 'alpine', expect two matches output json",
   102  			args:     []string{"alpine"},
   103  			flags:    strings.Split("--output json", " "),
   104  			expected: `[{"Name":"testing/alpine","Version":"0.2.0","Appversion":"2.3.4","Description":"Deploy a basic Alpine Linux pod"}]`,
   105  		},
   106  		{
   107  			name:     "search for 'maria', expect one match output yaml",
   108  			args:     []string{"maria"},
   109  			flags:    strings.Split("--output yaml", " "),
   110  			expected: "- AppVersion: \"\"\n  Description: Chart for MariaDB\n  Name: testing/mariadb\n  Version: 0.3.0\n\n",
   111  		},
   112  		{
   113  			name:     "search for 'alpine', expect two matches output yaml",
   114  			args:     []string{"alpine"},
   115  			flags:    strings.Split("--output yaml", " "),
   116  			expected: "- AppVersion: 2.3.4\n  Description: Deploy a basic Alpine Linux pod\n  Name: testing/alpine\n  Version: 0.2.0\n\n",
   117  		},
   118  	}
   119  
   120  	cleanup := resetEnv()
   121  	defer cleanup()
   122  
   123  	settings.Home = "testdata/helmhome"
   124  
   125  	runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
   126  		return newSearchCmd(out)
   127  	})
   128  }