fix(helm): refactor search command tests to remove duplication

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
pull/3688/head
Arash Deshmeh 7 years ago
parent 4b145622a2
commit 1e4770248c

@ -17,78 +17,72 @@ limitations under the License.
package main package main
import ( import (
"bytes" "io"
"strings"
"testing" "testing"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
) )
func TestSearchCmd(t *testing.T) { func TestSearchCmd(t *testing.T) {
tests := []struct { tests := []releaseCase{
name string
args []string
flags []string
expect string
regexp bool
fail bool
}{
{ {
name: "search for 'maria', expect one match", name: "search for 'maria', expect one match",
args: []string{"maria"}, args: []string{"maria"},
expect: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/mariadb\t0.3.0 \t \tChart for MariaDB", expected: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/mariadb\t0.3.0 \t \tChart for MariaDB",
}, },
{ {
name: "search for 'alpine', expect two matches", name: "search for 'alpine', expect two matches",
args: []string{"alpine"}, args: []string{"alpine"},
expect: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \t2.3.4 \tDeploy a basic Alpine Linux pod", expected: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \t2.3.4 \tDeploy a basic Alpine Linux pod",
}, },
{ {
name: "search for 'alpine' with versions, expect three matches", name: "search for 'alpine' with versions, expect three matches",
args: []string{"alpine"}, args: []string{"alpine"},
flags: []string{"--versions"}, flags: []string{"--versions"},
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", 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",
}, },
{ {
name: "search for 'alpine' with version constraint, expect one match with version 0.1.0", name: "search for 'alpine' with version constraint, expect one match with version 0.1.0",
args: []string{"alpine"}, args: []string{"alpine"},
flags: []string{"--version", ">= 0.1, < 0.2"}, flags: []string{"--version", ">= 0.1, < 0.2"},
expect: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \t1.2.3 \tDeploy a basic Alpine Linux pod", expected: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \t1.2.3 \tDeploy a basic Alpine Linux pod",
}, },
{ {
name: "search for 'alpine' with version constraint, expect one match with version 0.1.0", name: "search for 'alpine' with version constraint, expect one match with version 0.1.0",
args: []string{"alpine"}, args: []string{"alpine"},
flags: []string{"--versions", "--version", ">= 0.1, < 0.2"}, flags: []string{"--versions", "--version", ">= 0.1, < 0.2"},
expect: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \t1.2.3 \tDeploy a basic Alpine Linux pod", expected: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \t1.2.3 \tDeploy a basic Alpine Linux pod",
}, },
{ {
name: "search for 'alpine' with version constraint, expect one match with version 0.2.0", name: "search for 'alpine' with version constraint, expect one match with version 0.2.0",
args: []string{"alpine"}, args: []string{"alpine"},
flags: []string{"--version", ">= 0.1"}, flags: []string{"--version", ">= 0.1"},
expect: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \t2.3.4 \tDeploy a basic Alpine Linux pod", expected: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \t2.3.4 \tDeploy a basic Alpine Linux pod",
}, },
{ {
name: "search for 'alpine' with version constraint and --versions, expect two matches", name: "search for 'alpine' with version constraint and --versions, expect two matches",
args: []string{"alpine"}, args: []string{"alpine"},
flags: []string{"--versions", "--version", ">= 0.1"}, flags: []string{"--versions", "--version", ">= 0.1"},
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", 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",
}, },
{ {
name: "search for 'syzygy', expect no matches", name: "search for 'syzygy', expect no matches",
args: []string{"syzygy"}, args: []string{"syzygy"},
expect: "No results found", expected: "No results found",
}, },
{ {
name: "search for 'alp[a-z]+', expect two matches", name: "search for 'alp[a-z]+', expect two matches",
args: []string{"alp[a-z]+"}, args: []string{"alp[a-z]+"},
flags: []string{"--regexp"}, flags: []string{"--regexp"},
expect: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \t2.3.4 \tDeploy a basic Alpine Linux pod", expected: "NAME \tCHART VERSION\tAPP VERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \t2.3.4 \tDeploy a basic Alpine Linux pod",
regexp: true,
}, },
{ {
name: "search for 'alp[', expect failure to compile regexp", name: "search for 'alp[', expect failure to compile regexp",
args: []string{"alp["}, args: []string{"alp["},
flags: []string{"--regexp"}, flags: []string{"--regexp"},
regexp: true, err: true,
fail: true,
}, },
} }
@ -97,19 +91,7 @@ func TestSearchCmd(t *testing.T) {
settings.Home = "testdata/helmhome" settings.Home = "testdata/helmhome"
for _, tt := range tests { runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
buf := bytes.NewBuffer(nil) return newSearchCmd(out)
cmd := newSearchCmd(buf) })
cmd.ParseFlags(tt.flags)
if err := cmd.RunE(cmd, tt.args); err != nil {
if tt.fail {
continue
}
t.Fatalf("%s: unexpected error %s", tt.name, err)
}
got := strings.TrimSpace(buf.String())
if got != tt.expect {
t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got)
}
}
} }

Loading…
Cancel
Save