Skip to content
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

chore: add tests for sshkey (resource + data) #94

Merged
merged 11 commits into from
Jun 2, 2021
67 changes: 67 additions & 0 deletions env0/data_sshkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package env0

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

func TestUnitSshKeyDataSourceById(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really feels like we can DRY those two test functions. I think it's worth an effort 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resourceType := "env0_ssh_key"
resourceName := "test"
resourceFullName := dataSourceAccessor(resourceType, resourceName)
sshKey := client.SshKey{
Id: "id0",
Name: "name0",
Value: "Key🔑",
}

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

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey}, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to be more specific with .Times(1)?
This is also relevant for all other tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20f7ef7
worked only for resource tests file. data must have AnyTimes from some reason

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell maybe how many times it runs? (you can check out using debugger).
I'm asking because it's important to understand this tho.If it's only two - we can add a setting that prevents the second time from running so we'd be able to assert the number of calls easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do you see it on the debugger? what line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤙

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})
}

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

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

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey}, nil)
})
}
41 changes: 41 additions & 0 deletions env0/resource_sshkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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{
ProviderFactories: testUnitProviders,
yaronya marked this conversation as resolved.
Show resolved Hide resolved
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@razbensimon So the decrypted ssh key exists on the returned object? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the resource, not the data tests

Copy link
Contributor

@yaronya yaronya Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but still, I get that it should be a part of the SshKeyCreatePayload but I'm not sure it should be a part of the ssh key schema because nobody uses it and it's a secret value. WDYT? (shouldn't be on this PR btw)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put sensitive for now, and lets talk with the team

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

),
},
},
}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@razbensimon You got a redundant Return() call here 🙊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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