generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 50
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
97f64d9
commit 8883d81
Showing
7 changed files
with
346 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package clients | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcp-sdk-go/auth" | ||
"github.com/hashicorp/hcp-sdk-go/auth/workload" | ||
) | ||
|
||
func Test_loadCredentialFile(t *testing.T) { | ||
tcs := map[string]struct { | ||
config ClientConfig | ||
want *auth.CredentialFile | ||
}{ | ||
"empty config": { | ||
config: ClientConfig{}, | ||
}, | ||
"ignores with only resource": { | ||
config: ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
}, | ||
}, | ||
"loads with file": { | ||
config: ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
WorkloadIdentityTokenFile: "/path/to/token/file", | ||
}, | ||
want: &auth.CredentialFile{ | ||
Scheme: auth.CredentialFileSchemeWorkload, | ||
Workload: &workload.IdentityProviderConfig{ | ||
ProviderResourceName: "my_resource", | ||
File: &workload.FileCredentialSource{ | ||
Path: "/path/to/token/file", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"loads with token": { | ||
config: ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
WorkloadIdentityToken: "my_token", | ||
}, | ||
want: &auth.CredentialFile{ | ||
Scheme: auth.CredentialFileSchemeWorkload, | ||
Workload: &workload.IdentityProviderConfig{ | ||
ProviderResourceName: "my_resource", | ||
Token: &workload.CredentialTokenSource{ | ||
Token: "my_token", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"defaults to token": { | ||
config: ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
WorkloadIdentityTokenFile: "/path/to/token/file", | ||
WorkloadIdentityToken: "my_token", | ||
}, | ||
want: &auth.CredentialFile{ | ||
Scheme: auth.CredentialFileSchemeWorkload, | ||
Workload: &workload.IdentityProviderConfig{ | ||
ProviderResourceName: "my_resource", | ||
Token: &workload.CredentialTokenSource{ | ||
Token: "my_token", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range tcs { | ||
t.Run(name, func(t *testing.T) { | ||
got := loadCredentialFile(tc.config) | ||
if cmp.Diff(tc.want, got) != "" { | ||
t.Errorf("mismatch (-want +got): %s", cmp.Diff(tc.want, got)) | ||
} | ||
}) | ||
} | ||
|
||
} |
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,74 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||
|
||
"github.com/hashicorp/terraform-provider-hcp/internal/clients" | ||
) | ||
|
||
func Test_readWorkloadIdentity(t *testing.T) { | ||
tcs := map[string]struct { | ||
model WorkloadIdentityFrameworkModel | ||
want clients.ClientConfig | ||
wantDiags diag.Diagnostics | ||
}{ | ||
"missing token and file": { | ||
model: WorkloadIdentityFrameworkModel{ | ||
ResourceName: basetypes.NewStringValue("my_resource"), | ||
}, | ||
wantDiags: diag.Diagnostics{ | ||
diag.NewErrorDiagnostic("invalid workload_identity", "at least one of `token_file` or `token` must be set"), | ||
}, | ||
want: clients.ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
}, | ||
}, | ||
"token": { | ||
model: WorkloadIdentityFrameworkModel{ | ||
ResourceName: basetypes.NewStringValue("my_resource"), | ||
Token: basetypes.NewStringValue("my_token"), | ||
}, | ||
want: clients.ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
WorkloadIdentityToken: "my_token", | ||
}, | ||
}, | ||
"file": { | ||
model: WorkloadIdentityFrameworkModel{ | ||
ResourceName: basetypes.NewStringValue("my_resource"), | ||
TokenFile: basetypes.NewStringValue("/path/to/token/file"), | ||
}, | ||
want: clients.ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
WorkloadIdentityTokenFile: "/path/to/token/file", | ||
}, | ||
}, | ||
"both": { | ||
model: WorkloadIdentityFrameworkModel{ | ||
ResourceName: basetypes.NewStringValue("my_resource"), | ||
Token: basetypes.NewStringValue("my_token"), | ||
TokenFile: basetypes.NewStringValue("/path/to/token/file"), | ||
}, | ||
want: clients.ClientConfig{ | ||
WorkloadIdentityResourceName: "my_resource", | ||
WorkloadIdentityToken: "my_token", | ||
WorkloadIdentityTokenFile: "/path/to/token/file", | ||
}, | ||
}, | ||
} | ||
for name, tc := range tcs { | ||
t.Run(name, func(t *testing.T) { | ||
got, gotDiags := readWorkloadIdentity(tc.model, clients.ClientConfig{}) | ||
if diff := cmp.Diff(tc.want, got); diff != "" { | ||
t.Errorf("unexpected result (-want +got):\n%s", diff) | ||
} | ||
if diff := cmp.Diff(tc.wantDiags, gotDiags); diff != "" { | ||
t.Errorf("unexpected diagnostics (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
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.