Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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())
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down
31 changes: 11 additions & 20 deletions src/core/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"sync"
"time"

"github.com/switcherapi/switcher-gitops/src/model"
Expand Down Expand Up @@ -32,35 +31,27 @@ 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
c.status = 1
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)
Expand Down
53 changes: 18 additions & 35 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"context"
"encoding/json"
"sync"
"testing"
"time"

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down