diff --git a/git/gitlab/gitlab_test.go b/git/gitlab/gitlab_test.go index 22036fbe..d09dfe94 100644 --- a/git/gitlab/gitlab_test.go +++ b/git/gitlab/gitlab_test.go @@ -18,6 +18,7 @@ import ( "go.uber.org/zap" "github.com/projectsyn/lieutenant-operator/git/manager" + "github.com/projectsyn/lieutenant-operator/testutils" ) func testGetHTTPServer(statusCode int, body []byte) *httptest.Server { @@ -82,7 +83,7 @@ func TestGitlab_Read(t *testing.T) { } //goland:noinspection HttpUrlsUsage -func testGetCreateServer() *httptest.Server { +func testGetCreateServer(t *testing.T) *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/api/v4/projects/3/deploy_keys", func(res http.ResponseWriter, req *http.Request) { @@ -117,6 +118,8 @@ func testGetCreateServer() *httptest.Server { } }) + mux.HandleFunc("/", testutils.LogNotFoundHandler(t)) + return httptest.NewServer(mux) } @@ -141,7 +144,7 @@ func TestGitlab_Create(t *testing.T) { projectname: "test", description: "desc", }, - httpServer: testGetCreateServer(), + httpServer: testGetCreateServer(t), wantErr: false, }, { @@ -152,7 +155,7 @@ func TestGitlab_Create(t *testing.T) { projectname: "test", description: "desc", }, - httpServer: testGetCreateServer(), + httpServer: testGetCreateServer(t), wantErr: false, }, { @@ -247,7 +250,7 @@ func TestGitlab_Delete(t *testing.T) { } //goland:noinspection HttpUrlsUsage -func testGetUpdateServer(fail bool) *httptest.Server { +func testGetUpdateServer(t *testing.T, fail bool) *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/api/v4/projects/3/deploy_keys", func(res http.ResponseWriter, req *http.Request) { @@ -288,6 +291,8 @@ func testGetUpdateServer(fail bool) *httptest.Server { mux.HandleFunc("/api/v4/projects/3/deploy_keys/3", deleteOk) + mux.HandleFunc("/", testutils.LogNotFoundHandler(t)) + return httptest.NewServer(mux) } @@ -313,7 +318,7 @@ func TestGitlab_Update(t *testing.T) { }, }, wantErr: false, - httpServer: testGetUpdateServer(false), + httpServer: testGetUpdateServer(t, false), }, { name: "update failed", @@ -326,7 +331,7 @@ func TestGitlab_Update(t *testing.T) { }, }, wantErr: true, - httpServer: testGetUpdateServer(true), + httpServer: testGetUpdateServer(t, true), }, } @@ -388,13 +393,12 @@ func TestGitlab_Type(t *testing.T) { } //goland:noinspection HttpUrlsUsage -func testGetCommitServer(files []string) *httptest.Server { +func testGetCommitServer(t *testing.T, files []string) *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/api/v4/projects/3/repository/tree", func(res http.ResponseWriter, req *http.Request) { if len(files) == 0 { - res.WriteHeader(http.StatusNotFound) - _, _ = res.Write([]byte(`{"error":"Tree Not Found"}`)) + _, _ = res.Write([]byte(`[]`)) return } @@ -464,6 +468,8 @@ func testGetCommitServer(files []string) *httptest.Server { _, _ = res.Write([]byte(`{"id":"ed899a2f4b50b4370feeea94676502b42383c746","short_id":"ed899a2f4b5","title":"some commit message","author_name":"Example User","author_email":"user@example.com","committer_name":"Example User","committer_email":"user@example.com","created_at":"2016-09-20T09:26:24.000-07:00","message":"some commit message","parent_ids":["ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"],"committed_date":"2016-09-20T09:26:24.000-07:00","authored_date":"2016-09-20T09:26:24.000-07:00","stats":{"additions":2,"deletions":2,"total":4},"status":null,"web_url":"https://localhost:8080/thedude/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746"}`)) }) + mux.HandleFunc("/", testutils.LogNotFoundHandler(t)) + return httptest.NewServer(mux) } @@ -479,7 +485,7 @@ func TestGitlab_CommitTemplateFiles(t *testing.T) { }{ "set template files": { wantErr: false, - httpServer: testGetCommitServer([]string{"file1"}), + httpServer: testGetCommitServer(t, []string{"file1"}), fields: fields{ project: &gitlab.Project{ ID: 3, @@ -493,7 +499,7 @@ func TestGitlab_CommitTemplateFiles(t *testing.T) { }, "set existing file": { wantErr: false, - httpServer: testGetCommitServer([]string{"file1"}), + httpServer: testGetCommitServer(t, []string{"file1"}), fields: fields{ project: &gitlab.Project{ ID: 3, @@ -507,7 +513,7 @@ func TestGitlab_CommitTemplateFiles(t *testing.T) { }, "set multiple template files": { wantErr: false, - httpServer: testGetCommitServer([]string{"file1"}), + httpServer: testGetCommitServer(t, []string{"file1"}), fields: fields{ project: &gitlab.Project{ ID: 3, @@ -523,7 +529,7 @@ func TestGitlab_CommitTemplateFiles(t *testing.T) { }, "delete file": { wantErr: false, - httpServer: testGetCommitServer([]string{"file1"}), + httpServer: testGetCommitServer(t, []string{"file1"}), fields: fields{ project: &gitlab.Project{ ID: 3, @@ -537,7 +543,7 @@ func TestGitlab_CommitTemplateFiles(t *testing.T) { }, "set and delete file while empty": { wantErr: false, - httpServer: testGetCommitServer([]string{}), + httpServer: testGetCommitServer(t, []string{}), fields: fields{ project: &gitlab.Project{ ID: 3, diff --git a/go.mod b/go.mod index eaf0fd89..166ecf5e 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/prometheus/client_golang v1.19.1 github.com/ryankurte/go-structparse v1.2.0 github.com/stretchr/testify v1.9.0 - github.com/xanzy/go-gitlab v0.105.0 + github.com/xanzy/go-gitlab v0.106.0 go.uber.org/zap v1.27.0 k8s.io/api v0.30.1 k8s.io/apimachinery v0.30.1 diff --git a/go.sum b/go.sum index 805d595b..cf21a2d0 100644 --- a/go.sum +++ b/go.sum @@ -484,8 +484,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= -github.com/xanzy/go-gitlab v0.105.0 h1:3nyLq0ESez0crcaM19o5S//SvezOQguuIHZ3wgX64hM= -github.com/xanzy/go-gitlab v0.105.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= +github.com/xanzy/go-gitlab v0.106.0 h1:EDfD03K74cIlQo2EducfiupVrip+Oj02bq9ofw5F8sA= +github.com/xanzy/go-gitlab v0.106.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ= github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/testutils/log_not_found.go b/testutils/log_not_found.go new file mode 100644 index 00000000..750198b6 --- /dev/null +++ b/testutils/log_not_found.go @@ -0,0 +1,15 @@ +package testutils + +import ( + "net/http" + "testing" +) + +// LogNotFoundHandler returns a http.HandlerFunc that returns a 404 response and logs the request to the test log. +func LogNotFoundHandler(t *testing.T) http.HandlerFunc { + return func(res http.ResponseWriter, req *http.Request) { + t.Logf("Unregistered request (404): %s %s", req.Method, req.URL.Path) + res.WriteHeader(http.StatusNotFound) + _, _ = res.Write([]byte(`not found`)) + } +} diff --git a/vault/client_test.go b/vault/client_test.go index e1c51abc..e2d317c7 100644 --- a/vault/client_test.go +++ b/vault/client_test.go @@ -12,10 +12,12 @@ import ( "github.com/go-logr/logr" "github.com/go-logr/zapr" "github.com/hashicorp/vault/api" - synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" + + synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1" + "github.com/projectsyn/lieutenant-operator/testutils" ) func testGetHTTPServer(statusCode int, body []byte) *httptest.Server { @@ -224,7 +226,7 @@ func TestBankVaultClient_RemoveSecrets(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { instanceClient = nil - server := getVersionHTTPServer() + server := getVersionHTTPServer(t) err = os.Setenv(api.EnvVaultToken, "myroot") require.NoError(t, err) @@ -243,7 +245,7 @@ func TestBankVaultClient_RemoveSecrets(t *testing.T) { } } -func getVersionHTTPServer() *httptest.Server { +func getVersionHTTPServer(t *testing.T) *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/v1/kv/delete/kv2/test/foo", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -337,6 +339,8 @@ func getVersionHTTPServer() *httptest.Server { }) + mux.HandleFunc("/", testutils.LogNotFoundHandler(t)) + return httptest.NewServer(mux) } @@ -381,7 +385,7 @@ func TestBankVaultClient_getVersionList(t *testing.T) { } } -func getListHTTPServer() *httptest.Server { +func getListHTTPServer(t *testing.T) *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/v1/kv/metadata/test", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -392,6 +396,8 @@ func getListHTTPServer() *httptest.Server { }`) }) + mux.HandleFunc("/", testutils.LogNotFoundHandler(t)) + return httptest.NewServer(mux) } @@ -424,7 +430,7 @@ func TestBankVaultClient_listSecrets(t *testing.T) { t.Run(tt.name, func(t *testing.T) { instanceClient = nil - server := getListHTTPServer() + server := getListHTTPServer(t) err = os.Setenv(api.EnvVaultToken, "myroot") require.NoError(t, err)