Skip to content

Added forceprune check to limit sync diff capabilities #30

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

Merged
merged 1 commit into from
Sep 5, 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ GitOps Domain Snapshot Orchestrator for Switcher API
![Switcher API: Cloud-based Feature Flag API](https://github.com/switcherapi/switcherapi-assets/blob/master/logo/switcherapi_grey.png)

# About
**Switcher GitOps** is Domain Snapshot Orchestrator for Switcher API. It allows you to manage your feature flags and configurations in a GitOps manner. It is a simple and easy way to manage your feature flags and configurations in a versioned manner.
**Switcher GitOps** is used to orchestrate Domain Snapshots for Switcher API. It allows managing feature flags and configurations lifecycle.

# Features
- Manage Switchers with GitOps managed environment
- Auto Sync enables a fully integrated environment with Switcher API Management, Slack App and GitOps working simultaneously
- Manages Switchers with GitOps workflow (repository as a source of truth)
- Repository synchronization allows integrated tools such as Switcher API Management and Switcher Slack App to work in sync
- Flexible settings allow to define the best workflow for your ecosystem

# Integrated tests

Expand Down
16 changes: 10 additions & 6 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ 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
c.updateDomainStatus(account, model.StatusOutSync, "Syncing up...")
c.updateDomainStatus(account, model.StatusOutSync, model.MessageSyncingUp)

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

if err != nil {
c.updateDomainStatus(account, model.StatusError, "Failed to check for changes - "+err.Error())
Expand All @@ -100,12 +100,12 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
}

// Update account status: Synced
c.updateDomainStatus(account, model.StatusSynced, "Synced successfully")
c.updateDomainStatus(account, model.StatusSynced, model.MessageSynced)
}

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

if err != nil {
return model.DiffResult{}, model.Snapshot{}, err
Expand All @@ -122,7 +122,11 @@ func (c *CoreHandler) checkForChanges(domainId string, environment string, conte
// Compare Snapshots and get diff
diffNew := c.ComparatorService.CheckSnapshotDiff(fromRepo, fromApi, NEW)
diffChanged := c.ComparatorService.CheckSnapshotDiff(fromApi, fromRepo, CHANGED)
diffDeleted := c.ComparatorService.CheckSnapshotDiff(fromApi, fromRepo, DELETED)

var diffDeleted model.DiffResult
if account.Settings.ForcePrune {
diffDeleted = c.ComparatorService.CheckSnapshotDiff(fromApi, fromRepo, DELETED)
}

return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted}), snapshotApi.Snapshot, nil
}
Expand Down
38 changes: 35 additions & 3 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,40 @@ func TestStartAccountHandler(t *testing.T) {

account := givenAccount()
account.Domain.ID = "123-out-sync"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())

// Wait for goroutine to process
time.Sleep(1 * time.Second)

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

tearDown()
})

t.Run("Should sync and prune successfully when repository is out of sync", func(t *testing.T) {
// Given
fakeGitService := NewFakeGitService()
fakeGitService.content = `{
"domain": {
"group": []
}
}`
fakeApiService := NewFakeApiService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
account.Domain.ID = "123-out-sync-prune"
account.Domain.Version = "1"
account.Settings.ForcePrune = true
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
Expand All @@ -101,7 +134,7 @@ func TestStartAccountHandler(t *testing.T) {
// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "Synced successfully")
assert.Contains(t, accountFromDb.Domain.Message, model.MessageSynced)
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
assert.Equal(t, "2", accountFromDb.Domain.Version)
assert.NotEqual(t, "", accountFromDb.Domain.LastDate)
Expand All @@ -119,7 +152,6 @@ func TestStartAccountHandler(t *testing.T) {

account := givenAccount()
account.Domain.ID = "123-newer-version"
account.Domain.Version = "0"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
Expand All @@ -131,7 +163,7 @@ func TestStartAccountHandler(t *testing.T) {
// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByAccountId(string(accountCreated.ID.Hex()))
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "Synced successfully")
assert.Contains(t, accountFromDb.Domain.Message, model.MessageSynced)
assert.Equal(t, "111", accountFromDb.Domain.LastCommit)
assert.Equal(t, "1", accountFromDb.Domain.Version)
assert.NotEqual(t, "", accountFromDb.Domain.LastDate)
Expand Down
5 changes: 5 additions & 0 deletions src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const (
StatusError = "Error"
)

const (
MessageSyncingUp = "Syncing up..."
MessageSynced = "Synced successfully"
)

const (
FilePath = "resources/"
)
Expand Down
Loading