Skip to content

Commit

Permalink
remove validOpenYurtVersions from yurtctl test
Browse files Browse the repository at this point in the history
  • Loading branch information
Congrool committed Sep 23, 2022
1 parent 2372076 commit 3331b90
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 42 deletions.
32 changes: 29 additions & 3 deletions hack/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,39 @@ project_info() {
echo "-X ${PROJECT_INFO_PKG}.gitCommit=${GIT_COMMIT}"
echo "-X ${PROJECT_INFO_PKG}.buildDate=${BUILD_DATE}"

ALL_VERSIONS=$(git for-each-ref refs/tags | awk '{print $3}' | awk -F '/' '{print $3}' | tr "\n" ",")
ALL_VERSIONS=${ALL_VERSIONS::-1} # remove last redundant comma
echo "-X ${PROJECT_INFO_PKG}.allVersions=${ALL_VERSIONS}"
maintainingVersions=$(get_maintained_versions | tr " " ",")
versionSeparator=","
echo "-X ${PROJECT_INFO_PKG}.separator=${versionSeparator}"
echo "-X ${PROJECT_INFO_PKG}.maintainingVersions=${maintainingVersions}"
}

# get_binary_dir_with_arch generated the binary's directory with GOOS and GOARCH.
# eg: ./_output/bin/darwin/arm64/
get_binary_dir_with_arch(){
echo $1/$(go env GOOS)/$(go env GOARCH)
}

# get openyurt versions we still maintain
# returned versions are separated by space
get_maintained_versions() {
# we currently maintain latest 3 versions including all their maintaince releases,
# such as v1.0.0-rc1 v1.0.0 v0.7.0 v0.7.1 v0.6.0 v0.6.1
MAINTAINED_VERSION_NUM=${MAINTAINED_VERSION_NUM:-3}
allVersions=$(git for-each-ref refs/tags --sort=authordate | awk '{print $3}' | awk -F '/' '{print $3}')
latestVersion=$(git for-each-ref refs/tags --sort=authordate | awk 'END{print}' |awk '{print $3}' | awk -F '/' '{print $3}')
major=$(echo $latestVersion | awk -F '.' '{print $1}')
major=${major#v}
minor=$(echo $latestVersion | awk -F '.' '{print $2}')
versions=""

for ((cnt=0;cnt<$MAINTAINED_VERSION_NUM;cnt++)); do
versions+=" "$(echo $allVersions | tr " " "\n" | grep -E "v$major\.$minor\..*")
if [ $minor -eq 0 ]; then
major=$[$major-1]
minor=$(echo $allVersions | tr " " "\n" | grep -E -o "v$major\.[0-9]+\..*" | awk 'END{print}' | awk -F '.' '{print $2}')
else
minor=$[$minor-1]
fi
done
echo $versions
}
15 changes: 8 additions & 7 deletions pkg/projectinfo/projectinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
)

var (
projectPrefix = "yurt"
labelPrefix = "openyurt.io"
gitVersion = "v0.0.0"
gitCommit = "unknown"
buildDate = "1970-01-01T00:00:00Z"
allVersions = "unknown"
projectPrefix = "yurt"
labelPrefix = "openyurt.io"
gitVersion = "v0.0.0"
gitCommit = "unknown"
buildDate = "1970-01-01T00:00:00Z"
maintainingVersions = "unknown"
separator = ","
)

func ShortAgentVersion() string {
Expand Down Expand Up @@ -133,6 +134,6 @@ func Get() Info {
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
AllVersions: strings.Split(allVersions, ","),
AllVersions: strings.Split(maintainingVersions, separator),
}
}
15 changes: 4 additions & 11 deletions pkg/yurtctl/cmd/yurttest/kindinit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/klog/v2"

nodeservant "github.com/openyurtio/openyurt/pkg/node-servant"
"github.com/openyurtio/openyurt/pkg/projectinfo"
strutil "github.com/openyurtio/openyurt/pkg/util/strings"
tmplutil "github.com/openyurtio/openyurt/pkg/util/templates"
"github.com/openyurtio/openyurt/pkg/yurtctl/constants"
Expand All @@ -53,19 +54,11 @@ var (
"v1.22",
"v1.23",
}
validOpenYurtVersions = []string{
"v0.5.0",
"v0.6.0",
"v0.6.1",
"v0.7.0",
"v0.7.1",
"v1.0.0",
"latest",
}
validKindVersions = []string{
"v0.11.1",
"v0.12.0",
}
AllValidOpenYurtVersions = append(projectinfo.Get().AllVersions, "latest")

