Skip to content

Commit

Permalink
ROX-19013 PR Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ludydoo committed Sep 11, 2023
1 parent 4f4c7db commit f20a5b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
4 changes: 3 additions & 1 deletion internal/dinosaur/pkg/gitops/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 26 additions & 33 deletions internal/dinosaur/pkg/gitops/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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()
Expand Down

0 comments on commit f20a5b7

Please sign in to comment.