Skip to content

Commit

Permalink
Fix test after library updates makes new list request
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Jul 8, 2024
1 parent daa36ce commit 5b6c3c9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
34 changes: 20 additions & 14 deletions git/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -117,6 +118,8 @@ func testGetCreateServer() *httptest.Server {
}
})

mux.HandleFunc("/", testutils.LogNotFoundHandler(t))

return httptest.NewServer(mux)
}

Expand All @@ -141,7 +144,7 @@ func TestGitlab_Create(t *testing.T) {
projectname: "test",
description: "desc",
},
httpServer: testGetCreateServer(),
httpServer: testGetCreateServer(t),
wantErr: false,
},
{
Expand All @@ -152,7 +155,7 @@ func TestGitlab_Create(t *testing.T) {
projectname: "test",
description: "desc",
},
httpServer: testGetCreateServer(),
httpServer: testGetCreateServer(t),
wantErr: false,
},
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

}
Expand All @@ -313,7 +318,7 @@ func TestGitlab_Update(t *testing.T) {
},
},
wantErr: false,
httpServer: testGetUpdateServer(false),
httpServer: testGetUpdateServer(t, false),
},
{
name: "update failed",
Expand All @@ -326,7 +331,7 @@ func TestGitlab_Update(t *testing.T) {
},
},
wantErr: true,
httpServer: testGetUpdateServer(true),
httpServer: testGetUpdateServer(t, true),
},
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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":"[email protected]","committer_name":"Example User","committer_email":"[email protected]","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)
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions testutils/log_not_found.go
Original file line number Diff line number Diff line change
@@ -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`))
}
}
16 changes: 11 additions & 5 deletions vault/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -337,6 +339,8 @@ func getVersionHTTPServer() *httptest.Server {

})

mux.HandleFunc("/", testutils.LogNotFoundHandler(t))

return httptest.NewServer(mux)
}

Expand Down Expand Up @@ -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)
Expand All @@ -392,6 +396,8 @@ func getListHTTPServer() *httptest.Server {
}`)
})

mux.HandleFunc("/", testutils.LogNotFoundHandler(t))

return httptest.NewServer(mux)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5b6c3c9

Please sign in to comment.