Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix version command to show correct information (#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
- Loading branch information
1 parent
a87f2e0
commit 0f775a4
Showing
7 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2019 Istio Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"os/exec" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"istio.io/pkg/version" | ||
) | ||
|
||
var ( | ||
binaries *string | ||
releasedir *string | ||
) | ||
|
||
func init() { | ||
releasedir = flag.String("base-dir", "", "directory for binaries") | ||
binaries = flag.String("binaries", "", "space separated binaries to test") | ||
flag.Parse() | ||
|
||
} | ||
|
||
func TestVersion(t *testing.T) { | ||
binariesToTest := strings.Split(*binaries, " ") | ||
if len(binariesToTest) == 0 { | ||
t.Fatal("No binaries to test. Pass the --binaries flag.") | ||
} | ||
for _, b := range binariesToTest { | ||
cmd := path.Join(*releasedir, b) | ||
t.Run(b, func(t *testing.T) { | ||
out, err := exec.Command(cmd, "version", "-ojson").Output() | ||
if err != nil { | ||
t.Fatalf("Version failed with error: %v. Output: %v", err, string(out)) | ||
} | ||
|
||
var resp version.Version | ||
if err := json.Unmarshal(out, &resp); err != nil { | ||
t.Fatalf("Failed to marshal to json: %v. Output: %v", err, string(out)) | ||
} | ||
|
||
verInfo := resp.ClientVersion | ||
|
||
validateField(t, "Version", verInfo.Version) | ||
validateField(t, "GitRevision", verInfo.GitRevision) | ||
validateField(t, "User", verInfo.User) | ||
validateField(t, "Host", verInfo.Host) | ||
validateField(t, "GolangVersion", verInfo.GolangVersion) | ||
validateField(t, "DockerHub", verInfo.DockerHub) | ||
validateField(t, "BuildStatus", verInfo.BuildStatus) | ||
validateField(t, "GitTag", verInfo.GitTag) | ||
}) | ||
} | ||
} | ||
|
||
// TODO we may be able to do more validation of fields here, but because it changes based on the environment this is not easy | ||
// For now ensuring the fields even get populated is most important. | ||
func validateField(t *testing.T, field, s string) { | ||
t.Helper() | ||
if s == "unknown" || s == "" { | ||
t.Errorf("Field %v was invalid. Got: %v.", field, s) | ||
} | ||
} |