2019-10-10 18:45:17 +03:00
package alpine_test
2019-04-30 07:02:09 +03:00
import (
2021-01-11 18:08:29 +03:00
"flag"
2019-04-30 07:02:09 +03:00
"io/ioutil"
2021-01-11 18:08:29 +03:00
"net/http"
"net/http/httptest"
"net/url"
2019-10-10 18:45:17 +03:00
"os"
2019-04-30 07:02:09 +03:00
"testing"
2019-10-07 23:59:50 +03:00
2021-01-11 18:08:29 +03:00
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-10-08 03:28:23 +03:00
2019-10-10 18:45:17 +03:00
"github.com/aquasecurity/vuln-list-update/alpine"
2019-04-30 07:02:09 +03:00
)
2021-01-11 18:08:29 +03:00
var update = flag . Bool ( "update" , false , "update golden files" )
2019-04-30 07:02:09 +03:00
2021-01-11 18:08:29 +03:00
func TestUpdater_Update ( t * testing . T ) {
type fields struct {
appFs afero . Fs
retry int
2019-04-30 07:02:09 +03:00
}
2021-01-11 18:08:29 +03:00
tests := [ ] struct {
name string
fields fields
fileNames map [ string ] string
goldenFiles map [ string ] string
wantErr string
2019-10-10 18:45:17 +03:00
} {
{
name : "happy path" ,
2021-01-11 18:08:29 +03:00
fields : fields {
appFs : afero . NewMemMapFs ( ) ,
retry : 0 ,
2019-10-10 18:45:17 +03:00
} ,
2021-01-11 18:08:29 +03:00
fileNames : map [ string ] string {
"/" : "testdata/index.html" ,
"/v3.11" : "testdata/311.html" ,
"/v3.12" : "testdata/312.html" ,
"/v3.11/main.json" : "testdata/311-main.json" ,
"/v3.11/community.json" : "testdata/311-community.json" ,
"/v3.12/main.json" : "testdata/312-main.json" ,
"/v3.12/community.json" : "testdata/312-community.json" ,
2019-10-10 18:45:17 +03:00
} ,
2021-01-11 18:08:29 +03:00
goldenFiles : map [ string ] string {
"/tmp/alpine/3.11/main/apache2.json" : "testdata/golden/311-apache2.json" ,
"/tmp/alpine/3.12/main/ansible.json" : "testdata/golden/312-ansible.json" ,
2019-10-10 18:45:17 +03:00
} ,
} ,
{
2021-01-11 18:08:29 +03:00
name : "no release" ,
fields : fields {
appFs : afero . NewMemMapFs ( ) ,
retry : 0 ,
} ,
fileNames : map [ string ] string {
"/" : "testdata/norelease.html" ,
2019-10-08 03:30:20 +03:00
} ,
2019-10-10 18:45:17 +03:00
} ,
{
2021-01-11 18:08:29 +03:00
name : "404" ,
fields : fields {
appFs : afero . NewMemMapFs ( ) ,
retry : 0 ,
2019-10-08 03:28:23 +03:00
} ,
2021-01-11 18:08:29 +03:00
fileNames : map [ string ] string {
"/" : "testdata/index.html" ,
2019-10-08 03:28:23 +03:00
} ,
2021-01-11 18:08:29 +03:00
wantErr : "status code: 404" ,
2019-10-10 18:45:17 +03:00
} ,
}
2021-01-11 18:08:29 +03:00
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
ts := httptest . NewServer ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
fileName , ok := tt . fileNames [ r . URL . Path ]
if ! ok {
http . NotFound ( w , r )
return
}
http . ServeFile ( w , r , fileName )
} ) )
defer ts . Close ( )
baseURL , err := url . Parse ( ts . URL )
require . NoError ( t , err )
u := alpine . NewUpdater ( alpine . WithVulnListDir ( "/tmp" ) , alpine . WithBaseURL ( baseURL ) ,
alpine . WithAppFs ( tt . fields . appFs ) , alpine . WithRetry ( tt . fields . retry ) )
err = u . Update ( )
if tt . wantErr != "" {
require . NotNil ( t , err )
assert . Contains ( t , err . Error ( ) , tt . wantErr )
return
2019-10-10 18:45:17 +03:00
} else {
assert . NoError ( t , err )
2021-01-11 18:08:29 +03:00
}
2019-10-10 18:45:17 +03:00
2021-01-11 18:08:29 +03:00
fileCount := 0
err = afero . Walk ( tt . fields . appFs , "/" , func ( path string , info os . FileInfo , err error ) error {
if err != nil {
return err
}
if info . IsDir ( ) {
2019-10-10 18:45:17 +03:00
return nil
2021-01-11 18:08:29 +03:00
}
fileCount ++
actual , err := afero . ReadFile ( tt . fields . appFs , path )
assert . NoError ( t , err , path )
goldenPath , ok := tt . goldenFiles [ path ]
require . True ( t , ok , path )
if * update {
err = ioutil . WriteFile ( goldenPath , actual , 0666 )
require . NoError ( t , err , goldenPath )
}
expected , err := ioutil . ReadFile ( goldenPath )
assert . NoError ( t , err , goldenPath )
assert . JSONEq ( t , string ( expected ) , string ( actual ) , path )
return nil
} )
assert . Equal ( t , len ( tt . goldenFiles ) , fileCount )
assert . NoError ( t , err )
2019-10-10 18:45:17 +03:00
} )
}
2019-10-08 03:28:23 +03:00
}