Skip to content

Commit

Permalink
add connect-init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhanushkodi committed Feb 11, 2022
1 parent b0c2ca9 commit 3a31667
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions control-plane/subcommand/connect-init/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -68,6 +69,7 @@ func TestRun_ServicePollingWithACLsAndTLS(t *testing.T) {
includeServiceAccountName bool
serviceAccountNameMismatch bool
expFail bool
multiport bool
}{
{
name: "ACLs enabled, no tls",
Expand All @@ -91,6 +93,13 @@ func TestRun_ServicePollingWithACLsAndTLS(t *testing.T) {
serviceAccountName: "web",
serviceName: "web",
},
{
name: "ACLs enabled, multiport service",
tls: false,
serviceAccountName: "counting-admin",
serviceName: "counting-admin",
multiport: true,
},
{
name: "ACLs enabled, service name annotation doesn't match service account name",
tls: false,
Expand Down Expand Up @@ -152,6 +161,9 @@ func TestRun_ServicePollingWithACLsAndTLS(t *testing.T) {

// Register Consul services.
testConsulServices := []api.AgentServiceRegistration{consulCountingSvc, consulCountingSvcSidecar}
if tt.multiport {
testConsulServices = append(testConsulServices, consulCountingSvcMultiport, consulCountingSvcSidecarMultiport)
}
for _, svc := range testConsulServices {
require.NoError(t, consulClient.Agent().ServiceRegister(&svc))
}
Expand All @@ -173,6 +185,7 @@ func TestRun_ServicePollingWithACLsAndTLS(t *testing.T) {
"-bearer-token-file", bearerFile,
"-acl-token-sink", tokenFile,
"-proxy-id-file", proxyFile,
"-multiport=" + strconv.FormatBool(tt.multiport),
}
// Add the CA File if necessary since we're not setting CONSUL_CACERT in tt ENV.
if tt.tls {
Expand Down Expand Up @@ -201,7 +214,11 @@ func TestRun_ServicePollingWithACLsAndTLS(t *testing.T) {
// Validate contents of proxyFile.
data, err := ioutil.ReadFile(proxyFile)
require.NoError(t, err)
require.Contains(t, string(data), "counting-counting-sidecar-proxy")
if tt.multiport {
require.Contains(t, string(data), "counting-admin-sidecar-proxy-id")
} else {
require.Contains(t, string(data), "counting-counting-sidecar-proxy")
}
})
}
}
Expand All @@ -210,8 +227,10 @@ func TestRun_ServicePollingWithACLsAndTLS(t *testing.T) {
func TestRun_ServicePollingOnly(t *testing.T) {
t.Parallel()
cases := []struct {
name string
tls bool
name string
tls bool
serviceName string
multiport bool
}{
{
name: "ACLs disabled, no tls",
Expand All @@ -221,6 +240,12 @@ func TestRun_ServicePollingOnly(t *testing.T) {
name: "ACLs disabled, tls",
tls: true,
},
{
name: "Multiport, ACLs disabled, no tls",
tls: false,
serviceName: "counting-admin",
multiport: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -260,6 +285,9 @@ func TestRun_ServicePollingOnly(t *testing.T) {

// Register Consul services.
testConsulServices := []api.AgentServiceRegistration{consulCountingSvc, consulCountingSvcSidecar}
if tt.multiport {
testConsulServices = append(testConsulServices, consulCountingSvcMultiport, consulCountingSvcSidecarMultiport)
}
for _, svc := range testConsulServices {
require.NoError(t, consulClient.Agent().ServiceRegister(&svc))
}
Expand All @@ -275,8 +303,14 @@ func TestRun_ServicePollingOnly(t *testing.T) {
"-pod-name", testPodName,
"-pod-namespace", testPodNamespace,
"-proxy-id-file", proxyFile,
"-multiport=" + strconv.FormatBool(tt.multiport),
"-http-addr", fmt.Sprintf("%s://%s", cfg.Scheme, cfg.Address)}

// In a multiport case, the service name will be passed in to the test.
if tt.serviceName != "" {
flags = append(flags, "-service-name", tt.serviceName)
}

// Add the CA File if necessary since we're not setting CONSUL_CACERT in tt ENV.
if tt.tls {
flags = append(flags, "-ca-file", caFile)
Expand All @@ -289,7 +323,11 @@ func TestRun_ServicePollingOnly(t *testing.T) {
// Validate contents of proxyFile.
data, err := ioutil.ReadFile(proxyFile)
require.NoError(t, err)
require.Contains(t, string(data), "counting-counting-sidecar-proxy")
if tt.multiport {
require.Contains(t, string(data), "counting-admin-sidecar-proxy-id")
} else {
require.Contains(t, string(data), "counting-counting-sidecar-proxy")
}
})
}

Expand Down Expand Up @@ -940,4 +978,32 @@ var (
metaKeyKubeServiceName: "counting",
},
}
consulCountingSvcMultiport = api.AgentServiceRegistration{
ID: "counting-admin-id",
Name: "counting-admin",
Address: "127.0.0.1",
Meta: map[string]string{
metaKeyPodName: "counting-pod",
metaKeyKubeNS: "default-ns",
metaKeyKubeServiceName: "counting-admin",
},
}
consulCountingSvcSidecarMultiport = api.AgentServiceRegistration{
ID: "counting-admin-sidecar-proxy-id",
Name: "counting-admin-sidecar-proxy",
Kind: "connect-proxy",
Proxy: &api.AgentServiceConnectProxyConfig{
DestinationServiceName: "counting-admin",
DestinationServiceID: "counting-admin-id",
Config: nil,
Upstreams: nil,
},
Port: 9999,
Address: "127.0.0.1",
Meta: map[string]string{
metaKeyPodName: "counting-pod",
metaKeyKubeNS: "default-ns",
metaKeyKubeServiceName: "counting-admin",
},
}
)

0 comments on commit 3a31667

Please sign in to comment.