Skip to content

Commit

Permalink
Added new test to the provider
Browse files Browse the repository at this point in the history
Added the ssh key test
Fixed the go mod

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jul 3, 2020
1 parent 91509d2 commit 2601717
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions civo/datasource_ssh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package civo

import (
"crypto/rand"
"crypto/rsa"
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"golang.org/x/crypto/ssh"
)

func TestAccDataSourceCivoSSHKey_basic(t *testing.T) {
datasourceName := "data.civo_ssh_key.foobar"
name := acctest.RandomWithPrefix("sshkey-test")
pubKey, err := testAccGenerateDataSourceCivoSSHKeyPublic()
if err != nil {
t.Fatalf("Unable to generate public key: %v", err)
return
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoSSHKeyConfig(name, pubKey),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
),
},
},
})
}

func testAccGenerateDataSourceCivoSSHKeyPublic() (string, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return "", fmt.Errorf("Unable to generate key: %v", err)
}

publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return "", fmt.Errorf("Unable to generate key: %v", err)
}

return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(publicKey))), nil
}

func testAccDataSourceCivoSSHKeyConfig(name string, key string) string {
return fmt.Sprintf(`
resource "civo_ssh_key" "foobar" {
name = "%s"
public_key = "%s"
}
data "civo_ssh_key" "foobar" {
name = civo_ssh_key.foobar.name
}
`, name, key)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/ulikunitz/xz v0.5.7 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/zclconf/go-cty v1.3.1 // indirect
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 // indirect
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4
golang.org/x/tools v0.0.0-20200623045635-ff88973b1e4e // indirect
google.golang.org/genproto v0.0.0-20200311144346-b662892dd51b // indirect
google.golang.org/grpc v1.28.0 // indirect
Expand Down

0 comments on commit 2601717

Please sign in to comment.