2022-01-18 22:45:06 +09:00
package rocky_test
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2023-09-20 09:23:39 +03:00
"github.com/aquasecurity/vuln-list-update/rocky"
2022-01-18 22:45:06 +09:00
)
func Test_Update ( t * testing . T ) {
tests := [ ] struct {
2022-05-26 10:31:24 +06:00
name string
releasesFilePath string
rootDir string
repository [ ] string
wantErr string
2022-01-18 22:45:06 +09:00
} {
{
2022-05-26 10:31:24 +06:00
name : "happy path" ,
releasesFilePath : "testdata/fixtures/releases/happy.html" ,
rootDir : "testdata/fixtures/happy" ,
repository : [ ] string { "BaseOS" } ,
2022-01-18 22:45:06 +09:00
} ,
{
2022-05-26 10:31:24 +06:00
name : "bad repomd response" ,
releasesFilePath : "testdata/fixtures/releases/happy.html" ,
rootDir : "testdata/fixtures/repomd_invalid" ,
repository : [ ] string { "BaseOS" } ,
2022-06-20 16:36:58 +06:00
wantErr : "failed to get security advisories from" ,
2022-01-18 22:45:06 +09:00
} ,
{
2022-05-26 10:31:24 +06:00
name : "bad updateInfo response" ,
releasesFilePath : "testdata/fixtures/releases/happy.html" ,
rootDir : "testdata/fixtures/updateinfo_invalid" ,
repository : [ ] string { "BaseOS" } ,
wantErr : "failed to fetch updateInfo" ,
2022-01-18 22:45:06 +09:00
} ,
2022-01-24 21:17:54 +09:00
{
2022-05-26 10:31:24 +06:00
name : "no updateInfo field(BaseOS)" ,
releasesFilePath : "testdata/fixtures/releases/happy.html" ,
rootDir : "testdata/fixtures/no_updateinfo_field" ,
repository : [ ] string { "BaseOS" } ,
2022-06-20 16:36:58 +06:00
wantErr : "failed to get security advisories from" ,
2022-01-24 21:17:54 +09:00
} ,
{
2022-05-26 10:31:24 +06:00
name : "no updateInfo field(extras)" ,
releasesFilePath : "testdata/fixtures/releases/happy.html" ,
rootDir : "testdata/fixtures/no_updateinfo_field" ,
repository : [ ] string { "extras" } ,
} ,
2022-06-20 16:36:58 +06:00
{
name : "no repomd file" ,
releasesFilePath : "testdata/fixtures/releases/happy.html" ,
rootDir : "testdata/fixtures/releases" ,
repository : [ ] string { "BaseOS" } ,
wantErr : "failed to get security advisories from" ,
} ,
2022-05-26 10:31:24 +06:00
{
name : "empty list of releases" ,
releasesFilePath : "testdata/fixtures/releases/empty.html" ,
repository : [ ] string { "BaseOS" } ,
wantErr : "list is empty" ,
2022-01-24 21:17:54 +09:00
} ,
2022-01-18 22:45:06 +09:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2022-05-26 10:31:24 +06:00
mux := http . NewServeMux ( )
mux . Handle ( "/pub/rocky/" , http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
http . ServeFile ( w , r , tt . releasesFilePath )
} ) )
mux . Handle ( "/pub/rocky/8.5/extras/x86_64/os/repodata/" , http . StripPrefix ( "/pub/rocky/8.5/extras/x86_64/os/repodata/" , http . FileServer ( http . Dir ( tt . rootDir ) ) ) )
mux . Handle ( "/pub/rocky/8.5/BaseOS/x86_64/os/repodata/" , http . StripPrefix ( "/pub/rocky/8.5/BaseOS/x86_64/os/repodata/" , http . FileServer ( http . Dir ( tt . rootDir ) ) ) )
tsUpdateInfoURL := httptest . NewServer ( mux )
2022-01-18 22:45:06 +09:00
defer tsUpdateInfoURL . Close ( )
dir := t . TempDir ( )
2022-06-20 16:36:58 +06:00
rc := rocky . NewConfig ( rocky . With ( "%s/%s/%s/%s/os/" , dir , 0 , tt . repository , [ ] string { "x86_64" } , [ ] string { tsUpdateInfoURL . URL + "/pub/rocky" } ) )
2022-05-26 10:31:24 +06:00
if err := rc . Update ( ) ; tt . wantErr != "" {
2022-01-18 22:45:06 +09:00
require . Error ( t , err )
2022-05-26 10:31:24 +06:00
assert . Contains ( t , err . Error ( ) , tt . wantErr )
2022-01-18 22:45:06 +09:00
return
}
err := filepath . Walk ( dir , func ( path string , info os . FileInfo , errfp error ) error {
if errfp != nil {
return errfp
}
if info . IsDir ( ) {
return nil
}
dir , file := filepath . Split ( path )
want , err := os . ReadFile ( filepath . Join ( "testdata" , "golden" , filepath . Base ( dir ) , file ) )
assert . NoError ( t , err , "failed to open the golden file" )
got , err := os . ReadFile ( path )
assert . NoError ( t , err , "failed to open the result file" )
assert . JSONEq ( t , string ( want ) , string ( got ) )
return nil
} )
assert . Nil ( t , err , "filepath walk error" )
} )
}
}