R.B. Boyer a72f868218
testing/deployer: update deployer to use v2 catalog constructs when requested (#19046)
This updates the testing/deployer (aka "topology test") framework to conditionally 
configure and launch catalog constructs using v2 resources. This is controlled via a 
Version field on the Node construct in a topology.Config. This only functions for a 
dataplane type and has other restrictions that match the rest of v2 (no peering, no 
wanfed, no mesh gateways).

Like config entries, you can statically provide a set of initial resources to be synced 
when bringing up the cluster (beyond those that are generated for you such as 
workloads, services, etc).

If you want to author a test that can be freely converted between v1 and v2 then that 
is possible. If you switch to the multi-port definition on a topology.Service (aka 
"workload/instance") then that makes v1 ineligible.

This also adds a starter set of "on every PR" integration tests for single and multiport 
under test-integ/catalogv2
2023-11-02 14:25:48 -05:00

120 lines
2.8 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package utils
import (
"flag"
"os"
"strings"
"github.com/hashicorp/consul/testing/deployer/topology"
"github.com/hashicorp/go-version"
)
var (
targetImageName string
TargetVersion string
LatestImageName string
LatestVersion string
FollowLog bool
Debug bool
Version_1_14, _ = version.NewVersion("1.14")
)
const (
DefaultImageNameCE = "hashicorp/consul"
DefaultImageNameENT = "hashicorp/consul-enterprise"
ImageVersionSuffixENT = "-ent"
)
func init() {
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+")")
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)")
}
func GetTargetImageName() string {
if Debug {
return targetImageName + "-dbg"
}
return targetImageName
}
func GetLatestImageName() string {
if Debug {
return LatestImageName + "-dbg"
}
return LatestImageName
}
func TargetImages() topology.Images {
img := DockerImage(targetImageName, TargetVersion)
var set topology.Images
if IsEnterprise() {
set.ConsulEnterprise = img
} else {
set.ConsulCE = img
}
if cdp := os.Getenv("DEPLOYER_CONSUL_DATAPLANE_IMAGE"); cdp != "" {
set.Dataplane = cdp
}
return set
}
func IsEnterprise() bool { return isInEnterpriseRepo }
func DockerImage(image, version string) string {
v := image + ":" + version
if strings.Contains(image, DefaultImageNameENT) && isSemVer(version) {
// Enterprise versions get a suffix.
v += ImageVersionSuffixENT
}
return v
}
func isSemVer(ver string) bool {
_, err := version.NewVersion(ver)
return err == nil
}
// 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)
}
// 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
}