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

pull/8605/head
andres-portainer 2 years ago committed by GitHub
parent b498cd657f
commit eda07614ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,8 +22,7 @@ func Test_SatisfiesAPIKeyServiceInterface(t *testing.T) {
func Test_GenerateApiKey(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())
@ -76,8 +75,7 @@ func Test_GenerateApiKey(t *testing.T) {
func Test_GetAPIKey(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())
@ -96,8 +94,7 @@ func Test_GetAPIKey(t *testing.T) {
func Test_GetAPIKeys(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())
@ -117,8 +114,7 @@ func Test_GetAPIKeys(t *testing.T) {
func Test_GetDigestUserAndKey(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())
@ -153,8 +149,7 @@ func Test_GetDigestUserAndKey(t *testing.T) {
func Test_UpdateAPIKey(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())
@ -199,8 +194,7 @@ func Test_UpdateAPIKey(t *testing.T) {
func Test_DeleteAPIKey(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())
@ -240,8 +234,7 @@ func Test_DeleteAPIKey(t *testing.T) {
func Test_InvalidateUserKeyCache(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
service := NewAPIKeyService(store.APIKeyRepository(), store.User())

@ -29,8 +29,7 @@ func TestService_StackByWebhookID(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode. Normally takes ~1s to run.")
}
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
b := stackBuilder{t: t, store: store}
b.createNewStack(newGuidString(t))
@ -87,8 +86,7 @@ func Test_RefreshableStacks(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode. Normally takes ~1s to run.")
}
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
staticStack := portainer.Stack{ID: 1}
stackWithWebhook := portainer.Stack{ID: 2, AutoUpdate: &portainer.AutoUpdateSettings{Webhook: "webhook"}}

@ -10,8 +10,7 @@ import (
func Test_teamByName(t *testing.T) {
t.Run("When store is empty should return ErrObjectNotFound", func(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
_, err := store.Team().TeamByName("name")
assert.ErrorIs(t, err, errors.ErrObjectNotFound)
@ -19,8 +18,7 @@ func Test_teamByName(t *testing.T) {
})
t.Run("When there is no object with the same name should return ErrObjectNotFound", func(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
teamBuilder := teamBuilder{
t: t,
@ -35,8 +33,7 @@ func Test_teamByName(t *testing.T) {
})
t.Run("When there is an object with the same name should return the object", func(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
teamBuilder := teamBuilder{
t: t,

@ -11,8 +11,7 @@ import (
)
func TestCreateBackupFolders(t *testing.T) {
_, store, teardown := MustNewTestStore(t, true, true)
defer teardown()
_, store := MustNewTestStore(t, true, true)
connection := store.GetConnection()
backupPath := path.Join(connection.GetStorePath(), backupDefaults.backupDir)
@ -28,9 +27,7 @@ func TestCreateBackupFolders(t *testing.T) {
}
func TestStoreCreation(t *testing.T) {
_, store, teardown := MustNewTestStore(t, true, true)
defer teardown()
_, store := MustNewTestStore(t, true, true)
if store == nil {
t.Error("Expect to create a store")
}
@ -41,9 +38,8 @@ func TestStoreCreation(t *testing.T) {
}
func TestBackup(t *testing.T) {
_, store, teardown := MustNewTestStore(t, true, true)
_, store := MustNewTestStore(t, true, true)
connection := store.GetConnection()
defer teardown()
t.Run("Backup should create default db backup", func(t *testing.T) {
v := models.Version{
@ -71,8 +67,7 @@ func TestBackup(t *testing.T) {
}
func TestRemoveWithOptions(t *testing.T) {
_, store, teardown := MustNewTestStore(t, true, true)
defer teardown()
_, store := MustNewTestStore(t, true, true)
t.Run("successfully removes file if existent", func(t *testing.T) {
store.createBackupFolders()

@ -27,8 +27,7 @@ const (
// TestStoreFull an eventually comprehensive set of tests for the Store.
// The idea is what we write to the store, we should read back.
func TestStoreFull(t *testing.T) {
_, store, teardown := MustNewTestStore(t, true, true)
defer teardown()
_, store := MustNewTestStore(t, true, true)
testCases := map[string]func(t *testing.T){
"User Accounts": func(t *testing.T) {

@ -163,8 +163,7 @@ func TestMigrateData(t *testing.T) {
}
func Test_getBackupRestoreOptions(t *testing.T) {
_, store, teardown := MustNewTestStore(t, false, true)
defer teardown()
_, store := MustNewTestStore(t, false, true)
options := getBackupRestoreOptions(store.commonBackupDir())
@ -182,8 +181,7 @@ func Test_getBackupRestoreOptions(t *testing.T) {
func TestRollback(t *testing.T) {
t.Run("Rollback should restore upgrade after backup", func(t *testing.T) {
version := models.Version{SchemaVersion: "2.4.0"}
_, store, teardown := MustNewTestStore(t, true, false)
defer teardown()
_, store := MustNewTestStore(t, true, false)
err := store.VersionService.UpdateVersion(&version)
if err != nil {
@ -240,7 +238,7 @@ func migrateDBTestHelper(t *testing.T, srcPath, wantPath string, overrideInstanc
// Parse source json to db.
// When we create a new test store, it sets its version field automatically to latest.
_, store, _ := MustNewTestStore(t, true, false)
_, store := MustNewTestStore(t, true, false)
fmt.Println("store.path=", store.GetConnection().GetDatabaseFilePath())
store.connection.DeleteObject("version", []byte("VERSION"))

@ -14,27 +14,19 @@ const dummyLogoURL = "example.com"
// for unit testing usage only since using NewStore will cause cycle import inside migrator pkg
func initTestingSettingsService(dbConn portainer.Connection, preSetObj map[string]interface{}) error {
//insert a obj
if err := dbConn.UpdateObject("settings", []byte("SETTINGS"), preSetObj); err != nil {
return err
}
return nil
return dbConn.UpdateObject("settings", []byte("SETTINGS"), preSetObj)
}
func setup(store *Store) error {
var err error
dummySettingsObj := map[string]interface{}{
"LogoURL": dummyLogoURL,
}
err = initTestingSettingsService(store.connection, dummySettingsObj)
if err != nil {
return err
}
return nil
return initTestingSettingsService(store.connection, dummySettingsObj)
}
func TestMigrateSettings(t *testing.T) {
_, store, teardown := MustNewTestStore(t, false, true)
defer teardown()
_, store := MustNewTestStore(t, false, true)
err := setup(store)
if err != nil {
@ -46,9 +38,11 @@ func TestMigrateSettings(t *testing.T) {
if updatedSettings.LogoURL != dummyLogoURL { // ensure a pre-migrate setting isn't unset
t.Errorf("unexpected value changes in the updated settings, want LogoURL value: %s, got LogoURL value: %s", dummyLogoURL, updatedSettings.LogoURL)
}
if updatedSettings.OAuthSettings.SSO != false { // I recon golang defaulting will make this false
t.Errorf("unexpected default OAuth SSO setting, want: false, got: %t", updatedSettings.OAuthSettings.SSO)
}
if updatedSettings.OAuthSettings.LogoutURI != "" {
t.Errorf("unexpected default OAuth HideInternalAuth setting, want:, got: %s", updatedSettings.OAuthSettings.LogoutURI)
}
@ -72,18 +66,23 @@ func TestMigrateSettings(t *testing.T) {
DockerhubService: store.DockerHubService,
AuthorizationService: authorization.NewService(store),
})
if err := m.MigrateSettingsToDB30(); err != nil {
t.Errorf("failed to update settings: %v", err)
}
if err != nil {
t.Errorf("failed to retrieve the updated settings: %v", err)
}
if updatedSettings.LogoURL != dummyLogoURL {
t.Errorf("unexpected value changes in the updated settings, want LogoURL value: %s, got LogoURL value: %s", dummyLogoURL, updatedSettings.LogoURL)
}
if updatedSettings.OAuthSettings.SSO != false {
t.Errorf("unexpected default OAuth SSO setting, want: false, got: %t", updatedSettings.OAuthSettings.SSO)
}
if updatedSettings.OAuthSettings.LogoutURI != "" {
t.Errorf("unexpected default OAuth HideInternalAuth setting, want:, got: %s", updatedSettings.OAuthSettings.LogoutURI)
}

@ -10,8 +10,7 @@ import (
)
func TestMigrateStackEntryPoint(t *testing.T) {
_, store, teardown := MustNewTestStore(t, false, true)
defer teardown()
_, store := MustNewTestStore(t, false, true)
stackService := store.Stack()

@ -15,13 +15,15 @@ func (store *Store) GetConnection() portainer.Connection {
return store.connection
}
func MustNewTestStore(t testing.TB, init, secure bool) (bool, *Store, func()) {
func MustNewTestStore(t testing.TB, init, secure bool) (bool, *Store) {
newStore, store, teardown, err := NewTestStore(t, init, secure)
if err != nil {
log.Fatal().Err(err).Msg("")
}
return newStore, store, teardown
t.Cleanup(teardown)
return newStore, store
}
func NewTestStore(t testing.TB, init, secure bool) (bool, *Store, func(), error) {

@ -91,8 +91,7 @@ func singleAPIRequest(h *Handler, jwt string, is *assert.Assertions, expect stri
func Test_customTemplateGitFetch(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create user(s)
user1 := &portainer.User{ID: 1, Username: "user-1", Role: portainer.StandardUserRole, PortainerAuthorizations: authorization.DefaultPortainerAuthorizations()}

@ -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)

@ -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

@ -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

@ -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)

@ -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)

@ -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 {

@ -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)

@ -73,42 +73,35 @@ var endpointTestCases = []endpointTestCase{
},
}
func setupHandler(t *testing.T) (*Handler, func(), error) {
func mustSetupHandler(t *testing.T) *Handler {
tmpDir := t.TempDir()
fs, err := filesystem.NewService(tmpDir, "")
if err != nil {
return nil, nil, fmt.Errorf("could not start a new filesystem service: %w", err)
t.Fatalf("could not start a new filesystem service: %s", err)
}
_, store, storeTeardown := datastore.MustNewTestStore(t, true, true)
_, store := datastore.MustNewTestStore(t, true, true)
ctx := context.Background()
shutdownCtx, cancelFn := context.WithCancel(ctx)
teardown := func() {
cancelFn()
storeTeardown()
}
t.Cleanup(cancelFn)
jwtService, err := jwt.NewService("1h", store)
if err != nil {
teardown()
return nil, nil, fmt.Errorf("could not start a new jwt service: %w", err)
t.Fatalf("could not start a new JWT service: %s", err)
}
apiKeyService := apikey.NewAPIKeyService(nil, nil)
settings, err := store.Settings().Settings()
if err != nil {
teardown()
return nil, nil, fmt.Errorf("could not create new settings: %w", err)
t.Fatalf("could not create new settings: %s", err)
}
settings.TrustOnFirstConnect = true
err = store.Settings().UpdateSettings(settings)
if err != nil {
teardown()
return nil, nil, fmt.Errorf("could not update settings: %w", err)
t.Fatalf("could not update settings: %s", err)
}
handler := NewHandler(
@ -120,7 +113,7 @@ func setupHandler(t *testing.T) (*Handler, func(), error) {
handler.ReverseTunnelService = chisel.NewService(store, shutdownCtx)
return handler, teardown, nil
return handler
}
func createEndpoint(handler *Handler, endpoint portainer.Endpoint, endpointRelation portainer.EndpointRelation) (err error) {
@ -138,22 +131,16 @@ func createEndpoint(handler *Handler, endpoint portainer.Endpoint, endpointRelat
}
func TestMissingEdgeIdentifier(t *testing.T) {
handler, teardown, err := setupHandler(t)
defer teardown()
if err != nil {
t.Fatal(err)
}
handler := mustSetupHandler(t)
endpointID := portainer.EndpointID(45)
err = createEndpoint(handler, portainer.Endpoint{
err := createEndpoint(handler, portainer.Endpoint{
ID: endpointID,
Name: "endpoint-id-45",
Type: portainer.EdgeAgentOnDockerEnvironment,
URL: "https://portainer.io:9443",
EdgeID: "edge-id",
}, portainer.EndpointRelation{EndpointID: endpointID})
if err != nil {
t.Fatal(err)
}
@ -172,15 +159,10 @@ func TestMissingEdgeIdentifier(t *testing.T) {
}
func TestWithEndpoints(t *testing.T) {
handler, teardown, err := setupHandler(t)
defer teardown()
if err != nil {
t.Fatal(err)
}
handler := mustSetupHandler(t)
for _, test := range endpointTestCases {
err = createEndpoint(handler, test.endpoint, test.endpointRelation)
err := createEndpoint(handler, test.endpoint, test.endpointRelation)
if err != nil {
t.Fatal(err)
}
@ -203,12 +185,7 @@ func TestWithEndpoints(t *testing.T) {
}
func TestLastCheckInDateIncreases(t *testing.T) {
handler, teardown, err := setupHandler(t)
defer teardown()
if err != nil {
t.Fatal(err)
}
handler := mustSetupHandler(t)
endpointID := portainer.EndpointID(56)
endpoint := portainer.Endpoint{
@ -224,7 +201,7 @@ func TestLastCheckInDateIncreases(t *testing.T) {
EndpointID: endpoint.ID,
}
err = createEndpoint(handler, endpoint, endpointRelation)
err := createEndpoint(handler, endpoint, endpointRelation)
if err != nil {
t.Fatal(err)
}
@ -254,12 +231,7 @@ func TestLastCheckInDateIncreases(t *testing.T) {
}
func TestEmptyEdgeIdWithAgentPlatformHeader(t *testing.T) {
handler, teardown, err := setupHandler(t)
defer teardown()
if err != nil {
t.Fatal(err)
}
handler := mustSetupHandler(t)
endpointID := portainer.EndpointID(44)
edgeId := "edge-id"
@ -274,7 +246,7 @@ func TestEmptyEdgeIdWithAgentPlatformHeader(t *testing.T) {
EndpointID: endpoint.ID,
}
err = createEndpoint(handler, endpoint, endpointRelation)
err := createEndpoint(handler, endpoint, endpointRelation)
if err != nil {
t.Fatal(err)
}
@ -302,12 +274,7 @@ func TestEmptyEdgeIdWithAgentPlatformHeader(t *testing.T) {
}
func TestEdgeStackStatus(t *testing.T) {
handler, teardown, err := setupHandler(t)
defer teardown()
if err != nil {
t.Fatal(err)
}
handler := mustSetupHandler(t)
endpointID := portainer.EndpointID(7)
endpoint := portainer.Endpoint{
@ -343,7 +310,7 @@ func TestEdgeStackStatus(t *testing.T) {
}
handler.DataStore.EdgeStack().Create(edgeStack.ID, &edgeStack)
err = createEndpoint(handler, endpoint, endpointRelation)
err := createEndpoint(handler, endpoint, endpointRelation)
if err != nil {
t.Fatal(err)
}
@ -374,12 +341,7 @@ func TestEdgeStackStatus(t *testing.T) {
}
func TestEdgeJobsResponse(t *testing.T) {
handler, teardown, err := setupHandler(t)
defer teardown()
if err != nil {
t.Fatal(err)
}
handler := mustSetupHandler(t)
endpointID := portainer.EndpointID(77)
endpoint := portainer.Endpoint{
@ -395,7 +357,7 @@ func TestEdgeJobsResponse(t *testing.T) {
EndpointID: endpoint.ID,
}
err = createEndpoint(handler, endpoint, endpointRelation)
err := createEndpoint(handler, endpoint, endpointRelation)
if err != nil {
t.Fatal(err)
}

@ -19,8 +19,7 @@ import (
func TestEndpointDeleteEdgeGroupsConcurrently(t *testing.T) {
const endpointsCount = 100
_, store, teardown := datastore.MustNewTestStore(t, true, false)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, false)
user := &portainer.User{ID: 2, Username: "admin", Role: portainer.AdministratorRole}
err := store.User().Create(user)

@ -22,7 +22,6 @@ type endpointListTest struct {
}
func Test_EndpointList_AgentVersion(t *testing.T) {
version1Endpoint := portainer.Endpoint{
ID: 1,
GroupID: 1,
@ -39,15 +38,13 @@ func Test_EndpointList_AgentVersion(t *testing.T) {
noVersionEndpoint := portainer.Endpoint{ID: 3, Type: portainer.AgentOnDockerEnvironment, GroupID: 1}
notAgentEnvironments := portainer.Endpoint{ID: 4, Type: portainer.DockerEnvironment, GroupID: 1}
handler, teardown := setupEndpointListHandler(t, []portainer.Endpoint{
handler := setupEndpointListHandler(t, []portainer.Endpoint{
notAgentEnvironments,
version1Endpoint,
version2Endpoint,
noVersionEndpoint,
})
defer teardown()
type endpointListAgentVersionTest struct {
endpointListTest
filter []string
@ -111,7 +108,7 @@ func Test_endpointList_edgeFilter(t *testing.T) {
regularTrustedEdgeStandard := portainer.Endpoint{ID: 4, UserTrusted: true, Edge: portainer.EnvironmentEdgeSettings{AsyncMode: false}, GroupID: 1, Type: portainer.EdgeAgentOnDockerEnvironment}
regularEndpoint := portainer.Endpoint{ID: 5, GroupID: 1, Type: portainer.DockerEnvironment}
handler, teardown := setupEndpointListHandler(t, []portainer.Endpoint{
handler := setupEndpointListHandler(t, []portainer.Endpoint{
trustedEdgeAsync,
untrustedEdgeAsync,
regularUntrustedEdgeStandard,
@ -119,8 +116,6 @@ func Test_endpointList_edgeFilter(t *testing.T) {
regularEndpoint,
})
defer teardown()
type endpointListEdgeTest struct {
endpointListTest
edgeAsync *bool
@ -184,9 +179,9 @@ func Test_endpointList_edgeFilter(t *testing.T) {
}
}
func setupEndpointListHandler(t *testing.T, endpoints []portainer.Endpoint) (handler *Handler, teardown func()) {
func setupEndpointListHandler(t *testing.T, endpoints []portainer.Endpoint) *Handler {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
_, store := datastore.MustNewTestStore(t, true, true)
for _, endpoint := range endpoints {
err := store.Endpoint().Create(&endpoint)
@ -197,13 +192,13 @@ func setupEndpointListHandler(t *testing.T, endpoints []portainer.Endpoint) (han
is.NoError(err, "error creating a user")
bouncer := testhelpers.NewTestRequestBouncer()
handler = NewHandler(bouncer, nil)
handler := NewHandler(bouncer, nil)
handler.DataStore = store
handler.ComposeStackManager = testhelpers.NewComposeStackManager()
handler.SnapshotService, _ = snapshot.NewService("1s", store, nil, nil, nil)
return handler, teardown
return handler
}
func buildEndpointListRequest(query string) *http.Request {

@ -39,9 +39,7 @@ func Test_Filter_AgentVersion(t *testing.T) {
notAgentEnvironments,
}
handler, teardown := setupFilterTest(t, endpoints)
defer teardown()
handler := setupFilterTest(t, endpoints)
tests := []filterTest{
{
@ -89,9 +87,7 @@ func Test_Filter_edgeFilter(t *testing.T) {
regularEndpoint,
}
handler, teardown := setupFilterTest(t, endpoints)
defer teardown()
handler := setupFilterTest(t, endpoints)
tests := []filterTest{
{
@ -155,9 +151,9 @@ func runTest(t *testing.T, test filterTest, handler *Handler, endpoints []portai
}
func setupFilterTest(t *testing.T, endpoints []portainer.Endpoint) (handler *Handler, teardown func()) {
func setupFilterTest(t *testing.T, endpoints []portainer.Endpoint) *Handler {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
_, store := datastore.MustNewTestStore(t, true, true)
for _, endpoint := range endpoints {
err := store.Endpoint().Create(&endpoint)
@ -168,9 +164,9 @@ func setupFilterTest(t *testing.T, endpoints []portainer.Endpoint) (handler *Han
is.NoError(err, "error creating a user")
bouncer := testhelpers.NewTestRequestBouncer()
handler = NewHandler(bouncer, nil)
handler := NewHandler(bouncer, nil)
handler.DataStore = store
handler.ComposeStackManager = testhelpers.NewComposeStackManager()
return handler, teardown
return handler
}

@ -70,10 +70,8 @@ func Test_updateEdgeGroups(t *testing.T) {
}
testFn := func(t *testing.T, testCase testCase) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
err := store.Endpoint().Create(testCase.endpoint)
is.NoError(err)

@ -74,10 +74,8 @@ func Test_updateTags(t *testing.T) {
}
testFn := func(t *testing.T, testCase testCase) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
err := store.Endpoint().Create(testCase.endpoint)
is.NoError(err)

@ -22,8 +22,7 @@ import (
func Test_helmDelete(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
err := store.Endpoint().Create(&portainer.Endpoint{ID: 1})
is.NoError(err, "Error creating environment")

@ -25,8 +25,7 @@ import (
func Test_helmInstall(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
err := store.Endpoint().Create(&portainer.Endpoint{ID: 1})
is.NoError(err, "error creating environment")

@ -24,8 +24,7 @@ import (
func Test_helmList(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
err := store.Endpoint().Create(&portainer.Endpoint{ID: 1})
assert.NoError(t, err, "error creating environment")

@ -13,8 +13,7 @@ import (
)
func TestHandler_webhookInvoke(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
webhookID := newGuidString(t)
store.StackService.Create(&portainer.Stack{

@ -17,8 +17,7 @@ import (
func TestTagDeleteEdgeGroupsConcurrently(t *testing.T) {
const tagsCount = 100
_, store, teardown := datastore.MustNewTestStore(t, true, false)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, false)
user := &portainer.User{ID: 2, Username: "admin", Role: portainer.AdministratorRole}
err := store.User().Create(user)

@ -21,8 +21,7 @@ import (
func Test_teamList(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create admin
adminUser := &portainer.User{ID: 1, Username: "admin", Role: portainer.AdministratorRole}

@ -21,8 +21,7 @@ import (
func Test_userCreateAccessToken(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create admin and standard user(s)
adminUser := &portainer.User{ID: 1, Username: "admin", Role: portainer.AdministratorRole}

@ -17,8 +17,7 @@ import (
func Test_deleteUserRemovesAccessTokens(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create standard user
user := &portainer.User{ID: 2, Username: "standard", Role: portainer.StandardUserRole}

@ -20,8 +20,7 @@ import (
func Test_userGetAccessTokens(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create admin and standard user(s)
adminUser := &portainer.User{ID: 1, Username: "admin", Role: portainer.AdministratorRole}

@ -23,8 +23,7 @@ import (
func Test_userList(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create admin and standard user(s)
adminUser := &portainer.User{ID: 1, Username: "admin", Role: portainer.AdministratorRole}

@ -18,8 +18,7 @@ import (
func Test_userRemoveAccessToken(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create admin and standard user(s)
adminUser := &portainer.User{ID: 1, Username: "admin", Role: portainer.AdministratorRole}

@ -17,8 +17,7 @@ import (
func Test_updateUserRemovesAccessTokens(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create standard user
user := &portainer.User{ID: 2, Username: "standard", Role: portainer.StandardUserRole}

@ -37,8 +37,7 @@ func tokenLookupFail(r *http.Request) *portainer.TokenData {
func Test_mwAuthenticateFirst(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
jwtService, err := jwt.NewService("1h", store)
assert.NoError(t, err, "failed to create a copy of service")
@ -260,8 +259,7 @@ func Test_extractAPIKeyQueryParam(t *testing.T) {
func Test_apiKeyLookup(t *testing.T) {
is := assert.New(t)
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
// create standard user
user := &portainer.User{ID: 2, Username: "standard", Role: portainer.StandardUserRole}

@ -5,11 +5,11 @@ import (
"strings"
"testing"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/datastore"
gittypes "github.com/portainer/portainer/api/git/types"
"github.com/portainer/portainer/api/internal/testhelpers"
portainer "github.com/portainer/portainer/api"
gittypes "github.com/portainer/portainer/api/git/types"
"github.com/stretchr/testify/assert"
)
@ -55,8 +55,7 @@ func (s *noopDeployer) StopRemoteSwarmStack(stack *portainer.Stack, endpoint *po
}
func Test_redeployWhenChanged_FailsWhenCannotFindStack(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
err := RedeployWhenChanged(1, nil, store, nil)
assert.Error(t, err)
@ -64,8 +63,7 @@ func Test_redeployWhenChanged_FailsWhenCannotFindStack(t *testing.T) {
}
func Test_redeployWhenChanged_DoesNothingWhenNotAGitBasedStack(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
admin := &portainer.User{ID: 1, Username: "admin"}
err := store.User().Create(admin)
@ -79,8 +77,7 @@ func Test_redeployWhenChanged_DoesNothingWhenNotAGitBasedStack(t *testing.T) {
}
func Test_redeployWhenChanged_DoesNothingWhenNoGitChanges(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
tmpDir := t.TempDir()
@ -110,8 +107,7 @@ func Test_redeployWhenChanged_DoesNothingWhenNoGitChanges(t *testing.T) {
func Test_redeployWhenChanged_FailsWhenCannotClone(t *testing.T) {
cloneErr := errors.New("failed to clone")
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
admin := &portainer.User{ID: 1, Username: "admin"}
err := store.User().Create(admin)
@ -138,8 +134,7 @@ func Test_redeployWhenChanged_FailsWhenCannotClone(t *testing.T) {
}
func Test_redeployWhenChanged(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
tmpDir := t.TempDir()
@ -191,8 +186,7 @@ func Test_redeployWhenChanged(t *testing.T) {
}
func Test_getUserRegistries(t *testing.T) {
_, store, teardown := datastore.MustNewTestStore(t, true, true)
defer teardown()
_, store := datastore.MustNewTestStore(t, true, true)
endpointID := 123

Loading…
Cancel
Save