-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a86887c
commit 9765abb
Showing
12 changed files
with
359 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package repository | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resources/armresources" | ||
"github.com/cloudskiff/driftctl/pkg/remote/cache" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func Test_ArmResources_ListAllResourceGroups(t *testing.T) { | ||
expectedResults := []*armresources.ResourceGroup{ | ||
{ | ||
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/elie-dev"), | ||
Name: to.StringPtr("elie-dev"), | ||
}, | ||
{ | ||
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/william-dev"), | ||
Name: to.StringPtr("william-dev"), | ||
}, | ||
{ | ||
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/driftctl-sj-tests"), | ||
Name: to.StringPtr("driftctl-sj-tests"), | ||
}, | ||
} | ||
|
||
testcases := []struct { | ||
name string | ||
mocks func(*mockArmResourcesListPager, *cache.MockCache) | ||
expected []*armresources.ResourceGroup | ||
wantErr string | ||
}{ | ||
{ | ||
name: "should return resource groups", | ||
mocks: func(mockPager *mockArmResourcesListPager, mockCache *cache.MockCache) { | ||
mockPager.On("Err").Return(nil).Times(3) | ||
mockPager.On("NextPage", mock.Anything).Return(true).Times(2) | ||
mockPager.On("NextPage", mock.Anything).Return(false).Times(1) | ||
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{ | ||
ResourceGroupsListResult: armresources.ResourceGroupsListResult{ | ||
ResourceGroupListResult: armresources.ResourceGroupListResult{ | ||
Value: []*armresources.ResourceGroup{ | ||
{ | ||
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/elie-dev"), | ||
Name: to.StringPtr("elie-dev"), | ||
}, | ||
{ | ||
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/william-dev"), | ||
Name: to.StringPtr("william-dev"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}).Times(1) | ||
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{ | ||
ResourceGroupsListResult: armresources.ResourceGroupsListResult{ | ||
ResourceGroupListResult: armresources.ResourceGroupListResult{ | ||
Value: []*armresources.ResourceGroup{ | ||
{ | ||
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/driftctl-sj-tests"), | ||
Name: to.StringPtr("driftctl-sj-tests"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}).Times(1) | ||
|
||
mockCache.On("Get", "armResourcesListAllResourceGroups").Return(nil).Times(1) | ||
mockCache.On("Put", "armResourcesListAllResourceGroups", expectedResults).Return(true).Times(1) | ||
}, | ||
expected: expectedResults, | ||
}, | ||
{ | ||
name: "should hit cache and return resource groups", | ||
mocks: func(mockPager *mockArmResourcesListPager, mockCache *cache.MockCache) { | ||
mockCache.On("Get", "armResourcesListAllResourceGroups").Return(expectedResults).Times(1) | ||
}, | ||
expected: expectedResults, | ||
}, | ||
{ | ||
name: "should return remote error", | ||
mocks: func(mockPager *mockArmResourcesListPager, mockCache *cache.MockCache) { | ||
mockPager.On("NextPage", mock.Anything).Return(true).Times(1) | ||
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{ | ||
ResourceGroupsListResult: armresources.ResourceGroupsListResult{ | ||
ResourceGroupListResult: armresources.ResourceGroupListResult{ | ||
Value: []*armresources.ResourceGroup{}, | ||
}, | ||
}, | ||
}).Times(1) | ||
mockPager.On("Err").Return(errors.New("remote error")).Times(1) | ||
|
||
mockCache.On("Get", "armResourcesListAllResourceGroups").Return(nil).Times(1) | ||
}, | ||
wantErr: "remote error", | ||
}, | ||
{ | ||
name: "should return remote error after fetching all pages", | ||
mocks: func(mockPager *mockArmResourcesListPager, mockCache *cache.MockCache) { | ||
mockPager.On("NextPage", mock.Anything).Return(true).Times(1) | ||
mockPager.On("NextPage", mock.Anything).Return(false).Times(1) | ||
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{ | ||
ResourceGroupsListResult: armresources.ResourceGroupsListResult{ | ||
ResourceGroupListResult: armresources.ResourceGroupListResult{ | ||
Value: []*armresources.ResourceGroup{}, | ||
}, | ||
}, | ||
}).Times(1) | ||
mockPager.On("Err").Return(nil).Times(1) | ||
mockPager.On("Err").Return(errors.New("remote error")).Times(1) | ||
|
||
mockCache.On("Get", "armResourcesListAllResourceGroups").Return(nil).Times(1) | ||
}, | ||
wantErr: "remote error", | ||
}, | ||
} | ||
|
||
for _, tt := range testcases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fakeClient := &mockArmResourcesClient{} | ||
mockPager := &mockArmResourcesListPager{} | ||
mockCache := &cache.MockCache{} | ||
|
||
fakeClient.On("List", mock.Anything).Maybe().Return(mockPager) | ||
|
||
tt.mocks(mockPager, mockCache) | ||
|
||
s := &armResourcesRepository{ | ||
client: fakeClient, | ||
cache: mockCache, | ||
} | ||
got, err := s.ListAllResourceGroups() | ||
if tt.wantErr != "" { | ||
assert.EqualError(t, err, tt.wantErr) | ||
} else { | ||
assert.Nil(t, err) | ||
} | ||
|
||
fakeClient.AssertExpectations(t) | ||
mockPager.AssertExpectations(t) | ||
mockCache.AssertExpectations(t) | ||
|
||
if !reflect.DeepEqual(got, tt.expected) { | ||
t.Errorf("ListAllResourceGroups() got = %v, want %v", got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
pkg/remote/azurerm/repository/mock_armResourcesListPager.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
package azurerm | ||
|
||
import "github.com/cloudskiff/driftctl/pkg/resource" | ||
|
||
const AzureResourceGroupResourceType = "azurerm_resource_group" | ||
|
||
func initAzureResourceGroupMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) { | ||
resourceSchemaRepository.SetNormalizeFunc(AzureResourceGroupResourceType, func(res *resource.Resource) { | ||
val := res.Attrs | ||
val.SafeDelete([]string{"timeouts"}) | ||
}) | ||
resourceSchemaRepository.SetHumanReadableAttributesFunc(AzureResourceGroupResourceType, func(res *resource.Resource) map[string]string { | ||
val := res.Attrs | ||
attrs := make(map[string]string) | ||
if name := val.GetString("name"); name != nil && *name != "" { | ||
attrs["Name"] = *name | ||
} | ||
return attrs | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package azurerm_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudskiff/driftctl/test" | ||
"github.com/cloudskiff/driftctl/test/acceptance" | ||
) | ||
|
||
func TestAcc_Azure_ResourceGroup(t *testing.T) { | ||
acceptance.Run(t, acceptance.AccTestCase{ | ||
TerraformVersion: "0.15.5", | ||
Paths: []string{"./testdata/acc/azurerm_resource_group"}, | ||
Args: []string{ | ||
"scan", | ||
"--to", "azure+tf", | ||
"--filter", "Type=='azurerm_resource_group' && contains(Id, 'acc-test-res-group-')", | ||
}, | ||
Checks: []acceptance.AccCheck{ | ||
{ | ||
Check: func(result *test.ScanResult, stdout string, err error) { | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
result.AssertInfrastructureIsInSync() | ||
result.AssertManagedCount(2) | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.