Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client version output with --component option #1408

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.10.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.7.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.10.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.5.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.7.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.10.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.10.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.5.0
11 changes: 10 additions & 1 deletion pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ func Command(p cli.Params) *cobra.Command {
fmt.Fprintf(cmd.OutOrStdout(), "Invalid component value\n")
}
} else {
fmt.Fprintf(cmd.OutOrStdout(), "Client version: %s\n", clientVersion)
switch component {
case "":
fmt.Fprintf(cmd.OutOrStdout(), "Client version: %s\n", clientVersion)
case "client":
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", clientVersion)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right though, because as is, tkn version --component=pipeline with a error connecting kubernetes, it will print the client version without an error, which doesn't make sense to me.

We should print the client version if the component is client, otherwise we should either error out or print unknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my bad.
I will correct it.

case "pipeline", "triggers", "dashboard":
fmt.Fprintf(cmd.OutOrStdout(), "unknown\n")
default:
fmt.Fprintf(cmd.OutOrStdout(), "Invalid component value\n")
}
}

if !check || clientVersion == devVersion {
Expand Down
87 changes: 75 additions & 12 deletions pkg/cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,84 @@ func TestVersionGood(t *testing.T) {
golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name()))
}

func TestComponentVersion(t *testing.T) {
v := clientVersion
defer func() { clientVersion = v }()
func TestGetComponentVersions(t *testing.T) {
pipelineDeploymentLabels := map[string]string{
"app.kubernetes.io/part-of": "tekton-pipelines",
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/name": "controller",
}
triggersDeploymentLabels := map[string]string{
"app.kubernetes.io/part-of": "tekton-triggers",
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/name": "controller",
}
dashboardDeploymentLabels := map[string]string{
"app.kubernetes.io/part-of": "tekton-dashboard",
"app.kubernetes.io/component": "dashboard",
"app.kubernetes.io/name": "dashboard",
}
pipelineDeployment := getDeploymentData("pipeline-dep", "", pipelineDeploymentLabels, map[string]string{"app.kubernetes.io/version": "v0.10.0"}, nil)
triggersDeployment := getDeploymentData("triggers-dep", "", triggersDeploymentLabels, map[string]string{"app.kubernetes.io/version": "v0.5.0"}, nil)
dashboardDeployment := getDeploymentData("dashboard-dep", "", dashboardDeploymentLabels, map[string]string{"app.kubernetes.io/version": "v0.7.0"}, nil)

t.Run("test_client", func(t *testing.T) {})
clientVersion = "v1.2.3"
testParams := []struct {
name string
namespace string
userProvidedNamespace string
deployment []*v1.Deployment
goldenFile bool
}{{
name: "deployment only with pipeline installed",
namespace: "test",
userProvidedNamespace: "test",
deployment: []*v1.Deployment{pipelineDeployment},
goldenFile: true,
}, {
name: "deployment with pipeline and triggers installed",
namespace: "test",
userProvidedNamespace: "test",
deployment: []*v1.Deployment{pipelineDeployment, triggersDeployment},
goldenFile: true,
}, {
name: "deployment with pipeline and dashboard installed",
namespace: "test",
userProvidedNamespace: "test",
deployment: []*v1.Deployment{pipelineDeployment, dashboardDeployment},
goldenFile: true,
}, {
name: "deployment with pipeline, triggers and dashboard installed",
namespace: "test",
userProvidedNamespace: "test",
deployment: []*v1.Deployment{pipelineDeployment, triggersDeployment, dashboardDeployment},
goldenFile: true,
},
}
components := []string{"client", "pipeline", "dashboard", "triggers"}

seedData, _ := test.SeedTestData(t, pipelinetest.Data{})
for _, tp := range testParams {
t.Run(tp.name, func(t *testing.T) {
for _, c := range components {
seedData, _ := test.SeedTestData(t, pipelinetest.Data{})
cs := pipelinetest.Clients{Kube: seedData.Kube}
p := &test.Params{Kube: cs.Kube}
version := Command(p)
cls, err := p.Clients()
if err != nil {
t.Errorf("failed to get client: %v", err)
}
// To add multiple deployments in a particular namespace
for _, v := range tp.deployment {
if _, err := cls.Kube.AppsV1().Deployments(tp.namespace).Create(context.Background(), v, metav1.CreateOptions{}); err != nil {
t.Errorf("failed to create deployment: %v", err)
}
}
got, _ := test.ExecuteCommand(version, "version", "-n", "test", "--component", c)
fmt.Println(t.Name())
golden.Assert(t, got, fmt.Sprintf("%s-%s.golden", t.Name(), c))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of generating golden files can't we just do assert?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree with @vinamra28, could be a follow-up though 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. I'll track this in an issue and merge this one 🙂

}
})
}

cs := pipelinetest.Clients{Kube: seedData.Kube}
p := &test.Params{Kube: cs.Kube}
version := Command(p)
got, _ := test.ExecuteCommand(version, "version", "--component", "client")
expected := "v1.2.3\n"
test.AssertOutput(t, expected, got)
}

func TestVersionBad(t *testing.T) {
Expand Down