Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiryl1 committed Jun 26, 2023
1 parent 43ec832 commit a9cd957
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/dependency/credHelper/cred_helper_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

type ConfigFile struct {
AuthConfigs map[string]interface{} `json:"auths"`
AuthConfigs map[string]interface{} `json:"auths,omitempty"`
HTTPHeaders map[string]string `json:"HttpHeaders,omitempty"`
PsFormat string `json:"psFormat,omitempty"`
ImagesFormat string `json:"imagesFormat,omitempty"`
Expand Down Expand Up @@ -165,7 +165,7 @@ func (bin *binaries) Installed() bool {
}
defer file.Close()
if strings.Compare(hash.String(), bin.hcfg.hash) != 0 {
bin.l.Error("Hash of the installed credential helper binary does not match")
bin.l.Info("Hash of the installed credential helper binary does not match")
err := bin.fs.Remove(bin.fullInstallPath())
if err != nil {
bin.l.Error(err)
Expand Down
120 changes: 118 additions & 2 deletions pkg/dependency/credHelper/cred_helper_binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package credHelper

import (
"fmt"
"github.com/runfinch/finch/pkg/config"
"io/fs"
"testing"
Expand All @@ -31,6 +32,7 @@ func Test_credHelperConfigName(t *testing.T) {
helperConfig{"docker-credential-cred-helper", "", "",
"", ""}).credHelperConfigName()
assert.Equal(t, "cred-helper", got)

}
func Test_fullInstallPath(t *testing.T) {
t.Parallel()
Expand All @@ -41,6 +43,96 @@ func Test_fullInstallPath(t *testing.T) {
assert.Equal(t, "/folder/docker-credential-cred-helper", got)
}

func Test_updateConfigFile(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
mockSvc func(t *testing.T, mFs afero.Fs, l *mocks.Logger)
postRunCheck func(t *testing.T, fs afero.Fs)
want error
}{
{
name: "happy path",
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) {
require.NoError(t, mFs.MkdirAll("/mock_prefix/.finch/", fs.ModeDir))
JSONstr := fmt.Sprintf("{\"credsStore\":\"%s\"}", "binary")
fileData := []byte(JSONstr)
_, err := mFs.Create("mock_prefix/.finch/config.json")
require.NoError(t, err)
err = afero.WriteFile(mFs, "mock_prefix/.finch/config.json",
fileData, 0o666)

require.NoError(t, err)
},
postRunCheck: func(t *testing.T, fs afero.Fs) {},
want: nil,
},
{
name: "file doesn't exist",
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) {
require.NoError(t, mFs.MkdirAll("/mock_prefix/.finch/", fs.ModeDir))

},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "mock_prefix/.finch/config.json")
require.NoError(t, err)
JSONstr := fmt.Sprintf("{\"credsStore\":\"%s\"}", "binary")
fileData := []byte(JSONstr)
assert.Equal(t, fileData, fileBytes)

require.NoError(t, err)
},
want: nil,
},
{
name: "file exists incorrect content",
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) {
require.NoError(t, mFs.MkdirAll("/mock_prefix/.finch/", fs.ModeDir))
JSONstr := fmt.Sprintf("{\"credsStore\":\"%s\"}", "abcd")
fileData := []byte(JSONstr)
_, err := mFs.Create("mock_prefix/.finch/config.json")
require.NoError(t, err)
err = afero.WriteFile(mFs, "mock_prefix/.finch/config.json",
fileData, 0o666)

require.NoError(t, err)

},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "mock_prefix/.finch/config.json")
require.NoError(t, err)
JSONstr := fmt.Sprintf("{\"credsStore\":\"%s\"}", "binary")
fileData := []byte(JSONstr)
assert.Equal(t, fileData, fileBytes)

require.NoError(t, err)
},
want: nil,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
mFs := afero.NewMemMapFs()
l := mocks.NewLogger(ctrl)
tc.mockSvc(t, mFs, l)
hc := helperConfig{"docker-credential-binary", "",
"sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "mock_prefix/cred-helpers/",
"mock_prefix/.finch/"}
//hash of an empty file
got := updateConfigFile(newCredHelperBinary(mockFinchPath, mFs, nil, l, nil, "", hc))

assert.Equal(t, tc.want, got)
tc.postRunCheck(t, mFs)
})
}
}

func TestBinaries_Installed(t *testing.T) {
t.Parallel()

Expand All @@ -64,11 +156,35 @@ func TestBinaries_Installed(t *testing.T) {
want: true,
},
{
name: "installation path doesn't exist",
name: "installation folder doesn't exist",
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) {
},
want: false,
},
{
name: "folder exists file doesn't exist",
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) {
require.NoError(t, mFs.MkdirAll("/mock_prefix/cred-helpers/", fs.ModeDir))

},
want: false,
},
{
name: "file and folder exist hash doesn't match ",
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) {
require.NoError(t, mFs.MkdirAll("/mock_prefix/cred-helpers/", fs.ModeDir))
fileData := []byte("123")
_, err := mFs.Create("mock_prefix/cred-helpers/docker-credential-binary")
require.NoError(t, err)
err = afero.WriteFile(mFs, "mock_prefix/cred-helpers/docker-credential-binary",
fileData, 0o666)

require.NoError(t, err)
l.EXPECT().Info("Hash of the installed credential helper binary does not match")
},
want: false,
}}
},
}

for _, tc := range testCases {
tc := tc
Expand Down

0 comments on commit a9cd957

Please sign in to comment.