Skip to content

Commit 0f775a4

Browse files
howardjohnistio-testing
authored andcommitted
Fix version command to show correct information (istio#14754)
* Fix version command to show correct information A recent change made the ldflags not get passed properly leading to the version command giving "unknown". This change fixes this issue, and adds a tests to ensure similar changes are caught in the future. During this it was discovered some of our binaries don't include the version command, so those were added to sdsclient and mixgen. * Fix lint and codecov * Fix lint
1 parent a87f2e0 commit 0f775a4

File tree

7 files changed

+96
-2
lines changed

7 files changed

+96
-2
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@ ${ISTIO_OUT}/sdsclient:
370370
# Build will rebuild the go binaries.
371371
build: depend $(PILOT_GO_BINS_SHORT) mixc mixs mixgen node_agent node_agent_k8s istio_ca istioctl galley sdsclient
372372

373+
.PHONY: version-test
374+
# Do not run istioctl since is different (connects to kubernetes)
375+
version-test:
376+
go test ./tests/version/... -v --base-dir ${ISTIO_OUT} --binaries="$(PILOT_GO_BINS_SHORT) mixc mixs mixgen node_agent node_agent_k8s istio_ca galley sdsclient"
377+
373378
# The following are convenience aliases for most of the go targets
374379
# The first block is for aliases that are the same as the actual binary,
375380
# while the ones that follow need slight adjustments to their names.

bin/gobuild.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ GOBINARY=${GOBINARY:-go}
3636
GOPKG="$GOPATH/pkg"
3737
BUILDINFO=${BUILDINFO:-""}
3838
STATIC=${STATIC:-1}
39-
LDFLAGS=${LDFLAGS:-extldflags -static}
39+
LDFLAGS=${LDFLAGS:--extldflags -static}
4040
GOBUILDFLAGS=${GOBUILDFLAGS:-""}
4141
# Split GOBUILDFLAGS by spaces into an array called GOBUILDFLAGS_ARRAY.
4242
IFS=' ' read -r -a GOBUILDFLAGS_ARRAY <<< "$GOBUILDFLAGS"

codecov.skip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ istio.io/istio/samples
1818
istio.io/istio/security/tests/integration
1919
istio.io/istio/tests/codecov
2020
istio.io/istio/tests/e2e
21+
istio.io/istio/tests/version
2122
istio.io/istio/tests/integration2/examples
2223
istio.io/istio/tests/integration2/qualification
2324
istio.io/istio/tests/integration_old

mixer/tools/mixgen/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ import (
1919

2020
"istio.io/istio/mixer/cmd/shared"
2121
"istio.io/istio/mixer/tools/mixgen/cmd"
22+
"istio.io/pkg/version"
2223
)
2324

2425
func main() {
2526
rootCmd := cmd.GetRootCmd(os.Args[1:], shared.Printf, shared.Fatalf)
2627

28+
rootCmd.AddCommand(version.CobraCommand())
29+
2730
if err := rootCmd.Execute(); err != nil {
2831
os.Exit(-1)
2932
}

prow/istio-unit-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ cd "${ROOT}"
3434
# Integration/e2e tests in the other scripts are run against GKE or real clusters.
3535
JUNIT_UNIT_TEST_XML="${ARTIFACTS_DIR}/junit_unit-tests.xml" \
3636
T="-v" \
37-
make build localTestEnv test
37+
make build localTestEnv test version-test

security/tools/sdsclient/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020

2121
"github.com/spf13/cobra"
2222

23+
"istio.io/pkg/version"
24+
2325
"istio.io/istio/pkg/cmd"
2426
"istio.io/istio/security/pkg/testing/sdsc"
2527
"istio.io/pkg/env"
@@ -48,6 +50,10 @@ var (
4850
}
4951
)
5052

53+
func init() {
54+
rootCmd.AddCommand(version.CobraCommand())
55+
}
56+
5157
func main() {
5258
if err := rootCmd.Execute(); err != nil {
5359
log.Fatalf("failed to start the sdsclient, error %v", err)

tests/version/version_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"encoding/json"
19+
"flag"
20+
"os/exec"
21+
"path"
22+
"strings"
23+
"testing"
24+
25+
"istio.io/pkg/version"
26+
)
27+
28+
var (
29+
binaries *string
30+
releasedir *string
31+
)
32+
33+
func init() {
34+
releasedir = flag.String("base-dir", "", "directory for binaries")
35+
binaries = flag.String("binaries", "", "space separated binaries to test")
36+
flag.Parse()
37+
38+
}
39+
40+
func TestVersion(t *testing.T) {
41+
binariesToTest := strings.Split(*binaries, " ")
42+
if len(binariesToTest) == 0 {
43+
t.Fatal("No binaries to test. Pass the --binaries flag.")
44+
}
45+
for _, b := range binariesToTest {
46+
cmd := path.Join(*releasedir, b)
47+
t.Run(b, func(t *testing.T) {
48+
out, err := exec.Command(cmd, "version", "-ojson").Output()
49+
if err != nil {
50+
t.Fatalf("Version failed with error: %v. Output: %v", err, string(out))
51+
}
52+
53+
var resp version.Version
54+
if err := json.Unmarshal(out, &resp); err != nil {
55+
t.Fatalf("Failed to marshal to json: %v. Output: %v", err, string(out))
56+
}
57+
58+
verInfo := resp.ClientVersion
59+
60+
validateField(t, "Version", verInfo.Version)
61+
validateField(t, "GitRevision", verInfo.GitRevision)
62+
validateField(t, "User", verInfo.User)
63+
validateField(t, "Host", verInfo.Host)
64+
validateField(t, "GolangVersion", verInfo.GolangVersion)
65+
validateField(t, "DockerHub", verInfo.DockerHub)
66+
validateField(t, "BuildStatus", verInfo.BuildStatus)
67+
validateField(t, "GitTag", verInfo.GitTag)
68+
})
69+
}
70+
}
71+
72+
// TODO we may be able to do more validation of fields here, but because it changes based on the environment this is not easy
73+
// For now ensuring the fields even get populated is most important.
74+
func validateField(t *testing.T, field, s string) {
75+
t.Helper()
76+
if s == "unknown" || s == "" {
77+
t.Errorf("Field %v was invalid. Got: %v.", field, s)
78+
}
79+
}

0 commit comments

Comments
 (0)