diff --git a/pkg/setup/setup_test.go b/pkg/setup/setup_test.go index 7e7483f4f5d88a46ee5bd8676123b5fb9f88d43c..a3729263b0eab70e670658d890f12ccccda49cfa 100644 --- a/pkg/setup/setup_test.go +++ b/pkg/setup/setup_test.go @@ -90,9 +90,9 @@ func newSetup(content *content.Content, t *testing.T) *Setup { func TestSetupInstall(t *testing.T) { sps := &spaces.Space{ - ID: spaceID, - OrgID: "org", - State: spaces.StateReady, + ID: spaceID, + OrgID: "org", + StateInfo: &spaces.StateInfo{State: spaces.StateReady}, } t.Run("Success, nothing to install", func(t *testing.T) { @@ -668,7 +668,7 @@ func TestSetupInstall(t *testing.T) { t.Run("Success on retry when space not available", 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", State: spaces.StateMigration}, nil).Twice() + 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(sps, nil).Once() setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) @@ -682,7 +682,7 @@ func TestSetupInstall(t *testing.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", State: spaces.StateMigration}, nil).Twice() + 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.WithWaitForSpaceReady().Install(context.Background()) @@ -772,9 +772,9 @@ func TestSetupInstall(t *testing.T) { func TestSetupUninstall(t *testing.T) { sps := &spaces.Space{ - ID: spaceID, - OrgID: "org", - State: spaces.StateReady, + ID: spaceID, + OrgID: "org", + StateInfo: &spaces.StateInfo{State: spaces.StateReady}, } t.Run("Success, nothing to uninstall", func(t *testing.T) { @@ -1051,7 +1051,7 @@ func TestSetupUninstall(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", State: spaces.StateMigration}, nil).Twice() + 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(sps, nil).Once() setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) @@ -1067,7 +1067,7 @@ func TestSetupUninstall(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", State: spaces.StateMigration}, nil).Twice() + 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) @@ -1080,9 +1080,9 @@ func TestSetupUninstall(t *testing.T) { func TestSetupCheck(t *testing.T) { sps := &spaces.Space{ - ID: spaceID, - OrgID: "org", - State: spaces.StateReady, + ID: spaceID, + OrgID: "org", + StateInfo: &spaces.StateInfo{State: spaces.StateReady}, } t.Run("Success, nothing to check", func(t *testing.T) { @@ -1275,7 +1275,7 @@ func TestSetupCheck(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", State: spaces.StatePreparing}, nil).Twice() + 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() setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) @@ -1291,7 +1291,7 @@ func TestSetupCheck(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", State: spaces.StatePreparing}, nil).Twice() + 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() setup := NewSetup(&content.Content{Spaces: sp}, spaceID, envID, logger) diff --git a/pkg/spaces/service.go b/pkg/spaces/service.go index 520d8cd91403714bc543ee789d926fd6f1349312..421e7d8b70800b3bf64714afab2aa8ebae2113aa 100644 --- a/pkg/spaces/service.go +++ b/pkg/spaces/service.go @@ -63,7 +63,7 @@ func IsSpaceAvailable(ctx context.Context, svc Spaces, spaceID string) error { if err != nil { return errors.Wrap(err, "fail to get space") } - if sp.State == StateReady || sp.State == StateNew { + if sp.StateInfo == nil || sp.StateInfo != nil && (sp.StateInfo.State == StateNew || sp.StateInfo.State == StateReady) { return nil } return ErrSpaceNotAvailable @@ -74,10 +74,10 @@ func IsReadAvailable(ctx context.Context, svc Spaces, spaceID string) error { if err != nil { return errors.Wrap(err, "fail to get space") } - if sp.State == StateReady || sp.State == StateNew || sp.State == StateMigration { + if sp.StateInfo == nil || sp.StateInfo != nil && sp.StateInfo.State == StateNew || sp.StateInfo.State == StateReady || sp.StateInfo.State == StateMigration { return nil } - return errors.WithContext(ErrSpaceNotAvailable, "state", sp.State) + return errors.WithContext(ErrSpaceNotAvailable, "state", sp.StateInfo.State) } func WaitSpaceAvailable(ctx context.Context, svc Spaces, spaceID string, logger *zap.Logger) error { diff --git a/pkg/spaces/service_test.go b/pkg/spaces/service_test.go index 30594bcea39f8ae5a7e3dc85a1bd2cdd35da2054..2dc84a88843bc0bef55fb1a567fb82c4442bc71b 100644 --- a/pkg/spaces/service_test.go +++ b/pkg/spaces/service_test.go @@ -50,3 +50,40 @@ func TestIsSpaceAvailable(t *testing.T) { }) } } + +func TestIsReadAvailable(t *testing.T) { + tests := []struct { + name string + space *Space + wantErr bool + }{ + { + "Space has nil StateInfo: available", + &Space{ID: "space", OrgID: "org", Name: "test-space"}, + false, + }, + { + "Space state is StateReady: available", + &Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StateReady}}, + false, + }, + { + "Space state is StatePreparing: not available", + &Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StatePreparing}}, + true, + }, + { + "Space state is StateMigration: not available", + &Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StateMigration}}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + spaces := &dummySpaces{space: tt.space} + if err := IsReadAvailable(context.Background(), spaces, "space"); (err != nil) != tt.wantErr { + t.Errorf("IsReadAvailable() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}