github.com/migueleliasweb/helm@v2.6.1+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 \tVERSION\tDESCRIPTION \ntesting/mariadb\t0.3.0 \tChart for MariaDB", 38 }, 39 { 40 name: "search for 'alpine', expect two matches", 41 args: []string{"alpine"}, 42 expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \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 \tVERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \tDeploy a basic Alpine Linux pod\ntesting/alpine\t0.1.0 \tDeploy a basic Alpine Linux pod", 49 }, 50 { 51 name: "search for 'syzygy', expect no matches", 52 args: []string{"syzygy"}, 53 expect: "No results found", 54 }, 55 { 56 name: "search for 'alp[a-z]+', expect two matches", 57 args: []string{"alp[a-z]+"}, 58 flags: []string{"--regexp"}, 59 expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \tDeploy a basic Alpine Linux pod", 60 regexp: true, 61 }, 62 { 63 name: "search for 'alp[', expect failure to compile regexp", 64 args: []string{"alp["}, 65 flags: []string{"--regexp"}, 66 regexp: true, 67 fail: true, 68 }, 69 } 70 71 cleanup := resetEnv() 72 defer cleanup() 73 74 settings.Home = "testdata/helmhome" 75 76 for _, tt := range tests { 77 buf := bytes.NewBuffer(nil) 78 cmd := newSearchCmd(buf) 79 cmd.ParseFlags(tt.flags) 80 if err := cmd.RunE(cmd, tt.args); err != nil { 81 if tt.fail { 82 continue 83 } 84 t.Fatalf("%s: unexpected error %s", tt.name, err) 85 } 86 got := strings.TrimSpace(buf.String()) 87 if got != tt.expect { 88 t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got) 89 } 90 } 91 }