1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 07:49:41 +02:00

chore(unit-test): simplify teardown EE-5536 (#9015)

This commit is contained in:
andres-portainer 2023-05-30 11:02:22 -03:00 committed by GitHub
parent b498cd657f
commit eda07614ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 110 additions and 218 deletions

View file

@ -13,8 +13,7 @@ import (
// Create
func TestCreateAndInspect(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
// Create Endpoint, EdgeGroup and EndpointRelation
endpoint := createEndpoint(t, handler.DataStore)
@ -100,8 +99,7 @@ func TestCreateAndInspect(t *testing.T) {
}
func TestCreateWithInvalidPayload(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)

View file

@ -12,8 +12,7 @@ import (
// Delete
func TestDeleteAndInspect(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
// Create
endpoint := createEndpoint(t, handler.DataStore)
@ -73,8 +72,7 @@ func TestDeleteAndInspect(t *testing.T) {
}
func TestDeleteInvalidEdgeStack(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
cases := []struct {
Name string

View file

@ -8,8 +8,7 @@ import (
// Inspect
func TestInspectInvalidEdgeID(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
cases := []struct {
Name string

View file

@ -10,8 +10,7 @@ import (
)
func TestDeleteStatus(t *testing.T) {
handler, _, teardown := setupHandler(t)
defer teardown()
handler, _ := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)

View file

@ -13,8 +13,7 @@ import (
// Update Status
func TestUpdateStatusAndInspect(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)
@ -79,8 +78,7 @@ func TestUpdateStatusAndInspect(t *testing.T) {
}
}
func TestUpdateStatusWithInvalidPayload(t *testing.T) {
handler, _, teardown := setupHandler(t)
defer teardown()
handler, _ := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)

View file

@ -19,28 +19,25 @@ import (
)
// Helpers
func setupHandler(t *testing.T) (*Handler, string, func()) {
func setupHandler(t *testing.T) (*Handler, string) {
t.Helper()
_, store, storeTeardown := datastore.MustNewTestStore(t, true, true)
_, store := datastore.MustNewTestStore(t, true, true)
jwtService, err := jwt.NewService("1h", store)
if err != nil {
storeTeardown()
t.Fatal(err)
}
user := &portainer.User{ID: 2, Username: "admin", Role: portainer.AdministratorRole}
err = store.User().Create(user)
if err != nil {
storeTeardown()
t.Fatal(err)
}
apiKeyService := apikey.NewAPIKeyService(store.APIKeyRepository(), store.User())
rawAPIKey, _, err := apiKeyService.GenerateApiKey(*user, "test")
if err != nil {
storeTeardown()
t.Fatal(err)
}
@ -56,7 +53,6 @@ func setupHandler(t *testing.T) (*Handler, string, func()) {
fs, err := filesystem.NewService(tmpDir, "")
if err != nil {
storeTeardown()
t.Fatal(err)
}
handler.FileService = fs
@ -74,7 +70,7 @@ func setupHandler(t *testing.T) (*Handler, string, func()) {
handler.GitService = testhelpers.NewGitService(errors.New("Clone error"), "git-service-id")
return handler, rawAPIKey, storeTeardown
return handler, rawAPIKey
}
func createEndpointWithId(t *testing.T, store dataservices.DataStore, endpointID portainer.EndpointID) portainer.Endpoint {

View file

@ -14,8 +14,7 @@ import (
// Update
func TestUpdateAndInspect(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)
@ -116,8 +115,7 @@ func TestUpdateAndInspect(t *testing.T) {
}
func TestUpdateWithInvalidEdgeGroups(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)
@ -197,8 +195,7 @@ func TestUpdateWithInvalidEdgeGroups(t *testing.T) {
}
func TestUpdateWithInvalidPayload(t *testing.T) {
handler, rawAPIKey, teardown := setupHandler(t)
defer teardown()
handler, rawAPIKey := setupHandler(t)
endpoint := createEndpoint(t, handler.DataStore)
edgeStack := createEdgeStack(t, handler.DataStore, endpoint.ID)