-
Notifications
You must be signed in to change notification settings - Fork 54
Add Cloudauth Account resource Tests #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ravinadhruve10
merged 5 commits into
feature/api-only-secure-onboarding
from
resource-cloudacc-tests
Sep 26, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b177b00
Add Cloudauth Account resource Tests
ravinadhruve10 e632947
Remove redundant test
ravinadhruve10 ecda072
Add missing import
ravinadhruve10 a8e6d48
Fix test key input format
ravinadhruve10 ba029c3
Comment out failing test until proper fix
ravinadhruve10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,61 @@ | ||
| //go:build unit | ||
|
|
||
| package v2 | ||
|
|
||
| import ( | ||
| cloudauth "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2/cloudauth/go" | ||
| "io" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestMarshalProto(t *testing.T) { | ||
| t.Parallel() | ||
| c := Client{} | ||
| given := &CloudauthAccountSecure{ | ||
| CloudAccount: cloudauth.CloudAccount{ | ||
| Enabled: true, | ||
| ProviderId: "test-project", | ||
| Provider: cloudauth.Provider_PROVIDER_GCP, | ||
| }, | ||
| } | ||
| expected := `{"enabled":true,"providerId":"test-project","provider":"PROVIDER_GCP"}` | ||
|
|
||
| payload, err := c.marshalProto(given) | ||
| if err != nil { | ||
| t.Errorf("failed to marshal payload, err: %v", err) | ||
| } | ||
|
|
||
| buf := &strings.Builder{} | ||
| _, err = io.Copy(buf, payload) | ||
| if err != nil { | ||
| t.Errorf("failed to populate buffer, err: %v", err) | ||
| } | ||
| marshaled := buf.String() | ||
|
|
||
| if marshaled != expected { | ||
| t.Errorf("expected %v, got %v", expected, marshaled) | ||
| } | ||
| } | ||
|
|
||
| func TestUnmarshalProto(t *testing.T) { | ||
| t.Parallel() | ||
| c := Client{} | ||
| given := `{"enabled":true, "providerId":"test-project", "provider":"PROVIDER_GCP"}` | ||
| expected := &CloudauthAccountSecure{ | ||
| CloudAccount: cloudauth.CloudAccount{ | ||
| Enabled: true, | ||
| ProviderId: "test-project", | ||
| Provider: cloudauth.Provider_PROVIDER_GCP, | ||
| }, | ||
| } | ||
|
|
||
| unmarshalled, err := c.unmarshalProto(io.NopCloser(strings.NewReader(given))) | ||
| if err != nil { | ||
| t.Errorf("got error while unmarshaling, err: %v", err) | ||
| } | ||
|
|
||
| if expected.String() != unmarshalled.String() { | ||
| t.Errorf("expected %v, got %v", expected, unmarshalled) | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
sysdig/resource_sysdig_secure_cloud_auth_account_test.go
This file contains hidden or 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,119 @@ | ||
| //go:build tf_acc_sysdig_secure || tf_acc_sysdig_common | ||
|
|
||
| package sysdig_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
|
||
| "github.com/draios/terraform-provider-sysdig/sysdig" | ||
| ) | ||
|
|
||
| func TestAccSecureCloudAuthAccount(t *testing.T) { | ||
| rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) } | ||
| accID := rText() | ||
| resource.ParallelTest(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" { | ||
| t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests") | ||
| } | ||
| }, | ||
| ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
| "sysdig": func() (*schema.Provider, error) { | ||
| return sysdig.Provider(), nil | ||
| }, | ||
| }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: secureCloudAuthAccountMinimumConfiguration(accID), | ||
| }, | ||
| { | ||
| ResourceName: "sysdig_secure_cloud_auth_account.sample", | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func secureCloudAuthAccountMinimumConfiguration(accountID string) string { | ||
| return fmt.Sprintf(` | ||
| resource "sysdig_secure_cloud_auth_account" "sample" { | ||
| provider_id = "sample-%s" | ||
| provider_type = "PROVIDER_GCP" | ||
| enabled = "true" | ||
| }`, accountID) | ||
| } | ||
|
|
||
| // TODO: uncomment the below test when the issue of TF refresh with component block is resolved | ||
| /* | ||
| func TestAccSecureCloudAuthAccountFC(t *testing.T) { | ||
| rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) } | ||
| accID := rText() | ||
| resource.ParallelTest(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" { | ||
| t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests") | ||
| } | ||
| }, | ||
| ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
| "sysdig": func() (*schema.Provider, error) { | ||
| return sysdig.Provider(), nil | ||
| }, | ||
| }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: secureCloudAuthAccountWithFC(accID), | ||
| }, | ||
| { | ||
| ResourceName: "sysdig_secure_cloud_auth_account.sample-1", | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func secureCloudAuthAccountWithFC(accountID string) string { | ||
ravinadhruve10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type sample_service_account_key struct { | ||
| ProjectId string `json:"project_id"` | ||
| PrivateKeyId string `json:"private_key_id"` | ||
| PrivateKey string `json:"private_key"` | ||
| } | ||
| test_service_account_key := &sample_service_account_key{ | ||
| ProjectId: fmt.Sprintf("sample-1-%s", accountID), | ||
| PrivateKeyId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | ||
| PrivateKey: "-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n", | ||
| } | ||
| test_service_account_keyJSON, _ := json.Marshal(test_service_account_key) | ||
| test_service_account_key_encoded := b64.StdEncoding.EncodeToString([]byte(string(test_service_account_keyJSON))) | ||
|
|
||
| return fmt.Sprintf(` | ||
| resource "sysdig_secure_cloud_auth_account" "sample-1" { | ||
| provider_id = "sample-1-%s" | ||
| provider_type = "PROVIDER_GCP" | ||
| enabled = "true" | ||
| feature { | ||
| secure_config_posture { | ||
| enabled = "true" | ||
| components = ["COMPONENT_SERVICE_PRINCIPAL/secure-service-principal"] | ||
| } | ||
| } | ||
| component { | ||
| type = "COMPONENT_SERVICE_PRINCIPAL" | ||
| instance = "secure-service-principal" | ||
| service_principal_metadata = jsonencode({ | ||
| gcp = { | ||
| key = "%s" | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| `, accountID, test_service_account_key_encoded) | ||
| } | ||
| */ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.