driftctl/pkg/remote/github/github_repository_supplier_...

86 lines
2.4 KiB
Go
Raw Normal View History

2021-02-15 13:24:20 +00:00
package github
import (
"context"
"testing"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
2021-05-21 14:09:45 +00:00
resourcegithub "github.com/cloudskiff/driftctl/pkg/resource/github"
2021-02-15 13:24:20 +00:00
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
dritftctlmocks "github.com/cloudskiff/driftctl/test/mocks"
2021-05-21 14:09:45 +00:00
testresource "github.com/cloudskiff/driftctl/test/resource"
2021-02-15 13:24:20 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestGithubRepositorySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
2021-02-16 09:51:40 +00:00
mocks func(client *MockGithubRepository)
2021-02-15 13:24:20 +00:00
err error
}{
{
test: "no github repos",
dirName: "github_repository_empty",
2021-02-16 09:51:40 +00:00
mocks: func(client *MockGithubRepository) {
2021-02-16 10:46:03 +00:00
client.On("ListRepositories").Return([]string{}, nil)
2021-02-15 13:24:20 +00:00
},
err: nil,
},
{
test: "Multiple github repos Table",
dirName: "github_repository_multiple",
2021-02-16 09:51:40 +00:00
mocks: func(client *MockGithubRepository) {
2021-02-16 10:46:03 +00:00
client.On("ListRepositories").Return([]string{
"driftctl",
"driftctl-demos",
2021-02-15 13:24:20 +00:00
}, nil)
},
err: nil,
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
2021-05-21 14:09:45 +00:00
repo := testresource.InitFakeSchemaRepository(terraform.GITHUB, "4.4.0")
resourcegithub.InitResourcesMetadata(repo)
factory := terraform.NewTerraformResourceFactory(repo)
deserializer := resource.NewDeserializer(factory)
2021-02-16 09:51:40 +00:00
mockedRepo := MockGithubRepository{}
2021-02-15 13:24:20 +00:00
c.mocks(&mockedRepo)
if shouldUpdate {
provider, err := InitTestGithubProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
2021-05-21 14:09:45 +00:00
supplierLibrary.AddSupplier(NewGithubRepositorySupplier(provider, &mockedRepo, deserializer))
2021-02-15 13:24:20 +00:00
}
t.Run(c.test, func(tt *testing.T) {
provider := dritftctlmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.GITHUB), shouldUpdate)
s := &GithubRepositorySupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
2021-02-15 13:24:20 +00:00
&mockedRepo,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
2021-05-21 14:09:45 +00:00
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-02-15 13:24:20 +00:00
})
}
}