Files
trivy/pkg/commands/clean/run_test.go
Teppei Fukuda 8d0ae1f5de feat!: add clean subcommand (#6993)
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Co-authored-by: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com>
Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
2024-06-25 09:06:27 +00:00

136 lines
3.1 KiB
Go

package clean_test
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/trivy/pkg/commands/clean"
"github.com/aquasecurity/trivy/pkg/flag"
)
func TestRun(t *testing.T) {
tests := []struct {
name string
cleanOpts flag.CleanOptions
wantErr bool
checkFunc func(*testing.T, string)
}{
{
name: "clean all",
cleanOpts: flag.CleanOptions{
CleanAll: true,
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
assert.NoDirExists(t, dir)
},
},
{
name: "clean scan cache",
cleanOpts: flag.CleanOptions{
CleanScanCache: true,
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
assert.NoDirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
assert.DirExists(t, filepath.Join(dir, "java-db"))
assert.DirExists(t, filepath.Join(dir, "policy"))
},
},
{
name: "clean vulnerability DB",
cleanOpts: flag.CleanOptions{
CleanVulnerabilityDB: true,
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
assert.NoDirExists(t, filepath.Join(dir, "db"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "java-db"))
assert.DirExists(t, filepath.Join(dir, "policy"))
},
},
{
name: "clean Java DB",
cleanOpts: flag.CleanOptions{
CleanJavaDB: true,
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
assert.NoDirExists(t, filepath.Join(dir, "java-db"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
assert.DirExists(t, filepath.Join(dir, "policy"))
},
},
{
name: "clean check bundle",
cleanOpts: flag.CleanOptions{
CleanChecksBundle: true,
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
assert.NoDirExists(t, filepath.Join(dir, "policy"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
assert.DirExists(t, filepath.Join(dir, "java-db"))
},
},
{
name: "no clean option specified",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary directory for testing
tempDir := t.TempDir()
// Create test directories and files
createTestFiles(t, tempDir)
opts := flag.Options{
GlobalOptions: flag.GlobalOptions{
CacheDir: tempDir,
},
CleanOptions: tt.cleanOpts,
}
err := clean.Run(context.Background(), opts)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
if tt.checkFunc != nil {
tt.checkFunc(t, tempDir)
}
})
}
}
func createTestFiles(t *testing.T, dir string) {
subdirs := []string{
"fanal",
"db",
"java-db",
"policy",
}
for _, subdir := range subdirs {
err := os.MkdirAll(filepath.Join(dir, subdir), 0755)
require.NoError(t, err)
testFile := filepath.Join(dir, subdir, "testfile.txt")
err = os.WriteFile(testFile, []byte("test content"), 0644)
require.NoError(t, err)
}
}