Skip to content

Commit

Permalink
test: fix v2 timing issue with partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
DanStough committed Oct 13, 2023
1 parent 53345f3 commit ea645bc
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func TestMeshConfigController_createsMeshConfig(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

r := c.reconciler(fakeClient, testClient.Cfg, testClient.Watcher, logrtest.New(t))
namespacedName := types.NamespacedName{
Namespace: metav1.NamespaceDefault,
Expand Down Expand Up @@ -284,6 +292,15 @@ func TestMeshConfigController_updatesMeshConfig(t *testing.T) {
})
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// We haven't run reconcile yet, so we must create the MeshConfig
// in Consul ourselves.
{
Expand Down Expand Up @@ -400,6 +417,14 @@ func TestMeshConfigController_deletesMeshConfig(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// We haven't run reconcile yet, so we must create the config entry
// in Consul ourselves.
{
Expand Down Expand Up @@ -466,6 +491,14 @@ func TestMeshConfigController_errorUpdatesSyncStatus(t *testing.T) {
c.Experiments = []string{"resource-apis"}
})

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Stop the server before calling reconcile imitating a server that's not running.
_ = testClient.TestServer.Stop()

Expand Down Expand Up @@ -547,6 +580,14 @@ func TestMeshConfigController_setsSyncedToTrue(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

reconciler := &TrafficPermissionsController{
Client: fakeClient,
Log: logrtest.New(t),
Expand Down Expand Up @@ -622,6 +663,14 @@ func TestMeshConfigController_doesNotCreateUnownedMeshConfig(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

unmanagedResource := trafficpermissions.Resource(constants.DefaultConsulNS, constants.DefaultConsulPartition)
unmanagedResource.Metadata = make(map[string]string) // Zero out the metadata

Expand Down Expand Up @@ -725,6 +774,14 @@ func TestMeshConfigController_doesNotDeleteUnownedConfig(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

reconciler := &TrafficPermissionsController{
Client: fakeClient,
Log: logrtest.New(t),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ package endpointsv2
import (
"context"
"fmt"
"testing"
"time"

mapset "github.com/deckarep/golang-set"
logrtest "github.com/go-logr/logr/testr"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
inject "github.com/hashicorp/consul-k8s/control-plane/connect-inject/common"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
"github.com/hashicorp/consul-k8s/control-plane/consul"
"github.com/hashicorp/consul-k8s/control-plane/helper/test"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/sdk/testutil"
Expand All @@ -31,7 +29,12 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
inject "github.com/hashicorp/consul-k8s/control-plane/connect-inject/common"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
"github.com/hashicorp/consul-k8s/control-plane/consul"
"github.com/hashicorp/consul-k8s/control-plane/helper/test"
)

const (
Expand Down Expand Up @@ -2193,6 +2196,14 @@ func runReconcileCase(t *testing.T, tc reconcileCase) {
c.Experiments = []string{"resource-apis"}
})

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the Endpoints controller.
ep := &Controller{
Client: fakeClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package pod
import (
"context"
"testing"
"time"

mapset "github.com/deckarep/golang-set"
logrtest "github.com/go-logr/logr/testr"
Expand Down Expand Up @@ -674,6 +675,14 @@ func runControllerTest(t *testing.T, tc testCase) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the partition in Consul.
if tc.partition != "" {
testClient.Cfg.APIClientConfig.Partition = tc.partition
Expand Down
102 changes: 101 additions & 1 deletion control-plane/connect-inject/controllers/pod/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ func TestWorkloadWrite(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -307,6 +315,14 @@ func TestWorkloadDelete(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -391,6 +407,14 @@ func TestHealthStatusWrite(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -507,6 +531,14 @@ func TestProxyConfigurationWrite(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -602,6 +634,9 @@ func TestProxyConfigurationWrite(t *testing.T) {
},
},
},
TransparentProxy: &pbmesh.TransparentProxy{
OutboundListenerPort: 15001,
},
},
BootstrapConfig: &pbmesh.BootstrapConfig{
PrometheusBindAddr: "0.0.0.0:5678",
Expand Down Expand Up @@ -645,6 +680,9 @@ func TestProxyConfigurationWrite(t *testing.T) {
},
},
},
TransparentProxy: &pbmesh.TransparentProxy{
OutboundListenerPort: 15001,
},
},
BootstrapConfig: &pbmesh.BootstrapConfig{
PrometheusBindAddr: "0.0.0.0:21234",
Expand All @@ -663,6 +701,9 @@ func TestProxyConfigurationWrite(t *testing.T) {
},
DynamicConfig: &pbmesh.DynamicConfig{
Mode: pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT,
TransparentProxy: &pbmesh.TransparentProxy{
OutboundListenerPort: 15001,
},
},
},
},
Expand Down Expand Up @@ -700,6 +741,14 @@ func TestProxyConfigurationDelete(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -979,6 +1028,14 @@ func TestDestinationsWrite(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

pc := &Controller{
Log: logrtest.New(t),
K8sNamespaceConfig: common.K8sNamespaceConfig{
Expand Down Expand Up @@ -1062,6 +1119,14 @@ func TestDestinationsDelete(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

pc := &Controller{
Log: logrtest.New(t),
K8sNamespaceConfig: common.K8sNamespaceConfig{
Expand Down Expand Up @@ -1147,6 +1212,14 @@ func TestReconcileCreatePod(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -1343,6 +1416,14 @@ func TestReconcileUpdatePod(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -1499,6 +1580,9 @@ func TestReconcileUpdatePod(t *testing.T) {
},
},
},
TransparentProxy: &pbmesh.TransparentProxy{
OutboundListenerPort: 15001,
},
},
BootstrapConfig: &pbmesh.BootstrapConfig{
PrometheusBindAddr: "0.0.0.0:21234",
Expand Down Expand Up @@ -1604,6 +1688,14 @@ func TestReconcileDeletePod(t *testing.T) {
resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher)
require.NoError(t, err)

require.Eventually(t, func() bool {
_, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil)
if err != nil {
return false
}
return true
}, 5*time.Second, 500*time.Millisecond)

// Create the pod controller.
pc := &Controller{
Client: fakeClient,
Expand Down Expand Up @@ -1835,7 +1927,7 @@ func createCriticalHealthStatus(name string, namespace string) *pbcatalog.Health
// createProxyConfiguration creates a proxyConfiguration that matches the pod from createPod,
// assuming that metrics, telemetry, and overwrite probes are enabled separately.
func createProxyConfiguration(podName string, mode pbmesh.ProxyMode) *pbmesh.ProxyConfiguration {
return &pbmesh.ProxyConfiguration{
mesh := &pbmesh.ProxyConfiguration{
Workloads: &pbcatalog.WorkloadSelector{
Names: []string{podName},
},
Expand Down Expand Up @@ -1866,6 +1958,14 @@ func createProxyConfiguration(podName string, mode pbmesh.ProxyMode) *pbmesh.Pro
TelemetryCollectorBindSocketDir: DefaultTelemetryBindSocketDir,
},
}

if mode == pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT {
mesh.DynamicConfig.TransparentProxy = &pbmesh.TransparentProxy{
OutboundListenerPort: 15001,
}
}

return mesh
}

// createCriticalHealthStatus creates a failing HealthStatus that matches the pod from createPod.
Expand Down
Loading

0 comments on commit ea645bc

Please sign in to comment.