Skip to content

Commit

Permalink
interal/provider: ensuring that all resources support import
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Mar 23, 2022
1 parent ddd2d05 commit 2b63a8d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"fmt"
"log"
"testing"
"time"
)
Expand Down Expand Up @@ -89,3 +90,10 @@ func TestResourcesSupportCustomTimeouts(t *testing.T) {
func TestProvider_impl(t *testing.T) {
_ = AzureProvider()
}

func TestProvider_counts(t *testing.T) {
// @tombuildsstuff: this is less a unit test and more a useful placeholder tbh
provider := TestAzureProvider()
log.Printf("Data Sources: %d", len(provider.DataSourcesMap))
log.Printf("Resources: %d", len(provider.ResourcesMap))
}
48 changes: 48 additions & 0 deletions internal/provider/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,51 @@ func TestTypedResourcesContainValidModelObjects(t *testing.T) {
}
}
}

func TestTypedResourcesContainValidIDParsers(t *testing.T) {
// This test confirms that all of the Typed Resources return an ID Validation method
// which is used to ensure that each of the resources will validate the Resource ID
// during import time. Whilst this may seem unnecessary as it's an interface method
// since we could return nil, this test is double-checking.
//
// Untyped Resources are checked via TestUntypedResourcesContainImporters
for _, service := range SupportedTypedServices() {
t.Logf("Service %q..", service.Name())
for _, resource := range service.Resources() {
t.Logf("- Resource %q..", resource.ResourceType())
obj := resource.IDValidationFunc()
if obj == nil {
t.Fatalf("IDValidationFunc returns nil - all resources must return an ID Validation function")
}
}
}
}

func TestUntypedResourcesContainImporters(t *testing.T) {
// Typed Resources are checked via TestTypedResourcesContainValidIDParsers
// as if an ID Parser is returned it's automatically used (and it's a required
// method on the sdk.Resource interface)
for _, service := range SupportedUntypedServices() {
deprecatedResourcesWhichDontSupportImport := map[string]struct{}{
// @tombuildsstuff: new resources shouldn't be added to this list - instead add an Import function
// to the resource, for example:
//
// Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
// _, err := ParseTheId(id)
// return err
// })
"azurerm_security_center_server_vulnerability_assessment": {},
"azurerm_template_deployment": {},
}
for k, v := range service.SupportedResources() {
if _, ok := deprecatedResourcesWhichDontSupportImport[k]; ok {
t.Logf("the resource %q doesn't support import but it's deprecated so we're skipping..", k)
continue
}

if v.Importer == nil {
t.Fatalf("all resources must support import, however the resource %q does not support import", k)
}
}
}
}

0 comments on commit 2b63a8d

Please sign in to comment.