Skip to content

Commit

Permalink
#81 use new globalConfigRepo in configUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 7, 2024
1 parent 79a5f48 commit 121d44f
Show file tree
Hide file tree
Showing 10 changed files with 1,035 additions and 100 deletions.
25 changes: 15 additions & 10 deletions pkg/application/ecosystemConfigUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem"
"github.com/cloudogu/k8s-registry-lib/config"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -83,24 +84,28 @@ func (useCase *EcosystemConfigUseCase) ApplyConfig(ctx context.Context, blueprin
func (useCase *EcosystemConfigUseCase) applyGlobalConfigDiffs(ctx context.Context, globalConfigDiffsByAction map[domain.ConfigAction][]domain.GlobalConfigEntryDiff) error {
var errs []error

globalConfig, err := useCase.globalConfigRepository.Get(ctx)
if err != nil {
return err
}

updatedEntries := globalConfig.Config
entryDiffsToSet := globalConfigDiffsByAction[domain.ConfigActionSet]
var entriesToSet = make([]*ecosystem.GlobalConfigEntry, 0, len(entryDiffsToSet))
for _, diff := range entryDiffsToSet {
entry := &ecosystem.GlobalConfigEntry{
Key: diff.Key,
Value: common.GlobalConfigValue(diff.Expected.Value),
}
entriesToSet = append(entriesToSet, entry)
var err error
updatedEntries, err = updatedEntries.Set(diff.Key, common.GlobalConfigValue(diff.Expected.Value))
errs = append(errs, err)
}

entryDiffsToRemove := globalConfigDiffsByAction[domain.ConfigActionRemove]
var keysToDelete = make([]common.GlobalConfigKey, 0, len(entryDiffsToRemove))
for _, diff := range entryDiffsToRemove {
keysToDelete = append(keysToDelete, diff.Key)
updatedEntries = updatedEntries.Delete(diff.Key)
}

errs = append(errs, callIfNotEmpty(ctx, entriesToSet, useCase.globalConfigEntryRepository.SaveAll))
errs = append(errs, callIfNotEmpty(ctx, keysToDelete, useCase.globalConfigEntryRepository.DeleteAllByKeys))
if len(entryDiffsToSet) != 0 || len(entryDiffsToRemove) != 0 {
_, err = useCase.globalConfigRepository.Update(ctx, config.GlobalConfig{Config: updatedEntries})
errs = append(errs, err)
}

return errors.Join(errs...)
}
Expand Down
72 changes: 46 additions & 26 deletions pkg/application/ecosystemConfigUseCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
blueprintRepoMock := newMockBlueprintSpecRepository(t)
doguConfigMock := newMockDoguConfigEntryRepository(t)
sensitiveDoguConfigMock := newMockSensitiveDoguConfigEntryRepository(t)
globalConfigMock := newMockGlobalConfigEntryRepository(t)
globalConfigRepoMock := newMockGlobalConfigRepository(t)

redmineDiffToEncrypt := getSensitiveDoguConfigEntryDiffForAction("key", "value", testSimpleDoguNameRedmine, domain.ConfigActionSet)
casDiffToEncrypt := getSensitiveDoguConfigEntryDiffForAction("key", "value", testSimpleDoguNameCas, domain.ConfigActionSet)
Expand Down Expand Up @@ -63,13 +63,16 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
doguConfigMock.EXPECT().DeleteAllByKeys(testCtx, mock.Anything).Return(nil).Times(1)
sensitiveDoguConfigMock.EXPECT().SaveAll(testCtx, mock.Anything).Return(nil).Times(1)
sensitiveDoguConfigMock.EXPECT().DeleteAllByKeys(testCtx, mock.Anything).Return(nil).Times(1)
globalConfigMock.EXPECT().SaveAll(testCtx, mock.Anything).Return(nil).Times(1)
globalConfigMock.EXPECT().DeleteAllByKeys(testCtx, mock.Anything).Return(nil).Times(1)

entries, _ := config.MapToEntries(map[string]any{})
globalConfig := config.CreateGlobalConfig(entries)
globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil)
globalConfigRepoMock.EXPECT().Update(testCtx, mock.Anything).Return(globalConfig, nil)

blueprintRepoMock.EXPECT().GetById(testCtx, testBlueprintID).Return(spec, nil)
blueprintRepoMock.EXPECT().Update(testCtx, mock.Anything).Return(nil).Times(2)

sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigEntryRepository: globalConfigMock}
sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigRepository: globalConfigRepoMock}

// when
err := sut.ApplyConfig(testCtx, testBlueprintID)
Expand Down Expand Up @@ -119,7 +122,7 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
assert.Equal(t, domain.StatusPhaseRegistryConfigApplied, spec.Status)
})

