From 6fb884b2dbdc37ef2e1aea924040822ce38584bd Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 3 Jun 2022 10:53:57 -0700 Subject: [PATCH] Fix small nits in confmap tests (#5473) Signed-off-by: Bogdan Drutu --- confmap/confmap_test.go | 5 +- confmap/resolver_test.go | 108 ++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 50 deletions(-) diff --git a/confmap/confmap_test.go b/confmap/confmap_test.go index 8f2fe6c7f9a..a1a9d6a8a48 100644 --- a/confmap/confmap_test.go +++ b/confmap/confmap_test.go @@ -103,8 +103,7 @@ func TestToStringMap(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - conf := newConfFromFile(t, test.fileName) - assert.Equal(t, test.stringMap, conf) + assert.Equal(t, test.stringMap, newConfFromFile(t, test.fileName)) }) } } @@ -221,7 +220,7 @@ func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncErrorUnmarshal(t *testing.T) } // newConfFromFile creates a new Conf by reading the given file. -func newConfFromFile(t *testing.T, fileName string) map[string]interface{} { +func newConfFromFile(t testing.TB, fileName string) map[string]interface{} { content, err := ioutil.ReadFile(filepath.Clean(fileName)) require.NoErrorf(t, err, "unable to read the file %v", fileName) diff --git a/confmap/resolver_test.go b/confmap/resolver_test.go index 0a94681ef8d..6d01ee12547 100644 --- a/confmap/resolver_test.go +++ b/confmap/resolver_test.go @@ -58,17 +58,33 @@ func (m *mockProvider) Shutdown(context.Context) error { return m.errS } -type errFileProvider struct{} +type fakeProvider struct { + scheme string + ret func(ctx context.Context, uri string, watcher WatcherFunc) (Retrieved, error) +} + +func newFileProvider(t testing.TB) Provider { + return newFakeProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (Retrieved, error) { + return NewRetrieved(newConfFromFile(t, uri[5:])) + }) +} + +func newFakeProvider(scheme string, ret func(ctx context.Context, uri string, watcher WatcherFunc) (Retrieved, error)) Provider { + return &fakeProvider{ + scheme: scheme, + ret: ret, + } +} -func (m *errFileProvider) Retrieve(_ context.Context, uri string, watcher WatcherFunc) (Retrieved, error) { - return Retrieved{}, errors.New(uri) +func (f *fakeProvider) Retrieve(ctx context.Context, uri string, watcher WatcherFunc) (Retrieved, error) { + return f.ret(ctx, uri, watcher) } -func (m *errFileProvider) Scheme() string { - return "file" +func (f *fakeProvider) Scheme() string { + return f.scheme } -func (m *errFileProvider) Shutdown(context.Context) error { +func (f *fakeProvider) Shutdown(context.Context) error { return nil } @@ -116,34 +132,28 @@ func TestResolverErrors(t *testing.T) { { name: "watch error", locations: []string{"mock:", "err:"}, - providers: func() []Provider { - conf := newConfFromFile(t, filepath.Join("testdata", "config.yaml")) - return []Provider{&mockProvider{}, &mockProvider{scheme: "err", retM: conf, errW: errors.New("watch_err")}} - }(), + providers: []Provider{ + &mockProvider{}, + &mockProvider{scheme: "err", retM: map[string]interface{}{}, errW: errors.New("watch_err")}, + }, expectWatchErr: true, }, { name: "close error", locations: []string{"mock:", "err:"}, - providers: func() []Provider { - conf := newConfFromFile(t, filepath.Join("testdata", "config.yaml")) - return []Provider{ - &mockProvider{}, - &mockProvider{scheme: "err", retM: conf, errC: errors.New("close_err")}, - } - }(), + providers: []Provider{ + &mockProvider{}, + &mockProvider{scheme: "err", retM: map[string]interface{}{}, errC: errors.New("close_err")}, + }, expectCloseErr: true, }, { name: "shutdown error", locations: []string{"mock:", "err:"}, - providers: func() []Provider { - conf := newConfFromFile(t, filepath.Join("testdata", "config.yaml")) - return []Provider{ - &mockProvider{}, - &mockProvider{scheme: "err", retM: conf, errS: errors.New("close_err")}, - } - }(), + providers: []Provider{ + &mockProvider{}, + &mockProvider{scheme: "err", retM: map[string]interface{}{}, errS: errors.New("close_err")}, + }, expectShutdownErr: true, }, } @@ -157,21 +167,21 @@ func TestResolverErrors(t *testing.T) { assert.Error(t, errN) return } - assert.NoError(t, errN) + require.NoError(t, errN) errW := <-resolver.Watch() if tt.expectWatchErr { assert.Error(t, errW) return } - assert.NoError(t, errW) + require.NoError(t, errW) _, errC := resolver.Resolve(context.Background()) if tt.expectCloseErr { assert.Error(t, errC) return } - assert.NoError(t, errN) + require.NoError(t, errN) errS := resolver.Shutdown(context.Background()) if tt.expectShutdownErr { @@ -221,7 +231,12 @@ func TestBackwardsCompatibilityForFilePath(t *testing.T) { }, } for _, tt := range tests { - resolver, err := NewResolver(ResolverSettings{URIs: []string{tt.location}, Providers: makeMapProvidersMap(&errFileProvider{}), Converters: nil}) + resolver, err := NewResolver(ResolverSettings{ + URIs: []string{tt.location}, + Providers: makeMapProvidersMap(newFakeProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (Retrieved, error) { + return Retrieved{}, errors.New(uri) + })), + Converters: nil}) assert.NoError(t, err) _, err = resolver.Resolve(context.Background()) assert.Contains(t, err.Error(), tt.errMessage, tt.name) @@ -229,11 +244,10 @@ func TestBackwardsCompatibilityForFilePath(t *testing.T) { } func TestResolver(t *testing.T) { - provider := func() Provider { - return &mockProvider{retM: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))} - }() - - resolver, err := NewResolver(ResolverSettings{URIs: []string{"mock:"}, Providers: makeMapProvidersMap(provider), Converters: nil}) + resolver, err := NewResolver(ResolverSettings{ + URIs: []string{"mock:"}, + Providers: makeMapProvidersMap(&mockProvider{retM: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))}), + Converters: nil}) require.NoError(t, err) _, errN := resolver.Resolve(context.Background()) assert.NoError(t, errN) @@ -254,35 +268,34 @@ func TestResolver(t *testing.T) { } func TestResolverNoLocations(t *testing.T) { - _, err := NewResolver(ResolverSettings{URIs: []string{}, Providers: makeMapProvidersMap(&mockProvider{}), Converters: nil}) + _, err := NewResolver(ResolverSettings{ + URIs: []string{}, + Providers: makeMapProvidersMap(&mockProvider{}), + Converters: nil}) assert.Error(t, err) } func TestResolverNoProviders(t *testing.T) { - _, err := NewResolver(ResolverSettings{URIs: []string{filepath.Join("testdata", "config.yaml")}, Providers: nil, Converters: nil}) + _, err := NewResolver(ResolverSettings{ + URIs: []string{filepath.Join("testdata", "config.yaml")}, + Providers: nil, + Converters: nil}) assert.Error(t, err) } func TestResolverShutdownClosesWatch(t *testing.T) { - provider := func() Provider { - return &mockProvider{retM: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))} - }() - - resolver, err := NewResolver(ResolverSettings{URIs: []string{"mock:"}, Providers: makeMapProvidersMap(provider), Converters: nil}) - _, errN := resolver.Resolve(context.Background()) + resolver, err := NewResolver(ResolverSettings{ + URIs: []string{filepath.Join("testdata", "config.yaml")}, + Providers: makeMapProvidersMap(newFileProvider(t)), + Converters: nil}) require.NoError(t, err) - + _, errN := resolver.Resolve(context.Background()) assert.NoError(t, errN) var watcherWG sync.WaitGroup watcherWG.Add(1) go func() { - // The mock implementation sends a first watch event. errW, ok := <-resolver.Watch() - assert.Nil(t, errW) - assert.True(t, ok) - - errW, ok = <-resolver.Watch() // Channel is closed, no exception assert.Nil(t, errW) assert.False(t, ok) @@ -292,6 +305,7 @@ func TestResolverShutdownClosesWatch(t *testing.T) { assert.NoError(t, resolver.Shutdown(context.Background())) watcherWG.Wait() } + func makeMapProvidersMap(providers ...Provider) map[string]Provider { ret := make(map[string]Provider, len(providers)) for _, provider := range providers {