From 6b67584d477946cd83b8fa70d9cb3e4ce53703d7 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 13:27:18 +0200 Subject: [PATCH 01/16] Fix DERP name in integration tests Signed-off-by: Kristoffer Dalby --- integration_common_test.go | 2 +- integration_embedded_derp_test.go | 5 +++-- integration_general_test.go | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/integration_common_test.go b/integration_common_test.go index 9cce12fa..a72af91f 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -19,7 +19,7 @@ import ( ) const ( - headscaleHostname = "headscale-derp" + headscaleHostname = "headscale" DOCKER_EXECUTE_TIMEOUT = 10 * time.Second ) diff --git a/integration_embedded_derp_test.go b/integration_embedded_derp_test.go index a31006e1..4a8a337f 100644 --- a/integration_embedded_derp_test.go +++ b/integration_embedded_derp_test.go @@ -27,6 +27,7 @@ import ( ) const ( + headscaleDerpHostname = "headscale-derp" namespaceName = "derpnamespace" totalContainers = 3 ) @@ -112,7 +113,7 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { } headscaleOptions := &dockertest.RunOptions{ - Name: headscaleHostname, + Name: headscaleDerpHostname, Mounts: []string{ fmt.Sprintf( "%s/integration_test/etc_embedded_derp:/etc/headscale", @@ -127,7 +128,7 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { }, } - err = s.pool.RemoveContainerByName(headscaleHostname) + err = s.pool.RemoveContainerByName(headscaleDerpHostname) if err != nil { s.FailNow( fmt.Sprintf( diff --git a/integration_general_test.go b/integration_general_test.go index 5abdccbb..2f61cfe9 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -236,7 +236,7 @@ func (s *IntegrationTestSuite) SetupSuite() { } headscaleOptions := &dockertest.RunOptions{ - Name: "headscale", + Name: headscaleHostname, Mounts: []string{ fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath), }, From 4904ccc3c3598a391d46e9150f6f84d6e92156a4 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 13:39:57 +0200 Subject: [PATCH 02/16] Make sure mock container is removed before started Signed-off-by: Kristoffer Dalby --- integration_oidc_test.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 70f793b2..55b87fd8 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -25,7 +25,8 @@ import ( ) const ( - oidcHeadscaleHostname = "headscale" + oidcHeadscaleHostname = "headscale-oidc" + oidcMockHostname = "headscale-mock-oidc" oidcNamespaceName = "oidcnamespace" totalOidcContainers = 3 ) @@ -113,8 +114,7 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { s.Suite.T().Log("Setting up mock OIDC") mockOidcOptions := &dockertest.RunOptions{ - Name: "mockoidc", - Hostname: "mockoidc", + Name: oidcMockHostname, Cmd: []string{"headscale", "mockoidc"}, ExposedPorts: []string{"10000/tcp"}, Networks: []*dockertest.Network{&s.network}, @@ -133,6 +133,18 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { ContextDir: ".", } + err = s.pool.RemoveContainerByName(oidcMockHostname) + if err != nil { + s.FailNow( + fmt.Sprintf( + "Could not remove existing container before building test: %s", + err, + ), + "", + ) + } + + if pmockoidc, err := s.pool.BuildAndRunWithBuildOptions( headscaleBuildOptions, mockOidcOptions, From 201f81ce00a34c867ecc229fbbae4f32aa626cb8 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 14:40:28 +0200 Subject: [PATCH 03/16] Make sure mockoidc is up, has unique name and removed if exist Signed-off-by: Kristoffer Dalby --- cmd/headscale/cli/mockoidc.go | 2 +- integration_general_test.go | 11 +++++++++++ integration_oidc_test.go | 30 +++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/cmd/headscale/cli/mockoidc.go b/cmd/headscale/cli/mockoidc.go index 4313bbf6..8f9c904a 100644 --- a/cmd/headscale/cli/mockoidc.go +++ b/cmd/headscale/cli/mockoidc.go @@ -61,7 +61,7 @@ func mockOIDC() error { return err } - listener, err := net.Listen("tcp", fmt.Sprintf("mockoidc:%d", port)) + listener, err := net.Listen("tcp", fmt.Sprintf("headscale-mock-oidc:%d", port)) if err != nil { return err } diff --git a/integration_general_test.go b/integration_general_test.go index 2f61cfe9..5bfebe1d 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -191,6 +191,17 @@ func (s *IntegrationTestSuite) tailscaleContainer( }, } + err = s.pool.RemoveContainerByName(hostname) + if err != nil { + s.FailNow( + fmt.Sprintf( + "Could not remove existing container before building test: %s", + err, + ), + "", + ) + } + pts, err := s.pool.BuildAndRunWithBuildOptions( tailscaleBuildOptions, tailscaleOptions, diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 55b87fd8..3f9a7a9e 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -144,7 +144,6 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { ) } - if pmockoidc, err := s.pool.BuildAndRunWithBuildOptions( headscaleBuildOptions, mockOidcOptions, @@ -154,6 +153,31 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { s.FailNow(fmt.Sprintf("Could not start mockOIDC container: %s", err), "") } + s.Suite.T().Logf("Waiting for headscale mock oidc to be ready for tests") + hostEndpoint := fmt.Sprintf("localhost:%s", s.mockOidc.GetPort("10000/tcp")) + + if err := s.pool.Retry(func() error { + url := fmt.Sprintf("http://%s/oidc/.well-known/openid-configuration", hostEndpoint) + resp, err := http.Get(url) + if err != nil { + log.Printf("headscale mock OIDC tests is not ready: %s\n", err) + return err + } + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("status code not OK") + } + + return nil + }); err != nil { + // TODO(kradalby): If we cannot access headscale, or any other fatal error during + // test setup, we need to abort and tear down. However, testify does not seem to + // support that at the moment: + // https://github.com/stretchr/testify/issues/849 + return // fmt.Errorf("Could not connect to headscale: %s", err) + } + s.Suite.T().Log("headscale-mock-oidc container is ready for embedded OIDC tests") + oidcCfg := fmt.Sprintf(` oidc: issuer: http://%s:10000/oidc @@ -228,10 +252,10 @@ oidc: } s.Suite.T().Logf("Waiting for headscale to be ready for embedded OIDC tests") - hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8443/tcp")) + hostMockEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8443/tcp")) if err := s.pool.Retry(func() error { - url := fmt.Sprintf("https://%s/health", hostEndpoint) + url := fmt.Sprintf("https://%s/health", hostMockEndpoint) insecureTransport := http.DefaultTransport.(*http.Transport).Clone() insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} client := &http.Client{Transport: insecureTransport} From 382a37f1e16a51b35d7975415b96a3e50274068c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 14:41:20 +0200 Subject: [PATCH 04/16] Test against last patch version Signed-off-by: Kristoffer Dalby --- integration_common_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_common_test.go b/integration_common_test.go index a72af91f..de85a3bd 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -32,7 +32,7 @@ var ( tailscaleVersions = []string{ // "head", // "unstable", - "1.30.0", + "1.30.2", "1.28.0", "1.26.2", "1.24.2", From 37a4d41d0ef729daf195f6bda4f6d95781940727 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 15:37:41 +0200 Subject: [PATCH 05/16] Make addr configurable Signed-off-by: Kristoffer Dalby --- cmd/headscale/cli/mockoidc.go | 6 +++++- integration_oidc_test.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/headscale/cli/mockoidc.go b/cmd/headscale/cli/mockoidc.go index 8f9c904a..165a5115 100644 --- a/cmd/headscale/cli/mockoidc.go +++ b/cmd/headscale/cli/mockoidc.go @@ -46,6 +46,10 @@ func mockOIDC() error { if clientSecret == "" { return errMockOidcClientSecretNotDefined } + addrStr := os.Getenv("MOCKOIDC_ADDR") + if addrStr == "" { + return errMockOidcPortNotDefined + } portStr := os.Getenv("MOCKOIDC_PORT") if portStr == "" { return errMockOidcPortNotDefined @@ -61,7 +65,7 @@ func mockOIDC() error { return err } - listener, err := net.Listen("tcp", fmt.Sprintf("headscale-mock-oidc:%d", port)) + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addrStr, port)) if err != nil { return err } diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 3f9a7a9e..4d3b39e1 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -122,6 +122,7 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { "10000/tcp": {{HostPort: "10000"}}, }, Env: []string{ + fmt.Sprintf("MOCKOIDC_ADDR=%s", oidcMockHostname), "MOCKOIDC_PORT=10000", "MOCKOIDC_CLIENT_ID=superclient", "MOCKOIDC_CLIENT_SECRET=supersecret", @@ -330,6 +331,8 @@ func (s *IntegrationOIDCTestSuite) AuthenticateOIDC( resp, err := client.Get(loginURL.String()) assert.Nil(s.T(), err) + log.Printf("auth body, err: %#v, %s", resp, err) + body, err := io.ReadAll(resp.Body) assert.Nil(s.T(), err) From a0ec3690b6ea49c2343f8dcd975868811e921ec0 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 16:02:34 +0200 Subject: [PATCH 06/16] Fix error declaration Signed-off-by: Kristoffer Dalby --- integration_general_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration_general_test.go b/integration_general_test.go index 5bfebe1d..7185c606 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -191,7 +191,7 @@ func (s *IntegrationTestSuite) tailscaleContainer( }, } - err = s.pool.RemoveContainerByName(hostname) + err := s.pool.RemoveContainerByName(hostname) if err != nil { s.FailNow( fmt.Sprintf( @@ -202,7 +202,7 @@ func (s *IntegrationTestSuite) tailscaleContainer( ) } - pts, err := s.pool.BuildAndRunWithBuildOptions( + pts, err = s.pool.BuildAndRunWithBuildOptions( tailscaleBuildOptions, tailscaleOptions, DockerRestartPolicy, From aca3a667c49803fcb92052aa3a8a6648cdc99117 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 3 Oct 2022 16:15:39 +0200 Subject: [PATCH 07/16] Fix declaration of pointer Signed-off-by: Kristoffer Dalby --- integration_general_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_general_test.go b/integration_general_test.go index 7185c606..1f0e8be6 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -202,7 +202,7 @@ func (s *IntegrationTestSuite) tailscaleContainer( ) } - pts, err = s.pool.BuildAndRunWithBuildOptions( + pts, err := s.pool.BuildAndRunWithBuildOptions( tailscaleBuildOptions, tailscaleOptions, DockerRestartPolicy, From 85df2c80a84860290901a249f2cf081e876e66e1 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 10:45:23 +0200 Subject: [PATCH 08/16] 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 84cb63cf..33fc1b50 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 de85a3bd..a11c5b13 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 4d3b39e1..b438a41b 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 10fa7751..7db58a2a 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 From f3dbfc9045ca5f49c80e90cbe15770d3c2d00b94 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 10:45:41 +0200 Subject: [PATCH 09/16] Style change Signed-off-by: Kristoffer Dalby --- integration_common_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/integration_common_test.go b/integration_common_test.go index a11c5b13..1d69972b 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -115,13 +115,19 @@ func ExecuteCommand( fmt.Println("stdout: ", stdout.String()) fmt.Println("stderr: ", stderr.String()) - return stdout.String(), stderr.String(), fmt.Errorf("command failed with: %s", stderr.String()) + return stdout.String(), stderr.String(), fmt.Errorf( + "command failed with: %s", + stderr.String(), + ) } return stdout.String(), stderr.String(), nil case <-time.After(execConfig.timeout): - return stdout.String(), stderr.String(), fmt.Errorf("command timed out after %s", execConfig.timeout) + return stdout.String(), stderr.String(), fmt.Errorf( + "command timed out after %s", + execConfig.timeout, + ) } } From 88931001fd996ae50b198407fceb8f9ff36279e2 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 10:45:51 +0200 Subject: [PATCH 10/16] Fail correctly if container exist Signed-off-by: Kristoffer Dalby --- integration_oidc_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/integration_oidc_test.go b/integration_oidc_test.go index b438a41b..dd3c99dd 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -412,7 +412,13 @@ func (s *IntegrationOIDCTestSuite) tailscaleContainer( DockerAllowNetworkAdministration, ) if err != nil { - log.Fatalf("Could not start tailscale container version %s: %s", version, err) + s.FailNow( + fmt.Sprintf( + "Could not start tailscale container version %s: %s", + version, + err, + ), + ) } log.Printf("Created %s container\n", hostname) From 22cabc16d7ce39671ff7e887dc2f6392e4dac290 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 10:56:22 +0200 Subject: [PATCH 11/16] No interactive tty Signed-off-by: Kristoffer Dalby --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 33fc1b50..0369435e 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ test_integration_general: test_integration_oidc: 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 ./... + docker run -t --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 From a94ed0586e0b93c2f8fdd86495189fd0c244bfe5 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 13:23:40 +0200 Subject: [PATCH 12/16] Run all integration tests fully in docker Signed-off-by: Kristoffer Dalby --- Makefile | 30 ++++++++++++++++++++---- integration_embedded_derp_test.go | 38 ++++++++++++++++++++----------- integration_general_test.go | 4 +++- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 0369435e..eeac2805 100644 --- a/Makefile +++ b/Makefile @@ -27,18 +27,40 @@ test: test_integration: test_integration_cli test_integration_derp test_integration_oidc test_integration_general test_integration_cli: - go test -failfast -tags integration_cli,integration -timeout 30m -count=1 ./... + docker network rm $$(docker network ls --filter name=headscale --quiet) || true + docker network create headscale-test || true + docker run -t --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_cli,integration -timeout 30m -count=1 ./... test_integration_derp: - go test -failfast -tags integration_derp,integration -timeout 30m -count=1 ./... + docker network rm $$(docker network ls --filter name=headscale --quiet) || true + docker network create headscale-test || true + docker run -t --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_derp,integration -timeout 30m -count=1 ./... test_integration_general: - go test -failfast -tags integration_general,integration -timeout 30m -count=1 ./... + docker network rm $$(docker network ls --filter name=headscale --quiet) || true + docker network create headscale-test || true + docker run -t --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_general,integration -timeout 30m -count=1 ./... test_integration_oidc: docker network rm $$(docker network ls --filter name=headscale --quiet) || true docker network create headscale-test || true - docker run -t --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 ./... + docker run -t --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_embedded_derp_test.go b/integration_embedded_derp_test.go index 4a8a337f..421e01d0 100644 --- a/integration_embedded_derp_test.go +++ b/integration_embedded_derp_test.go @@ -28,18 +28,19 @@ import ( const ( headscaleDerpHostname = "headscale-derp" - namespaceName = "derpnamespace" - totalContainers = 3 + namespaceName = "derpnamespace" + totalContainers = 3 ) type IntegrationDERPTestSuite struct { suite.Suite stats *suite.SuiteInformation - pool dockertest.Pool - networks map[int]dockertest.Network // so we keep the containers isolated - headscale dockertest.Resource - saveLogs bool + pool dockertest.Pool + network dockertest.Network + containerNetworks map[int]dockertest.Network // so we keep the containers isolated + headscale dockertest.Resource + saveLogs bool tailscales map[string]dockertest.Resource joinWaitGroup sync.WaitGroup @@ -54,7 +55,7 @@ func TestDERPIntegrationTestSuite(t *testing.T) { s := new(IntegrationDERPTestSuite) s.tailscales = make(map[string]dockertest.Resource) - s.networks = make(map[int]dockertest.Network) + s.containerNetworks = make(map[int]dockertest.Network) s.saveLogs = saveLogs suite.Run(t, s) @@ -79,7 +80,7 @@ func TestDERPIntegrationTestSuite(t *testing.T) { log.Printf("Could not purge resource: %s\n", err) } - for _, network := range s.networks { + for _, network := range s.containerNetworks { if err := network.Close(); err != nil { log.Printf("Could not close network: %s\n", err) } @@ -94,9 +95,15 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "") } + network, err := GetFirstOrCreateNetwork(&s.pool, "headscale-test") + if err != nil { + s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "") + } + s.network = network + for i := 0; i < totalContainers; i++ { if pnetwork, err := s.pool.CreateNetwork(fmt.Sprintf("headscale-derp-%d", i)); err == nil { - s.networks[i] = *pnetwork + s.containerNetworks[i] = *pnetwork } else { s.FailNow(fmt.Sprintf("Could not create network: %s", err), "") } @@ -121,6 +128,7 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { ), }, Cmd: []string{"headscale", "serve"}, + Networks: []*dockertest.Network{&s.network}, ExposedPorts: []string{"8443/tcp", "3478/udp"}, PortBindings: map[docker.Port][]docker.PortBinding{ "8443/tcp": {{HostPort: "8443"}}, @@ -154,13 +162,15 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { hostname, container := s.tailscaleContainer( fmt.Sprint(i), version, - s.networks[i], + s.containerNetworks[i], ) s.tailscales[hostname] = *container } log.Println("Waiting for headscale to be ready for embedded DERP tests") - hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8443/tcp")) + hostEndpoint := 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", hostEndpoint) @@ -321,7 +331,7 @@ func (s *IntegrationDERPTestSuite) TearDownSuite() { log.Printf("Could not purge resource: %s\n", err) } - for _, network := range s.networks { + for _, network := range s.containerNetworks { if err := network.Close(); err != nil { log.Printf("Could not close network: %s\n", err) } @@ -429,7 +439,9 @@ func (s *IntegrationDERPTestSuite) TestPingAllPeersByHostname() { } func (s *IntegrationDERPTestSuite) TestDERPSTUN() { - headscaleSTUNAddr := fmt.Sprintf("localhost:%s", s.headscale.GetPort("3478/udp")) + headscaleSTUNAddr := fmt.Sprintf("%s:%s", + s.headscale.GetIPInNetwork(&s.network), + s.headscale.GetPort("3478/udp")) client := stun.NewClient() client.SetVerbose(true) client.SetVVerbose(true) diff --git a/integration_general_test.go b/integration_general_test.go index 1f0e8be6..d24c4d95 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -289,7 +289,9 @@ func (s *IntegrationTestSuite) SetupSuite() { } log.Println("Waiting for headscale to be ready for core integration tests") - hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8080/tcp")) + hostEndpoint := fmt.Sprintf("%s:%s", + s.headscale.GetIPInNetwork(&s.network), + s.headscale.GetPort("8080/tcp")) if err := s.pool.Retry(func() error { url := fmt.Sprintf("http://%s/health", hostEndpoint) From ed2236aa24a8c375f8b8c9d3574b8f1017e323f2 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 13:49:24 +0200 Subject: [PATCH 13/16] Add buildtags to pls Signed-off-by: Kristoffer Dalby --- flake.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 050fd747..f7d0c669 100644 --- a/flake.nix +++ b/flake.nix @@ -128,7 +128,13 @@ }; in rec { # `nix develop` - devShell = pkgs.mkShell {buildInputs = devDeps;}; + devShell = pkgs.mkShell { + buildInputs = devDeps; + + shellHook = '' + export GOFLAGS=-tags="integration,integration_general,integration_oidc,integration_cli,integration_derp" + ''; + }; # `nix build` packages = with pkgs; { From a846e13c78b29e93ea2e11a80572d2d928b012ab Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 14:29:21 +0200 Subject: [PATCH 14/16] Expose and use ports consistently Signed-off-by: Kristoffer Dalby --- integration_cli_test.go | 9 ++++++++- integration_general_test.go | 4 ++++ integration_oidc_test.go | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/integration_cli_test.go b/integration_cli_test.go index bf302d2d..cc9d33b2 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -13,6 +13,7 @@ import ( v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/ory/dockertest/v3" + "github.com/ory/dockertest/v3/docker" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -63,6 +64,10 @@ func (s *IntegrationCLITestSuite) SetupTest() { Mounts: []string{ fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath), }, + ExposedPorts: []string{"8080/tcp"}, + PortBindings: map[docker.Port][]docker.PortBinding{ + "8080/tcp": {{HostPort: "8080"}}, + }, Networks: []*dockertest.Network{&s.network}, Cmd: []string{"headscale", "serve"}, } @@ -87,7 +92,9 @@ func (s *IntegrationCLITestSuite) SetupTest() { fmt.Println("Created headscale container for CLI tests") fmt.Println("Waiting for headscale to be ready for CLI tests") - hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8080/tcp")) + hostEndpoint := fmt.Sprintf("%s:%s", + s.headscale.GetIPInNetwork(&s.network), + s.headscale.GetPort("8080/tcp")) if err := s.pool.Retry(func() error { url := fmt.Sprintf("http://%s/health", hostEndpoint) diff --git a/integration_general_test.go b/integration_general_test.go index d24c4d95..4087c9db 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -189,6 +189,10 @@ func (s *IntegrationTestSuite) tailscaleContainer( Cmd: []string{ "tailscaled", "--tun=tsdev", }, + ExposedPorts: []string{"8080/tcp"}, + PortBindings: map[docker.Port][]docker.PortBinding{ + "8080/tcp": {{HostPort: "8080"}}, + }, } err := s.pool.RemoveContainerByName(hostname) diff --git a/integration_oidc_test.go b/integration_oidc_test.go index dd3c99dd..27c1e7bc 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -109,10 +109,10 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { Name: oidcMockHostname, Cmd: []string{"headscale", "mockoidc"}, ExposedPorts: []string{"10000/tcp"}, - Networks: []*dockertest.Network{&s.network}, PortBindings: map[docker.Port][]docker.PortBinding{ "10000/tcp": {{HostPort: "10000"}}, }, + Networks: []*dockertest.Network{&s.network}, Env: []string{ fmt.Sprintf("MOCKOIDC_ADDR=%s", oidcMockHostname), "MOCKOIDC_PORT=10000", From d900f48d38ae145b009bd683a0f4d71fc0a6efb1 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 14:39:49 +0200 Subject: [PATCH 15/16] expose right porsts Signed-off-by: Kristoffer Dalby --- integration_general_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/integration_general_test.go b/integration_general_test.go index 4087c9db..500247df 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -189,10 +189,6 @@ func (s *IntegrationTestSuite) tailscaleContainer( Cmd: []string{ "tailscaled", "--tun=tsdev", }, - ExposedPorts: []string{"8080/tcp"}, - PortBindings: map[docker.Port][]docker.PortBinding{ - "8080/tcp": {{HostPort: "8080"}}, - }, } err := s.pool.RemoveContainerByName(hostname) @@ -255,6 +251,10 @@ func (s *IntegrationTestSuite) SetupSuite() { Mounts: []string{ fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath), }, + ExposedPorts: []string{"8080/tcp"}, + PortBindings: map[docker.Port][]docker.PortBinding{ + "8080/tcp": {{HostPort: "8080"}}, + }, Networks: []*dockertest.Network{&s.network}, Cmd: []string{"headscale", "serve"}, } From 8ee35c9c226cfe6ed8ae8b46bc6be3a96810a223 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Oct 2022 22:10:33 +0200 Subject: [PATCH 16/16] Stuff Signed-off-by: Kristoffer Dalby --- integration_cli_test.go | 12 ++++++------ integration_common_test.go | 1 + integration_embedded_derp_test.go | 3 ++- integration_general_test.go | 8 ++++---- integration_oidc_test.go | 2 +- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/integration_cli_test.go b/integration_cli_test.go index cc9d33b2..0f5d69ad 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -43,11 +43,11 @@ func (s *IntegrationCLITestSuite) SetupTest() { 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), "") + network, err := GetFirstOrCreateNetwork(&s.pool, headscaleNetwork) + if err != nil { + s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "") } + s.network = network headscaleBuildOptions := &dockertest.BuildOptions{ Dockerfile: "Dockerfile", @@ -64,12 +64,12 @@ func (s *IntegrationCLITestSuite) SetupTest() { Mounts: []string{ fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath), }, + Cmd: []string{"headscale", "serve"}, + Networks: []*dockertest.Network{&s.network}, ExposedPorts: []string{"8080/tcp"}, PortBindings: map[docker.Port][]docker.PortBinding{ "8080/tcp": {{HostPort: "8080"}}, }, - Networks: []*dockertest.Network{&s.network}, - Cmd: []string{"headscale", "serve"}, } err = s.pool.RemoveContainerByName(headscaleHostname) diff --git a/integration_common_test.go b/integration_common_test.go index 1d69972b..f3c39232 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -19,6 +19,7 @@ import ( ) const ( + headscaleNetwork = "headscale-test" headscaleHostname = "headscale" DOCKER_EXECUTE_TIMEOUT = 10 * time.Second ) diff --git a/integration_embedded_derp_test.go b/integration_embedded_derp_test.go index 421e01d0..c83f6106 100644 --- a/integration_embedded_derp_test.go +++ b/integration_embedded_derp_test.go @@ -95,7 +95,7 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "") } - network, err := GetFirstOrCreateNetwork(&s.pool, "headscale-test") + network, err := GetFirstOrCreateNetwork(&s.pool, headscaleNetwork) if err != nil { s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "") } @@ -120,6 +120,7 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { } headscaleOptions := &dockertest.RunOptions{ + Name: headscaleDerpHostname, Mounts: []string{ fmt.Sprintf( diff --git a/integration_general_test.go b/integration_general_test.go index 500247df..5b5abf07 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -230,11 +230,11 @@ func (s *IntegrationTestSuite) 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), "") + network, err := GetFirstOrCreateNetwork(&s.pool, headscaleNetwork) + if err != nil { + s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "") } + s.network = network headscaleBuildOptions := &dockertest.BuildOptions{ Dockerfile: "Dockerfile", diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 27c1e7bc..ba0b00f6 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -96,7 +96,7 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() { s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "") } - network, err := GetFirstOrCreateNetwork(&s.pool, "headscale-test") + network, err := GetFirstOrCreateNetwork(&s.pool, headscaleNetwork) if err != nil { s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "") }