t.Run("should return on mark apply config start error", func(t *testing.T) {
t.Run("should return on mark apply config start error", func(t *testing.T) {
// given
blueprintRepoMock := newMockBlueprintSpecRepository(t)

Expand Down Expand Up @@ -151,7 +154,7 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
blueprintRepoMock := newMockBlueprintSpecRepository(t)
doguConfigMock := newMockDoguConfigEntryRepository(t)
sensitiveDoguConfigMock := newMockSensitiveDoguConfigEntryRepository(t)
globalConfigMock := newMockGlobalConfigEntryRepository(t)
globalConfigMock := newMockGlobalConfigRepository(t)

casDiffToEncrypt := getSensitiveDoguConfigEntryDiffForAction("key", "value", testSimpleDoguNameCas, domain.ConfigActionSet)
spec := &domain.BlueprintSpec{
Expand Down Expand Up @@ -184,12 +187,16 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
doguConfigMock.EXPECT().SaveAll(testCtx, mock.Anything).Return(assert.AnError).Times(1)

sensitiveDoguConfigMock.EXPECT().SaveAll(testCtx, mock.Anything).Return(assert.AnError).Times(1)
globalConfigMock.EXPECT().SaveAll(testCtx, mock.Anything).Return(assert.AnError).Times(1)

entries, _ := config.MapToEntries(map[string]any{})
globalConfig := config.CreateGlobalConfig(entries)
globalConfigMock.EXPECT().Get(testCtx).Return(globalConfig, nil)
globalConfigMock.EXPECT().Update(testCtx, mock.Anything).Return(globalConfig, assert.AnError)

blueprintRepoMock.EXPECT().GetById(testCtx, testBlueprintID).Return(spec, nil)
blueprintRepoMock.EXPECT().Update(testCtx, mock.Anything).Return(nil).Times(2)

sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigEntryRepository: globalConfigMock}
sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigRepository: globalConfigMock}

// when
err := sut.ApplyConfig(testCtx, testBlueprintID)
Expand Down Expand Up @@ -269,39 +276,46 @@ func TestEcosystemConfigUseCase_applyDoguConfigDiffs(t *testing.T) {
func TestEcosystemConfigUseCase_applyGlobalConfigDiffs(t *testing.T) {
t.Run("should save diffs with action set", func(t *testing.T) {
// given
globalConfigMock := newMockGlobalConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, globalConfigMock, nil)
diff1 := getSetGlobalConfigEntryDiff("/key", "value")
diff2 := getSetGlobalConfigEntryDiff("/key1", "value1")
globalConfigMock := newMockGlobalConfigRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, nil, globalConfigMock)
diff1 := getSetGlobalConfigEntryDiff("/key1", "value1")
diff2 := getSetGlobalConfigEntryDiff("/key2", "value2")
byAction := map[domain.ConfigAction][]domain.GlobalConfigEntryDiff{domain.ConfigActionSet: {diff1, diff2}}

expectedEntry1 := &ecosystem.GlobalConfigEntry{
Key: diff1.Key,
Value: common.GlobalConfigValue(diff1.Expected.Value),
}
expectedEntry2 := &ecosystem.GlobalConfigEntry{
Key: diff2.Key,
Value: common.GlobalConfigValue(diff2.Expected.Value),
}
entries, _ := config.MapToEntries(map[string]any{})
globalConfig := config.CreateGlobalConfig(entries)

updatedEntries, err := globalConfig.Set(diff1.Key, common.GlobalConfigValue(diff1.Expected.Value))
require.NoError(t, err)
updatedEntries, err = updatedEntries.Set(diff2.Key, common.GlobalConfigValue(diff2.Expected.Value))
require.NoError(t, err)

globalConfigMock.EXPECT().SaveAll(testCtx, []*ecosystem.GlobalConfigEntry{expectedEntry1, expectedEntry2}).Return(nil).Times(1)
globalConfigMock.EXPECT().Get(testCtx).Return(globalConfig, nil)
globalConfigMock.EXPECT().Update(testCtx, config.GlobalConfig{Config: updatedEntries}).Return(globalConfig, nil)

// when
err := sut.applyGlobalConfigDiffs(testCtx, byAction)
err = sut.applyGlobalConfigDiffs(testCtx, byAction)

// then
require.NoError(t, err)
})

t.Run("should delete diffs with action remove", func(t *testing.T) {
// given
globalConfigMock := newMockGlobalConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, globalConfigMock, nil)
globalConfigMock := newMockGlobalConfigRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, nil, globalConfigMock)
diff1 := getRemoveGlobalConfigEntryDiff("/key")
diff2 := getRemoveGlobalConfigEntryDiff("/key1")
byAction := map[domain.ConfigAction][]domain.GlobalConfigEntryDiff{domain.ConfigActionRemove: {diff1, diff2}}

globalConfigMock.EXPECT().DeleteAllByKeys(testCtx, []common.GlobalConfigKey{diff1.Key, diff2.Key}).Return(nil).Times(1)
entries, _ := config.MapToEntries(map[string]any{})
globalConfig := config.CreateGlobalConfig(entries)

updatedEntries := globalConfig.Delete(diff1.Key)
updatedEntries = updatedEntries.Delete(diff2.Key)

globalConfigMock.EXPECT().Get(testCtx).Return(globalConfig, nil)
globalConfigMock.EXPECT().Update(testCtx, config.GlobalConfig{Config: updatedEntries}).Return(globalConfig, nil)

// when
err := sut.applyGlobalConfigDiffs(testCtx, byAction)
Expand All @@ -312,12 +326,18 @@ func TestEcosystemConfigUseCase_applyGlobalConfigDiffs(t *testing.T) {

t.Run("should return nil on action none", func(t *testing.T) {
// given
sut := NewEcosystemConfigUseCase(nil, nil, nil, newMockGlobalConfigEntryRepository(t), nil)
globalConfigMock := newMockGlobalConfigRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, nil, globalConfigMock)
diff1 := domain.GlobalConfigEntryDiff{
NeededAction: domain.ConfigActionNone,
}
byAction := map[domain.ConfigAction][]domain.GlobalConfigEntryDiff{domain.ConfigActionNone: {diff1}}

entries, _ := config.MapToEntries(map[string]any{})
globalConfig := config.CreateGlobalConfig(entries)

globalConfigMock.EXPECT().Get(testCtx).Return(globalConfig, nil)

// when
err := sut.applyGlobalConfigDiffs(testCtx, byAction)

Expand Down
152 changes: 152 additions & 0 deletions pkg/application/mock_doguConfigRepository_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 121d44f

Please sign in to comment.