From 8fa5d49eba5991e7039b362d8f9500f0858cdb9f Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Mon, 18 Dec 2023 21:09:50 +0300
Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?=
 =?UTF-8?q?=D0=BE=D1=81=D0=BB=D0=B5=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?=
 =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20perxis-go?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/setup_test.go    | 30 +++++++++++++++---------------
 pkg/spaces/service.go      |  6 +++---
 pkg/spaces/service_test.go | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 18 deletions(-)

diff --git a/pkg/setup/setup_test.go b/pkg/setup/setup_test.go
index 7e7483f4..a3729263 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 520d8cd9..421e7d8b 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 30594bce..2dc84a88 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)
+			}
+		})
+	}
+}
-- 
GitLab