2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2023-05-11 22:49:49 +00:00
|
|
|
"strings"
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-04-18 13:49:53 +00:00
|
|
|
targetImageName string
|
2023-01-11 21:34:27 +00:00
|
|
|
TargetVersion string
|
|
|
|
|
|
|
|
LatestImageName string
|
|
|
|
LatestVersion string
|
|
|
|
|
2023-04-18 13:49:53 +00:00
|
|
|
FollowLog bool
|
|
|
|
|
|
|
|
Debug bool
|
2023-02-03 15:20:22 +00:00
|
|
|
Version_1_14, _ = version.NewVersion("1.14")
|
2023-01-11 21:34:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-07-14 21:13:41 +00:00
|
|
|
DefaultImageNameOSS = "hashicorp/consul"
|
2023-01-11 21:34:27 +00:00
|
|
|
DefaultImageNameENT = "hashicorp/consul-enterprise"
|
|
|
|
ImageVersionSuffixENT = "-ent"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-04-18 13:49:53 +00:00
|
|
|
flag.BoolVar(&Debug, "debug", false, "run consul with dlv to enable live debugging")
|
|
|
|
flag.StringVar(&targetImageName, "target-image", defaultImageName, "docker image name to be used under test (Default: "+defaultImageName+")")
|
2023-01-11 21:34:27 +00:00
|
|
|
flag.StringVar(&TargetVersion, "target-version", "local", "docker image version to be used as UUT (unit under test)")
|
|
|
|
|
|
|
|
flag.StringVar(&LatestImageName, "latest-image", defaultImageName, "docker image name to be used under test (Default: "+defaultImageName+")")
|
|
|
|
flag.StringVar(&LatestVersion, "latest-version", "latest", "docker image to be used as latest")
|
|
|
|
|
|
|
|
flag.BoolVar(&FollowLog, "follow-log", true, "follow container log in output (Default: true)")
|
2023-04-18 13:49:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTargetImageName() string {
|
|
|
|
if Debug {
|
|
|
|
return targetImageName + "-dbg"
|
|
|
|
}
|
|
|
|
return targetImageName
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 16:02:31 +00:00
|
|
|
func GetLatestImageName() string {
|
|
|
|
if Debug {
|
|
|
|
return LatestImageName + "-dbg"
|
|
|
|
}
|
|
|
|
return LatestImageName
|
|
|
|
}
|
|
|
|
|
2023-05-16 19:57:24 +00:00
|
|
|
func IsEnterprise() bool { return isInEnterpriseRepo }
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
func DockerImage(image, version string) string {
|
|
|
|
v := image + ":" + version
|
2023-05-12 17:03:30 +00:00
|
|
|
if strings.Contains(image, DefaultImageNameENT) && isSemVer(version) {
|
2023-01-11 21:34:27 +00:00
|
|
|
// Enterprise versions get a suffix.
|
|
|
|
v += ImageVersionSuffixENT
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSemVer(ver string) bool {
|
|
|
|
_, err := version.NewVersion(ver)
|
|
|
|
return err == nil
|
|
|
|
}
|
2023-03-29 16:51:21 +00:00
|
|
|
|
|
|
|
// ensure version a >= b
|
|
|
|
func VersionGTE(a, b string) bool {
|
|
|
|
av := version.Must(version.NewVersion(a))
|
|
|
|
bv := version.Must(version.NewVersion(b))
|
|
|
|
return av.GreaterThanOrEqual(bv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure version a < b
|
|
|
|
func VersionLT(a, b string) bool {
|
|
|
|
av := version.Must(version.NewVersion(a))
|
|
|
|
bv := version.Must(version.NewVersion(b))
|
|
|
|
return av.LessThan(bv)
|
|
|
|
}
|
2023-05-11 22:49:49 +00:00
|
|
|
|
|
|
|
// SideCarVersion returns version based on the agent
|
|
|
|
// version in the test: if agent has local, the sidecar
|
|
|
|
// version is target-version; otherwise use "latest-version"
|
|
|
|
func SideCarVersion(agentVersion string) string {
|
|
|
|
imageVersion := ""
|
|
|
|
if strings.Contains(agentVersion, "local") {
|
|
|
|
imageVersion = "target-version"
|
|
|
|
} else {
|
|
|
|
imageVersion = "latest-version"
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageVersion
|
|
|
|
}
|