Merge pull request #3671 from adshmh/fix-duplication-of-test-code-on-helm-version

refactor helm version command tests to remove duplication.
pull/2824/head
Matthew Fisher 7 years ago committed by GitHub
commit 5d3705a688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,50 +16,50 @@ limitations under the License.
package main package main
import ( import (
"bytes" "fmt"
"strings" "io"
"regexp"
"testing" "testing"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/version" "k8s.io/helm/pkg/version"
) )
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
lver := regexp.QuoteMeta(version.GetVersionProto().SemVer)
sver := regexp.QuoteMeta("1.2.3-fakeclient+testonly")
clientVersion := fmt.Sprintf("Client: &version\\.Version{SemVer:\"%s\", GitCommit:\"\", GitTreeState:\"\"}\n", lver)
serverVersion := fmt.Sprintf("Server: &version\\.Version{SemVer:\"%s\", GitCommit:\"\", GitTreeState:\"\"}\n", sver)
lver := version.GetVersionProto().SemVer tests := []releaseCase{
sver := "1.2.3-fakeclient+testonly" {
name: "default",
tests := []struct { args: []string{},
name string expected: clientVersion + serverVersion,
client, server bool },
args []string {
fail bool name: "client",
}{ args: []string{},
{"default", true, true, []string{}, false}, flags: []string{"-c"},
{"client", true, false, []string{"-c"}, false}, expected: clientVersion,
{"server", false, true, []string{"-s"}, false}, },
{"template", true, true, []string{"--template='{{ .Client.SemVer }} {{ .Server.SemVer }}'"}, false}, {
name: "server",
args: []string{},
flags: []string{"-s"},
expected: serverVersion,
},
{
name: "template",
args: []string{},
flags: []string{"--template", "{{ .Client.SemVer }} {{ .Server.SemVer }}"},
expected: lver + " " + sver,
},
} }
settings.TillerHost = "fake-localhost" settings.TillerHost = "fake-localhost"
for _, tt := range tests { runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
b := new(bytes.Buffer) return newVersionCmd(c, out)
c := &helm.FakeClient{} })
cmd := newVersionCmd(c, b)
cmd.ParseFlags(tt.args)
if err := cmd.RunE(cmd, tt.args); err != nil {
if tt.fail {
continue
}
t.Fatal(err)
}
if tt.client && !strings.Contains(b.String(), lver) {
t.Errorf("Expected %q to contain %q", b.String(), lver)
}
if tt.server && !strings.Contains(b.String(), sver) {
t.Errorf("Expected %q to contain %q", b.String(), sver)
}
}
} }

Loading…
Cancel
Save