diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-08-28 08:22:46 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-08-28 08:28:40 +0300 |
commit | a49524dc7d9c664a48bbd62c11591637e4209e5b (patch) | |
tree | b08b186f3b410e524ae14fa306109c1e5c0abf8d | |
parent | 9474a11af3f797d1fc6da71f929669faa9426b1d (diff) |
Convert TestDomainSource to table test
Take apiCalled as arg
-rw-r--r-- | acceptance_test.go | 186 | ||||
-rw-r--r-- | helpers_test.go | 8 |
2 files changed, 90 insertions, 104 deletions
diff --git a/acceptance_test.go b/acceptance_test.go index df7210a9..1fae9c8f 100644 --- a/acceptance_test.go +++ b/acceptance_test.go @@ -1708,115 +1708,101 @@ func TestTLSVersions(t *testing.T) { } } -func TestGitlabDomainsSource(t *testing.T) { +func TestDomainsSource(t *testing.T) { skipUnlessEnabled(t) - t.Run("gitlab_domain_source", func(t *testing.T) { - source, apiCalled := NewGitlabDomainsSourceStub(t) - defer source.Close() - - gitLabAPISecretKey := CreateGitLabAPISecretKeyFixtureFile(t) - - pagesArgs := []string{"-gitlab-server", source.URL, "-api-secret-key", gitLabAPISecretKey, "-domain-config-source", "gitlab"} - - teardown := RunPagesProcessWithEnvs(t, true, *pagesBinary, listeners, "", []string{}, pagesArgs...) - defer teardown() - - t.Run("when a domain exists", func(t *testing.T) { - response, err := GetPageFromListener(t, httpListener, "new-source-test.gitlab.io", "/my/pages/project/") - require.NoError(t, err) - - defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) - require.NoError(t, err) - - require.Equal(t, http.StatusOK, response.StatusCode) - require.Equal(t, "New Pages GitLab Source TEST OK\n", string(body)) - require.True(t, *apiCalled) - }) - - t.Run("when a domain does not exists", func(t *testing.T) { - response, err := GetPageFromListener(t, httpListener, "non-existent-domain.gitlab.io", "/path") - defer response.Body.Close() - require.NoError(t, err) - - require.Equal(t, http.StatusNotFound, response.StatusCode) - require.True(t, *apiCalled) - }) - }) - - t.Run("disk-domain-source", func(t *testing.T) { - source, apiCalled := NewGitlabDomainsSourceStub(t) - defer source.Close() - - gitLabAPISecretKey := CreateGitLabAPISecretKeyFixtureFile(t) - - pagesArgs := []string{"-gitlab-server", source.URL, "-api-secret-key", gitLabAPISecretKey, "-domain-config-source", "disk"} - - teardown := RunPagesProcessWithEnvs(t, true, *pagesBinary, listeners, "", []string{}, pagesArgs...) - defer teardown() - - t.Run("when a domain exists", func(t *testing.T) { - // test.domain.com sourced from disk configuration - response, err := GetPageFromListener(t, httpListener, "test.domain.com", "/") - require.NoError(t, err) - - defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) - require.NoError(t, err) - - require.Equal(t, http.StatusOK, response.StatusCode) - require.Equal(t, "main-dir\n", string(body)) - - // apiCalled == false implies that disk was ready and the configuration was loaded successfully - require.False(t, *apiCalled) - }) - - t.Run("when a domain does not exists", func(t *testing.T) { - response, err := GetPageFromListener(t, httpListener, "non-existent-domain.gitlab.io", "/path") - defer response.Body.Close() - require.NoError(t, err) - - require.Equal(t, http.StatusNotFound, response.StatusCode) - require.False(t, *apiCalled) - }) - }) - - // TODO: modify mock so we can test auto when API is not ready https://gitlab.com/gitlab-org/gitlab/-/issues/218358 - t.Run("auto-domain-source", func(t *testing.T) { - source, apiCalled := NewGitlabDomainsSourceStub(t) - defer source.Close() - - gitLabAPISecretKey := CreateGitLabAPISecretKeyFixtureFile(t) + type args struct { + configSource string + domain string + urlSuffix string + } + type want struct { + statusCode int + content string + apiCalled bool + } + tests := []struct { + name string + args args + want want + }{ + { + name: "gitlab_source_domain_exists", + args: args{ + configSource: "gitlab", + domain: "new-source-test.gitlab.io", + urlSuffix: "/my/pages/project/", + }, + want: want{ + statusCode: http.StatusOK, + content: "New Pages GitLab Source TEST OK\n", + apiCalled: true, + }, + }, + { + name: "gitlab_source_domain_does_not_exist", + args: args{ + configSource: "gitlab", + domain: "non-existent-domain.gitlab.io", + }, + want: want{ + statusCode: http.StatusNotFound, + apiCalled: true, + }, + }, + { + name: "disk_source_domain_exists", + args: args{ + configSource: "disk", + // test.domain.com sourced from disk configuration + domain: "test.domain.com", + urlSuffix: "/", + }, + want: want{ + statusCode: http.StatusOK, + content: "main-dir\n", + apiCalled: false, + }, + }, + { + name: "disk_source_domain_does_not_exist", + args: args{ + configSource: "disk", + domain: "non-existent-domain.gitlab.io", + }, + want: want{ + statusCode: http.StatusNotFound, + apiCalled: false, + }, + }, + // TODO: modify mock so we can test domain-config-source=auto when API/disk is not ready https://gitlab.com/gitlab-org/gitlab/-/issues/218358 + } - pagesArgs := []string{"-gitlab-server", source.URL, "-api-secret-key", gitLabAPISecretKey, "-domain-config-source", "disk"} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var apiCalled bool + source := NewGitlabDomainsSourceStub(t, &apiCalled) + defer source.Close() - teardown := RunPagesProcessWithEnvs(t, true, *pagesBinary, listeners, "", []string{}, pagesArgs...) - defer teardown() + gitLabAPISecretKey := CreateGitLabAPISecretKeyFixtureFile(t) - t.Run("when a domain exists", func(t *testing.T) { - // test.domain.com sourced from disk configuration - response, err := GetPageFromListener(t, httpListener, "test.domain.com", "/") - require.NoError(t, err) + pagesArgs := []string{"-gitlab-server", source.URL, "-api-secret-key", gitLabAPISecretKey, "-domain-config-source", tt.args.configSource} + teardown := RunPagesProcessWithEnvs(t, true, *pagesBinary, listeners, "", []string{}, pagesArgs...) + defer teardown() - defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) + response, err := GetPageFromListener(t, httpListener, tt.args.domain, tt.args.urlSuffix) require.NoError(t, err) - require.Equal(t, http.StatusOK, response.StatusCode) - require.Equal(t, "main-dir\n", string(body)) - - // apiCalled == false implies that disk was ready and the configuration was loaded successfully - require.False(t, *apiCalled) - }) + require.Equal(t, tt.want.statusCode, response.StatusCode) + if tt.want.statusCode == http.StatusOK { + defer response.Body.Close() + body, err := ioutil.ReadAll(response.Body) + require.NoError(t, err) - t.Run("when a domain does not exists", func(t *testing.T) { - response, err := GetPageFromListener(t, httpListener, "non-existent-domain.gitlab.io", "/path") - defer response.Body.Close() - require.NoError(t, err) + require.Equal(t, tt.want.content, string(body), "content mismatch") + } - require.Equal(t, http.StatusNotFound, response.StatusCode) - require.False(t, *apiCalled) + require.Equal(t, tt.want.apiCalled, apiCalled, "api called mismatch") }) - }) + } } diff --git a/helpers_test.go b/helpers_test.go index 16032c38..926fc372 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -420,15 +420,15 @@ func waitForRoundtrips(t *testing.T, listeners []ListenSpec, timeout time.Durati require.Equal(t, len(listeners), nListening, "all listeners must be accepting TCP connections") } -func NewGitlabDomainsSourceStub(t *testing.T) (*httptest.Server, *bool) { - called := false +func NewGitlabDomainsSourceStub(t *testing.T, apiCalled *bool) *httptest.Server { + *apiCalled = false mux := http.NewServeMux() mux.HandleFunc("/api/v4/internal/pages/status", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) handler := func(w http.ResponseWriter, r *http.Request) { - called = true + *apiCalled = true domain := r.URL.Query().Get("host") path := "shared/lookups/" + domain + ".json" @@ -450,7 +450,7 @@ func NewGitlabDomainsSourceStub(t *testing.T) (*httptest.Server, *bool) { } mux.HandleFunc("/api/v4/internal/pages", handler) - return httptest.NewServer(mux), &called + return httptest.NewServer(mux) } func newConfigFile(configs ...string) (string, error) { |