Skip to content

Commit

Permalink
Merge pull request #117 from go-gandi/mutable-acceptance-test
Browse files Browse the repository at this point in the history
Mutable acceptance test
  • Loading branch information
nlewo authored May 30, 2022
2 parents 7b1fd7c + 5fce70c commit a1c842f
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 313 deletions.
101 changes: 101 additions & 0 deletions gandi/resource_livedns_record_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package gandi

import (
"fmt"
"os"
"reflect"
"sort"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"

"github.com/go-gandi/go-gandi"
"github.com/go-gandi/go-gandi/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccRecord_basic(t *testing.T) {
Expand Down Expand Up @@ -208,3 +212,100 @@ func TestKeepRecordsInApiAndTF(t *testing.T) {
}
})
}

func testAccConfigMutableRecord() string {
return `
resource "gandi_livedns_record" "terraform_provider_gandi_com" {
zone = "terraform-provider-gandi.com"
name = "mutable"
mutable = true
type = "TXT"
ttl = 3600
values = ["terraform-1"]
}
`
}

func updateRecord(values []string) {
config := config.Config{
APIURL: os.Getenv("GANDI_URL"),
APIKey: os.Getenv("GANDI_KEY"),
Debug: logging.IsDebugOrHigher(),
}
liveDNS := gandi.NewLiveDNSClient(config)
_, err := liveDNS.UpdateDomainRecordByNameAndType(
"terraform-provider-gandi.com",
"mutable",
"TXT",
3600,
values)
if err != nil {
return
}
}

func checkRecordValuesOnAPI(state *terraform.State, expected []string) error {
config := config.Config{
APIURL: os.Getenv("GANDI_URL"),
APIKey: os.Getenv("GANDI_KEY"),
Debug: logging.IsDebugOrHigher(),
}
liveDNS := gandi.NewLiveDNSClient(config)
rec, err := liveDNS.GetDomainRecordByNameAndType(
"terraform-provider-gandi.com",
"mutable",
"TXT")
if err != nil {
return err
}
sort.Strings(rec.RrsetValues)
sort.Strings(expected)
if !reflect.DeepEqual(rec.RrsetValues, expected) {
return fmt.Errorf("TXT record values on the API are not the expected ones")
}
return nil
}

// TestAccRecord_mutable tests the mutable attribute on record ressource.
// - we create a mutable record with Terraform (this record can already exist on the API)
// - we add a value to this record with the API
// - we reapply Terraform and it should ignore this new value (this value is not removed as it would be if mutable = false)
// - the record is deleted from the Terraform state and the manually added value should still exist on the API
func TestAccRecord_mutable(t *testing.T) {
resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: func(state *terraform.State) error {
return checkRecordValuesOnAPI(
state,
[]string{"\"manual-1\""})
},
Steps: []resource.TestStep{
{
Config: testAccConfigMutableRecord(),
},
{
// The record is updated: a values is
// added and Terraform should ignore
// it.
PreConfig: func() {
updateRecord([]string{"terraform-1", "manual-1"})
},
Config: testAccConfigMutableRecord(),
Check: resource.ComposeAggregateTestCheckFunc(
// The terraform state should only contains a single value
resource.TestCheckResourceAttr(
"gandi_livedns_record.terraform_provider_gandi_com",
"values.0",
"terraform-1"),
// The API should contains two values
func(state *terraform.State) error {
return checkRecordValuesOnAPI(
state,
[]string{"\"terraform-1\"", "\"manual-1\""})
}),
},
},
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
github.com/fatih/color v1.9.0 // indirect
github.com/go-gandi/go-gandi v0.4.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
github.com/hashicorp/yamux v0.0.0-20190923154419-df201c70410d // indirect
github.com/oklog/run v1.1.0 // indirect
)
Loading

0 comments on commit a1c842f

Please sign in to comment.