Skip to content

Commit

Permalink
add tests for sshkey resource
Browse files Browse the repository at this point in the history
  • Loading branch information
razbensimon committed Jun 2, 2021
1 parent 9b30b96 commit 92d1827
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions env0/resource_sshkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package env0

import (
"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)

func TestUnitSshKeyResource(t *testing.T) {
resourceType := "env0_ssh_key"
resourceName := "test"
resourceFullName := resourceAccessor(resourceType, resourceName)
sshKey := client.SshKey{
Id: "id0",
Name: "name0",
Value: "Key🔑",
}

create := resourceConfigCreate(resourceType, resourceName, map[string]string{
"name": sshKey.Name,
"value": sshKey.Value,
})
testCase := resource.TestCase{
ProviderFactories: testUnitProviders,
Steps: []resource.TestStep{
{
Config: create,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "id", sshKey.Id),
resource.TestCheckResourceAttr(resourceFullName, "name", sshKey.Name),
resource.TestCheckResourceAttr(resourceFullName, "value", sshKey.Value),
),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().SshKeyCreate(client.SshKeyCreatePayload{Name: sshKey.Name, Value: sshKey.Value}).Times(1).Return(sshKey, nil).Return(sshKey, nil)
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey}, nil)
mock.EXPECT().SshKeyDelete(sshKey.Id).Times(1).Return(nil)
})
}
2 changes: 1 addition & 1 deletion env0/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func resourceConfigCreate(resourceType string, resourceName string, fields map[s
func hclConfigCreate(source TFSource, resourceType string, resourceName string, fields map[string]string) string {
hclFields := ""
for key, value := range fields {
hclFields += fmt.Sprintf("\n\t\"%s\" = \"%s\"", key, value)
hclFields += fmt.Sprintf("\n\t%s = \"%s\"", key, value)
}
if hclFields != "" {
hclFields += "\n"
Expand Down

0 comments on commit 92d1827

Please sign in to comment.