From 85df2c80a84860290901a249f2cf081e876e66e1 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 10:45:23 +0200 Subject: [PATCH] Run oidc tests fully in docker Signed-off-by: Kristoffer Dalby --- Makefile | 4 ++- integration_common_test.go | 19 ++++++++++++ integration_oidc_test.go | 36 ++++++++++++---------- integration_test/etc_oidc/base_config.yaml | 2 +- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 84cb63c..33fc1b5 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,9 @@ test_integration_general: go test -failfast -tags integration_general,integration -timeout 30m -count=1 ./... test_integration_oidc: - go test -failfast -tags integration_oidc,integration -timeout 30m -count=1 ./... + docker network rm $$(docker network ls --filter name=headscale --quiet) || true + docker network create headscale-test || true + docker run -it --rm --network headscale-test -v $$PWD:$$PWD -w $$PWD -v /var/run/docker.sock:/var/run/docker.sock golang:1 go test -failfast -tags integration_oidc,integration -timeout 30m -count=1 ./... coverprofile_func: go tool cover -func=coverage.out diff --git a/integration_common_test.go b/integration_common_test.go index de85a3b..a11c5b1 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -316,3 +316,22 @@ func GetEnvBool(key string) (bool, error) { return v, nil } + +func GetFirstOrCreateNetwork(pool *dockertest.Pool, name string) (dockertest.Network, error) { + networks, err := pool.NetworksByName(name) + if err != nil || len(networks) == 0 { + + if _, err := pool.CreateNetwork(name); err == nil { + // Create does not give us an updated version of the resource, so we need to + // get it again. + networks, err := pool.NetworksByName(name) + if err != nil { + return dockertest.Network{}, err + } + + return networks[0], nil + } + } + + return networks[0], nil +} diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 4d3b39e..b438a41 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -26,7 +26,7 @@ import ( const ( oidcHeadscaleHostname = "headscale-oidc" - oidcMockHostname = "headscale-mock-oidc" + oidcMockHostname = "headscale-mock-oidc" oidcNamespaceName = "oidcnamespace" totalOidcContainers = 3 ) @@ -96,19 +96,11 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "") } - if pnetwork, err := s.pool.CreateNetwork("headscale-test"); err == nil { - s.network = *pnetwork - } else { - s.FailNow(fmt.Sprintf("Could not create network: %s", err), "") - } - - // Create does not give us an updated version of the resource, so we need to - // get it again. - networks, err := s.pool.NetworksByName("headscale-test") + network, err := GetFirstOrCreateNetwork(&s.pool, "headscale-test") if err != nil { - s.FailNow(fmt.Sprintf("Could not get network: %s", err), "") + s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "") } - s.network = networks[0] + s.network = network log.Printf("Network config: %v", s.network.Network.IPAM.Config[0]) @@ -155,7 +147,11 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { } s.Suite.T().Logf("Waiting for headscale mock oidc to be ready for tests") - hostEndpoint := fmt.Sprintf("localhost:%s", s.mockOidc.GetPort("10000/tcp")) + hostEndpoint := fmt.Sprintf( + "%s:%s", + s.mockOidc.GetIPInNetwork(&s.network), + s.mockOidc.GetPort("10000/tcp"), + ) if err := s.pool.Retry(func() error { url := fmt.Sprintf("http://%s/oidc/.well-known/openid-configuration", hostEndpoint) @@ -253,7 +249,11 @@ oidc: } s.Suite.T().Logf("Waiting for headscale to be ready for embedded OIDC tests") - hostMockEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8443/tcp")) + hostMockEndpoint := fmt.Sprintf( + "%s:%s", + s.headscale.GetIPInNetwork(&s.network), + s.headscale.GetPort("8443/tcp"), + ) if err := s.pool.Retry(func() error { url := fmt.Sprintf("https://%s/health", hostMockEndpoint) @@ -347,7 +347,6 @@ func (s *IntegrationOIDCTestSuite) joinOIDC( endpoint, hostname string, tailscale dockertest.Resource, ) (*url.URL, error) { - command := []string{ "tailscale", "up", @@ -536,7 +535,12 @@ func (s *IntegrationOIDCTestSuite) TestPingAllPeersByAddress() { []string{}, ) assert.Nil(t, err) - log.Printf("result for %s: stdout: %s, stderr: %s\n", hostname, stdout, stderr) + log.Printf( + "result for %s: stdout: %s, stderr: %s\n", + hostname, + stdout, + stderr, + ) assert.Contains(t, stdout, "pong") }) } diff --git a/integration_test/etc_oidc/base_config.yaml b/integration_test/etc_oidc/base_config.yaml index 10fa775..7db58a2 100644 --- a/integration_test/etc_oidc/base_config.yaml +++ b/integration_test/etc_oidc/base_config.yaml @@ -11,7 +11,7 @@ private_key_path: private.key noise: private_key_path: noise_private.key listen_addr: 0.0.0.0:8443 -server_url: https://localhost:8443 +server_url: https://headscale-oidc:8443 tls_cert_path: "/etc/headscale/tls/server.crt" tls_key_path: "/etc/headscale/tls/server.key" tls_client_auth_mode: disabled