Files
trivy/internal/app_test.go
Simarpreet Singh 94eb7cc592 Expose Trivy and VulnDB version through --version (#435)
* app: Expose Trivy and VulnDB version through --version

Signed-off-by: Simarpreet Singh <simar@linux.com>

* pkg: Use time.Time as value not reference.

Based on: 64db180151

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Use various formatted outputs

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Take value of --cache-dir for cacheDir

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Refactor and test showVersion

Signed-off-by: Simarpreet Singh <simar@linux.com>

* library: lighten names by remove version suffix

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Show types and add parity of table and JSON

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Switch to show using UTC time

Signed-off-by: Simarpreet Singh <simar@linux.com>

* mod: Update to latest trivy-db master.

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Use c.App.Writer for os.Stdout

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Replace table output with docker version style output

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Fix output to show as "Version" for Trivy version.

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Move VersionInfo struct out to app.go

Signed-off-by: Simarpreet Singh <simar@linux.com>
2020-03-20 10:19:44 -07:00

95 lines
1.9 KiB
Go

package internal
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/stretchr/testify/assert"
)
type fakeIOWriter struct {
written []byte
}
func (f *fakeIOWriter) Write(p []byte) (n int, err error) {
f.written = append(f.written, p...)
return len(p), nil
}
func Test_showVersion(t *testing.T) {
type args struct {
cacheDir string
outputFormat string
version string
}
tests := []struct {
name string
args args
createDB bool
expectedOutput string
}{
{
name: "happy path, table output",
args: args{
outputFormat: "table",
version: "v1.2.3",
},
expectedOutput: `Version: v1.2.3
Vulnerability DB:
Type: Light
Version: 42
UpdatedAt: 2020-03-16 23:40:20 +0000 UTC
NextUpdate: 2020-03-16 23:57:00 +0000 UTC
`,
createDB: true,
},
{
name: "happy path, JSON output",
args: args{
outputFormat: "json",
version: "1.2.3",
},
expectedOutput: `{"Version":"1.2.3","VulnerabilityDB":{"Version":42,"Type":1,"NextUpdate":"2020-03-16T23:57:00Z","UpdatedAt":"2020-03-16T23:40:20Z"}}
`,
createDB: true,
},
{
name: "sad path, no DB is available",
args: args{
outputFormat: "table",
version: "1.2.3",
},
expectedOutput: `unable to display current version: unexpected end of JSON input`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d, _ := ioutil.TempDir("", "Test_showVersion-*")
defer func() {
os.RemoveAll(d)
}()
if tt.createDB {
db.Init(d)
db.Config{}.SetMetadata(db.Metadata{
Version: 42,
Type: 1,
NextUpdate: time.Unix(1584403020, 0),
UpdatedAt: time.Unix(1584402020, 0),
})
db.Close()
}
var wb []byte
fw := fakeIOWriter{written: wb}
showVersion(d, tt.args.outputFormat, tt.args.version, &fw)
assert.Equal(t, tt.expectedOutput, string(fw.written), tt.name)
})
}
}