-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from 4 commits
c71a999
429fb0a
151b192
e5c6cfc
fa38ab5
20f7ef7
a1a0f56
414a3cd
a3bca40
7887c41
abf5877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try to be more specific with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 20f7ef7 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where do you see it on the debugger? what line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤙 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} |
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @razbensimon So the decrypted ssh key exists on the returned object? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put sensitive for now, and lets talk with the team There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @razbensimon You got a redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} |
There was a problem hiding this comment.
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 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a1a0f56
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.