Skip to content

Commit

Permalink
Refactor to make implementations package private.
Browse files Browse the repository at this point in the history
  • Loading branch information
donatwork committed Dec 8, 2024
1 parent a525031 commit 8abae75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ func main() {
// Run the service as a pre-init step.
if os.Getenv(gocsi.EnvVarMode) == "MDM-Info" {
fmt.Fprintf(os.Stdout, "PowerFlex Container Storage Interface (CSI) Plugin starting in pre-init mode.")
service.ArrayConfigurationProviderImpl = &service.DefaultArrayConfigurationProvider{}
service.FileWriterProviderImpl = &service.DefaultFileWriterProvider{}
svc := service.NewPreInitService()
err := svc.PreInit()
if err != nil {
Expand Down
14 changes: 8 additions & 6 deletions service/preinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package service

import (
"fmt"
"io/fs"
"os"
"strings"

Expand All @@ -41,9 +42,6 @@ type FileWriterProvider interface {
WriteFile(filename string, data []byte, perm os.FileMode) error
}

var ArrayConfigurationProviderImpl ArrayConfigurationProvider
var FileWriterProviderImpl FileWriterProvider

type DefaultArrayConfigurationProvider struct{}

func (s *DefaultArrayConfigurationProvider) GetArrayConfiguration() ([]*ArrayConnectionData, error) {
Expand All @@ -66,10 +64,15 @@ func (s *DefaultFileWriterProvider) WriteFile(filename string, data []byte, perm
return os.WriteFile(filename, data, perm)
}

var (
arrayConfigurationProviderImpl ArrayConfigurationProvider = &DefaultArrayConfigurationProvider{}
fileWriterProviderImpl FileWriterProvider = &DefaultFileWriterProvider{}
)

func (s *service) PreInit() error {
Log.Infof("PreInit running")

arrayConfig, err := ArrayConfigurationProviderImpl.GetArrayConfiguration()
arrayConfig, err := arrayConfigurationProviderImpl.GetArrayConfiguration()
if err != nil {
return err
}
Expand Down Expand Up @@ -113,8 +116,7 @@ func (s *service) PreInit() error {
}

Log.Infof("Saving MDM list to %s, MDM=%s", nodeMdmsFile, mdmData)
// #nosec G306 - false positive, or a bug in gosec
err = FileWriterProviderImpl.WriteFile(nodeMdmsFile, []byte(fmt.Sprintf("MDM=%s\n", mdmData)), 444)
err = fileWriterProviderImpl.WriteFile(nodeMdmsFile, []byte(fmt.Sprintf("MDM=%s\n", mdmData)), fs.FileMode(0o444))
return err
}

Expand Down
40 changes: 32 additions & 8 deletions service/preinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package service

import (
"fmt"
"io/fs"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -57,20 +58,20 @@ func TestPreInit(t *testing.T) {
expectedResult string
}{
{
name: "no connection info",
name: "should error on no connection info",
connectionInfo: []*ArrayConnectionData{},
errorExpected: true,
expectedResult: "array connection data is empty",
},
{
name: "error getting connection info",
name: "should handle error getting connection info",
connectionInfo: nil,
connectionError: fmt.Errorf("don't care about error text"),
errorExpected: true,
expectedResult: "don't care about error text",
},
{
name: "zone label is different",
name: "should error when zone labels different",
connectionInfo: []*ArrayConnectionData{
{
Zone: ZoneInfo{
Expand All @@ -88,11 +89,34 @@ func TestPreInit(t *testing.T) {
errorExpected: true,
expectedResult: "zone label key is not the same for all arrays",
},
{
name: "should configure all MDMs when no zone label single array",
connectionInfo: []*ArrayConnectionData{
{
Mdm: "192.168.1.1,192.168.1.2",
},
},
errorExpected: false,
expectedResult: "192.168.1.1,192.168.1.2",
},
{
name: "should configure all MDMs when no zone label multi array",
connectionInfo: []*ArrayConnectionData{
{
Mdm: "192.168.1.1,192.168.1.2",
},
{
Mdm: "192.168.2.1,192.168.2.2",
},
},
errorExpected: false,
expectedResult: "192.168.1.1,192.168.1.2,192.168.2.1,192.168.2.2",
},

// {
// name: "",
// connectionInfo: []*ArrayConnectionData{},
// errorExpected: true,
// errorExpected: false,
// expectedResult: "",
// },
}
Expand All @@ -102,21 +126,21 @@ func TestPreInit(t *testing.T) {
mockArrayConfigurationProvider := &MockArrayConfigurationProvider{}
mockFileWriterProvider := &MockFileWriterProvider{}

ArrayConfigurationProviderImpl = mockArrayConfigurationProvider
FileWriterProviderImpl = mockFileWriterProvider
arrayConfigurationProviderImpl = mockArrayConfigurationProvider
fileWriterProviderImpl = mockFileWriterProvider
K8sClientset = fake.NewClientset()
svc := NewPreInitService()

mockArrayConfigurationProvider.On("GetArrayConfiguration").Return(test.connectionInfo, test.connectionError)
mockFileWriterProvider.On("WriteFile").Return(nil)
mockFileWriterProvider.On("WriteFile", mock.Anything, mock.Anything, mock.Anything).Return(nil)

err := svc.PreInit()
if test.errorExpected {
assert.Equal(t, test.expectedResult, err.Error())
} else {
assert.Nil(t, err)
mockFileWriterProvider.AssertCalled(t, "WriteFile", nodeMdmsFile,
[]byte(fmt.Sprintf("MDM=%s\n", test.expectedResult)), 0444)
[]byte(fmt.Sprintf("MDM=%s\n", test.expectedResult)), fs.FileMode(0o444))
}
})
}
Expand Down

0 comments on commit 8abae75

Please sign in to comment.