Skip to content

Commit

Permalink
test: increase code coverage (project-copacetic#704)
Browse files Browse the repository at this point in the history
Signed-off-by: Miaha Cybersec <[email protected]>
Signed-off-by: Miaha <[email protected]>
Co-authored-by: Ashna Mehrotra <[email protected]>
  • Loading branch information
MiahaCybersec and ashnamehrotra authored Aug 14, 2024
1 parent 44cc2ad commit f0cb34a
Show file tree
Hide file tree
Showing 8 changed files with 601 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/tonistiigi/fsutil v0.0.0-20240424095704-91a3fc46842c
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
golang.org/x/sync v0.8.0
google.golang.org/grpc v1.65.0
Expand Down Expand Up @@ -122,7 +123,6 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
github.com/tonistiigi/fsutil v0.0.0-20240424095704-91a3fc46842c // indirect
github.com/tonistiigi/go-csvvalue v0.0.0-20240710180619-ddb21b71c0b4 // indirect
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab // indirect
Expand Down
28 changes: 26 additions & 2 deletions mocks/mock_gwclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/tonistiigi/fsutil/types"

"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/client/llb/sourceresolver"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
Expand Down Expand Up @@ -45,12 +47,12 @@ func (m *MockGWClient) ResolveImageConfig(ctx context.Context, ref string, opt s

digestResult, ok1 := args.Get(1).(digest.Digest)
if !ok1 {
return "", digest.Digest(""), nil, fmt.Errorf("type assertion to digest.Digest failed")
return "", "", nil, fmt.Errorf("type assertion to digest.Digest failed")
}

byteResult, ok2 := args.Get(2).([]byte)
if !ok2 {
return "", digest.Digest(""), nil, fmt.Errorf("type assertion to []byte failed")
return "", "", nil, fmt.Errorf("type assertion to []byte failed")
}

return args.String(0), digestResult, byteResult, args.Error(3)
Expand Down Expand Up @@ -139,3 +141,25 @@ func (m *MockReference) Evaluate(ctx context.Context) error {

return evalErr
}

func (m *MockReference) StatFile(ctx context.Context, req gwclient.StatRequest) (*types.Stat, error) {
args := m.Called(ctx, req)

typesStat, ok := args.Get(0).(*types.Stat)
if !ok {
return nil, fmt.Errorf("type assertion to types.Stat failed")
}

return typesStat, args.Error(1)
}

func (m *MockReference) ReadDir(ctx context.Context, req gwclient.ReadDirRequest) ([]*types.Stat, error) {
args := m.Called(ctx, req)

typesStat, ok := args.Get(0).([]*types.Stat)
if !ok {
return nil, fmt.Errorf("type assertion to types failed")
}

return typesStat, args.Error(1)
}
3 changes: 3 additions & 0 deletions pkg/buildkit/connhelpers/buildx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ func TestBuildx(t *testing.T) {

_, err = connhelper.GetConnectionHelper("buildx://foobar")
assert.NoError(t, err)

_, err = connhelper.GetConnectionHelper("buildx://foorbar/something")
assert.Error(t, err)
}
44 changes: 44 additions & 0 deletions pkg/buildkit/connhelpers/docker_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package connhelpers

import (
"os"
"testing"

"github.com/moby/buildkit/client/connhelper"
Expand All @@ -11,3 +12,46 @@ func TestDocker(t *testing.T) {
_, err := connhelper.GetConnectionHelper("docker://")
assert.NoError(t, err)
}

func TestGetDockerTransport(t *testing.T) {
tests := []struct {
name string
addr string
envDockerHost string
wantErr bool
}{
{
name: "Empty addr, DOCKER_HOST env set",
addr: "",
envDockerHost: "unix:///var/run/docker.sock",
wantErr: false,
},
{
name: "Non-empty addr",
addr: "tcp://localhost:2375",
wantErr: false,
},
{
name: "invalid addr",
addr: "1234://this/is/not/real:at-all-",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set DOCKER_HOST environment variable if specified in the test case
if tt.envDockerHost != "" {
os.Setenv("DOCKER_HOST", tt.envDockerHost)
} else {
os.Unsetenv("DOCKER_HOST")
}

_, err := getDockerTransport(tt.addr)
if (err != nil) != tt.wantErr {
t.Errorf("getDockerTransport() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
103 changes: 103 additions & 0 deletions pkg/pkgmgr/apk_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package pkgmgr

import (
"context"
_ "embed"
"reflect"
"strings"
"testing"

"github.com/project-copacetic/copacetic/mocks"

"github.com/moby/buildkit/client/llb"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/project-copacetic/copacetic/pkg/buildkit"
"github.com/project-copacetic/copacetic/pkg/types/unversioned"
)
Expand Down Expand Up @@ -210,3 +218,98 @@ func Test_apkManager_GetPackageType(t *testing.T) {
})
}
}

func Test_InstallUpdates_APK(t *testing.T) {
tests := []struct {
name string
manifest *unversioned.UpdateManifest
ignoreErrors bool
mockSetup func(reference *mocks.MockReference)
expectedState bool
expectedPkgs []string
expectedError string
}{
{
name: "Update specific packages",
mockSetup: func(mr *mocks.MockReference) {
mr.On("ReadFile", mock.Anything, mock.Anything).Return([]byte("package1-1.0.1\npackage2-2.0.2\n"), nil)
},
manifest: &unversioned.UpdateManifest{
Updates: unversioned.UpdatePackages{
{Name: "package1", FixedVersion: "1.0.1"},
{Name: "package2", FixedVersion: "2.0.1"},
},
},
ignoreErrors: false,
expectedState: true,
expectedPkgs: nil,
expectedError: "",
},
{
name: "Nil manifest",
mockSetup: func(mr *mocks.MockReference) {
mr.On("ReadFile", mock.Anything, mock.Anything).Return([]byte("package1-1.0.1\npackage2-2.0.1\n"), nil)
},
manifest: nil,
expectedState: true,
expectedPkgs: nil,
expectedError: "",
},
{
name: "Ignore errors",
manifest: &unversioned.UpdateManifest{
Updates: unversioned.UpdatePackages{
{Name: "package1", FixedVersion: "2.0.0"},
},
},
ignoreErrors: true,
mockSetup: func(mr *mocks.MockReference) {
mr.On("ReadFile", mock.Anything, mock.Anything).Return([]byte("package1-1.0.1\n"), nil)
},
expectedState: true,
expectedPkgs: []string{"package1"},
expectedError: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockGWClient := new(mocks.MockGWClient)
mockRef := new(mocks.MockReference)

mockResult := &gwclient.Result{}
mockResult.SetRef(mockRef)

mockGWClient.On("Solve", mock.Anything, mock.Anything).Return(mockResult, nil)

if tt.mockSetup != nil {
tt.mockSetup(mockRef)
}

am := &apkManager{
config: &buildkit.Config{
Client: mockGWClient,
ImageState: llb.Scratch(),
},
workingFolder: "/tmp",
}

state, pkgs, err := am.InstallUpdates(context.TODO(), tt.manifest, tt.ignoreErrors)

if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
} else {
assert.NoError(t, err)
}

if tt.expectedState {
assert.NotNil(t, state)
} else {
assert.Nil(t, state)
}

assert.Equal(t, tt.expectedPkgs, pkgs)
})
}
}
77 changes: 77 additions & 0 deletions pkg/pkgmgr/dpkg_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package pkgmgr

import (
"context"
_ "embed"
"errors"
"fmt"
"reflect"
"strings"
"testing"

"github.com/moby/buildkit/client/llb"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/project-copacetic/copacetic/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/project-copacetic/copacetic/pkg/buildkit"
"github.com/project-copacetic/copacetic/pkg/types/unversioned"
)
Expand Down Expand Up @@ -439,3 +446,73 @@ func Test_GetPackageInfo(t *testing.T) {
})
}
}

