Skip to content

Commit

Permalink
Display insecure in the version when building without TLS (#1378)
Browse files Browse the repository at this point in the history
* adding insecure to version
* correct order for tags in the non-prerelease case
* only display insecure on insecure build tags
* handling pre-releases correctly
* adding tests
  • Loading branch information
AnomalRoil authored Jul 16, 2024
1 parent 8f4c7ca commit 1bbc15a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 149,7 @@ build:

# create the "drand" binary in the current folder without TLS connections, useful for tests.
build_insecure:
go build -tags conn_insecure -o drand -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)" ./cmd/drand
go build -tags conn_insecure -o drand -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(VER_PACKAGE).BUILDTAGS=insecure -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)" ./cmd/drand

build_all: drand

Expand Down
9 changes: 7 additions & 2 deletions common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,7 @@ package common
import (
"fmt"
"os"
"strings"

pbcommon "github.com/drand/drand/v2/protobuf/drand"
)
Expand All @@ -13,7 14,7 @@ import (
var version = Version{
Major: 2,
Minor: 0,
Patch: 1,
Patch: 2,
Prerelease: "",
}

Expand All @@ -25,6 26,7 @@ var version = Version{
var (
COMMIT = ""
BUILDDATE = ""
BUILDTAGS = ""
)

func GetAppVersion() Version {
Expand Down Expand Up @@ -64,8 66,11 @@ func (v Version) ToProto() *pbcommon.NodeVersion {

func (v Version) String() string {
pre := ""
if strings.Contains(BUILDTAGS, "insecure") {
pre = "-insecure"
}
if v.Prerelease != "" {
pre = "-"
pre = "-"
}
return fmt.Sprintf("%d.%d.%d%s%s", v.Major, v.Minor, v.Patch, pre, v.Prerelease)
}
61 changes: 61 additions & 0 deletions common/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 182,64 @@ func TestVersionCompatible(tm *testing.T) {
})
}
}

func TestVersionBuildTags(t *testing.T) {
for _, tt := range []struct {
version Version
expected string
buildtags string
shouldWork bool
}{
{
version123pre,
"1.2.3-pre",
"",
true,
},
{
version123pre,
"1.2.3-pre",
"conn_insecure",
false,
},
{
version123pre,
"1.2.3-insecure-pre",
"conn_insecure",
true,
},
{
version123,
"1.2.3-insecure",
"conn_insecure",
true,
},
{
version205,
"2.0.5",
"conn_insecure",
false,
},
{
version205,
"2.0.5-insecure",
"conn_insecure",
true,
},
{
version200pre,
"2.0.0-pre",
"verysecure",
true,
},
} {
BUILDTAGS = tt.buildtags
actual := tt.version.String()
if actual != tt.expected && tt.shouldWork {
t.Fatalf("Incorrect version string. Actual: %s, expected: %s", actual, tt.expected)
}
if actual == tt.expected && !tt.shouldWork {
t.Fatalf("Incorrect matching string. Actual: %s, expected: %s", actual, tt.expected)
}
}
}

0 comments on commit 1bbc15a

Please sign in to comment.