diff --git a/control-plane/connect-inject/controllers/serviceaccount/serviceaccount_controller_test.go b/control-plane/connect-inject/controllers/serviceaccount/serviceaccount_controller_test.go index d2ea94c22d..27bb909d2c 100644 --- a/control-plane/connect-inject/controllers/serviceaccount/serviceaccount_controller_test.go +++ b/control-plane/connect-inject/controllers/serviceaccount/serviceaccount_controller_test.go @@ -6,7 +6,6 @@ package serviceaccount import ( "context" "testing" - "time" "github.com/google/go-cmp/cmp" "google.golang.org/protobuf/proto" @@ -30,7 +29,6 @@ import ( "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" ) @@ -226,13 +224,6 @@ func runReconcileCase(t *testing.T, tc reconcileCase) { DenyK8sNamespacesSet: mapset.NewSetWith(), }, } - resourceClient, err := consul.NewResourceServiceClient(sa.ConsulServerConnMgr) - require.NoError(t, err) - - require.Eventually(t, func() bool { - _, _, err := testClient.APIClient.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil) - return err == nil - }, 5*time.Second, 500*time.Millisecond) // Default ns and partition if not specified in test. if tc.targetConsulNs == "" { @@ -245,9 +236,9 @@ func runReconcileCase(t *testing.T, tc reconcileCase) { // If existing resource specified, create it and ensure it exists. if tc.existingResource != nil { writeReq := &pbresource.WriteRequest{Resource: tc.existingResource} - _, err = resourceClient.Write(context.Background(), writeReq) + _, err := testClient.ResourceClient.Write(context.Background(), writeReq) require.NoError(t, err) - test.ResourceHasPersisted(t, context.Background(), resourceClient, tc.existingResource.Id) + test.ResourceHasPersisted(t, context.Background(), testClient.ResourceClient, tc.existingResource.Id) } // Run actual reconcile and verify results. @@ -264,7 +255,7 @@ func runReconcileCase(t *testing.T, tc reconcileCase) { } require.False(t, resp.Requeue) - expectedWorkloadIdentityMatches(t, resourceClient, tc.svcAccountName, tc.targetConsulNs, tc.targetConsulPartition, tc.expectedResource) + expectedWorkloadIdentityMatches(t, testClient.ResourceClient, tc.svcAccountName, tc.targetConsulNs, tc.targetConsulPartition, tc.expectedResource) } func expectedWorkloadIdentityMatches(t *testing.T, client pbresource.ResourceServiceClient, name, namespace, partition string, expectedResource *pbresource.Resource) { diff --git a/control-plane/helper/test/test_util.go b/control-plane/helper/test/test_util.go index 823b7b5fc7..bed3702993 100644 --- a/control-plane/helper/test/test_util.go +++ b/control-plane/helper/test/test_util.go @@ -119,8 +119,8 @@ func TestServerWithMockConnMgrWatcher(t *testing.T, callback testutil.ServerConf // also cause test flakes. require.Eventually(t, func() bool { - _, _, err := client.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil) - return err == nil + partition, _, err := client.Partitions().Read(context.Background(), constants.DefaultConsulPartition, nil) + return err == nil && partition != nil }, eventuallyWaitFor, eventuallyTickEvery, diff --git a/control-plane/subcommand/mesh-init/command_ent_test.go b/control-plane/subcommand/mesh-init/command_ent_test.go index ad3ea8c87d..59c710f6eb 100644 --- a/control-plane/subcommand/mesh-init/command_ent_test.go +++ b/control-plane/subcommand/mesh-init/command_ent_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/require" "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" "github.com/hashicorp/consul-k8s/control-plane/namespaces" ) @@ -52,10 +51,8 @@ func TestRun_WithNamespaces(t *testing.T) { c.Experiments = []string{"resource-apis"} serverCfg = c }) - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) - _, err = EnsurePartitionExists(testClient.APIClient, c.consulPartition) + _, err := EnsurePartitionExists(testClient.APIClient, c.consulPartition) require.NoError(t, err) partitionedCfg := testClient.Cfg.APIClientConfig @@ -68,7 +65,7 @@ func TestRun_WithNamespaces(t *testing.T) { require.NoError(t, err) // Register Consul workload. - loadResource(t, resourceClient, getWorkloadID(testPodName, c.consulNamespace, c.consulPartition), getWorkload(), nil) + loadResource(t, testClient.ResourceClient, getWorkloadID(testPodName, c.consulNamespace, c.consulPartition), getWorkload(), nil) ui := cli.NewMockUi() cmd := Command{ diff --git a/control-plane/subcommand/mesh-init/command_test.go b/control-plane/subcommand/mesh-init/command_test.go index 87f38bf011..90756cee70 100644 --- a/control-plane/subcommand/mesh-init/command_test.go +++ b/control-plane/subcommand/mesh-init/command_test.go @@ -24,7 +24,6 @@ import ( "google.golang.org/protobuf/types/known/anypb" "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" ) @@ -100,11 +99,9 @@ func TestRun_MeshServices(t *testing.T) { c.Experiments = []string{"resource-apis"} serverCfg = c }) - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) - loadResource(t, resourceClient, getWorkloadID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), tt.workload, nil) - loadResource(t, resourceClient, getProxyConfigurationID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), tt.proxyConfiguration, nil) + loadResource(t, testClient.ResourceClient, getWorkloadID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), tt.workload, nil) + loadResource(t, testClient.ResourceClient, getProxyConfigurationID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), tt.proxyConfiguration, nil) ui := cli.NewMockUi() cmd := Command{ @@ -164,8 +161,6 @@ func TestRun_RetryServicePolling(t *testing.T) { c.Experiments = []string{"resource-apis"} serverCfg = c }) - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) // Start the consul service registration in a go func and delay it so that it runs // after the cmd.Run() starts. @@ -176,7 +171,7 @@ func TestRun_RetryServicePolling(t *testing.T) { // Wait a moment, this ensures that we are already in the retry logic. time.Sleep(time.Second * 2) // Register counting service. - loadResource(t, resourceClient, getWorkloadID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), getWorkload(), nil) + loadResource(t, testClient.ResourceClient, getWorkloadID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), getWorkload(), nil) }() ui := cli.NewMockUi() @@ -240,16 +235,14 @@ func TestRun_TrafficRedirection(t *testing.T) { c.Experiments = []string{"resource-apis"} serverCfg = c }) - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) // Add additional proxy configuration either to a config entry or to the service itself. if c.registerProxyConfiguration { - loadResource(t, resourceClient, getProxyConfigurationID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), getProxyConfiguration(), nil) + loadResource(t, testClient.ResourceClient, getProxyConfigurationID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), getProxyConfiguration(), nil) } // Register Consul workload. - loadResource(t, resourceClient, getWorkloadID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), getWorkload(), nil) + loadResource(t, testClient.ResourceClient, getWorkloadID(testPodName, constants.DefaultConsulNS, constants.DefaultConsulPartition), getWorkload(), nil) iptablesProvider := &fakeIptablesProvider{} iptablesCfg := iptables.Config{ diff --git a/control-plane/subcommand/partition-init/command_ent_test.go b/control-plane/subcommand/partition-init/command_ent_test.go index 72858243f4..21972a5a7a 100644 --- a/control-plane/subcommand/partition-init/command_ent_test.go +++ b/control-plane/subcommand/partition-init/command_ent_test.go @@ -21,7 +21,6 @@ import ( pbtenancy "github.com/hashicorp/consul/proto-public/pbtenancy/v2beta1" "github.com/hashicorp/consul/sdk/testutil" - "github.com/hashicorp/consul-k8s/control-plane/consul" "github.com/hashicorp/consul-k8s/control-plane/helper/test" ) @@ -96,10 +95,7 @@ func TestRun_PartitionCreate(t *testing.T) { v2tenancy: true, experiments: []string{"resource-apis", "v2tenancy"}, requirePartitionCreated: func(testClient *test.TestServerClient) { - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) - - _, err = resourceClient.Read(context.Background(), &pbresource.ReadRequest{ + _, err := testClient.ResourceClient.Read(context.Background(), &pbresource.ReadRequest{ Id: &pbresource.ID{ Name: partitionName, Type: pbtenancy.PartitionType, @@ -180,13 +176,10 @@ func TestRun_PartitionExists(t *testing.T) { v2tenancy: true, experiments: []string{"resource-apis", "v2tenancy"}, preCreatePartition: func(testClient *test.TestServerClient) { - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) - data, err := anypb.New(&pbtenancy.Partition{Description: partitionDesc}) require.NoError(t, err) - _, err = resourceClient.Write(context.Background(), &pbresource.WriteRequest{ + _, err = testClient.ResourceClient.Write(context.Background(), &pbresource.WriteRequest{ Resource: &pbresource.Resource{ Id: &pbresource.ID{ Name: partitionName, @@ -198,10 +191,7 @@ func TestRun_PartitionExists(t *testing.T) { require.NoError(t, err) }, requirePartitionNotCreated: func(testClient *test.TestServerClient) { - resourceClient, err := consul.NewResourceServiceClient(testClient.Watcher) - require.NoError(t, err) - - rsp, err := resourceClient.Read(context.Background(), &pbresource.ReadRequest{ + rsp, err := testClient.ResourceClient.Read(context.Background(), &pbresource.ReadRequest{ Id: &pbresource.ID{ Name: partitionName, Type: pbtenancy.PartitionType,