diff --git a/Makefile b/Makefile index ab35c3e..dcd27e0 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ apps: @temporal workflow result --workflow-id apps-manual test: - cd test && go test + cd test && CLOUDLAB_ENV=${env} go test fmt: nixfmt flake.nix diff --git a/test/smoke_test.go b/test/smoke_test.go index d91951e..d3a1dc9 100644 --- a/test/smoke_test.go +++ b/test/smoke_test.go @@ -1,30 +1,87 @@ package e2e import ( + "errors" "net/http" + "os" "testing" ) -func TestBlog(t *testing.T) { - resp, err := http.Get("https://khuedoan.com") // TODO get domain name automatically +func assertStatusCode(t *testing.T, url string, expectedStatusCode int) { + t.Helper() + assertStatusCodeWithRedirects(t, url, true, expectedStatusCode) +} + +func assertStatusCodeWithRedirects(t *testing.T, url string, followRedirects bool, expectedStatusCodes ...int) { + t.Helper() + + client := &http.Client{} + if !followRedirects { + client.CheckRedirect = func(_ *http.Request, _ []*http.Request) error { + return http.ErrUseLastResponse + } + } + + resp, err := client.Get(url) if err != nil { + if !errors.Is(err, http.ErrUseLastResponse) { + t.Fatal(err) + } + } + + if resp == nil { t.Fatal(err) } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - t.Fatalf("expected status code to be 200, but got %d", resp.StatusCode) + for _, expectedStatusCode := range expectedStatusCodes { + if resp.StatusCode == expectedStatusCode { + return + } } + + t.Fatalf("expected status code %v, but got %d for %s", expectedStatusCodes, resp.StatusCode, url) +} + +func TestBlog(t *testing.T) { + assertStatusCode(t, "https://khuedoan.com", http.StatusOK) // TODO get domain name automatically } func TestHomelabDocs(t *testing.T) { - resp, err := http.Get("https://homelab.khuedoan.com") // TODO get domain name automatically - if err != nil { - t.Fatal(err) + assertStatusCode(t, "https://homelab.khuedoan.com", http.StatusOK) // TODO get domain name automatically +} + +// TODO remove env-specific stuff once we migrate everything to the same setup +func TestStaging(t *testing.T) { + if os.Getenv("CLOUDLAB_ENV") != "staging" && os.Getenv("SMOKE_STAGING") == "" { + t.Skip("set CLOUDLAB_ENV=staging or SMOKE_STAGING=1 to run staging smoke tests") + } + + testCases := []struct { + name string + url string + expectedStatusCode int + }{ + { + name: "Vault", + url: "https://vault.staging.khuedoan.com", + expectedStatusCode: http.StatusTemporaryRedirect, + }, + { + name: "Dex", + url: "https://dex.staging.khuedoan.com", + expectedStatusCode: http.StatusOK, + }, + { + name: "Forgejo", + url: "https://code.staging.khuedoan.com", + expectedStatusCode: http.StatusSeeOther, + }, } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - t.Fatalf("expected status code to be 200, but got %d", resp.StatusCode) + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + assertStatusCodeWithRedirects(t, testCase.url, false, testCase.expectedStatusCode) + }) } }