diff --git a/control-plane/api-gateway/cache/consul_test.go b/control-plane/api-gateway/cache/consul_test.go index 895c59e8c9..3a4423f6b4 100644 --- a/control-plane/api-gateway/cache/consul_test.go +++ b/control-plane/api-gateway/cache/consul_test.go @@ -1323,7 +1323,7 @@ func TestCache_Write(t *testing.T) { GRPCPort: port, APITimeout: 0, }, - ConsulServerConnMgr: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + ConsulServerConnMgr: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), NamespacesEnabled: false, Logger: logrtest.NewTestLogger(t), }) @@ -1601,7 +1601,7 @@ func Test_Run(t *testing.T) { GRPCPort: port, APITimeout: 0, }, - ConsulServerConnMgr: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + ConsulServerConnMgr: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), NamespacesEnabled: false, Logger: logrtest.NewTestLogger(t), }) @@ -2002,7 +2002,7 @@ func TestCache_Delete(t *testing.T) { GRPCPort: port, APITimeout: 0, }, - ConsulServerConnMgr: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + ConsulServerConnMgr: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), NamespacesEnabled: false, Logger: logrtest.NewTestLogger(t), }) diff --git a/control-plane/catalog/to-consul/syncer_test.go b/control-plane/catalog/to-consul/syncer_test.go index ece2e1dd05..ab2cfee0a2 100644 --- a/control-plane/catalog/to-consul/syncer_test.go +++ b/control-plane/catalog/to-consul/syncer_test.go @@ -234,7 +234,7 @@ func TestConsulSyncer_stopsGracefully(t *testing.T) { testClient := &test.TestServerClient{ Cfg: &consul.Config{APIClientConfig: &api.Config{}, HTTPPort: port}, - Watcher: test.MockConnMgrForIPAndPort(t, parsedURL.Host, port), + Watcher: test.MockConnMgrForIPAndPort(t, parsedURL.Host, port, false), } // Start the syncer. diff --git a/control-plane/connect-inject/webhook/mesh_webhook_test.go b/control-plane/connect-inject/webhook/mesh_webhook_test.go index 4345403bcd..2b71c08500 100644 --- a/control-plane/connect-inject/webhook/mesh_webhook_test.go +++ b/control-plane/connect-inject/webhook/mesh_webhook_test.go @@ -250,6 +250,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/spec/volumes", @@ -343,6 +347,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -398,6 +406,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -472,6 +484,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -532,6 +548,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -617,6 +637,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -754,6 +778,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "replace", Path: "/spec/containers/0/livenessProbe/httpGet/port", @@ -817,6 +845,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -876,6 +908,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -961,6 +997,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/labels", @@ -1031,6 +1071,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/spec/dnsPolicy", @@ -1103,6 +1147,10 @@ func TestHandlerHandle(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), + }, // Note: no DNS policy/config additions. }, }, @@ -1275,6 +1323,10 @@ func TestHandlerHandle_ValidateOverwriteProbes(t *testing.T) { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationOriginalPod), }, + { + Operation: "add", + Path: "/metadata/annotations/" + escapeJSONPointer(constants.LegacyAnnotationConsulK8sVersion), + }, { Operation: "add", Path: "/metadata/annotations/" + escapeJSONPointer(constants.AnnotationConsulK8sVersion), @@ -1359,6 +1411,7 @@ func TestHandlerDefaultAnnotations(t *testing.T) { map[string]string{ constants.AnnotationOriginalPod: "{\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"containers\":null},\"status\":{}}", constants.LegacyAnnotationConsulK8sVersion: version.GetHumanVersion(), + constants.AnnotationConsulK8sVersion: version.GetHumanVersion(), }, "", }, @@ -1380,6 +1433,7 @@ func TestHandlerDefaultAnnotations(t *testing.T) { map[string]string{ constants.AnnotationOriginalPod: "{\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"containers\":[{\"name\":\"web\",\"resources\":{}},{\"name\":\"web-side\",\"resources\":{}}]},\"status\":{}}", constants.LegacyAnnotationConsulK8sVersion: version.GetHumanVersion(), + constants.AnnotationConsulK8sVersion: version.GetHumanVersion(), }, "", }, @@ -1408,6 +1462,7 @@ func TestHandlerDefaultAnnotations(t *testing.T) { "consul.hashicorp.com/connect-service": "foo", constants.AnnotationOriginalPod: "{\"metadata\":{\"creationTimestamp\":null,\"annotations\":{\"consul.hashicorp.com/connect-service\":\"foo\"}},\"spec\":{\"containers\":[{\"name\":\"web\",\"resources\":{}},{\"name\":\"web-side\",\"resources\":{}}]},\"status\":{}}", constants.LegacyAnnotationConsulK8sVersion: version.GetHumanVersion(), + constants.AnnotationConsulK8sVersion: version.GetHumanVersion(), }, "", @@ -1437,6 +1492,7 @@ func TestHandlerDefaultAnnotations(t *testing.T) { constants.AnnotationPort: "http", constants.AnnotationOriginalPod: "{\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"containers\":[{\"name\":\"web\",\"ports\":[{\"name\":\"http\",\"containerPort\":8080}],\"resources\":{}},{\"name\":\"web-side\",\"resources\":{}}]},\"status\":{}}", constants.LegacyAnnotationConsulK8sVersion: version.GetHumanVersion(), + constants.AnnotationConsulK8sVersion: version.GetHumanVersion(), }, "", }, @@ -1464,6 +1520,7 @@ func TestHandlerDefaultAnnotations(t *testing.T) { constants.AnnotationPort: "8080", constants.AnnotationOriginalPod: "{\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"containers\":[{\"name\":\"web\",\"ports\":[{\"containerPort\":8080}],\"resources\":{}},{\"name\":\"web-side\",\"resources\":{}}]},\"status\":{}}", constants.LegacyAnnotationConsulK8sVersion: version.GetHumanVersion(), + constants.AnnotationConsulK8sVersion: version.GetHumanVersion(), }, "", }, diff --git a/control-plane/helper/test/test_util.go b/control-plane/helper/test/test_util.go index b3544a38bb..915c65704e 100644 --- a/control-plane/helper/test/test_util.go +++ b/control-plane/helper/test/test_util.go @@ -67,20 +67,14 @@ func TestServerWithMockConnMgrWatcher(t *testing.T, callback testutil.ServerConf TestServer: consulServer, APIClient: client, Cfg: consulConfig, - Watcher: MockConnMgrForIPAndPort(t, "127.0.0.1", cfg.Ports.GRPC), + Watcher: MockConnMgrForIPAndPort(t, "127.0.0.1", cfg.Ports.GRPC, true), } } -func MockConnMgrForIPAndPort(t *testing.T, ip string, port int) *consul.MockServerConnectionManager { +func MockConnMgrForIPAndPort(t *testing.T, ip string, port int, enableGRPCConn bool) *consul.MockServerConnectionManager { parsedIP := net.ParseIP(ip) connMgr := &consul.MockServerConnectionManager{} - conn, err := grpc.DialContext( - context.Background(), - fmt.Sprintf("%s:%d", parsedIP, port), - grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - mockState := discovery.State{ Address: discovery.Addr{ TCPAddr: net.TCPAddr{ @@ -88,7 +82,17 @@ func MockConnMgrForIPAndPort(t *testing.T, ip string, port int) *consul.MockServ Port: port, }, }, - GRPCConn: conn, + } + + // If the connection is enabled, some tests will receive extra HTTP API calls where + // the server is being dialed. + if enableGRPCConn { + conn, err := grpc.DialContext( + context.Background(), + fmt.Sprintf("%s:%d", parsedIP, port), + grpc.WithTransportCredentials(insecure.NewCredentials())) + require.NoError(t, err) + mockState.GRPCConn = conn } connMgr.On("State").Return(mockState, nil) connMgr.On("Run").Return(nil) diff --git a/control-plane/subcommand/server-acl-init/command_test.go b/control-plane/subcommand/server-acl-init/command_test.go index d7937d4946..c7d44c0695 100644 --- a/control-plane/subcommand/server-acl-init/command_test.go +++ b/control-plane/subcommand/server-acl-init/command_test.go @@ -1120,7 +1120,7 @@ func TestRun_NoLeader(t *testing.T) { cmd := Command{ UI: ui, clientset: k8s, - watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), } done := make(chan bool) @@ -1376,7 +1376,7 @@ func TestRun_ClientPolicyAndBindingRuleRetry(t *testing.T) { cmd := Command{ UI: ui, clientset: k8s, - watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), } responseCode := cmd.Run([]string{ "-timeout=1m", @@ -1525,7 +1525,7 @@ func TestRun_AlreadyBootstrapped(t *testing.T) { cmd := Command{ UI: ui, clientset: k8s, - watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), } responseCode := cmd.Run(cmdArgs) @@ -1710,7 +1710,7 @@ func TestRun_SkipBootstrapping_WhenServersAreDisabled(t *testing.T) { cmd := Command{ UI: ui, clientset: k8s, - watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port), + watcher: test.MockConnMgrForIPAndPort(t, serverURL.Hostname(), port, false), backend: &FakeSecretsBackend{bootstrapToken: bootToken}, } responseCode := cmd.Run([]string{ @@ -1754,7 +1754,7 @@ func TestRun_Timeout(t *testing.T) { cmd := Command{ UI: ui, clientset: k8s, - watcher: test.MockConnMgrForIPAndPort(t, "localhost", 12345), + watcher: test.MockConnMgrForIPAndPort(t, "localhost", 12345, false), } responseCode := cmd.Run([]string{