Skip to content

Commit

Permalink
Added the first test to the project
Browse files Browse the repository at this point in the history
Add the the test of dns domain name and dns domain record
Fix the version of Go in the Readme
  • Loading branch information
alejandrojnm committed Jun 15, 2020
1 parent 2127d82 commit 400524a
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Terraform Provider ![Travis build](https://travis-ci.org/civo/terraform-provider
Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
- [Terraform](https://www.terraform.io/downloads.html) 0.13.x
- [Go](https://golang.org/doc/install) 1.13 (to build the provider plugin)

Building The Provider
Expand Down
2 changes: 1 addition & 1 deletion civo/datasource_dns_domain_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// dataSourceDnsDomainName data source to get from the api a specific domain
// using the id or the name of the domain
func dataSourceDNSDomainNamego() *schema.Resource {
func dataSourceDNSDomainName() *schema.Resource {
return &schema.Resource{
Read: dataSourceDNSDomainNameRead,
Schema: map[string]*schema.Schema{
Expand Down
39 changes: 39 additions & 0 deletions civo/datasource_dns_domain_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package civo

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceCivoDnsDomainName(t *testing.T) {
datasourceName := "data.civo_dns_domain_name.domain"
domain := acctest.RandomWithPrefix("domian") + ".com"

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

func testAccDataSourceCivoDNSDomainName(domain string) string {
return fmt.Sprintf(`
resource "civo_dns_domain_name" "domain" {
name = "%[1]s"
}
data "civo_dns_domain_name" "domain" {
name = civo_dns_domain_name.domain.name
}
`, domain)
}
53 changes: 53 additions & 0 deletions civo/datasource_dns_domain_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package civo

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceCivoDNSDomainRecord_basic(t *testing.T) {
datasourceName := "data.civo_dns_domain_record.record"
domain := acctest.RandomWithPrefix("recordtest") + ".com"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoDNSDomainRecordConfigBasic(domain),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", "www"),
resource.TestCheckResourceAttr(datasourceName, "type", "a"),
resource.TestCheckResourceAttr(datasourceName, "ttl", "600"),
resource.TestCheckResourceAttr(datasourceName, "value", "192.168.1.1"),
resource.TestCheckResourceAttrSet(datasourceName, "id"),
resource.TestCheckResourceAttrSet(datasourceName, "domain_id"),
),
},
},
})
}

func testAccDataSourceCivoDNSDomainRecordConfigBasic(domain string) string {
return fmt.Sprintf(`
resource "civo_dns_domain_name" "domain" {
name = "%[1]s"
}
resource "civo_dns_domain_record" "record" {
domain_id = civo_dns_domain_name.domain.id
type = "a"
name = "www"
value = "192.168.1.1"
ttl = 600
}
data "civo_dns_domain_record" "record" {
domain_id = civo_dns_domain_name.domain.id
name = civo_dns_domain_record.record.name
}
`, domain)
}
2 changes: 1 addition & 1 deletion civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Provider() terraform.ResourceProvider {
"civo_instances_size": dataSourceInstancesSize(),
"civo_instances": dataSourceInstances(),
"civo_instance": dataSourceInstance(),
"civo_dns_domain_name": dataSourceDNSDomainNamego(),
"civo_dns_domain_name": dataSourceDNSDomainName(),
"civo_dns_domain_record": dataSourceDNSDomainRecord(),
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
Expand Down
31 changes: 31 additions & 0 deletions civo/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package civo

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"civo": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CIVO_TOKEN"); v == "" {
t.Fatal("CIVO_TOKEN must be set for acceptance tests")
}
}
147 changes: 147 additions & 0 deletions civo/resource_dns_domain_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
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 TestAccCivoDNSDomainName_basic(t *testing.T) {
var domain civogo.DNSDomain

// generate a random name for each test run
resName := "civo_dns_domain_name.foobar"
var domainName = acctest.RandomWithPrefix("tf-test") + ".example"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCivoDNSDomainNameDestroy,
Steps: []resource.TestStep{
{
// use a dynamic configuration with the random name from above
Config: testAccCheckCivoDNSDomainNameConfigBasic(domainName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the widget object
testAccCheckCivoDNSDomainNameResourceExists(resName, &domain),
// verify remote values
testAccCheckCivoDNSDomainNameValues(&domain, domainName),
// verify local values
resource.TestCheckResourceAttr(resName, "name", domainName),
),
},
},
})
}

func TestAccCivoDNSDomainName_update(t *testing.T) {
var domain civogo.DNSDomain

// generate a random name for each test run
resName := "civo_dns_domain_name.foobar"
var domainName = acctest.RandomWithPrefix("renamed-tf-test") + ".example"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCivoDNSDomainNameDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCivoDNSDomainNameConfigUpdates(domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCivoDNSDomainNameResourceExists(resName, &domain),
testAccCheckCivoDNSDomainNameValues(&domain, domainName),
resource.TestCheckResourceAttr(resName, "name", domainName),
),
},
{
// use a dynamic configuration with the random name from above
Config: testAccCheckCivoDNSDomainNameConfigUpdates(domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCivoDNSDomainNameResourceExists(resName, &domain),
testAccCheckCivoDNSDomainNameUpdated(&domain, domainName),
resource.TestCheckResourceAttr(resName, "name", domainName),
),
},
},
})
}

func testAccCheckCivoDNSDomainNameValues(domain *civogo.DNSDomain, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if domain.Name != name {
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, domain.Name)
}
return nil
}
}

// testAccCheckExampleResourceExists queries the API and retrieves the matching Widget.
func testAccCheckCivoDNSDomainNameResourceExists(n string, domain *civogo.DNSDomain) 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.FindDNSDomain(rs.Primary.ID)
if err != nil {
return fmt.Errorf("Domain not found: (%s) %s", rs.Primary.ID, err)
}

// If no error, assign the response Widget attribute to the widget pointer
*domain = *resp

// return fmt.Errorf("Domain (%s) not found", rs.Primary.ID)
return nil
}
}

func testAccCheckCivoDNSDomainNameUpdated(domain *civogo.DNSDomain, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if domain.Name != name {
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, domain.Name)
}
return nil
}
}

func testAccCheckCivoDNSDomainNameDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*civogo.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "civo_dns_domain_name" {
continue
}

_, err := client.FindDNSDomain(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Domain still exists")
}
}

return nil
}

func testAccCheckCivoDNSDomainNameConfigBasic(domain string) string {
return fmt.Sprintf(`
resource "civo_dns_domain_name" "foobar" {
name = "%s"
}`, domain)
}

func testAccCheckCivoDNSDomainNameConfigUpdates(domain string) string {
return fmt.Sprintf(`
resource "civo_dns_domain_name" "foobar" {
name = "%s"
}`, domain)
}

0 comments on commit 400524a

Please sign in to comment.