Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logic signatures for both repo and API sync functions #21

Merged
merged 1 commit into from
Aug 11, 2024
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
2 changes: 1 addition & 1 deletion src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func givenAccount() model.Account {
Domain: model.DomainDetails{
ID: "123",
Name: "Switcher GitOps",
Version: "",
Version: "0",
LastCommit: "",
LastDate: "",
Status: model.StatusOutSync,
Expand Down
58 changes: 39 additions & 19 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,32 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
// Update account status: Out of sync
account.Domain.LastCommit = repositoryData.CommitHash
account.Domain.LastDate = repositoryData.CommitDate
account.Domain.Status = model.StatusOutSync
account.Domain.Message = "Syncing up..."
c.AccountRepository.Update(&account)
c.updateDomainStatus(account, model.StatusOutSync, "Syncing up...")

// Check for changes
diff, err := c.checkForChanges(account.Domain.ID, account.Environment, repositoryData.Content)
diff, snapshotApi, err := c.checkForChanges(account.Domain.ID, account.Environment, repositoryData.Content)

if err != nil {
// Update account status: Error
account.Domain.Status = model.StatusError
account.Domain.Message = "Error syncing up"
c.AccountRepository.Update(&account)
c.updateDomainStatus(account, model.StatusError, "Failed to check for changes")
return
}

// Apply changes
c.applyChanges(account, diff)
if snapshotApi.Domain.Version > account.Domain.Version {
account = c.applyChangesToRepository(account, snapshotApi.Domain)
} else if len(diff.Changes) > 0 {
account = c.applyChangesToAPI(account, repositoryData)
}

// Update account status: Synced
account.Domain.Status = model.StatusSynced
account.Domain.Message = "Synced successfully"
c.AccountRepository.Update(&account)
c.updateDomainStatus(account, model.StatusSynced, "Synced successfully")
}

func (c *CoreHandler) checkForChanges(domainId string, environment string, content string) (model.DiffResult, error) {
func (c *CoreHandler) checkForChanges(domainId string, environment string, content string) (model.DiffResult, model.Snapshot, error) {
// Get Snapshot from API
snapshotJsonFromApi, err := c.ApiService.FetchSnapshot(domainId, environment)

if err != nil {
return model.DiffResult{}, err
return model.DiffResult{}, model.Snapshot{}, err
}

// Convert API JSON to model.Snapshot
Expand All @@ -115,19 +111,43 @@ func (c *CoreHandler) checkForChanges(domainId string, environment string, conte
diffChanged := c.ComparatorService.CheckSnapshotDiff(left, right, CHANGED)
diffDeleted := c.ComparatorService.CheckSnapshotDiff(left, right, DELETED)

return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted}), nil
return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted}), snapshotApi.Snapshot, nil
}

func (c *CoreHandler) applyChanges(account model.Account, diff model.DiffResult) {
// Apply changes
func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *model.RepositoryData) model.Account {
// Push changes to API
println("Pushing changes to API")

// Update domain
account.Domain.Version = "2"
account.Domain.LastCommit = repositoryData.CommitHash

return account
}

func (c *CoreHandler) applyChangesToRepository(account model.Account, domain model.Domain) model.Account {
// Push changes to repository
println("Pushing changes to repository")

// Update domain
account.Domain.Version = domain.Version
account.Domain.LastCommit = "111"

return account
}

func isRepositoryOutSync(account model.Account, lastCommit string) bool {
return account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit
}

func getTimeWindow(window string) (int, time.Duration) {
// Convert window string to time.Duration
duration, _ := time.ParseDuration(window)
return 1, duration
}

func (c *CoreHandler) updateDomainStatus(account model.Account, status string, message string) {
account.Domain.Status = status
account.Domain.Message = message
account.Domain.LastDate = time.Now().Format(time.ANSIC)
c.AccountRepository.Update(&account)
}
40 changes: 38 additions & 2 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestStartAccountHandler(t *testing.T) {
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
account.Domain.Version = "1"
coreHandler.AccountRepository.Create(&account)

// Prepare goroutine signals
Expand All @@ -82,8 +83,43 @@ func TestStartAccountHandler(t *testing.T) {
// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Equal(t, "Synced successfully", accountFromDb.Domain.Message)
assert.Contains(t, accountFromDb.Domain.Message, "Synced successfully")
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
assert.Equal(t, "2", accountFromDb.Domain.Version)
assert.NotEqual(t, "", accountFromDb.Domain.LastDate)

tearDown()
})

t.Run("Should sync successfully when API has a newer version", func(t *testing.T) {
// Given
fakeGitService := NewFakeGitService()
fakeApiService := NewFakeApiService(false)
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
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)

// Wait for the goroutine to run and terminate
time.Sleep(1 * time.Second)
quit <- true
wg.Wait()

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "Synced successfully")
assert.Equal(t, "111", accountFromDb.Domain.LastCommit)
assert.Equal(t, "1", accountFromDb.Domain.Version)
assert.NotEqual(t, "", accountFromDb.Domain.LastDate)

tearDown()
Expand Down Expand Up @@ -114,7 +150,7 @@ func TestStartAccountHandler(t *testing.T) {
// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
assert.Equal(t, "Error syncing up", accountFromDb.Domain.Message)
assert.Contains(t, accountFromDb.Domain.Message, "Failed to check for changes")
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
assert.NotEqual(t, "", accountFromDb.Domain.LastDate)

Expand Down
3 changes: 2 additions & 1 deletion src/model/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package model

type Domain struct {
Group []Group `json:"group,omitempty"`
Group []Group `json:"group,omitempty"`
Version string `json:"version,omitempty"`
}

type Group struct {
Expand Down