2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2023-11-02 19:25:48 +00:00
|
|
|
"os"
|
2023-05-11 22:49:49 +00:00
|
|
|
"strings"
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-07-18 23:41:30 +00:00
|
|
|
"github.com/hashicorp/consul/testing/deployer/topology"
|
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-08-22 14:46:03 +00:00
|
|
|
DefaultImageNameCE = "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-11-16 00:32:37 +00:00
|
|
|
func LatestImages() topology.Images {
|
|
|
|
img := DockerImage(LatestImageName, LatestVersion)
|
|
|
|
|
|
|
|
var set topology.Images
|
|
|
|
if IsEnterprise() {
|
|
|
|
set.ConsulEnterprise = img
|
|
|
|
} else {
|
|
|
|
set.ConsulCE = img
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: have a "latest" dataplane image for testing a service mesh
|
|
|
|
// complete upgrade of data plane
|
|
|
|
if cdp := os.Getenv("DEPLOYER_CONSUL_DATAPLANE_IMAGE"); cdp != "" {
|
|
|
|
set.Dataplane = cdp
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2023-07-18 23:41:30 +00:00
|
|
|
func TargetImages() topology.Images {
|
|
|
|
img := DockerImage(targetImageName, TargetVersion)
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
var set topology.Images
|
2023-07-18 23:41:30 +00:00
|
|
|
if IsEnterprise() {
|
2023-11-02 19:25:48 +00:00
|
|
|
set.ConsulEnterprise = img
|
2023-07-18 23:41:30 +00:00
|
|
|
} else {
|
2023-11-02 19:25:48 +00:00
|
|
|
set.ConsulCE = img
|
2023-07-18 23:41:30 +00:00
|
|
|
}
|
2023-11-02 19:25:48 +00:00
|
|
|
|
2023-11-16 00:32:37 +00:00
|
|
|
// TODO: have a "target" dataplane image for testing a service mesh
|
|
|
|
// complete upgrade of data plane
|
2023-11-02 19:25:48 +00:00
|
|
|
if cdp := os.Getenv("DEPLOYER_CONSUL_DATAPLANE_IMAGE"); cdp != "" {
|
|
|
|
set.Dataplane = cdp
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
2023-07-18 23:41:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|