-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for sshkey (resource + data) (#94)
* add tests for sshkey resource * add tests for sshkey data source * add sensitive for sshkey resource * fix quotes bug in helper tests
- Loading branch information
1 parent
c95fbb4
commit 4be2889
Showing
4 changed files
with
92 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package env0 | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestUnitSshKeyDataSourceById(t *testing.T) { | ||
testUnitSshKeyDataSource(t, "id") | ||
} | ||
|
||
func TestUnitSshKeyDataSourceByName(t *testing.T) { | ||
testUnitSshKeyDataSource(t, "name") | ||
} | ||
|
||
func testUnitSshKeyDataSource(t *testing.T, byKey string) { | ||
resourceType := "env0_ssh_key" | ||
resourceName := "test" | ||
resourceFullName := dataSourceAccessor(resourceType, resourceName) | ||
sshKey := client.SshKey{ | ||
Id: "id0", | ||
Name: "name0", | ||
Value: "Key🔑", | ||
} | ||
|
||
sshKeyAsJson, _ := json.Marshal(sshKey) | ||
var jsonData map[string]string | ||
_ = json.Unmarshal(sshKeyAsJson, &jsonData) | ||
|
||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]string{ | ||
byKey: jsonData[byKey], | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceFullName, "id", sshKey.Id), | ||
resource.TestCheckResourceAttr(resourceFullName, "name", sshKey.Name), | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
// TODO: AnyTimes because we find that READ runs for 5 times. need investigation. | ||
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey}, nil) | ||
}) | ||
} |
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,40 @@ | ||
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🔑", | ||
} | ||
|
||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]string{ | ||
"name": sshKey.Name, | ||
"value": sshKey.Value, | ||
}), | ||
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) | ||
mock.EXPECT().SshKeys().Times(1).Return([]client.SshKey{sshKey}, nil) | ||
mock.EXPECT().SshKeyDelete(sshKey.Id).Times(1).Return(nil) | ||
}) | ||
} |
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