Skip to content

Commit

Permalink
test: move some of these test helpers to new homes without changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rboyer committed Nov 2, 2023
1 parent 815c52a commit 0ca9619
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 226 deletions.
5 changes: 3 additions & 2 deletions test-integ/peering_commontopo/ac7_1_rotate_gw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"strings"
"testing"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testing/deployer/topology"
"github.com/stretchr/testify/require"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/test-integ/topoutil"
)

// TestRotateGW ensures that peered services continue to be able to talk to their
Expand Down Expand Up @@ -145,7 +146,7 @@ func (s *suiteRotateGW) setup(t *testing.T, ct *commonTopo) {
if clu.Datacenter == agentlessDC {
nodeKind = topology.NodeKindDataplane
}
clu.Nodes = append(clu.Nodes, newTopologyMeshGatewaySet(
clu.Nodes = append(clu.Nodes, topoutil.NewTopologyMeshGatewaySet(
nodeKind,
"default",
s.newMGWNodeName,
Expand Down
219 changes: 21 additions & 198 deletions test-integ/peering_commontopo/commontopo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@
package peering

import (
"bytes"
"context"
"fmt"
"strconv"
"testing"
"text/tabwriter"
"time"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
"github.com/hashicorp/consul/testing/deployer/sprawl"
"github.com/hashicorp/consul/testing/deployer/sprawl/sprawltest"
"github.com/hashicorp/consul/testing/deployer/topology"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
"github.com/hashicorp/consul/test-integ/topoutil"
)

// commonTopo helps create a shareable topology configured to represent
Expand All @@ -46,7 +44,7 @@ type commonTopo struct {

// set after Launch. Should be considered read-only
Sprawl *sprawl.Sprawl
Assert *asserter
Assert *topoutil.Asserter

// track per-DC services to prevent duplicates
services map[string]map[topology.ServiceID]struct{}
Expand Down Expand Up @@ -122,14 +120,14 @@ func (ct *commonTopo) Launch(t *testing.T) {
}
ct.Sprawl = sprawltest.Launch(t, ct.Cfg)

ct.Assert = newAsserter(ct.Sprawl)
ct.Assert = topoutil.NewAsserter(ct.Sprawl)
ct.postLaunchChecks(t)
}

// tests that use Relaunch might want to call this again afterwards
func (ct *commonTopo) postLaunchChecks(t *testing.T) {
t.Logf("TESTING RELATIONSHIPS: \n%s",
renderRelationships(computeRelationships(ct.Sprawl.Topology())),
topology.RenderRelationships(ct.Sprawl.Topology().ComputeRelationships()),
)

// check that exports line up as expected
Expand Down Expand Up @@ -317,41 +315,23 @@ func ConfigEntryPartition(p string) string {
return p
}

// disableNode is a no-op if the node is already disabled.
// DisableNode is a no-op if the node is already disabled.
func DisableNode(t *testing.T, cfg *topology.Config, clusterName string, nid topology.NodeID) *topology.Config {
nodes := cfg.Cluster(clusterName).Nodes
var found bool
for _, n := range nodes {
if n.ID() == nid {
found = true
if n.Disabled {
return cfg
}
t.Logf("disabling node %s in cluster %s", nid.String(), clusterName)
n.Disabled = true
break
}
changed, err := cfg.DisableNode(clusterName, nid)
require.NoError(t, err)
if changed {
t.Logf("disabling node %s in cluster %s", nid.String(), clusterName)
}
require.True(t, found, "expected to find nodeID %q in cluster %q", nid.String(), clusterName)
return cfg
}

// enableNode is a no-op if the node is already enabled.
// EnableNode is a no-op if the node is already enabled.
func EnableNode(t *testing.T, cfg *topology.Config, clusterName string, nid topology.NodeID) *topology.Config {
nodes := cfg.Cluster(clusterName).Nodes
var found bool
for _, n := range nodes {
if n.ID() == nid {
found = true
if !n.Disabled {
return cfg
}
t.Logf("enabling node %s in cluster %s", nid.String(), clusterName)
n.Disabled = false
break
}
changed, err := cfg.EnableNode(clusterName, nid)
require.NoError(t, err)
if changed {
t.Logf("enabling node %s in cluster %s", nid.String(), clusterName)
}
require.True(t, found, "expected to find nodeID %q in cluster %q", nid.String(), clusterName)
return cfg
}

Expand Down Expand Up @@ -386,7 +366,7 @@ func addMeshGateways(c *topology.Cluster) {
nodeKind = topology.NodeKindDataplane
}
for _, p := range c.Partitions {
c.Nodes = topology.MergeSlices(c.Nodes, newTopologyMeshGatewaySet(
c.Nodes = topology.MergeSlices(c.Nodes, topoutil.NewTopologyMeshGatewaySet(
nodeKind,
p.Name,
fmt.Sprintf("%s-%s-mgw", c.Name, p.Name),
Expand All @@ -402,7 +382,7 @@ func clusterWithJustServers(name string, numServers int) *topology.Cluster {
Enterprise: utils.IsEnterprise(),
Name: name,
Datacenter: name,
Nodes: newTopologyServerSet(
Nodes: topoutil.NewTopologyServerSet(
name+"-server",
numServers,
[]string{name},
Expand Down Expand Up @@ -458,168 +438,11 @@ func injectTenancies(clu *topology.Cluster) {
}
}

func newTopologyServerSet(
namePrefix string,
num int,
networks []string,
mutateFn func(i int, node *topology.Node),
) []*topology.Node {
var out []*topology.Node
for i := 1; i <= num; i++ {
name := namePrefix + strconv.Itoa(i)

node := &topology.Node{
Kind: topology.NodeKindServer,
Name: name,
}
for _, net := range networks {
node.Addresses = append(node.Addresses, &topology.Address{Network: net})
}

if mutateFn != nil {
mutateFn(i, node)
}

out = append(out, node)
}
return out
}

func newTopologyMeshGatewaySet(
nodeKind topology.NodeKind,
partition string,
namePrefix string,
num int,
networks []string,
mutateFn func(i int, node *topology.Node),
) []*topology.Node {
var out []*topology.Node
for i := 1; i <= num; i++ {
name := namePrefix + strconv.Itoa(i)

node := &topology.Node{
Kind: nodeKind,
Partition: partition,
Name: name,
Services: []*topology.Service{{
ID: topology.ServiceID{Name: "mesh-gateway"},
Port: 8443,
EnvoyAdminPort: 19000,
IsMeshGateway: true,
}},
}
for _, net := range networks {
node.Addresses = append(node.Addresses, &topology.Address{Network: net})
}

if mutateFn != nil {
mutateFn(i, node)
}

out = append(out, node)
}
return out
}

const HashicorpDockerProxy = "docker.mirror.hashicorp.services"

// Deprecated: topoutil.NewFortioServiceWithDefaults
func NewFortioServiceWithDefaults(
cluster string,
sid topology.ServiceID,
mut func(s *topology.Service),
) *topology.Service {
const (
httpPort = 8080
grpcPort = 8079
adminPort = 19000
)
sid.Normalize()

svc := &topology.Service{
ID: sid,
Image: HashicorpDockerProxy + "/fortio/fortio",
Port: httpPort,
EnvoyAdminPort: adminPort,
CheckTCP: "127.0.0.1:" + strconv.Itoa(httpPort),
Env: []string{
"FORTIO_NAME=" + cluster + "::" + sid.String(),
},
Command: []string{
"server",
"-http-port", strconv.Itoa(httpPort),
"-grpc-port", strconv.Itoa(grpcPort),
"-redirect-port", "-disabled",
},
}
if mut != nil {
mut(svc)
}
return svc
}

// computeRelationships will analyze a full topology and generate all of the
// downstream/upstream information for all of them.
func computeRelationships(topo *topology.Topology) []Relationship {
var out []Relationship
for _, cluster := range topo.Clusters {
for _, n := range cluster.Nodes {
for _, s := range n.Services {
for _, u := range s.Upstreams {
out = append(out, Relationship{
Caller: s,
Upstream: u,
})
}
}
}
}
return out
}

// renderRelationships will take the output of ComputeRelationships and display
// it in tabular form.
func renderRelationships(ships []Relationship) string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', tabwriter.Debug)
fmt.Fprintf(w, "DOWN\tnode\tservice\tport\tUP\tservice\t\n")
for _, r := range ships {
fmt.Fprintf(w,
"%s\t%s\t%s\t%d\t%s\t%s\t\n",
r.downCluster(),
r.Caller.Node.ID().String(),
r.Caller.ID.String(),
r.Upstream.LocalPort,
r.upCluster(),
r.Upstream.ID.String(),
)
}
fmt.Fprintf(w, "\t\t\t\t\t\t\n")

w.Flush()
return buf.String()
}

type Relationship struct {
Caller *topology.Service
Upstream *topology.Upstream
}

func (r Relationship) String() string {
return fmt.Sprintf(
"%s on %s in %s via :%d => %s in %s",
r.Caller.ID.String(),
r.Caller.Node.ID().String(),
r.downCluster(),
r.Upstream.LocalPort,
r.Upstream.ID.String(),
r.upCluster(),
)
}

func (r Relationship) downCluster() string {
return r.Caller.Node.Cluster
}

func (r Relationship) upCluster() string {
return r.Upstream.Cluster
return topoutil.NewFortioServiceWithDefaults(cluster, sid, mut)
}
Loading

0 comments on commit 0ca9619

Please sign in to comment.