kindNodeImageMap = map[string]map[string]string{
"v0.11.1": {
Expand Down Expand Up @@ -656,9 +649,9 @@ func validateKubernetesVersion(ver string) error {
}

func validateOpenYurtVersion(ver string, ignoreError bool) error {
if !strutil.IsInStringLst(validOpenYurtVersions, ver) && !ignoreError {
if !strutil.IsInStringLst(AllValidOpenYurtVersions, ver) && !ignoreError {
return fmt.Errorf("%s is not a valid openyurt version, all valid versions are %s. If you know what you're doing, you can set --ignore-error",
ver, strings.Join(validOpenYurtVersions, ","))
ver, strings.Join(AllValidOpenYurtVersions, ","))
}
return nil
}
Expand Down
44 changes: 23 additions & 21 deletions pkg/yurtctl/cmd/yurttest/kindinit/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"os"
"os/exec"
"strings"
"testing"

v1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -75,35 +74,30 @@ func TestValidateOpenYurtVersion(t *testing.T) {
cases := map[string]struct {
version string
ignore bool
want string
wantErr bool
}{
"valid": {
"v0.6.0",
false,
"",
false,
},
"unsupported": {
"0.5.10",
false,
fmt.Sprintf("0.5.10 is not a valid openyurt version, all valid versions are %s. If you know what you're doing, you can set --ignore-error",
strings.Join(validOpenYurtVersions, ",")),
true,
},
"ignoreError": {
"0.5.10",
true,
"",
false,
},
}
for name, c := range cases {
err := validateOpenYurtVersion(c.version, c.ignore)
if err == nil {
if c.want != "" {
t.Errorf("validateOpenYurtVersion failed at case %s, want: %s, got: nil", name, c.want)
if c.wantErr {
t.Errorf("validateOpenYurtVersion failed at case %s, wantErr: %v, got: nil", name, c.wantErr)
}
continue
}
if err.Error() != c.want {
t.Errorf("validateOpenYurtVersion failed at case %s, want: %s, got: %s", name, c.want, err.Error())
}
}
}
Expand Down Expand Up @@ -190,6 +184,7 @@ nodes:
}

func TestKindOptions_Validate(t *testing.T) {
AllValidOpenYurtVersions = []string{"v0.6.0", "v0.7.0"}
cases1 := []struct {
nodeNum int
kubernetesVersion string
Expand Down Expand Up @@ -217,10 +212,10 @@ func TestKindOptions_Validate(t *testing.T) {
{
3,
"v1.22",
"v0.1.0",
"v0.0.0",
false,
true,
"v0.1.0 is not a valid openyurt version",
"v0.0.0 is not a valid openyurt version",
},
}

Expand All @@ -229,28 +224,35 @@ func TestKindOptions_Validate(t *testing.T) {
kubernetesVersion string
openyurtVersion string
ignoreErr bool
want error
wantErr bool
}{
{
2,
"v1.22",
"v0.6.0",
false,
nil,
false,
},
{
2,
"v1.22",
"v0.1.0",
"v0.6.0",
true,
nil,
false,
},
{
1,
"v1.22",
"v0.100.0",
true,
nil,
false,
},
{
1,
"v1.22",
"v0.100.0",
false,
true,
},
}

Expand All @@ -261,7 +263,7 @@ func TestKindOptions_Validate(t *testing.T) {
o.OpenYurtVersion = v.openyurtVersion
o.IgnoreError = v.ignoreErr
err := o.Validate()
if v.wantErr && err == nil {
if (v.wantErr && err == nil) || (!v.wantErr && err != nil) {
t.Errorf("failed vaildate")
}
}
Expand All @@ -272,7 +274,7 @@ func TestKindOptions_Validate(t *testing.T) {
o.OpenYurtVersion = v.openyurtVersion
o.IgnoreError = v.ignoreErr
err := o.Validate()
if err != v.want {
if (v.wantErr && err == nil) || (!v.wantErr && err != nil) {
t.Errorf("failed vaildate")
}
}
Expand Down

0 comments on commit 3331b90

Please sign in to comment.