From f20a5b7343db94442f7a7fe92cb4a64c14f764a8 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Mon, 11 Sep 2023 16:32:37 +0200 Subject: [PATCH] ROX-19013 PR Comments --- internal/dinosaur/pkg/gitops/provider.go | 4 +- internal/dinosaur/pkg/gitops/provider_test.go | 59 ++++++++----------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/internal/dinosaur/pkg/gitops/provider.go b/internal/dinosaur/pkg/gitops/provider.go index 1c825a04b1..3a9ccc39da 100644 --- a/internal/dinosaur/pkg/gitops/provider.go +++ b/internal/dinosaur/pkg/gitops/provider.go @@ -25,10 +25,12 @@ type ConfigProvider interface { Get() (Config, error) } +type validationFn func(config Config) error + type provider struct { reader Reader lastWorkingConfig atomic.Pointer[Config] - validationFn func(config Config) error + validationFn validationFn } // NewProvider returns a new ConfigProvider. diff --git a/internal/dinosaur/pkg/gitops/provider_test.go b/internal/dinosaur/pkg/gitops/provider_test.go index 3e38d86d6c..d15a853c11 100644 --- a/internal/dinosaur/pkg/gitops/provider_test.go +++ b/internal/dinosaur/pkg/gitops/provider_test.go @@ -11,62 +11,64 @@ import ( func TestProvider_Get(t *testing.T) { - failingValidationFn := func(config Config) error { + var failingValidation validationFn = func(config Config) error { return assert.AnError } - - successfulValidationFn := func(config Config) error { + var successfulValidation validationFn = func(config Config) error { return nil } - - failingReader := mockReader{err: assert.AnError} - - successfulReader := mockReader{config: Config{}} + var failingReader Reader = &mockReader{err: assert.AnError} + var successfulReader Reader = &mockReader{config: Config{}} type tc struct { name string hasLastWorkingConfig bool - readerWillFail bool - validationWillFail bool + reader Reader + validator validationFn expectedErrorMetricCount int expectError bool } tcs := []tc{ { - name: "Successful without last working config", + name: "Successful without last working config", + hasLastWorkingConfig: false, + reader: successfulReader, + validator: successfulValidation, + expectedErrorMetricCount: 0, + expectError: false, }, { name: "Successful with last working config", hasLastWorkingConfig: true, - readerWillFail: false, - validationWillFail: false, + reader: successfulReader, + validator: successfulValidation, expectedErrorMetricCount: 0, expectError: false, }, { name: "Reader fails without last working config", hasLastWorkingConfig: false, - readerWillFail: true, - validationWillFail: false, + reader: failingReader, + validator: successfulValidation, expectedErrorMetricCount: 1, expectError: true, }, { name: "Reader fails with last working config", hasLastWorkingConfig: true, - readerWillFail: true, - validationWillFail: false, + reader: failingReader, + validator: successfulValidation, expectedErrorMetricCount: 1, expectError: false, }, { name: "Validation fails without last working config", hasLastWorkingConfig: false, - readerWillFail: false, - validationWillFail: true, + reader: failingReader, + validator: failingValidation, expectedErrorMetricCount: 1, expectError: true, }, { name: "Validation fails with last working config", hasLastWorkingConfig: true, - readerWillFail: false, - validationWillFail: true, + reader: failingReader, + validator: failingValidation, expectedErrorMetricCount: 1, expectError: false, }, @@ -78,23 +80,14 @@ func TestProvider_Get(t *testing.T) { if tc.hasLastWorkingConfig { // Get the config once to set the last working config - p.reader = &successfulReader - p.validationFn = successfulValidationFn + p.reader = successfulReader + p.validationFn = successfulValidation _, err := p.Get() require.NoError(t, err) } - if tc.readerWillFail { - p.reader = &failingReader - } else { - p.reader = &successfulReader - } - - if tc.validationWillFail { - p.validationFn = failingValidationFn - } else { - p.validationFn = successfulValidationFn - } + p.reader = tc.reader + p.validationFn = tc.validator errorCounter.Reset() _, err := p.Get()