Skip to content

Commit

Permalink
Update controller tests to handle new fake client requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancoleman committed Apr 23, 2024
1 parent 84dea6c commit 7798a64
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func TestControllerDoesNotInfinitelyReconcile(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
k8sClient := registerFieldIndexersForTest(fake.NewClientBuilder().WithScheme(s)).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithStatusSubresource(&gwv1beta1.Gateway{}, &gwv1beta1.HTTPRoute{}, &gwv1alpha2.TCPRoute{}, &v1alpha1.RouteAuthFilter{})
k8sClient := registerFieldIndexersForTest(fakeClient).Build()
consulTestServerClient := test.TestServerWithMockConnMgrWatcher(t, nil)
ctx, cancel := context.WithCancel(context.Background())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ func TestGatewayClassReconciler(t *testing.T) {
objs = append(objs, tc.gatewayClass)
}

fakeClient := registerFieldIndexersForTest(fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...)).Build()
fakeClient := registerFieldIndexersForTest(
fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(objs...).
WithStatusSubresource(&gwv1beta1.GatewayClass{})).Build()

r := &GatewayClassController{
Client: fakeClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestGatewayClassConfigReconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "consul-api-gateway",
DeletionTimestamp: &deletionTimestamp,
Finalizers: []string{gatewayClassConfigFinalizer},
},
}
return []runtime.Object{&gatewayClassConfig}
Expand All @@ -68,6 +69,7 @@ func TestGatewayClassConfigReconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "consul-api-gateway",
DeletionTimestamp: &deletionTimestamp,
Finalizers: []string{gatewayClassConfigFinalizer},
},
}
gatewayClass := gwv1beta1.GatewayClass{
Expand Down Expand Up @@ -98,7 +100,10 @@ func TestGatewayClassConfigReconcile(t *testing.T) {
require.NoError(t, gwv1beta1.Install(s))
require.NoError(t, v1alpha1.AddToScheme(s))

fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(tt.k8sObjects()...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(tt.k8sObjects()...).
WithStatusSubresource(&v1alpha1.GatewayClassConfig{}).
Build()

// Create the gateway class config controller.
gcc := &GatewayClassConfigController{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,10 @@ func TestReconcile_CreateUpdatePeeringAcceptor(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringAcceptor{}, &v1alpha1.PeeringAcceptorList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringAcceptor{}).
Build()

// Create test consul server.
testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
Expand Down Expand Up @@ -627,7 +630,10 @@ func TestReconcile_DeletePeeringAcceptor(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringAcceptor{}, &v1alpha1.PeeringAcceptorList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringAcceptor{}).
Build()

// Create test consulServer server
// Create test consul server.
Expand Down Expand Up @@ -775,7 +781,10 @@ func TestReconcile_VersionAnnotation(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringAcceptor{}, &v1alpha1.PeeringAcceptorList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringAcceptor{}).
Build()

// Create test consul server.
testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
Expand Down Expand Up @@ -1089,7 +1098,10 @@ func TestAcceptorUpdateStatus(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringAcceptor{}, &v1alpha1.PeeringAcceptorList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringAcceptor{}).
Build()
// Create the peering acceptor controller.
pac := &AcceptorController{
Client: fakeClient,
Expand Down Expand Up @@ -1202,7 +1214,10 @@ func TestAcceptorUpdateStatusError(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringAcceptor{}, &v1alpha1.PeeringAcceptorList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringAcceptor{}).
Build()
// Create the peering acceptor controller.
controller := &AcceptorController{
Client: fakeClient,
Expand All @@ -1219,6 +1234,7 @@ func TestAcceptorUpdateStatusError(t *testing.T) {
}
err := fakeClient.Get(context.Background(), acceptorName, acceptor)
require.NoError(t, err)
require.Len(t, acceptor.Status.Conditions, 1)
require.Equal(t, tt.expStatus.Conditions[0].Message, acceptor.Status.Conditions[0].Message)

})
Expand Down Expand Up @@ -1487,7 +1503,10 @@ func TestAcceptor_RequestsForPeeringTokens(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringAcceptor{}, &v1alpha1.PeeringAcceptorList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(tt.secret, &tt.acceptors).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(tt.secret, &tt.acceptors).
WithStatusSubresource(&v1alpha1.PeeringAcceptor{}).
Build()
controller := AcceptorController{
Client: fakeClient,
Log: logrtest.New(t),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ func TestReconcile_CreateUpdatePeeringDialer(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringDialer{}, &v1alpha1.PeeringDialerList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringDialer{}).
Build()

// Create the peering dialer controller
controller := &PeeringDialerController{
Expand Down Expand Up @@ -537,7 +540,10 @@ func TestReconcile_VersionAnnotationPeeringDialer(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringDialer{}, &v1alpha1.PeeringDialerList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringDialer{}).
Build()

// Create the peering dialer controller
controller := &PeeringDialerController{
Expand Down Expand Up @@ -754,7 +760,10 @@ func TestReconcileDeletePeeringDialer(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringDialer{}, &v1alpha1.PeeringDialerList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringDialer{}).
Build()

// Create test consul server.
testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
Expand Down Expand Up @@ -897,7 +906,10 @@ func TestDialerUpdateStatus(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringDialer{}, &v1alpha1.PeeringDialerList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringDialer{}).
Build()
// Create the peering dialer controller.
controller := &PeeringDialerController{
Client: fakeClient,
Expand Down Expand Up @@ -1010,7 +1022,11 @@ func TestDialerUpdateStatusError(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringDialer{}, &v1alpha1.PeeringDialerList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(k8sObjects...).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(k8sObjects...).
WithStatusSubresource(&v1alpha1.PeeringDialer{}).
Build()

// Create the peering dialer controller.
controller := &PeeringDialerController{
Client: fakeClient,
Expand All @@ -1027,6 +1043,7 @@ func TestDialerUpdateStatusError(t *testing.T) {
}
err := fakeClient.Get(context.Background(), dialerName, dialer)
require.NoError(t, err)
require.Len(t, dialer.Status.Conditions, 1)
require.Equal(t, tt.expStatus.Conditions[0].Message, dialer.Status.Conditions[0].Message)

})
Expand Down Expand Up @@ -1295,7 +1312,10 @@ func TestDialer_RequestsForPeeringTokens(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.PeeringDialer{}, &v1alpha1.PeeringDialerList{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(tt.secret, &tt.dialers).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).
WithRuntimeObjects(tt.secret, &tt.dialers).
WithStatusSubresource(&v1alpha1.PeeringDialer{}).
Build()
controller := PeeringDialerController{
Client: fakeClient,
Log: logrtest.New(t),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func TestConfigEntryControllers_createsConfigEntry(t *testing.T) {

s := runtime.NewScheme()
s.AddKnownTypes(v1alpha1.GroupVersion, c.configEntryResource)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(c.configEntryResource).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(c.configEntryResource).WithStatusSubresource(c.configEntryResource).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1075,7 +1075,7 @@ func TestConfigEntryControllers_updatesConfigEntry(t *testing.T) {

s := runtime.NewScheme()
s.AddKnownTypes(v1alpha1.GroupVersion, c.configEntryResource)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(c.configEntryResource).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(c.configEntryResource).WithStatusSubresource(c.configEntryResource).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1506,7 +1506,7 @@ func TestConfigEntryControllers_deletesConfigEntry(t *testing.T) {

s := runtime.NewScheme()
s.AddKnownTypes(v1alpha1.GroupVersion, c.configEntryResourceWithDeletion)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(c.configEntryResourceWithDeletion).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(c.configEntryResourceWithDeletion).WithStatusSubresource(c.configEntryResourceWithDeletion).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1567,7 +1567,7 @@ func TestConfigEntryControllers_errorUpdatesSyncStatus(t *testing.T) {

s := runtime.NewScheme()
s.AddKnownTypes(v1alpha1.GroupVersion, svcDefaults)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(svcDefaults).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(svcDefaults).WithStatusSubresource(svcDefaults).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1640,7 +1640,7 @@ func TestConfigEntryControllers_setsSyncedToTrue(t *testing.T) {
s.AddKnownTypes(v1alpha1.GroupVersion, svcDefaults)

// The config entry exists in kube but its status will be nil.
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(svcDefaults).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(svcDefaults).WithStatusSubresource(svcDefaults).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1712,7 +1712,7 @@ func TestConfigEntryControllers_doesNotCreateUnownedConfigEntry(t *testing.T) {
},
}
s.AddKnownTypes(v1alpha1.GroupVersion, svcDefaults)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(svcDefaults).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(svcDefaults).WithStatusSubresource(svcDefaults).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1796,7 +1796,7 @@ func TestConfigEntryControllers_doesNotDeleteUnownedConfig(t *testing.T) {
},
}
s.AddKnownTypes(v1alpha1.GroupVersion, svcDefaultsWithDeletion)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(svcDefaultsWithDeletion).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(svcDefaultsWithDeletion).WithStatusSubresource(svcDefaultsWithDeletion).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -1878,7 +1878,7 @@ func TestConfigEntryControllers_updatesStatusWhenDeleteFails(t *testing.T) {
},
}

fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(defaults, splitter).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(defaults, splitter).WithStatusSubresource(defaults, splitter).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
Expand Down Expand Up @@ -2010,7 +2010,7 @@ func TestConfigEntryController_Migration(t *testing.T) {
s := runtime.NewScheme()
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.ServiceDefaults{})

fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(&c.KubeResource).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(&c.KubeResource).WithStatusSubresource(&c.KubeResource).Build()
testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForServiceIntentions(t)
consulClient := testClient.APIClient
Expand Down Expand Up @@ -2291,7 +2291,7 @@ func TestConfigEntryControllers_assignServiceVirtualIP(t *testing.T) {
s := runtime.NewScheme()
s.AddKnownTypes(v1alpha1.GroupVersion, c.configEntryResource)
s.AddKnownTypes(schema.GroupVersion{Group: "", Version: "v1"}, &corev1.Service{})
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(&c.service, c.configEntryResource).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(&c.service, c.configEntryResource).WithStatusSubresource(&c.service, c.configEntryResource).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, nil)
testClient.TestServer.WaitForLeader(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
"github.com/hashicorp/consul-k8s/control-plane/helper/test"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"

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

logrtest "github.com/go-logr/logr/testr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestAPIGatewayController_ReconcileResourceExists(t *testing.T) {
},
}

fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(apiGW).Build()
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(apiGW).WithStatusSubresource(apiGW).Build()

testClient := test.TestServerWithMockConnMgrWatcher(t, func(c *testutil.TestServerConfig) {
c.Experiments = []string{"resource-apis"}
Expand Down
Loading

0 comments on commit 7798a64

Please sign in to comment.