-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor configuration_manager.go by extracting Downloader and S3Mana…
…gerBuilder interfaces; add unit tests
- Loading branch information
1 parent
c1889f6
commit 6382a53
Showing
6 changed files
with
253 additions
and
34 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
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
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
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
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
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,157 @@ | ||
package service | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aerospike/backup/pkg/model" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockDownloader struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockDownloader) Download(configFile string) ([]byte, error) { | ||
args := m.Called(configFile) | ||
return args.Get(0).([]byte), args.Error(1) | ||
} | ||
|
||
type MockS3Builder struct { | ||
} | ||
|
||
func (m *MockS3Builder) NewS3ConfigurationManager(storage *model.Storage) (ConfigurationManager, error) { | ||
if storage.Type == model.S3 { | ||
return &S3ConfigurationManager{}, nil | ||
} | ||
return nil, errors.New("wrong type") | ||
} | ||
|
||
func TestConfigManagerBuilder_NewConfigManager(t *testing.T) { | ||
mockLocal := new(MockDownloader) | ||
mockHttp := new(MockDownloader) | ||
|
||
tests := []struct { | ||
name string | ||
configFile string | ||
remote bool | ||
setMock func() | ||
expectError bool | ||
expectedType reflect.Type | ||
}{ | ||
// Configuration file is passed straight to the service. | ||
{ | ||
name: "local non-remote", | ||
configFile: "config.yaml", | ||
remote: false, | ||
setMock: func() {}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&FileConfigurationManager{}), | ||
}, | ||
{ | ||
name: "http non-remote", | ||
configFile: "https://example.com/config.yaml", | ||
remote: false, | ||
setMock: func() {}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&HTTPConfigurationManager{}), | ||
}, | ||
// Open/download remote config file, and based on it's content open/download backup config. | ||
{ | ||
name: "local remote local configuration", | ||
configFile: "/path/to/remote.yaml", | ||
remote: true, | ||
setMock: func() { | ||
mockLocal. | ||
On("Download", "/path/to/remote.yaml"). | ||
Return([]byte("path: config.yaml"), nil) | ||
}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&FileConfigurationManager{}), | ||
}, | ||
{ | ||
name: "local remote http configuration", | ||
configFile: "/path/to/remote.yaml", | ||
remote: true, | ||
setMock: func() { | ||
mockLocal. | ||
On("Download", "/path/to/remote.yaml"). | ||
Return([]byte("path: https://example.com/config.yaml"), nil) | ||
}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&HTTPConfigurationManager{}), | ||
}, | ||
{ | ||
name: "http remote local configuration", | ||
configFile: "https://example.com/config.yaml", | ||
remote: true, | ||
setMock: func() { | ||
mockHttp. | ||
On("Download", "https://example.com/config.yaml"). | ||
Return([]byte("path: config.yaml"), nil) | ||
}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&FileConfigurationManager{}), | ||
}, | ||
{ | ||
name: "http remote http", | ||
configFile: "http://path/to/remote.yaml", | ||
remote: true, | ||
setMock: func() { | ||
mockHttp. | ||
On("Download", "http://path/to/remote.yaml"). | ||
Return([]byte("path: https://example.com/config.yaml"), nil) | ||
}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&HTTPConfigurationManager{}), | ||
}, | ||
// S3 configuration, we can read it from local file or by http. | ||
{ | ||
name: "local s3", | ||
configFile: "config.yaml", | ||
remote: true, | ||
setMock: func() { | ||
mockLocal. | ||
On("Download", "config.yaml"). | ||
Return([]byte("type: 1\npath: s3://bucket/config.yaml\ns3-region: europe"), nil) | ||
}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&S3ConfigurationManager{}), | ||
}, | ||
{ | ||
name: "http s3", | ||
configFile: "https://example.com/config.yaml", | ||
remote: true, | ||
setMock: func() { | ||
mockHttp. | ||
On("Download", "https://example.com/config.yaml"). | ||
Return([]byte("type: 1\npath: s3://bucket/config.yaml\ns3-region: europe"), nil) | ||
}, | ||
expectError: false, | ||
expectedType: reflect.TypeOf(&S3ConfigurationManager{}), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockLocal = new(MockDownloader) | ||
mockHttp = new(MockDownloader) | ||
builder := &ConfigManagerBuilder{ | ||
http: mockHttp, | ||
file: mockLocal, | ||
s3Builder: &MockS3Builder{}, | ||
} | ||
tt.setMock() | ||
config, err := builder.NewConfigManager(tt.configFile, tt.remote) | ||
if tt.expectError { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
configType := reflect.TypeOf(config) | ||
require.Equal(t, tt.expectedType.String(), configType.String()) | ||
}) | ||
} | ||
} |