Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anubhav Gupta <mail.anubhav06@gmail.com>
  • Loading branch information
anubhav06 committed Aug 15, 2023
1 parent bf15f30 commit 6539d33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions pkg/patch/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func TestNewPatchCmd(t *testing.T) {
args: []string{"-i", "images/python:3.7-alpine", "-t", "3.7-alpine-patched"},
expected: "required flag(s) \"report\" not set",
},
{
name: "Missing report flag with ignore-errors flag",
args: []string{"-i", "images/python:3.7-alpine", "-t", "3.7-alpine-patched", "--ignore-errors"},
expected: "required flag(s) \"report\" not set",
},
}

// Run test cases
Expand Down
24 changes: 18 additions & 6 deletions pkg/pkgmgr/apk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,43 @@ func TestValidateAPKPackageVersions(t *testing.T) {

// Define some test cases with inputs and expected outputs
testCases := []struct {
name string
updates types.UpdatePackages
cmp VersionComparer
resultsPath string
expectedErr error
name string
updates types.UpdatePackages
cmp VersionComparer
resultsPath string
ignoreErrors bool
expectedErr error
}{
{
name: "valid updates",
updates: []types.UpdatePackage{{Name: "apk-tools", Version: "2.12.7-r0"}, {Name: "busybox", Version: "1.33.1-r8"}},
cmp: apkComparer,
resultsPath: "testdata/apk_valid.txt",
ignoreErrors: false,
expectedErr: nil,
},
{
name: "invalid version",
updates: []types.UpdatePackage{{Name: "apk-tools", Version: "1.0"}, {Name: "busybox", Version: "2.0"}},
cmp: apkComparer,
resultsPath: "testdata/apk_invalid.txt",
ignoreErrors: false,
expectedErr: fmt.Errorf("2 errors occurred:\n\t* invalid version x.y found for package apk-tools\n\t* invalid version a.b.c found for package busybox"),
},
{
name: "invalid version with ignore errors",
updates: []types.UpdatePackage{{Name: "apk-tools", Version: "1.0"}, {Name: "busybox", Version: "2.0"}},
cmp: apkComparer,
resultsPath: "testdata/apk_valid.txt",
ignoreErrors: true,
expectedErr: nil,
},
{
name: "expected 2 updates, installed 1",
updates: []types.UpdatePackage{{Name: "apk-tools", Version: "2.12.7-r0"}},
cmp: apkComparer,
resultsPath: "testdata/apk_valid.txt",
ignoreErrors: false,
expectedErr: fmt.Errorf("expected 2 updates, installed 1"),
},
}
Expand All @@ -136,7 +148,7 @@ func TestValidateAPKPackageVersions(t *testing.T) {
// Use t.Run to run each test case as a subtest
t.Run(tc.name, func(t *testing.T) {
// Run the function to be tested
err := validateAPKPackageVersions(tc.updates, tc.cmp, tc.resultsPath, false)
err := validateAPKPackageVersions(tc.updates, tc.cmp, tc.resultsPath, tc.ignoreErrors)
if tc.expectedErr != nil {
if err == nil || errors.Is(err, tc.expectedErr) {
t.Errorf("expected error %v, got %v", tc.expectedErr, err)
Expand Down
20 changes: 17 additions & 3 deletions pkg/pkgmgr/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func TestValidateRPMPackageVersions(t *testing.T) {
updates types.UpdatePackages
cmp VersionComparer
resultsPath string
ignoreErrors bool
expectedError error
}{
{
Expand All @@ -268,8 +269,9 @@ func TestValidateRPMPackageVersions(t *testing.T) {
{Name: "openssl", Version: "1.1.1k-21.cm2"},
{Name: "openssl-libs", Version: "1.1.1k-21.cm2"},
},
cmp: rpmComparer,
resultsPath: "testdata/rpm_valid.txt",
cmp: rpmComparer,
resultsPath: "testdata/rpm_valid.txt",
ignoreErrors: false,
},
{
name: "downloaded package version lower than required",
Expand All @@ -279,8 +281,20 @@ func TestValidateRPMPackageVersions(t *testing.T) {
},
cmp: rpmComparer,
resultsPath: "testdata/rpm_valid.txt",
ignoreErrors: false,
expectedError: fmt.Errorf("2 errors occurred:\n\t* downloaded package openssl version 2.1.1k-21.cm2 lower than required 3.1.1k-21.cm2 for update\n\t* downloaded package openssl-libs version 2.1.1k-21.cm2 lower than required 3.1.1k-21.cm2 for update"), // nolint:lll
},
{
name: "downloaded package version lower than required with ignore errors",
updates: types.UpdatePackages{
{Name: "openssl", Version: "3.1.1k-21.cm2"},
{Name: "openssl-libs", Version: "3.1.1k-21.cm2"},
},
cmp: rpmComparer,
resultsPath: "testdata/rpm_valid.txt",
ignoreErrors: true,
expectedError: nil,
},
{
name: "unexpected number of installed packages",
updates: types.UpdatePackages{
Expand All @@ -294,7 +308,7 @@ func TestValidateRPMPackageVersions(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := validateRPMPackageVersions(tc.updates, tc.cmp, tc.resultsPath, false)
err := validateRPMPackageVersions(tc.updates, tc.cmp, tc.resultsPath, tc.ignoreErrors)
if tc.expectedError != nil {
if err == nil || errors.Is(err, tc.expectedError) {
t.Errorf("expected error %v, got %v", tc.expectedError, err)
Expand Down

0 comments on commit 6539d33

Please sign in to comment.