From 4f202a6429660b443f66de0c6dcceaafbd4551c5 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Mon, 13 Jun 2022 13:17:18 +0800 Subject: [PATCH] Define all versions in a centralized place The versions such as V3_5, V3_6, etc. are defined everywhere in the repo. In this commit, we get all of the constant versions defined in a centralized place, so that they can be reused by all cases. --- api/go.mod | 4 ++ api/go.sum | 5 +++ api/version/version.go | 25 +++++++++++ api/version/version_test.go | 85 +++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 api/version/version_test.go diff --git a/api/go.mod b/api/go.mod index 39d24b22a..1af09c097 100644 --- a/api/go.mod +++ b/api/go.mod @@ -7,16 +7,20 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.2 github.com/grpc-ecosystem/grpc-gateway v1.16.0 + github.com/stretchr/testify v1.7.0 google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 google.golang.org/grpc v1.41.0 ) require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect golang.org/x/text v0.3.5 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) // Bad imports are sometimes causing attempts to pull that code. diff --git a/api/go.sum b/api/go.sum index e0d292fcd..106f5fd97 100644 --- a/api/go.sum +++ b/api/go.sum @@ -13,6 +13,7 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -52,12 +53,14 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4 github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -148,11 +151,13 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/api/version/version.go b/api/version/version.go index 6dce4d8ea..bd39791a9 100644 --- a/api/version/version.go +++ b/api/version/version.go @@ -33,6 +33,19 @@ var ( GitSHA = "Not provided (use ./build instead of go build)" ) +// Get all constant versions defined in a centralized place. +var ( + V3_0 = semver.Version{Major: 3, Minor: 0} + V3_1 = semver.Version{Major: 3, Minor: 1} + V3_2 = semver.Version{Major: 3, Minor: 2} + V3_3 = semver.Version{Major: 3, Minor: 3} + V3_4 = semver.Version{Major: 3, Minor: 4} + V3_5 = semver.Version{Major: 3, Minor: 5} + V3_6 = semver.Version{Major: 3, Minor: 6} + V3_7 = semver.Version{Major: 3, Minor: 7} + V4_0 = semver.Version{Major: 4, Minor: 0} +) + func init() { ver, err := semver.NewVersion(Version) if err == nil { @@ -55,3 +68,15 @@ func Cluster(v string) string { } return fmt.Sprintf("%s.%s", vs[0], vs[1]) } + +func Compare(ver1, ver2 semver.Version) int { + return ver1.Compare(ver2) +} + +func LessThan(ver1, ver2 semver.Version) bool { + return ver1.LessThan(ver2) +} + +func Equal(ver1, ver2 semver.Version) bool { + return ver1.Equal(ver2) +} diff --git a/api/version/version_test.go b/api/version/version_test.go new file mode 100644 index 000000000..532e7525a --- /dev/null +++ b/api/version/version_test.go @@ -0,0 +1,85 @@ +// Copyright 2022 The etcd 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 ( + "testing" + + "github.com/coreos/go-semver/semver" + "github.com/stretchr/testify/assert" +) + +func TestVersionCompare(t *testing.T) { + cases := []struct { + name string + ver1 semver.Version + ver2 semver.Version + expectedCompareResult int + expectedLessThanResult bool + expectedEqualResult bool + }{ + { + name: "ver1 should be great than ver2", + ver1: V3_5, + ver2: V3_4, + expectedCompareResult: 1, + expectedLessThanResult: false, + expectedEqualResult: false, + }, + { + name: "ver1(4.0) should be great than ver2", + ver1: V4_0, + ver2: V3_7, + expectedCompareResult: 1, + expectedLessThanResult: false, + expectedEqualResult: false, + }, + { + name: "ver1 should be less than ver2", + ver1: V3_5, + ver2: V3_6, + expectedCompareResult: -1, + expectedLessThanResult: true, + expectedEqualResult: false, + }, + { + name: "ver1 should be less than ver2 (4.0)", + ver1: V3_5, + ver2: V4_0, + expectedCompareResult: -1, + expectedLessThanResult: true, + expectedEqualResult: false, + }, + { + name: "ver1 should be equal to ver2", + ver1: V3_5, + ver2: V3_5, + expectedCompareResult: 0, + expectedLessThanResult: false, + expectedEqualResult: true, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + compareResult := Compare(tc.ver1, tc.ver2) + lessThanResult := LessThan(tc.ver1, tc.ver2) + equalResult := Equal(tc.ver1, tc.ver2) + + assert.Equal(t, tc.expectedCompareResult, compareResult) + assert.Equal(t, tc.expectedLessThanResult, lessThanResult) + assert.Equal(t, tc.expectedEqualResult, equalResult) + }) + } +}