Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DanStough committed Aug 31, 2023
1 parent ea4d003 commit 208a9c4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
6 changes: 3 additions & 3 deletions control-plane/api-gateway/cache/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down Expand Up @@ -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),
})
Expand Down Expand Up @@ -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),
})
Expand Down
2 changes: 1 addition & 1 deletion control-plane/catalog/to-consul/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
57 changes: 57 additions & 0 deletions control-plane/connect-inject/webhook/mesh_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.
},
},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
},
"",
},
Expand All @@ -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(),
},
"",
},
Expand Down Expand Up @@ -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(),
},

"",
Expand Down Expand Up @@ -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(),
},
"",
},
Expand Down Expand Up @@ -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(),
},
"",
},
Expand Down
22 changes: 13 additions & 9 deletions control-plane/helper/test/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,32 @@ 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{
IP: parsedIP,
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)
Expand Down
10 changes: 5 additions & 5 deletions control-plane/subcommand/server-acl-init/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 208a9c4

Please sign in to comment.