-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor cred_helper_binary for easier testing, added basic unit test…
…s, fixed unit test failure in nerdctl_config_applier_test
- Loading branch information
Showing
7 changed files
with
233 additions
and
35 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,153 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Ensures that the binaries required for networking are installed in a privileged location. | ||
// More information here: https://github.com/lima-vm/socket_vmnet | ||
package credHelper | ||
|
||
import ( | ||
"github.com/runfinch/finch/pkg/config" | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/runfinch/finch/pkg/mocks" | ||
"github.com/runfinch/finch/pkg/path" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
mockFinchPathString = "mock_prefix" | ||
mockFinchPath = path.Finch(mockFinchPathString) | ||
) | ||
|
||
func Test_credHelperConfigName(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := newCredHelperBinary("", nil, nil, nil, nil, "user", | ||
helperConfig{"docker-credential-cred-helper", "", "", | ||
"", ""}).credHelperConfigName() | ||
assert.Equal(t, "cred-helper", got) | ||
} | ||
func Test_fullInstallPath(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := newCredHelperBinary("", nil, nil, nil, nil, "user", | ||
helperConfig{"docker-credential-cred-helper", "", "", "/folder/", | ||
""}).fullInstallPath() | ||
assert.Equal(t, "/folder/docker-credential-cred-helper", got) | ||
} | ||
|
||
func TestBinaries_Installed(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
mockSvc func(t *testing.T, mFs afero.Fs, l *mocks.Logger) | ||
want bool | ||
}{ | ||
{ | ||
name: "happy path", | ||
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) { | ||
require.NoError(t, mFs.MkdirAll("/mock_prefix/cred-helpers/", fs.ModeDir)) | ||
fileData := []byte("") | ||
_, 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) | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "installation path doesn't exist", | ||
mockSvc: func(t *testing.T, mFs afero.Fs, l *mocks.Logger) { | ||
}, | ||
want: false, | ||
}} | ||
|
||
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 := newCredHelperBinary(mockFinchPath, mFs, nil, l, nil, "", hc).Installed() | ||
|
||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestBinaries_Install(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
mockSvc func( | ||
l *mocks.Logger, | ||
cmd *mocks.Command, | ||
creator *mocks.CommandCreator, | ||
mFs afero.Fs) | ||
want error | ||
}{ | ||
{ | ||
name: "happy path", | ||
mockSvc: func(l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, mFs afero.Fs) { | ||
_, err := mFs.Create("mock_prefix/cred-helpers/docker-credential-ecr-login") | ||
require.NoError(t, err) | ||
cmd.EXPECT().Output().Times(2) | ||
l.EXPECT().Info("Installing credential helper") | ||
creator.EXPECT().Create("mkdir", "-p", "mock_prefix/cred-helpers/").Return(cmd) | ||
creator.EXPECT().Create("curl", "--retry", "5", "--retry-max-time", "30", "--url", | ||
"https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com"+ | ||
"/0.7.0/linux-arm64/docker-credential-ecr-login", "--output", "mock_prefix/cred-helpers/docker-credential-ecr-login").Return(cmd) | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctrl := gomock.NewController(t) | ||
cmd := mocks.NewCommand(ctrl) | ||
mFs := afero.NewMemMapFs() | ||
l := mocks.NewLogger(ctrl) | ||
creator := mocks.NewCommandCreator(ctrl) | ||
tc.mockSvc(l, cmd, creator, mFs) | ||
|
||
credHelperUrl := "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com" + | ||
"/0.7.0/linux-arm64/docker-credential-ecr-login" | ||
|
||
hc := helperConfig{"docker-credential-ecr-login", credHelperUrl, | ||
"sha256:ff14a4da40d28a2d2d81a12a7c9c36294ddf8e6439780c4ccbc96622991f3714", | ||
"mock_prefix/cred-helpers/", | ||
"mock_prefix/.finch/"} | ||
fc := "ecr-login" | ||
got := newCredHelperBinary(mockFinchPath, mFs, creator, l, &config.Finch{CredsHelper: &fc}, "", hc).Install() | ||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestBinaries_RequiresRoot(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := newCredHelperBinary(mockFinchPath, nil, nil, nil, nil, "", | ||
helperConfig{}).RequiresRoot() | ||
assert.Equal(t, false, got) | ||
} |
Oops, something went wrong.