-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tfv: Fix GetOk field name in hand written iam files (#6745)
* tfv: Fix GetOk field name in hand written iam files * tfv: Add unit test for IAM FetchFullResource
- Loading branch information
Showing
9 changed files
with
154 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
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 | ||
} |
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
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
104 changes: 104 additions & 0 deletions
104
mmv1/third_party/validator/tests/source/iam_test.go.erb
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,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) | ||
} | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |