-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the SSH Key test and the Snapshot test
Signed-off-by: Alejandro JNM <[email protected]>
- Loading branch information
1 parent
a3077b4
commit 61987e1
Showing
2 changed files
with
319 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
// example.Widget represents a concrete Go type that represents an API resource | ||
func TestAccCivoSnapshot_basic(t *testing.T) { | ||
var snapshot civogo.Snapshot | ||
|
||
// generate a random name for each test run | ||
resName := "civo_snapshot.foobar" | ||
var snapshotName = acctest.RandomWithPrefix("tf-test") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCivoSnapshotDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// use a dynamic configuration with the random name from above | ||
Config: testAccCheckCivoSnapshotConfigBasic(snapshotName), | ||
// compose a basic test, checking both remote and local values | ||
Check: resource.ComposeTestCheckFunc( | ||
// query the API to retrieve the widget object | ||
testAccCheckCivoSnapshotResourceExists(resName, &snapshot), | ||
// verify remote values | ||
testAccCheckCivoSnapshotValues(&snapshot, snapshotName), | ||
// verify local values | ||
resource.TestCheckResourceAttr(resName, "name", snapshotName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccCivoSnapshot_update(t *testing.T) { | ||
var snapshot civogo.Snapshot | ||
|
||
// generate a random name for each test run | ||
resName := "civo_snapshot.foobar" | ||
var snapshotName = acctest.RandomWithPrefix("tf-test") | ||
var snapshotNameUpdate = acctest.RandomWithPrefix("rename-tf-test") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCivoSnapshotDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckCivoSnapshotConfigBasic(snapshotName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCivoSnapshotResourceExists(resName, &snapshot), | ||
testAccCheckCivoSnapshotValues(&snapshot, snapshotName), | ||
resource.TestCheckResourceAttr(resName, "name", snapshotName), | ||
), | ||
}, | ||
{ | ||
// use a dynamic configuration with the random name from above | ||
Config: testAccCheckCivoSnapshotConfigUpdates(snapshotNameUpdate), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCivoSnapshotResourceExists(resName, &snapshot), | ||
testAccCheckCivoSnapshotUpdated(&snapshot, snapshotNameUpdate), | ||
resource.TestCheckResourceAttr(resName, "name", snapshotNameUpdate), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCivoSnapshotValues(snapshot *civogo.Snapshot, name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if snapshot.Name != name { | ||
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, snapshot.Name) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// testAccCheckExampleResourceExists queries the API and retrieves the matching Widget. | ||
func testAccCheckCivoSnapshotResourceExists(n string, snapshot *civogo.Snapshot) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// find the corresponding state object | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
// retrieve the configured client from the test setup | ||
client := testAccProvider.Meta().(*civogo.Client) | ||
resp, err := client.FindSnapshot(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Snapshot not found: (%s) %s", rs.Primary.ID, err) | ||
} | ||
|
||
// If no error, assign the response Widget attribute to the widget pointer | ||
*snapshot = *resp | ||
|
||
// return fmt.Errorf("Domain (%s) not found", rs.Primary.ID) | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckCivoSnapshotUpdated(snapshot *civogo.Snapshot, name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if snapshot.Name != name { | ||
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, snapshot.Name) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckCivoSnapshotDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*civogo.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "civo_snapshot" { | ||
continue | ||
} | ||
|
||
_, err := client.FindSnapshot(rs.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("Snapshot still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckCivoSnapshotConfigBasic(name string) string { | ||
return fmt.Sprintf(` | ||
resource "civo_instance" "vm" { | ||
hostname = "instance-%s" | ||
} | ||
resource "civo_snapshot" "foobar" { | ||
name = "%s" | ||
instance_id = civo_instance.vm.id | ||
}`, name, name) | ||
} | ||
|
||
func testAccCheckCivoSnapshotConfigUpdates(name string) string { | ||
return fmt.Sprintf(` | ||
resource "civo_instance" "vm" { | ||
hostname = "instance-%s" | ||
} | ||
resource "civo_snapshot" "foobar" { | ||
name = "%s" | ||
instance_id = civo_instance.vm.id | ||
}`, name, name) | ||
} |
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,161 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
// example.Widget represents a concrete Go type that represents an API resource | ||
func TestAccCivoSSHKey_basic(t *testing.T) { | ||
var SSHKey civogo.SSHKey | ||
|
||
// generate a random name for each test run | ||
resName := "civo_ssh_key.foobar" | ||
var SSHKeyName = acctest.RandomWithPrefix("tf-test") | ||
publicKeyMaterial, _, err := acctest.RandSSHKeyPair("civo@ssh-acceptance-test") | ||
if err != nil { | ||
t.Fatalf("Cannot generate test SSH key pair: %s", err) | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCivoSSHKeyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// use a dynamic configuration with the random name from above | ||
Config: testAccCheckCivoSSHKeyConfigBasic(SSHKeyName, publicKeyMaterial), | ||
// compose a basic test, checking both remote and local values | ||
Check: resource.ComposeTestCheckFunc( | ||
// query the API to retrieve the widget object | ||
testAccCheckCivoSSHKeyResourceExists(resName, &SSHKey), | ||
// verify remote values | ||
testAccCheckCivoSSHKeyValues(&SSHKey, SSHKeyName), | ||
// verify local values | ||
resource.TestCheckResourceAttr(resName, "name", SSHKeyName), | ||
resource.TestCheckResourceAttr(resName, "public_key", publicKeyMaterial), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccCivoSSHKey_update(t *testing.T) { | ||
var SSHKey civogo.SSHKey | ||
|
||
// generate a random name for each test run | ||
resName := "civo_ssh_key.foobar" | ||
var SSHKeyName = acctest.RandomWithPrefix("tf-test") | ||
var SSHKeyNameUpdate = acctest.RandomWithPrefix("rename-tf-test") | ||
publicKeyMaterial, _, err := acctest.RandSSHKeyPair("civo@ssh-acceptance-test") | ||
if err != nil { | ||
t.Fatalf("Cannot generate test SSH key pair: %s", err) | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCivoSSHKeyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckCivoSSHKeyConfigBasic(SSHKeyName, publicKeyMaterial), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCivoSSHKeyResourceExists(resName, &SSHKey), | ||
testAccCheckCivoSSHKeyValues(&SSHKey, SSHKeyName), | ||
resource.TestCheckResourceAttr(resName, "name", SSHKeyName), | ||
resource.TestCheckResourceAttr(resName, "public_key", publicKeyMaterial), | ||
), | ||
}, | ||
{ | ||
// use a dynamic configuration with the random name from above | ||
Config: testAccCheckCivoSSHKeyConfigUpdates(SSHKeyNameUpdate, publicKeyMaterial), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCivoSSHKeyResourceExists(resName, &SSHKey), | ||
testAccCheckCivoSSHKeyUpdated(&SSHKey, SSHKeyNameUpdate), | ||
resource.TestCheckResourceAttr(resName, "name", SSHKeyNameUpdate), | ||
resource.TestCheckResourceAttr(resName, "public_key", publicKeyMaterial), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCivoSSHKeyValues(SSHKey *civogo.SSHKey, name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if SSHKey.Name != name { | ||
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, SSHKey.Name) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// testAccCheckExampleResourceExists queries the API and retrieves the matching Widget. | ||
func testAccCheckCivoSSHKeyResourceExists(n string, SSHKey *civogo.SSHKey) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// find the corresponding state object | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
// retrieve the configured client from the test setup | ||
client := testAccProvider.Meta().(*civogo.Client) | ||
resp, err := client.FindSSHKey(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Ssh key not found: (%s) %s", rs.Primary.ID, err) | ||
} | ||
|
||
// If no error, assign the response Widget attribute to the widget pointer | ||
*SSHKey = *resp | ||
|
||
// return fmt.Errorf("Domain (%s) not found", rs.Primary.ID) | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckCivoSSHKeyUpdated(SSHKey *civogo.SSHKey, name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if SSHKey.Name != name { | ||
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, SSHKey.Name) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckCivoSSHKeyDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*civogo.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "civo_ssh_key" { | ||
continue | ||
} | ||
|
||
_, err := client.FindSSHKey(rs.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("Ssh key still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckCivoSSHKeyConfigBasic(name string, key string) string { | ||
return fmt.Sprintf(` | ||
resource "civo_ssh_key" "foobar" { | ||
name = "%s" | ||
public_key = "%s" | ||
}`, name, key) | ||
} | ||
|
||
func testAccCheckCivoSSHKeyConfigUpdates(name string, key string) string { | ||
return fmt.Sprintf(` | ||
resource "civo_ssh_key" "foobar" { | ||
name = "%s" | ||
public_key = "%s" | ||
}`, name, key) | ||
} |