diff --git a/src/controller/account_test.go b/src/controller/account_test.go index 5dde2fe..50f3d5c 100644 --- a/src/controller/account_test.go +++ b/src/controller/account_test.go @@ -12,6 +12,8 @@ import ( "github.com/switcherapi/switcher-gitops/src/model" ) +const NOT_FOUND = "/not-found" + func TestCreateAccountHandler(t *testing.T) { t.Run("Should create an account", func(t *testing.T) { // Test @@ -61,7 +63,7 @@ func TestFetchAccountHandler(t *testing.T) { // Test payload := []byte("") - req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -76,7 +78,7 @@ func TestFetchAccountHandler(t *testing.T) { t.Run("Should not fetch an account by domain ID - not found", func(t *testing.T) { // Test payload := []byte("") - req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/111", bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+NOT_FOUND, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -92,7 +94,7 @@ func TestUpdateAccountHandler(t *testing.T) { // Test payload, _ := json.Marshal(accountV2) - req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV2.Domain.ID, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -107,7 +109,7 @@ func TestUpdateAccountHandler(t *testing.T) { t.Run("Should not update an account - invalid request", func(t *testing.T) { // Test payload := []byte("") - req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/invalid-request", bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -125,7 +127,7 @@ func TestUpdateAccountHandler(t *testing.T) { // Test payload, _ := json.Marshal(accountV1Copy) - req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+NOT_FOUND, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -140,7 +142,7 @@ func TestDeleteAccountHandler(t *testing.T) { accountController.CreateAccountHandler(givenAccountRequest(accountV1)) // Test - req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/123", nil) + req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, nil) response := executeRequest(req) // Assert @@ -149,12 +151,12 @@ func TestDeleteAccountHandler(t *testing.T) { t.Run("Should not delete an account - not found", func(t *testing.T) { // Test - req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/111", nil) + req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+NOT_FOUND, nil) response := executeRequest(req) // Assert assert.Equal(t, http.StatusInternalServerError, response.Code) - assert.Equal(t, "{\"error\":\"Error deleting account: Account not found for domain.id: 111\"}", response.Body.String()) + assert.Equal(t, "{\"error\":\"Error deleting account: Account not found for domain.id: not-found\"}", response.Body.String()) }) } diff --git a/src/controller/controller_test.go b/src/controller/controller_test.go index 0b86e14..221c7a6 100644 --- a/src/controller/controller_test.go +++ b/src/controller/controller_test.go @@ -61,7 +61,7 @@ var accountV1 = model.Account{ Repository: "switcherapi/switcher-gitops", Branch: "master", Domain: model.DomainDetails{ - ID: "123", + ID: "123-controller-test", Name: "Switcher GitOps", Version: "123", LastCommit: "123", @@ -79,7 +79,7 @@ var accountV2 = model.Account{ Repository: "switcherapi/switcher-gitops", Branch: "master", Domain: model.DomainDetails{ - ID: "123", + ID: "123-controller-test", Name: "Switcher GitOps", Version: "123", LastCommit: "123", diff --git a/src/core/core_test.go b/src/core/core_test.go index 0910eb2..8b91622 100644 --- a/src/core/core_test.go +++ b/src/core/core_test.go @@ -70,7 +70,7 @@ func givenAccount() model.Account { Branch: "master", Environment: "default", Domain: model.DomainDetails{ - ID: "123", + ID: "123-code-test", Name: "Switcher GitOps", Version: "0", LastCommit: "", diff --git a/src/core/handler.go b/src/core/handler.go index a92bd27..d89b376 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -1,7 +1,6 @@ package core import ( - "sync" "time" "github.com/switcherapi/switcher-gitops/src/model" @@ -32,7 +31,7 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) { // Iterate over accounts and start account handlers for _, account := range accounts { - go c.StartAccountHandler(account, make(chan bool), &sync.WaitGroup{}) + go c.StartAccountHandler(account) } // Update core handler status @@ -40,27 +39,19 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) { return c.status, nil } -func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool, wg *sync.WaitGroup) { - defer wg.Done() - - // Check if account is not active - if !account.Settings.Active { - return - } - - // Reads Window setting - timeWindow, unitWindow := getTimeWindow(account.Settings.Window) - +func (c *CoreHandler) StartAccountHandler(account model.Account) { for { - select { - case <-quit: + if !account.Settings.Active { return - default: - repositoryData, err := c.GitService.GetRepositoryData(account.Environment) + } + + account, _ := c.AccountRepository.FetchByDomainId(account.Domain.ID) + timeWindow, unitWindow := getTimeWindow(account.Settings.Window) + + repositoryData, err := c.GitService.GetRepositoryData(account.Environment) - if err == nil && isRepositoryOutSync(account, repositoryData.CommitHash) { - c.syncUp(account, repositoryData) - } + if err == nil && isRepositoryOutSync(*account, repositoryData.CommitHash) { + c.syncUp(*account, repositoryData) } time.Sleep(time.Duration(timeWindow) * unitWindow) diff --git a/src/core/handler_test.go b/src/core/handler_test.go index ca8f375..cb19b18 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -3,7 +3,6 @@ package core import ( "context" "encoding/json" - "sync" "testing" "time" @@ -23,6 +22,11 @@ func TestInitCoreHandlerCoroutine(t *testing.T) { // Test status, err := coreHandler.InitCoreHandlerCoroutine() + // Terminate the goroutine + account.Settings.Active = false + coreHandler.AccountRepository.Update(&account) + time.Sleep(1 * time.Second) + // Assert assert.Nil(t, err) assert.Equal(t, 1, status) @@ -37,16 +41,10 @@ func TestStartAccountHandler(t *testing.T) { account.Settings.Active = false coreHandler.AccountRepository.Create(&account) - // Prepare goroutine signals - var wg sync.WaitGroup - wg.Add(1) - // Test - go coreHandler.StartAccountHandler(account, make(chan bool), &wg) + go coreHandler.StartAccountHandler(account) - // Wait for the goroutine to run and terminate time.Sleep(1 * time.Second) - wg.Wait() // Assert accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID) @@ -67,18 +65,13 @@ func TestStartAccountHandler(t *testing.T) { account.Domain.Version = "1" coreHandler.AccountRepository.Create(&account) - // Prepare goroutine signals - var wg sync.WaitGroup - quit := make(chan bool) - wg.Add(1) - // Test - go coreHandler.StartAccountHandler(account, quit, &wg) + go coreHandler.StartAccountHandler(account) - // Wait for the goroutine to run and terminate + // Terminate the goroutine + account.Settings.Active = false + coreHandler.AccountRepository.Update(&account) time.Sleep(1 * time.Second) - quit <- true - wg.Wait() // Assert accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID) @@ -103,18 +96,13 @@ func TestStartAccountHandler(t *testing.T) { account.Domain.Version = "0" coreHandler.AccountRepository.Create(&account) - // Prepare goroutine signals - var wg sync.WaitGroup - quit := make(chan bool) - wg.Add(1) - // Test - go coreHandler.StartAccountHandler(account, quit, &wg) + go coreHandler.StartAccountHandler(account) - // Wait for the goroutine to run and terminate + // Terminate the goroutine + account.Settings.Active = false + coreHandler.AccountRepository.Update(&account) time.Sleep(1 * time.Second) - quit <- true - wg.Wait() // Assert accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID) @@ -136,18 +124,13 @@ func TestStartAccountHandler(t *testing.T) { account := givenAccount() coreHandler.AccountRepository.Create(&account) - // Prepare goroutine signals - var wg sync.WaitGroup - quit := make(chan bool) - wg.Add(1) - // Test - go coreHandler.StartAccountHandler(account, quit, &wg) + go coreHandler.StartAccountHandler(account) - // Wait for the goroutine to run and terminate + // Terminate the goroutine + account.Settings.Active = false + coreHandler.AccountRepository.Update(&account) time.Sleep(1 * time.Second) - quit <- true - wg.Wait() // Assert accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID) diff --git a/src/repository/repository_test.go b/src/repository/repository_test.go index c5bda3e..47b1530 100644 --- a/src/repository/repository_test.go +++ b/src/repository/repository_test.go @@ -42,7 +42,7 @@ func givenAccount(active bool) model.Account { Branch: "master", Environment: "default", Domain: model.DomainDetails{ - ID: "123", + ID: "123-repository-test", Name: "Switcher GitOps", Version: "123", LastCommit: "123", diff --git a/src/utils/util_test.go b/src/utils/util_test.go index c66b13c..85a2225 100644 --- a/src/utils/util_test.go +++ b/src/utils/util_test.go @@ -39,7 +39,7 @@ func givenAccount(active bool) model.Account { Repository: "switcherapi/switcher-gitops", Branch: "master", Domain: model.DomainDetails{ - ID: "123", + ID: "123-util-test", Name: "Switcher GitOps", Version: "123", LastCommit: "123",