Skip to content

Commit

Permalink
tfv: Fix GetOk field name in hand written iam files (#6745)
Browse files Browse the repository at this point in the history
* tfv: Fix GetOk field name in hand written iam files

* tfv: Add unit test for IAM FetchFullResource
  • Loading branch information
iyabchen authored Nov 11, 2022
1 parent bca878c commit 31a061c
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 7 deletions.
4 changes: 4 additions & 0 deletions mmv1/provider/terraform_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def compile_common_files(output_folder, products, _common_compile_file)
)

@non_defined_tests = retrieve_full_manifest_of_non_defined_tests
files = retrieve_full_list_of_test_files
@tests = files.map { |file| file.split('.')[0] } | []

test_source = retrieve_test_source_code_with_location('[b]').map do |location|
[location[0].sub('go.erb', 'go'), location[1]]
Expand All @@ -149,6 +151,8 @@ def compile_common_files(output_folder, products, _common_compile_file)
'third_party/terraform/utils/compute_operation.go.erb'],
['converters/google/resources/config.go',
'third_party/terraform/utils/config.go.erb'],
['converters/google/resources/config_test_utils.go',
'third_party/terraform/utils/config_test_utils.go.erb'],
['converters/google/resources/iam.go',
'third_party/terraform/utils/iam.go.erb'],
['converters/google/resources/compute_instance_helpers.go',
Expand Down
39 changes: 39 additions & 0 deletions mmv1/third_party/terraform/utils/config_test_utils.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% autogen_exception -%>
package google

import (
"net/http/httptest"
"strings"
)

// NewTestConfig create a config using the http test server.
func NewTestConfig(server *httptest.Server) *Config {
cfg := &Config{}
cfg.client = server.Client()
configureTestBasePaths(cfg, server.URL)
return cfg
}

func configureTestBasePaths(c *Config, url string) {
if !strings.HasSuffix(url, "/") {
url = url + "/"
}
// Generated Products
<% products.map.each do |product| -%>
c.<%= product[:definitions].name -%>BasePath = url
<% end -%>

// Handwritten Products / Versioned / Atypical Entries
c.CloudBillingBasePath = url
c.ComposerBasePath = url
c.ContainerBasePath = url
c.DataprocBasePath = url
c.DataflowBasePath = url
c.IamCredentialsBasePath = url
c.ResourceManagerV3BasePath = url
c.IAMBasePath = url
c.ServiceNetworkingBasePath = url
c.BigQueryBasePath = url
c.StorageTransferBasePath = url
c.BigtableAdminBasePath = url
}
2 changes: 1 addition & 1 deletion mmv1/third_party/validator/bigquery_dataset_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newBigqueryDatasetIamAsset(

func FetchBigqueryDatasetIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("{{dataset_id}}"); !ok {
if _, ok := d.GetOk("dataset_id"); !ok {
return Asset{}, ErrEmptyIdentityField
}

Expand Down
2 changes: 1 addition & 1 deletion mmv1/third_party/validator/kms_crypto_key_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func newKmsCryptoKeyIamAsset(

func FetchKmsCryptoKeyIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("{{crypto_key_id}}"); !ok {
if _, ok := d.GetOk("crypto_key_id"); !ok {
return Asset{}, ErrEmptyIdentityField
}

Expand Down
2 changes: 1 addition & 1 deletion mmv1/third_party/validator/pubsub_subscription_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newPubsubSubscriptionIamAsset(

func FetchPubsubSubscriptionIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("{{subscription}}"); !ok {
if _, ok := d.GetOk("subscription"); !ok {
return Asset{}, ErrEmptyIdentityField
}

Expand Down
4 changes: 2 additions & 2 deletions mmv1/third_party/validator/spanner_database_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func newSpannerDatabaseIamAsset(

func FetchSpannerDatabaseIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("{{instance}}"); !ok {
if _, ok := d.GetOk("instance"); !ok {
return Asset{}, ErrEmptyIdentityField
}

if _, ok := d.GetOk("{{database}}"); !ok {
if _, ok := d.GetOk("database"); !ok {
return Asset{}, ErrEmptyIdentityField
}

Expand Down
2 changes: 1 addition & 1 deletion mmv1/third_party/validator/spanner_instance_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newSpannerInstanceIamAsset(

func FetchSpannerInstanceIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("{{instance}}"); !ok {
if _, ok := d.GetOk("instance"); !ok {
return Asset{}, ErrEmptyIdentityField
}

Expand Down
2 changes: 1 addition & 1 deletion mmv1/third_party/validator/storage_bucket_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func newStorageBucketIamAsset(

func FetchStorageBucketIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("{{bucket}}"); !ok {
if _, ok := d.GetOk("bucket"); !ok {
return Asset{}, ErrEmptyIdentityField
}

Expand Down
104 changes: 104 additions & 0 deletions mmv1/third_party/validator/tests/source/iam_test.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<% autogen_exception -%>
package test

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

crmv1 "google.golang.org/api/cloudresourcemanager/v1"

resources "github.com/GoogleCloudPlatform/terraform-validator/converters/google/resources"
"github.com/GoogleCloudPlatform/terraform-validator/tfdata"
"github.com/GoogleCloudPlatform/terraform-validator/tfplan"
provider "github.com/hashicorp/terraform-provider-google/google"
)

func TestIAMFetchFullResource(t *testing.T) {
cases := []struct {
name string
}{
<% @tests.each do |test| -%>
<% if test.end_with?("iam_binding") || test.end_with?("iam_member") -%>
{name: "<%= test -%>"},
<% end -%>
<% end -%>
}

converters := resources.ResourceConverters()
schema := provider.Provider()

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var payload []byte
var err error
if strings.Contains(r.URL.String(), "dataset") {
obj := map[string][]interface{}{
"access": {&crmv1.Policy{}},
}
payload, err = json.Marshal(obj)
} else {
payload, err = (&crmv1.Policy{}).MarshalJSON()
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("failed to MarshalJSON: %s", err)))
return
}
w.Write(payload)
}))

// Using Cleanup instead of defer because t.Parallel() does not block t.Run.
t.Cleanup(func() {
server.Close()
})

cfg := resources.NewTestConfig(server)

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()

// Create a temporary directory for generating tfplan.json from template.
dir, err := ioutil.TempDir(tmpDir, "terraform")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

generateTestFiles(t, "../testdata/templates", dir, c.name+".tfplan.json")
path := filepath.Join(dir, c.name+".tfplan.json")

data, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("opening JSON plan file: %s", err)
}
changes, err := tfplan.ReadResourceChanges(data)
if err != nil {
t.Fatalf("ReadResourceChanges failed: %s", err)
}

for _, rc := range changes {
resource := schema.ResourcesMap[rc.Type]
rd := tfdata.NewFakeResourceData(
rc.Type,
resource.Schema,
rc.Change.After.(map[string]interface{}),
)
for _, converter := range converters[rd.Kind()] {
if converter.FetchFullResource == nil {
continue
}
_, err := converter.FetchFullResource(rd, cfg)
if err != nil {
t.Errorf("FetchFullResource() = %s, want = nil", err)
}
}
}

})
}
}

0 comments on commit 31a061c

Please sign in to comment.