mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-10 23:00:48 -08:00
* config_test: Add missing assertions for TestNew Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Add integration tests for image subcommand. Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor: bump up urfave/cli to v2.0 * refactor: apply DIY to image flags * refactor: reorder sub commands * feat: set hidden to global image options * test(integration): insert --cache-dir before sub command * README: update readme to reflect new usage Signed-off-by: Simarpreet Singh <simar@linux.com> * chore(README): add image subcommand * fix(flags): define aliases according to urfave/cli v2.0 style Co-authored-by: knqyf263 <knqyf263@gmail.com>
97 lines
1.6 KiB
Go
97 lines
1.6 KiB
Go
// +build integration
|
|
|
|
package integration
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"context"
|
|
"flag"
|
|
"io"
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
dbFile "github.com/aquasecurity/trivy/pkg/db"
|
|
|
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
func gunzipDB() (string, error) {
|
|
gz, err := os.Open("testdata/trivy.db.gz")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
zr, err := gzip.NewReader(gz)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
tmpDir, err := ioutil.TempDir("", "integration")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
dbPath := db.Path(tmpDir)
|
|
dbDir := filepath.Dir(dbPath)
|
|
err = os.MkdirAll(dbDir, 0700)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
file, err := os.Create(dbPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer file.Close()
|
|
|
|
if _, err = io.Copy(file, zr); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = dbFile.NewMetadata(afero.NewOsFs(), tmpDir).Store(db.Metadata{
|
|
Version: 1,
|
|
Type: 1,
|
|
NextUpdate: time.Time{},
|
|
UpdatedAt: time.Time{},
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return tmpDir, nil
|
|
}
|
|
|
|
func getFreePort() (int, error) {
|
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer l.Close()
|
|
return l.Addr().(*net.TCPAddr).Port, nil
|
|
}
|
|
|
|
func waitPort(ctx context.Context, addr string) error {
|
|
for {
|
|
conn, err := net.Dial("tcp", addr)
|
|
if err == nil && conn != nil {
|
|
return nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return err
|
|
default:
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
}
|