Skip to content

Commit

Permalink
test: improve test coverage (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
sozercan committed Feb 24, 2023
1 parent 0a7b367 commit 8d4a9c0
Show file tree
Hide file tree
Showing 20 changed files with 1,179 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/containerd/containerd v1.6.18
github.com/distribution/distribution v2.8.1+incompatible
github.com/docker/cli v23.0.1+incompatible
github.com/google/go-containerregistry v0.12.0
github.com/hashicorp/go-multierror v1.1.1
github.com/knqyf263/go-apk-version v0.0.0-20200609155635-041fdbb8563f
github.com/knqyf263/go-deb-version v0.0.0-20190517075300-09fca494f03d
Expand All @@ -27,6 +28,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
golang.org/x/sync v0.1.0
)

Expand All @@ -38,6 +40,7 @@ require (
github.com/containerd/continuity v0.3.0 // indirect
github.com/containerd/ttrpc v1.1.1-0.20220420014843-944ef4a40df3 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.20+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
Expand All @@ -49,7 +52,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-containerregistry v0.12.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
Expand All @@ -64,6 +66,7 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/samber/lo v1.36.0 // indirect
github.com/spdx/tools-golang v0.3.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
Expand Down
42 changes: 42 additions & 0 deletions pkg/patch/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ------------------------------------------------------------
// Copyright (c) Project Copacetic authors.
// Licensed under the MIT License.
// ------------------------------------------------------------

package patch

import "testing"

func TestNewPatchCmd(t *testing.T) {
tests := []struct {
name string
args []string
expected string
}{
{
name: "Missing image flag",
args: []string{"-r", "trivy.json", "-t", "3.7-alpine-patched"},
expected: "required flag(s) \"image\" not set",
},
{
name: "Missing report flag",
args: []string{"-i", "images/python:3.7-alpine", "-t", "3.7-alpine-patched"},
expected: "required flag(s) \"report\" not set",
},
}

// Run test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new command with the test args
cmd := NewPatchCmd()
cmd.SetArgs(tt.args)

// Run the command and capture the output
err := cmd.Execute()
if err == nil || err.Error() != tt.expected {
t.Errorf("Unexpected error: %v, expected: %v", err, tt.expected)
}
})
}
}
51 changes: 51 additions & 0 deletions pkg/patch/patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ------------------------------------------------------------
// Copyright (c) Project Copacetic authors.
// Licensed under the MIT License.
// ------------------------------------------------------------

package patch

import (
"os"
"testing"

log "github.com/sirupsen/logrus"
)

func TestRemoveIfNotDebug(t *testing.T) {
// Test removing working folder when not in debug mode
t.Run("RemoveWorkingFolder", func(t *testing.T) {
// Set log level to Info to simulate not being in debug mode
log.SetLevel(log.InfoLevel)

// Create a temporary working folder
workingFolder := t.TempDir()
defer os.RemoveAll(workingFolder)

removeIfNotDebug(workingFolder)

// Check that the working folder was removed
if _, err := os.Stat(workingFolder); err == nil {
t.Errorf("Working folder should have been removed but still exists")
}
})

// Test not removing working folder when in debug mode
t.Run("KeepWorkingFolderDebug", func(t *testing.T) {
// Set log level to Debug to simulate being in debug mode
log.SetLevel(log.DebugLevel)

// Create a temporary working folder
workingFolder := t.TempDir()

removeIfNotDebug(workingFolder)

// Check that the working folder still exists
if _, err := os.Stat(workingFolder); err != nil {
t.Errorf("Working folder should have been kept but was removed")
}

// Clean up the working folder manually
os.RemoveAll(workingFolder)
})
}
151 changes: 151 additions & 0 deletions pkg/pkgmgr/apk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// ------------------------------------------------------------
// Copyright (c) Project Copacetic authors.
// Licensed under the MIT License.
// ------------------------------------------------------------

package pkgmgr

import (
"errors"
"fmt"
"reflect"
"testing"

"github.com/project-copacetic/copacetic/pkg/types"
)

// TestApkReadResultsManifest tests the apkReadResultsManifest function.
func TestIsValidAPKVersion(t *testing.T) {
type args struct {
v string
}
tests := []struct {
name string
args args
want bool
}{
{"valid version", args{"1.0"}, true},
{"invalid version", args{"a.b"}, false},
{"valid version with suffix", args{"1.0-r0"}, true},
{"invalid version with suffix", args{"1.0-rx"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isValidAPKVersion(tt.args.v); got != tt.want {
t.Errorf("isValidAPKVersion() = %v, want %v", got, tt.want)
}
})
}
}

// TestApkReadResultsManifest tests the apkReadResultsManifest function.
func TestIsLessThanAPKVersion(t *testing.T) {
type args struct {
v1 string
v2 string
}
tests := []struct {
name string
args args
want bool
}{
{"less than with suffix", args{"1.0-r0", "1.0-r1"}, true},
{"equal with suffix", args{"1.0-r0", "1.0-r0"}, false},
{"greater than with suffix", args{"1.0-r2", "1.0-r1"}, false},
{"less than without suffix", args{"1.0", "2.0"}, true},
{"equal without suffix", args{"2.0", "2.0"}, false},
{"greater than without suffix", args{"3.0", "2.0"}, false},
{"less than mixed suffixes", args{"1a_rc4_p5_b7-6-x86_64", "3a_rc4_p5_b7-6-x86_64"}, true},
{"equal mixed suffixes", args{"3a_rc4_p5_b7-6-x86_64", "3a_rc4_p5_b7-6-x86_64"}, false},
{"greater than mixed suffixes", args{"5a_rc4_p5_b7-6-x86_64", "3a_rc4_p5_b7-6-x86_64"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLessThanAPKVersion(tt.args.v1, tt.args.v2); got != tt.want {
t.Errorf("isLessThanAPKVersion() = %v, want %v", got, tt.want)
}
})
}
}

// TestApkReadResultsManifest tests the apkReadResultsManifest function.
func TestApkReadResultsManifest(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{"valid file", args{"testdata/apk_valid.txt"}, []string{"apk-tools-2.12.7-r0", "busybox-1.33.1-r8"}, false},
{"file does not exist", args{"testdata/no_such_file.txt"}, nil, true},
{"empty file", args{"testdata/empty.txt"}, nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := apkReadResultsManifest(tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("apkReadResultsManifest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("apkReadResultsManifest() = %v, want %v", got, tt.want)
}
})
}
}

// TestValidateAPKPackageVersions tests the validateAPKPackageVersions function.
func TestValidateAPKPackageVersions(t *testing.T) {
apkComparer := VersionComparer{isValidAPKVersion, isLessThanAPKVersion}

// Define some test cases with inputs and expected outputs
testCases := []struct {
name string
updates types.UpdatePackages
cmp VersionComparer
resultsPath string
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",
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",
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: "expected 2 updates, installed 1",
updates: []types.UpdatePackage{{Name: "apk-tools", Version: "2.12.7-r0"}},
cmp: apkComparer,
resultsPath: "testdata/apk_valid.txt",
expectedErr: fmt.Errorf("expected 2 updates, installed 1"),
},
}

for _, tc := range testCases {
// 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)
if tc.expectedErr != nil {
if err == nil || errors.Is(err, tc.expectedErr) {
t.Errorf("expected error %v, got %v", tc.expectedErr, err)
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
})
}
}

0 comments on commit 8d4a9c0

Please sign in to comment.