-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#81 map error types in globalConfigRepository
Also add tests and go doc
- Loading branch information
1 parent
27e8c38
commit 11c590c
Showing
6 changed files
with
376 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
pkg/adapter/config/kubernetes/globalConfigRepository_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice" | ||
"github.com/cloudogu/k8s-registry-lib/config" | ||
"github.com/cloudogu/k8s-registry-lib/errors" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/net/context" | ||
"testing" | ||
) | ||
|
||
var testCtx = context.TODO() | ||
|
||
// testGlobalConfig is used as immutable structure for tests | ||
var testGlobalConfig = config.CreateGlobalConfig(map[config.Key]config.Value{ | ||
"fqdn": "domain.local", | ||
}) | ||
|
||
func TestNewGlobalConfigRepository(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
assert.Equal(t, repoMock, repo.repo) | ||
} | ||
|
||
func TestGlobalConfigRepository_Get(t *testing.T) { | ||
|
||
t.Run("get", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
repoMock.EXPECT().Get(testCtx).Return(testGlobalConfig, nil) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
actualConfig, err := repo.Get(testCtx) | ||
//then | ||
assert.NoError(t, err) | ||
assert.Equal(t, testGlobalConfig, actualConfig) | ||
}) | ||
t.Run("global config not found", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewNotFoundError(assert.AnError)) | ||
repoMock.EXPECT().Get(testCtx).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Get(testCtx) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.True(t, domainservice.IsNotFoundError(err), "error is no NotFoundError") | ||
}) | ||
t.Run("internal error if connection error happens", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewConnectionError(assert.AnError)) | ||
repoMock.EXPECT().Get(testCtx).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Get(testCtx) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.ErrorContains(t, err, "could not load global config due to connection problems") | ||
assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") | ||
}) | ||
t.Run("internal error on all other errors", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewGenericError(assert.AnError)) | ||
repoMock.EXPECT().Get(testCtx).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Get(testCtx) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.ErrorContains(t, err, "could not load global config due to an unknown problem") | ||
assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") | ||
}) | ||
} | ||
|
||
func TestGlobalConfigRepository_Update(t *testing.T) { | ||
t.Run("update global config", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
repoMock.EXPECT().Update(testCtx, testGlobalConfig).Return(testGlobalConfig, nil) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
actualConfig, err := repo.Update(testCtx, testGlobalConfig) | ||
//then | ||
assert.NoError(t, err) | ||
assert.Equal(t, testGlobalConfig, actualConfig) | ||
}) | ||
t.Run("global config not found", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewNotFoundError(assert.AnError)) | ||
repoMock.EXPECT().Update(testCtx, testGlobalConfig).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Update(testCtx, testGlobalConfig) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.True(t, domainservice.IsNotFoundError(err), "error is no NotFoundError") | ||
}) | ||
t.Run("conflicts while updating global config", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewConflictError(assert.AnError)) | ||
repoMock.EXPECT().Update(testCtx, testGlobalConfig).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Update(testCtx, testGlobalConfig) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.ErrorContains(t, err, "could not update global config due to conflicting changes") | ||
assert.True(t, domainservice.IsConflictError(err), "error is no ConflictError") | ||
}) | ||
t.Run("internal error if connection error happens", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewConnectionError(assert.AnError)) | ||
repoMock.EXPECT().Update(testCtx, testGlobalConfig).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Update(testCtx, testGlobalConfig) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.ErrorContains(t, err, "could not update global config due to connection problems") | ||
assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") | ||
}) | ||
t.Run("internal error on all other errors", func(t *testing.T) { | ||
repoMock := newMockK8sGlobalConfigRepo(t) | ||
//given | ||
// wrap the error because the original implementation does it too | ||
givenError := fmt.Errorf("wrapping error: %w", errors.NewGenericError(assert.AnError)) | ||
repoMock.EXPECT().Update(testCtx, testGlobalConfig).Return(testGlobalConfig, givenError) | ||
repo := NewGlobalConfigRepository(repoMock) | ||
//when | ||
_, err := repo.Update(testCtx, testGlobalConfig) | ||
//then | ||
assert.ErrorContains(t, err, givenError.Error()) | ||
assert.ErrorContains(t, err, "could not update global config due to an unknown problem") | ||
assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"github.com/cloudogu/k8s-registry-lib/config" | ||
) | ||
|
||
type k8sGlobalConfigRepo interface { | ||
Get(ctx context.Context) (config.GlobalConfig, error) | ||
Update(ctx context.Context, globalConfig config.GlobalConfig) (config.GlobalConfig, error) | ||
} |
151 changes: 151 additions & 0 deletions
151
pkg/adapter/config/kubernetes/mock_k8sGlobalConfigRepo_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.