From 929fffab7f8cee857cc3a334c41b684243aaab42 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Mon, 7 Jun 2021 16:52:00 +1000 Subject: Support custom domains in stub responses --- test/acceptance/serving_test.go | 3 +- test/acceptance/testdata/api_responses.go | 139 ++++++++++-------------------- 2 files changed, 47 insertions(+), 95 deletions(-) (limited to 'test') diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index 92727d28..28f80b59 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -163,7 +163,8 @@ func TestNestedSubgroups(t *testing.T) { func TestCustom404(t *testing.T) { skipUnlessEnabled(t) - teardown := RunPagesProcess(t, *pagesBinary, supportedListeners(), "") + + _, teardown := RunPagesProcessWithStubGitLabServer(t, true, *pagesBinary, supportedListeners(), []string{}) defer teardown() tests := []struct { diff --git a/test/acceptance/testdata/api_responses.go b/test/acceptance/testdata/api_responses.go index a1c99955..16e87f02 100644 --- a/test/acceptance/testdata/api_responses.go +++ b/test/acceptance/testdata/api_responses.go @@ -16,90 +16,32 @@ type responseFn func(*testing.T, string) api.VirtualDomain // DomainResponses holds the predefined API responses for certain domains // that can be used with the GitLab API stub in acceptance tests +// Assume the working dir is inside shared/pages/ var DomainResponses = map[string]responseFn{ - "zip-from-disk.gitlab.io": ZipFromFile, - "zip-from-disk-not-found.gitlab.io": ZipFromFileNotFound, - "zip-not-allowed-path.gitlab.io": ZipFromNotAllowedPath, - // test assume the working dir is inside shared/pages/ - "group.gitlab-example.com": GenerateVirtualDomainFromDir("group", "group.gitlab-example.com"), - "CapitalGroup.gitlab-example.com": GenerateVirtualDomainFromDir("CapitalGroup", "CapitalGroup.gitlab-example.com"), - "group.404.gitlab-example.com": GenerateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com"), - "domain.404.com": domain404, + "zip-from-disk.gitlab.io": customDomain(projectConfig{ + projectID: 123, + prefix: "/", + pathOnDisk: "@hashed/zip-from-disk.gitlab.io", + }), + "zip-from-disk-not-found.gitlab.io": customDomain(projectConfig{}), + // outside of working dir + "zip-not-allowed-path.gitlab.io": customDomain(projectConfig{pathOnDisk: "../../../../"}), + "group.gitlab-example.com": generateVirtualDomainFromDir("group", "group.gitlab-example.com"), + "CapitalGroup.gitlab-example.com": generateVirtualDomainFromDir("CapitalGroup", "CapitalGroup.gitlab-example.com"), + "group.404.gitlab-example.com": generateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com"), + "domain.404.com": customDomain(projectConfig{ + projectID: 1000, + prefix: "/", + pathOnDisk: "group.404/domain.404", + }), // NOTE: before adding more domains here, generate the zip archive by running (per project) // make zip PROJECT_SUBDIR=group/serving // make zip PROJECT_SUBDIR=group/project2 } -// ZipFromFile response for zip.gitlab.io -func ZipFromFile(t *testing.T, wd string) api.VirtualDomain { - t.Helper() - - return api.VirtualDomain{ - Certificate: "", - Key: "", - LookupPaths: []api.LookupPath{ - { - ProjectID: 123, - AccessControl: false, - HTTPSOnly: false, - Prefix: "/", - Source: api.Source{ - Type: "zip", - Path: fmt.Sprintf("file://%s/@hashed/67/06/670671cd97404156226e507973f2ab8330d3022ca96e0c93bdbdb320c41adcaf/pages_deployments/01/artifacts.zip", wd), - }, - }, - }, - } -} - -// ZipFromFileNotFound response for zip-from-disk-not-found.gitlab.io -func ZipFromFileNotFound(t *testing.T, wd string) api.VirtualDomain { - t.Helper() - - return api.VirtualDomain{ - Certificate: "", - Key: "", - LookupPaths: []api.LookupPath{ - { - ProjectID: 123, - AccessControl: false, - HTTPSOnly: false, - Prefix: "/", - Source: api.Source{ - Type: "zip", - Path: fmt.Sprintf("file://%s/@hashed/67/06/670671cd97404156226e507973f2ab8330d3022ca96e0c93bdbdb320c41adcaf/pages_deployments/01/unknown.zip", wd), - }, - }, - }, - } -} - -// ZipFromNotAllowedPath response for zip-not-allowed-path.gitlab.io -func ZipFromNotAllowedPath(t *testing.T, wd string) api.VirtualDomain { - t.Helper() - - return api.VirtualDomain{ - Certificate: "", - Key: "", - LookupPaths: []api.LookupPath{ - { - ProjectID: 123, - AccessControl: false, - HTTPSOnly: false, - Prefix: "/", - Source: api.Source{ - Type: "zip", - // path outside of `pages-root` - Path: "file:///some/random/path/public.zip", - }, - }, - }, - } -} - -// GenerateVirtualDomainFromDir walks the subdirectory inside of shared/pages/ to find any zip archives. +// generateVirtualDomainFromDir walks the subdirectory inside of shared/pages/ to find any zip archives. // It works for subdomains of pages-domain but not for custom domains (yet) -func GenerateVirtualDomainFromDir(dir, rootDomain string) responseFn { +func generateVirtualDomainFromDir(dir, rootDomain string) responseFn { return func(t *testing.T, wd string) api.VirtualDomain { t.Helper() @@ -161,25 +103,34 @@ func GenerateVirtualDomainFromDir(dir, rootDomain string) responseFn { } } -// domain404 hardcoding for now, will implement a better solution in a follow up MR -//TODO: remove hardcoded custom domains: https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/498 -func domain404(t *testing.T, wd string) api.VirtualDomain { - t.Helper() +type projectConfig struct { + projectID int + accessControl bool + https bool + prefix string + pathOnDisk string +} - return api.VirtualDomain{ - Certificate: "", - Key: "", - LookupPaths: []api.LookupPath{ - { - ProjectID: 123, - AccessControl: false, - HTTPSOnly: false, - Prefix: "/", - Source: api.Source{ - Type: "zip", - Path: fmt.Sprintf("file://%s/group.404/domain.404/public.zip", wd), +// customDomain with per project config +func customDomain(config projectConfig) responseFn { + return func(t *testing.T, wd string) api.VirtualDomain { + t.Helper() + + return api.VirtualDomain{ + Certificate: "", + Key: "", + LookupPaths: []api.LookupPath{ + { + ProjectID: config.projectID, + AccessControl: config.accessControl, + HTTPSOnly: config.https, + Prefix: config.prefix, + Source: api.Source{ + Type: "zip", + Path: fmt.Sprintf("file://%s/%s/public.zip", wd, config.pathOnDisk), + }, }, }, - }, + } } } -- cgit v1.2.3 From 4c1f1a4a6441da1f8e4f970de03f632b72383e29 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Thu, 10 Jun 2021 15:34:21 +1000 Subject: Fix test arguments --- test/acceptance/serving_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'test') diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index 28f80b59..09fba7e1 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -164,8 +164,7 @@ func TestNestedSubgroups(t *testing.T) { func TestCustom404(t *testing.T) { skipUnlessEnabled(t) - _, teardown := RunPagesProcessWithStubGitLabServer(t, true, *pagesBinary, supportedListeners(), []string{}) - defer teardown() + RunPagesProcessWithStubGitLabServer(t) tests := []struct { host string -- cgit v1.2.3 From 546ca8fcc2908d780712a0a1f7f9f8ae251ba152 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Tue, 15 Jun 2021 11:41:45 +1000 Subject: Add per project config for reading zips from disk --- test/acceptance/testdata/api_responses.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/acceptance/testdata/api_responses.go b/test/acceptance/testdata/api_responses.go index 16e87f02..2de6fea4 100644 --- a/test/acceptance/testdata/api_responses.go +++ b/test/acceptance/testdata/api_responses.go @@ -26,9 +26,9 @@ var DomainResponses = map[string]responseFn{ "zip-from-disk-not-found.gitlab.io": customDomain(projectConfig{}), // outside of working dir "zip-not-allowed-path.gitlab.io": customDomain(projectConfig{pathOnDisk: "../../../../"}), - "group.gitlab-example.com": generateVirtualDomainFromDir("group", "group.gitlab-example.com"), - "CapitalGroup.gitlab-example.com": generateVirtualDomainFromDir("CapitalGroup", "CapitalGroup.gitlab-example.com"), - "group.404.gitlab-example.com": generateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com"), + "group.gitlab-example.com": generateVirtualDomainFromDir("group", "group.gitlab-example.com", nil), + "CapitalGroup.gitlab-example.com": generateVirtualDomainFromDir("CapitalGroup", "CapitalGroup.gitlab-example.com", nil), + "group.404.gitlab-example.com": generateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com", nil), "domain.404.com": customDomain(projectConfig{ projectID: 1000, prefix: "/", @@ -41,7 +41,7 @@ var DomainResponses = map[string]responseFn{ // generateVirtualDomainFromDir walks the subdirectory inside of shared/pages/ to find any zip archives. // It works for subdomains of pages-domain but not for custom domains (yet) -func generateVirtualDomainFromDir(dir, rootDomain string) responseFn { +func generateVirtualDomainFromDir(dir, rootDomain string, perPrefixConfig map[string]projectConfig) responseFn { return func(t *testing.T, wd string) api.VirtualDomain { t.Helper() @@ -81,12 +81,17 @@ func generateVirtualDomainFromDir(dir, rootDomain string) responseFn { prefix = "/" } + cfg, ok := perPrefixConfig[prefix] + if !ok { + cfg = projectConfig{} + } + lookupPath := api.LookupPath{ // TODO: allow configuring response // Related MR in progress https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/498 - ProjectID: 123, - AccessControl: false, - HTTPSOnly: false, + ProjectID: cfg.projectID, + AccessControl: cfg.accessControl, + HTTPSOnly: cfg.https, Prefix: prefix, Source: api.Source{ Type: "zip", -- cgit v1.2.3 From 4cf524630a4cb677841d08c8dcbbb2a779d59ac1 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Tue, 15 Jun 2021 11:45:20 +1000 Subject: Remove prefix from config --- test/acceptance/testdata/api_responses.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/acceptance/testdata/api_responses.go b/test/acceptance/testdata/api_responses.go index 2de6fea4..860bd3a9 100644 --- a/test/acceptance/testdata/api_responses.go +++ b/test/acceptance/testdata/api_responses.go @@ -20,7 +20,6 @@ type responseFn func(*testing.T, string) api.VirtualDomain var DomainResponses = map[string]responseFn{ "zip-from-disk.gitlab.io": customDomain(projectConfig{ projectID: 123, - prefix: "/", pathOnDisk: "@hashed/zip-from-disk.gitlab.io", }), "zip-from-disk-not-found.gitlab.io": customDomain(projectConfig{}), @@ -31,7 +30,6 @@ var DomainResponses = map[string]responseFn{ "group.404.gitlab-example.com": generateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com", nil), "domain.404.com": customDomain(projectConfig{ projectID: 1000, - prefix: "/", pathOnDisk: "group.404/domain.404", }), // NOTE: before adding more domains here, generate the zip archive by running (per project) @@ -109,10 +107,10 @@ func generateVirtualDomainFromDir(dir, rootDomain string, perPrefixConfig map[st } type projectConfig struct { + // refer to makeGitLabPagesAccessStub for custom HTTP responses per projectID projectID int accessControl bool https bool - prefix string pathOnDisk string } @@ -129,7 +127,7 @@ func customDomain(config projectConfig) responseFn { ProjectID: config.projectID, AccessControl: config.accessControl, HTTPSOnly: config.https, - Prefix: config.prefix, + Prefix: "/", // prefix should always be `/` for custom domains Source: api.Source{ Type: "zip", Path: fmt.Sprintf("file://%s/%s/public.zip", wd, config.pathOnDisk), -- cgit v1.2.3 From a257bbf2e769f864bf59441554d19ee383eab9e0 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Thu, 17 Jun 2021 14:21:39 +1000 Subject: Update https redirect tests --- test/acceptance/serving_test.go | 47 +++++++++++++++++-------------- test/acceptance/testdata/api_responses.go | 17 +++++++++-- 2 files changed, 40 insertions(+), 24 deletions(-) (limited to 'test') diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index 09fba7e1..32291e52 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -334,30 +334,35 @@ func TestHttpToHttpsRedirectEnabled(t *testing.T) { require.Equal(t, http.StatusOK, rsp.StatusCode) } -func TestHttpsOnlyGroupEnabled(t *testing.T) { - skipUnlessEnabled(t) - - teardown := RunPagesProcess(t, *pagesBinary, supportedListeners(), "") - defer teardown() - - // TODO: allow configuring HTTPS responses from stub https://gitlab.com/gitlab-org/gitlab-pages/-/issues/571 - // Related MR in progress https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/498 - rsp, err := GetRedirectPage(t, httpListener, "group.https-only.gitlab-example.com", "project1/") - require.NoError(t, err) - defer rsp.Body.Close() - require.Equal(t, http.StatusMovedPermanently, rsp.StatusCode) -} +func TestHTTPSRedirect(t *testing.T) { + RunPagesProcessWithStubGitLabServer(t, + withListeners([]ListenSpec{httpListener}), + ) -func TestHttpsOnlyGroupDisabled(t *testing.T) { - skipUnlessEnabled(t) + tests := map[string]struct { + path string + expectedStatus int + }{ + "domain_https_only_true": { + path: "project1/", + expectedStatus: http.StatusMovedPermanently, + }, + "domain_https_only_false": { + path: "project2/", + expectedStatus: http.StatusOK, + }, + } - teardown := RunPagesProcess(t, *pagesBinary, supportedListeners(), "") - defer teardown() + for name, test := range tests { + t.Run(name, func(t *testing.T) { + // see testdata/api_responses.go for per prefix configuration response from the API + rsp, err := GetRedirectPage(t, httpListener, "group.https-only.gitlab-example.com", test.path) + require.NoError(t, err) + defer rsp.Body.Close() - rsp, err := GetPageFromListener(t, httpListener, "group.https-only.gitlab-example.com", "project2/") - require.NoError(t, err) - defer rsp.Body.Close() - require.Equal(t, http.StatusOK, rsp.StatusCode) + require.Equal(t, test.expectedStatus, rsp.StatusCode) + }) + } } func TestHttpsOnlyProjectEnabled(t *testing.T) { diff --git a/test/acceptance/testdata/api_responses.go b/test/acceptance/testdata/api_responses.go index 860bd3a9..8d423716 100644 --- a/test/acceptance/testdata/api_responses.go +++ b/test/acceptance/testdata/api_responses.go @@ -28,6 +28,16 @@ var DomainResponses = map[string]responseFn{ "group.gitlab-example.com": generateVirtualDomainFromDir("group", "group.gitlab-example.com", nil), "CapitalGroup.gitlab-example.com": generateVirtualDomainFromDir("CapitalGroup", "CapitalGroup.gitlab-example.com", nil), "group.404.gitlab-example.com": generateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com", nil), + "group.https-only.gitlab-example.com": generateVirtualDomainFromDir("group.https-only", "group.https-only.gitlab-example.com", map[string]projectConfig{ + "/project1": { + projectID: 1000, + https: true, + }, + "/project2": { + projectID: 1100, + https: false, + }, + }), "domain.404.com": customDomain(projectConfig{ projectID: 1000, pathOnDisk: "group.404/domain.404", @@ -85,8 +95,6 @@ func generateVirtualDomainFromDir(dir, rootDomain string, perPrefixConfig map[st } lookupPath := api.LookupPath{ - // TODO: allow configuring response - // Related MR in progress https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/498 ProjectID: cfg.projectID, AccessControl: cfg.accessControl, HTTPSOnly: cfg.https, @@ -127,7 +135,10 @@ func customDomain(config projectConfig) responseFn { ProjectID: config.projectID, AccessControl: config.accessControl, HTTPSOnly: config.https, - Prefix: "/", // prefix should always be `/` for custom domains + // prefix should always be `/` for custom domains, otherwise `resolvePath` will try + // to look for files under public/prefix/ when serving content instead of just public/ + // see internal/serving/disk/ for details + Prefix: "/", Source: api.Source{ Type: "zip", Path: fmt.Sprintf("file://%s/%s/public.zip", wd, config.pathOnDisk), -- cgit v1.2.3