diff --git a/pkg/setup/setup.go b/pkg/setup/setup.go index 5b238806369d3dedd9e52a7bdbe0901d0237565b..b087337fcb57e3cb1a76cad1e3354291b944bdb0 100644 --- a/pkg/setup/setup.go +++ b/pkg/setup/setup.go @@ -49,10 +49,11 @@ func NewSetup(content *content.Content, spaceID, environmentID string, logger *z logger = logger.With(zap.String("Space", spaceID), zap.String("Environment", environmentID)) return &Setup{ - SpaceID: spaceID, - EnvironmentID: environmentID, - content: content, - logger: logger, + SpaceID: spaceID, + EnvironmentID: environmentID, + content: content, + logger: logger, + waitSpaceAvailable: true, } } @@ -76,9 +77,9 @@ func (s *Setup) IsRemove() bool { return s.remove } -func (s *Setup) WithWaitSpaceAvailable() *Setup { +func (s *Setup) WithoutWaitSpace() *Setup { setup := *s - setup.waitSpaceAvailable = true + setup.waitSpaceAvailable = false return &setup } @@ -158,7 +159,7 @@ func (s *Setup) AddItem(item *items.Item, opt ...ItemsOption) *Setup { // Install выполняет установку необходимых требований func (s *Setup) Install(ctx context.Context) error { if s.waitSpaceAvailable { - if err := spaces.WaitSpaceAvailable(ctx, s.content.Spaces, s.SpaceID, s.logger); err != nil { + if err := spaces.WaitSpaceAvailable(ctx, s.content.Spaces, s.SpaceID); err != nil { return err } } @@ -180,7 +181,7 @@ func (s *Setup) Install(ctx context.Context) error { // Check выполняет проверку требований func (s *Setup) Check(ctx context.Context) error { if s.waitSpaceAvailable { - if err := spaces.WaitSpaceReadAvailable(ctx, s.content.Spaces, s.SpaceID, s.logger); err != nil { + if err := spaces.WaitSpaceReadable(ctx, s.content.Spaces, s.SpaceID); err != nil { return err } } @@ -202,7 +203,7 @@ func (s *Setup) Check(ctx context.Context) error { // Uninstall выполняет удаление установленных раннее требований func (s *Setup) Uninstall(ctx context.Context) error { if s.waitSpaceAvailable { - if err := spaces.WaitSpaceAvailable(ctx, s.content.Spaces, s.SpaceID, s.logger); err != nil { + if err := spaces.WaitSpaceAvailable(ctx, s.content.Spaces, s.SpaceID); err != nil { return err } } diff --git a/pkg/setup/setup_test.go b/pkg/setup/setup_test.go index cebbbde677dcaab305358868398e14d1a37a6ca0..604c2f6b29bae32756e2149ca572ce3b54920936 100644 --- a/pkg/setup/setup_test.go +++ b/pkg/setup/setup_test.go @@ -97,10 +97,13 @@ func TestSetupInstall(t *testing.T) { t.Run("Success, nothing to install", func(t *testing.T) { logger := zaptest.NewLogger(t, zaptest.WrapOptions()) - setup := NewSetup(nil, spaceID, envID, logger) + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := NewSetup(&content.Content{Spaces: spcMock}, spaceID, envID, logger) err := setup.Install(context.Background()) require.NoError(t, err) + spcMock.AssertExpectations(t) }) @@ -161,12 +164,16 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, Environments: envMocks, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -179,6 +186,7 @@ func TestSetupInstall(t *testing.T) { clMock.AssertExpectations(t) itmMock.AssertExpectations(t) envMocks.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Success, update existing records", func(t *testing.T) { @@ -222,12 +230,16 @@ func TestSetupInstall(t *testing.T) { Return(nil). Times(len(itms)) + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, Environments: envMocks, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -240,6 +252,7 @@ func TestSetupInstall(t *testing.T) { clMock.AssertExpectations(t) itmMock.AssertExpectations(t) envMocks.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Success, with force", func(t *testing.T) { @@ -276,11 +289,15 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithForce(true) @@ -293,6 +310,7 @@ func TestSetupInstall(t *testing.T) { collsMock.AssertExpectations(t) clMock.AssertExpectations(t) itmMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't install role, storage returns error", func(t *testing.T) { @@ -301,8 +319,12 @@ func TestSetupInstall(t *testing.T) { Return(nil, errors.New("can't get role")). Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ - Roles: rMock, + Roles: rMock, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -311,6 +333,7 @@ func TestSetupInstall(t *testing.T) { assert.ErrorContains(t, err, "failed to install role") rMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't install client, storage returns error", func(t *testing.T) { @@ -332,9 +355,13 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Clients: clMock, Roles: rMock, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -344,6 +371,7 @@ func TestSetupInstall(t *testing.T) { rMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't get collection, storage returns error", func(t *testing.T) { @@ -376,10 +404,14 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -390,6 +422,7 @@ func TestSetupInstall(t *testing.T) { rMock.AssertExpectations(t) collsMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't create collection, storage returns error", func(t *testing.T) { @@ -426,10 +459,14 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -440,6 +477,7 @@ func TestSetupInstall(t *testing.T) { rMock.AssertExpectations(t) collsMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't update collection, storage returns error", func(t *testing.T) { @@ -469,10 +507,14 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -483,6 +525,7 @@ func TestSetupInstall(t *testing.T) { rMock.AssertExpectations(t) collsMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't set schema, storage returns error", func(t *testing.T) { @@ -523,10 +566,14 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -537,6 +584,7 @@ func TestSetupInstall(t *testing.T) { rMock.AssertExpectations(t) collsMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't migrate, storage returns error", func(t *testing.T) { @@ -582,11 +630,15 @@ func TestSetupInstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Environments: envMocks, + Spaces: spcMock, }, t) err := setup.Install(context.Background()) @@ -597,6 +649,7 @@ func TestSetupInstall(t *testing.T) { rMock.AssertExpectations(t) collsMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't find action, storage returns error", func(t *testing.T) { @@ -644,12 +697,16 @@ func TestSetupInstall(t *testing.T) { Return(nil, errors.New("can't create item")). Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, Environments: envMocks, + Spaces: spcMock, }, t) setup = setup.WithForce(true) @@ -663,6 +720,7 @@ func TestSetupInstall(t *testing.T) { clMock.AssertExpectations(t) itmMock.AssertExpectations(t) envMocks.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Success on retry when space not available", func(t *testing.T) { @@ -672,7 +730,7 @@ func TestSetupInstall(t *testing.T) { sp.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) - err := setup.WithWaitSpaceAvailable().Install(context.Background()) + err := setup.Install(context.Background()) require.NoError(t, err) @@ -685,13 +743,21 @@ func TestSetupInstall(t *testing.T) { sp.On("Get", mock.Anything, mock.Anything).Return(&spaces.Space{ID: spaceID, OrgID: "org", StateInfo: &spaces.StateInfo{State: spaces.StateMigration}}, nil).Twice() sp.On("Get", mock.Anything, mock.Anything).Return(nil, errors.New("some error")).Once() setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) - err := setup.WithWaitSpaceAvailable().Install(context.Background()) + err := setup.Install(context.Background()) require.Error(t, err, "в момент выполнения пришла ошибка отличная от 'space not available'") sp.AssertExpectations(t) }) + t.Run("WithoutWaitSpace", func(t *testing.T) { + logger := zaptest.NewLogger(t, zaptest.WrapOptions()) + setup := NewSetup(nil, spaceID, envID, logger) + err := setup.WithoutWaitSpace().Install(context.Background()) + + require.NoError(t, err) + }) + //t.Run("Can't find task configs, storage returns error", func(t *testing.T) { // envMocks := &environmentMock.Environments{} // envMocks.On("Migrate", mock.Anything, spaceID, envID). @@ -780,11 +846,14 @@ func TestSetupUninstall(t *testing.T) { t.Run("Success, nothing to uninstall", func(t *testing.T) { logger := zaptest.NewLogger(t, zaptest.WrapOptions()) - setup := NewSetup(nil, spaceID, envID, logger) + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := NewSetup(&content.Content{Spaces: spcMock}, spaceID, envID, logger) err := setup.Uninstall(context.Background()) require.NoError(t, err) + spcMock.AssertExpectations(t) }) @@ -821,11 +890,15 @@ func TestSetupUninstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithRemove(true) @@ -838,6 +911,7 @@ func TestSetupUninstall(t *testing.T) { collsMock.AssertExpectations(t) clMock.AssertExpectations(t) itmMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Remove, with clients NotFound error", func(t *testing.T) { @@ -873,11 +947,15 @@ func TestSetupUninstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithRemove(true) @@ -890,6 +968,7 @@ func TestSetupUninstall(t *testing.T) { collsMock.AssertExpectations(t) clMock.AssertExpectations(t) itmMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't uninstall clients, storage returns error", func(t *testing.T) { @@ -907,10 +986,14 @@ func TestSetupUninstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithRemove(true) @@ -921,6 +1004,7 @@ func TestSetupUninstall(t *testing.T) { rMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) @@ -942,10 +1026,14 @@ func TestSetupUninstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Roles: rMock, Clients: clMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithRemove(true) @@ -955,6 +1043,7 @@ func TestSetupUninstall(t *testing.T) { assert.ErrorContains(t, err, "failed to uninstall role") rMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't uninstall collections, storage returns error", func(t *testing.T) { @@ -982,11 +1071,15 @@ func TestSetupUninstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithRemove(true) @@ -998,6 +1091,7 @@ func TestSetupUninstall(t *testing.T) { rMock.AssertExpectations(t) collsMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't uninstall actions, storage returns error", func(t *testing.T) { @@ -1031,11 +1125,15 @@ func TestSetupUninstall(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) setup = setup.WithRemove(true) @@ -1045,6 +1143,7 @@ func TestSetupUninstall(t *testing.T) { assert.ErrorContains(t, err, "failed to uninstall item") itmMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Success on retry when space not available", func(t *testing.T) { @@ -1056,7 +1155,7 @@ func TestSetupUninstall(t *testing.T) { setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) - err := setup.WithWaitSpaceAvailable().Uninstall(context.Background()) + err := setup.Uninstall(context.Background()) require.NoError(t, err) @@ -1072,10 +1171,20 @@ func TestSetupUninstall(t *testing.T) { setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) - err := setup.WithWaitSpaceAvailable().Uninstall(context.Background()) + err := setup.Uninstall(context.Background()) require.Error(t, err, "в момент выполнения пришла ошибка отличная от 'space not available'") }) + + t.Run("WithoutWaitSpace", func(t *testing.T) { + logger := zaptest.NewLogger(t, zaptest.WrapOptions()) + + setup := NewSetup(nil, spaceID, envID, logger) + + err := setup.WithoutWaitSpace().Uninstall(context.Background()) + + require.NoError(t, err) + }) } func TestSetupCheck(t *testing.T) { @@ -1088,11 +1197,15 @@ func TestSetupCheck(t *testing.T) { t.Run("Success, nothing to check", func(t *testing.T) { logger := zaptest.NewLogger(t, zaptest.WrapOptions()) - setup := NewSetup(nil, spaceID, envID, logger) + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + + setup := NewSetup(&content.Content{Spaces: spcMock}, spaceID, envID, logger) err := setup.Check(context.Background()) require.NoError(t, err) + spcMock.AssertExpectations(t) }) t.Run("Success", func(t *testing.T) { @@ -1128,11 +1241,15 @@ func TestSetupCheck(t *testing.T) { mock.MatchedBy(func(opt *items.FindOptions) bool { return opt.Regular && opt.Hidden && opt.Templates }), ).Return(getActions(), 0, nil).Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Collections: collsMock, Clients: clMock, Roles: rMock, Items: itmMock, + Spaces: spcMock, }, t) err := setup.Check(context.Background()) @@ -1144,6 +1261,7 @@ func TestSetupCheck(t *testing.T) { collsMock.AssertExpectations(t) clMock.AssertExpectations(t) itmMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't get role, storage returns error", func(t *testing.T) { @@ -1152,8 +1270,12 @@ func TestSetupCheck(t *testing.T) { Return(nil, errors.New("can't get role")). Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ - Roles: rMock, + Roles: rMock, + Spaces: spcMock, }, t) err := setup.Check(context.Background()) @@ -1162,6 +1284,7 @@ func TestSetupCheck(t *testing.T) { assert.ErrorContains(t, err, "role check error") rMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't get client, storage returns error", func(t *testing.T) { @@ -1177,9 +1300,13 @@ func TestSetupCheck(t *testing.T) { Return(nil, errors.New("can't get client")). Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Roles: rMock, Clients: clMock, + Spaces: spcMock, }, t) err := setup.Check(context.Background()) @@ -1189,6 +1316,7 @@ func TestSetupCheck(t *testing.T) { rMock.AssertExpectations(t) clMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't get collection, storage returns error", func(t *testing.T) { @@ -1211,10 +1339,14 @@ func TestSetupCheck(t *testing.T) { Once() } + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Roles: rMock, Clients: clMock, Collections: collsMock, + Spaces: spcMock, }, t) err := setup.Check(context.Background()) @@ -1225,6 +1357,7 @@ func TestSetupCheck(t *testing.T) { rMock.AssertExpectations(t) clMock.AssertExpectations(t) collsMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Can't get action, storage returns error", func(t *testing.T) { @@ -1254,11 +1387,15 @@ func TestSetupCheck(t *testing.T) { Return(nil, 0, nil). Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + setup := newSetup(&content.Content{ Roles: rMock, Clients: clMock, Collections: collsMock, Items: itmMock, + Spaces: spcMock, }, t) err := setup.Check(context.Background()) @@ -1269,38 +1406,49 @@ func TestSetupCheck(t *testing.T) { rMock.AssertExpectations(t) clMock.AssertExpectations(t) collsMock.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Success on retry when space is preparing", func(t *testing.T) { logger := zaptest.NewLogger(t, zaptest.WrapOptions()) - sp := &mocks.Spaces{} - sp.On("Get", mock.Anything, mock.Anything).Return(&spaces.Space{ID: spaceID, OrgID: "org", StateInfo: &spaces.StateInfo{State: spaces.StatePreparing}}, nil).Twice() - sp.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(&spaces.Space{ID: spaceID, OrgID: "org", StateInfo: &spaces.StateInfo{State: spaces.StatePreparing}}, nil).Twice() + spcMock.On("Get", mock.Anything, mock.Anything).Return(sps, nil).Once() - setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) + setup := NewSetup(&content.Content{Spaces: spcMock}, spaceID, envID, logger) - err := setup.WithWaitSpaceAvailable().Check(context.Background()) + err := setup.Check(context.Background()) require.NoError(t, err) - sp.AssertExpectations(t) + spcMock.AssertExpectations(t) }) t.Run("Error on retry", func(t *testing.T) { logger := zaptest.NewLogger(t, zaptest.WrapOptions()) - sp := &mocks.Spaces{} - sp.On("Get", mock.Anything, mock.Anything).Return(&spaces.Space{ID: spaceID, OrgID: "org", StateInfo: &spaces.StateInfo{State: spaces.StatePreparing}}, nil).Twice() - sp.On("Get", mock.Anything, mock.Anything).Return(nil, errors.New("some error")).Once() + spcMock := &mocks.Spaces{} + spcMock.On("Get", mock.Anything, mock.Anything).Return(&spaces.Space{ID: spaceID, OrgID: "org", StateInfo: &spaces.StateInfo{State: spaces.StatePreparing}}, nil).Twice() + spcMock.On("Get", mock.Anything, mock.Anything).Return(nil, errors.New("some error")).Once() - setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) + setup := NewSetup(&content.Content{Spaces: spcMock}, spaceID, envID, logger) - err := setup.WithWaitSpaceAvailable().Check(context.Background()) + err := setup.Check(context.Background()) require.Error(t, err, "в момент выполнения пришла ошибка отличная от 'Space is migrating'") }) + t.Run("WithoutWaitSpace", func(t *testing.T) { + logger := zaptest.NewLogger(t, zaptest.WrapOptions()) + + setup := NewSetup(nil, spaceID, envID, logger) + + err := setup.WithoutWaitSpace().Check(context.Background()) + + require.NoError(t, err) + }) + //t.Run("Can't get task config, storage returns error", func(t *testing.T) { // collsMock := &collectionMock.Collections{} // for _, collection := range getCollections() { diff --git a/pkg/spaces/service.go b/pkg/spaces/service.go index 4bbc7c7efb917b46c0114d82f7f1dc727b7771dc..40c6bd9ebf7a16f694bec40388dd4a7dd8208473 100644 --- a/pkg/spaces/service.go +++ b/pkg/spaces/service.go @@ -6,8 +6,6 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/errors" "github.com/avast/retry-go/v4" - - "go.uber.org/zap" ) // @microgen grpc @@ -74,7 +72,7 @@ func IsSpaceAvailable(ctx context.Context, svc Spaces, spaceID string) error { return ErrSpaceNotAvailable } -func IsSpaceReadAvailable(ctx context.Context, svc Spaces, spaceID string) error { +func IsSpaceReadable(ctx context.Context, svc Spaces, spaceID string) error { sp, err := svc.Get(ctx, spaceID) if err != nil { return errors.Wrap(err, "fail to get space") @@ -85,30 +83,24 @@ func IsSpaceReadAvailable(ctx context.Context, svc Spaces, spaceID string) error return errors.WithContext(ErrSpaceNotAvailable, "state", sp.StateInfo.State) } -func WaitSpaceAvailable(ctx context.Context, svc Spaces, spaceID string, logger *zap.Logger) error { +func WaitSpaceAvailable(ctx context.Context, svc Spaces, spaceID string) error { return retry.Do( func() error { return IsSpaceAvailable(ctx, svc, spaceID) }, retry.RetryIf(func(err error) bool { return errors.Is(err, ErrSpaceNotAvailable) }), - retry.OnRetry(func(n uint, err error) { - logger.Debug("Waiting for space to be available", zap.String("spaceID", spaceID), zap.Uint("Retry", n), zap.Error(err)) - }), retry.Delay(delay), retry.Attempts(attempts), retry.Context(ctx), ) } -func WaitSpaceReadAvailable(ctx context.Context, svc Spaces, spaceID string, logger *zap.Logger) error { +func WaitSpaceReadable(ctx context.Context, svc Spaces, spaceID string) error { return retry.Do( func() error { - return IsSpaceReadAvailable(ctx, svc, spaceID) + return IsSpaceReadable(ctx, svc, spaceID) }, retry.RetryIf(func(err error) bool { return errors.Is(err, ErrSpaceNotAvailable) }), - retry.OnRetry(func(n uint, err error) { - logger.Debug("Waiting for space to be available for read", zap.String("spaceID", spaceID), zap.Uint("Retry", n), zap.Error(err)) - }), retry.Delay(delay), retry.Attempts(attempts), retry.Context(ctx), diff --git a/pkg/spaces/service_test.go b/pkg/spaces/service_test.go index 8eb8d9cbb04b5a7d2fb71a82af0897ac0bb21c29..7f339e16a97c74b3da4d0bb613d490900c6cb2bc 100644 --- a/pkg/spaces/service_test.go +++ b/pkg/spaces/service_test.go @@ -51,7 +51,7 @@ func TestIsSpaceAvailable(t *testing.T) { } } -func TestIsReadAvailable(t *testing.T) { +func TestIsSpaceReadable(t *testing.T) { tests := []struct { name string space *Space @@ -81,8 +81,8 @@ func TestIsReadAvailable(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { spaces := &dummySpaces{space: tt.space} - if err := IsSpaceReadAvailable(context.Background(), spaces, "space"); (err != nil) != tt.wantErr { - t.Errorf("IsSpaceReadAvailable() error = %v, wantErr %v", err, tt.wantErr) + if err := IsSpaceReadable(context.Background(), spaces, "space"); (err != nil) != tt.wantErr { + t.Errorf("IsSpaceReadable() error = %v, wantErr %v", err, tt.wantErr) } }) }