2021-07-02 13:55:38 +00:00
package remote
import (
2021-08-20 13:15:51 +00:00
"errors"
2021-07-02 13:55:38 +00:00
"testing"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/mocks"
"github.com/snyk/driftctl/pkg/filter"
"github.com/snyk/driftctl/pkg/remote/alerts"
"github.com/snyk/driftctl/pkg/remote/cache"
"github.com/snyk/driftctl/pkg/remote/common"
remoteerr "github.com/snyk/driftctl/pkg/remote/error"
"github.com/snyk/driftctl/pkg/remote/github"
githubres "github.com/snyk/driftctl/pkg/resource/github"
"github.com/snyk/driftctl/pkg/terraform"
testresource "github.com/snyk/driftctl/test/resource"
tftest "github.com/snyk/driftctl/test/terraform"
2021-07-26 09:55:59 +00:00
"github.com/stretchr/testify/mock"
2021-07-02 13:55:38 +00:00
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/pkg/resource"
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/goldenfile"
2021-07-02 13:55:38 +00:00
"github.com/stretchr/testify/assert"
)
func TestScanGithubRepository ( t * testing . T ) {
tests := [ ] struct {
test string
dirName string
2021-08-20 13:15:51 +00:00
mocks func ( * github . MockGithubRepository , * mocks . AlerterInterface )
2021-07-02 13:55:38 +00:00
err error
} {
{
test : "no github repos" ,
dirName : "github_repository_empty" ,
2021-08-20 13:15:51 +00:00
mocks : func ( client * github . MockGithubRepository , alerter * mocks . AlerterInterface ) {
2021-07-02 13:55:38 +00:00
client . On ( "ListRepositories" ) . Return ( [ ] string { } , nil )
} ,
err : nil ,
} ,
{
test : "Multiple github repos Table" ,
dirName : "github_repository_multiple" ,
2021-08-20 13:15:51 +00:00
mocks : func ( client * github . MockGithubRepository , alerter * mocks . AlerterInterface ) {
2021-07-02 13:55:38 +00:00
client . On ( "ListRepositories" ) . Return ( [ ] string {
"driftctl" ,
"driftctl-demos" ,
} , nil )
} ,
err : nil ,
} ,
2021-08-20 13:15:51 +00:00
{
test : "cannot list repositories" ,
dirName : "github_repository_empty" ,
mocks : func ( client * github . MockGithubRepository , alerter * mocks . AlerterInterface ) {
client . On ( "ListRepositories" ) . Return ( nil , errors . New ( "Your token has not been granted the required scopes to execute this query." ) )
2021-09-06 15:00:38 +00:00
alerter . On ( "SendAlert" , githubres . GithubRepositoryResourceType , alerts . NewRemoteAccessDeniedAlert ( common . RemoteGithubTerraform , remoteerr . NewResourceListingErrorWithType ( errors . New ( "Your token has not been granted the required scopes to execute this query." ) , githubres . GithubRepositoryResourceType , githubres . GithubRepositoryResourceType ) , alerts . EnumerationPhase ) ) . Return ( )
2021-08-20 13:15:51 +00:00
} ,
err : nil ,
} ,
2021-07-02 13:55:38 +00:00
}
schemaRepository := testresource . InitFakeSchemaRepository ( "github" , "4.4.0" )
githubres . InitResourcesMetadata ( schemaRepository )
factory := terraform . NewTerraformResourceFactory ( schemaRepository )
deserializer := resource . NewDeserializer ( factory )
for _ , c := range tests {
t . Run ( c . test , func ( tt * testing . T ) {
shouldUpdate := c . dirName == * goldenfile . Update
scanOptions := ScannerOptions { Deep : true }
providerLibrary := terraform . NewProviderLibrary ( )
remoteLibrary := common . NewRemoteLibrary ( )
2021-08-20 13:15:51 +00:00
// Initialize mocks
alerter := & mocks . AlerterInterface { }
2021-07-02 13:55:38 +00:00
mockedRepo := github . MockGithubRepository { }
2021-08-20 13:15:51 +00:00
c . mocks ( & mockedRepo , alerter )
2021-07-02 13:55:38 +00:00
var repo github . GithubRepository = & mockedRepo
realProvider , err := tftest . InitTestGithubProvider ( providerLibrary , "4.4.0" )
if err != nil {
t . Fatal ( err )
}
provider := tftest . NewFakeTerraformProvider ( realProvider )
provider . WithResponse ( c . dirName )
if shouldUpdate {
err := realProvider . Init ( )
if err != nil {
t . Fatal ( err )
}
provider . ShouldUpdate ( )
repo = github . NewGithubRepository ( realProvider . GetConfig ( ) , cache . New ( 0 ) )
}
2021-07-05 13:33:42 +00:00
remoteLibrary . AddEnumerator ( github . NewGithubRepositoryEnumerator ( repo , factory ) )
2021-07-02 13:55:38 +00:00
remoteLibrary . AddDetailsFetcher ( githubres . GithubRepositoryResourceType , common . NewGenericDetailsFetcher ( githubres . GithubRepositoryResourceType , provider , deserializer ) )
2021-07-26 09:55:59 +00:00
testFilter := & filter . MockFilter { }
testFilter . On ( "IsTypeIgnored" , mock . Anything ) . Return ( false )
s := NewScanner ( remoteLibrary , alerter , scanOptions , testFilter )
2021-07-02 13:55:38 +00:00
got , err := s . Resources ( )
assert . Equal ( tt , err , c . err )
if err != nil {
return
}
test . TestAgainstGoldenFile ( got , githubres . GithubRepositoryResourceType , c . dirName , provider , deserializer , shouldUpdate , tt )
2021-08-20 13:15:51 +00:00
mockedRepo . AssertExpectations ( tt )
alerter . AssertExpectations ( tt )
2021-07-02 13:55:38 +00:00
} )
}
}