func Test_installUpdates_DPKG(t *testing.T) {
tests := []struct {
name string
updates unversioned.UpdatePackages
ignoreErrors bool
mockSetup func(reference *mocks.MockReference)
expectedResult []byte
expectedError string
}{
{
name: "Update all packages",
mockSetup: func(mr *mocks.MockReference) {
mr.On("ReadFile", mock.Anything, mock.Anything).Return([]byte(nil), nil)
},
ignoreErrors: false,
expectedResult: nil,
},
{
name: "Update specific packages",
mockSetup: func(mr *mocks.MockReference) {
mr.On("ReadFile", mock.Anything, mock.Anything).Return([]byte("package1-1.0.1\npackage2-2.0.2\n"), nil)
},
updates: unversioned.UpdatePackages{
{Name: "package1", FixedVersion: "1.0.1"},
{Name: "package2", FixedVersion: "2.0.1"},
},
ignoreErrors: false,
expectedResult: []byte("package1-1.0.1\npackage2-2.0.2\n"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := new(mocks.MockGWClient)
mockRef := new(mocks.MockReference)

mockResult := &gwclient.Result{}
mockResult.SetRef(mockRef)

mockClient.On("Solve", mock.Anything, mock.Anything).Return(mockResult, nil)

if tt.mockSetup != nil {
tt.mockSetup(mockRef)
}

dm := &dpkgManager{
config: &buildkit.Config{
Client: mockClient,
ImageState: llb.Scratch(),
},
}

updatedState, resultBytes, err := dm.installUpdates(context.TODO(), tt.updates, tt.ignoreErrors)

if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
assert.Nil(t, updatedState)
assert.Nil(t, resultBytes)
} else {
assert.NoError(t, err)
assert.NotNil(t, updatedState)
assert.Equal(t, tt.expectedResult, resultBytes)
}

mockClient.AssertExpectations(t)
mockRef.AssertExpectations(t)
})
}
}
Loading

0 comments on commit f0cb34a

Please sign in to comment.