Skip to content

Commit

Permalink
Use a local test server instead of example.com
Browse files Browse the repository at this point in the history
  • Loading branch information
karlkfi committed Mar 21, 2024
1 parent a6ea3e2 commit 593ffc8
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions api/krusty/accumulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package krusty_test

import (
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -169,10 +171,15 @@ spec:

func TestAccumulateResourcesErrors(t *testing.T) {
type testcase struct {
name string
resource string
isAbsolute bool
files map[string]string
name string
resource string
// resourceFunc generates a resource string using the URL to the local
// test server (optional).
resourceFunc func(string) string
// resourceServerSetup configures the local test server (optional).
resourceServerSetup func(*http.ServeMux)
isAbsolute bool
files map[string]string
// errFile, errDir are regex for the expected error message output
// when kustomize tries to accumulate resource as file and dir,
// respectively. The test substitutes occurrences of "%s" in the
Expand Down Expand Up @@ -221,18 +228,30 @@ resources:
for _, test := range []testcase{
{
name: "remote file not considered repo",
// The example.com second-level domain is reserved and
// safe to access, see RFC 2606.
resource: "https://example.com/segments-too-few-to-be-repo",
resourceFunc: func(url string) string {
return fmt.Sprintf("%s/segments-too-few-to-be-repo", url)
},
resourceServerSetup: func(server *http.ServeMux) {
server.HandleFunc("/", func(out http.ResponseWriter, req *http.Request) {
out.WriteHeader(404)

Check failure on line 236 in api/krusty/accumulation_test.go

View workflow job for this annotation

GitHub Actions / Lint

"404" can be replaced by http.StatusNotFound (usestdlibvars)
})
},
// It's acceptable for the error output of a remote file-like
// resource to not indicate the resource's status as a
// local directory. Though it is possible for a remote file-like
// resource to be a local directory, it is very unlikely.
errFile: `HTTP Error: status code 404 \(Not Found\)\z`,
},
{
name: "remote file qualifies as repo",
resource: "https://example.com/long/enough/to/have/org/and/repo",
name: "remote file qualifies as repo",
resourceFunc: func(url string) string {
return fmt.Sprintf("%s/long/enough/to/have/org/and/repo", url)
},
resourceServerSetup: func(server *http.ServeMux) {
server.HandleFunc("/", func(out http.ResponseWriter, req *http.Request) {
out.WriteHeader(500)

Check failure on line 252 in api/krusty/accumulation_test.go

View workflow job for this annotation

GitHub Actions / Lint

"500" can be replaced by http.StatusInternalServerError (usestdlibvars)
})
},
// TODO(4788): This error message is technically wrong. Just
// because we fail to GET a resource does not mean the resource is
// not a remote file. We should return the GET status code as well.
Expand Down Expand Up @@ -285,6 +304,19 @@ resources:
},
} {
t.Run(test.name, func(t *testing.T) {
if test.resourceFunc != nil {
// Configure test server handler
handler := http.NewServeMux()
if test.resourceServerSetup != nil {
test.resourceServerSetup(handler)
}
// Start test server
svr := httptest.NewServer(handler)
defer svr.Close()
// Generate resource with test server address
test.resource = test.resourceFunc(svr.URL)
}

// Should use real file system to indicate that we are creating
// new temporary directories on disk when we attempt to fetch repos.
fs, tmpDir := kusttest_test.Setup(t)
Expand Down

0 comments on commit 593ffc8

Please sign in to comment.