Skip to content

Commit

Permalink
Move acceptance test helpers to their own file
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Jan 28, 2020
1 parent df81c64 commit 4b1bef8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 83 deletions.
89 changes: 89 additions & 0 deletions megaport/acctest_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package megaport

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)

var (
testAccConfigTemplates = &template.Template{}
)

func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
r := make(map[string]interface{}, len(a))
for k, v := range a {
r[k] = v
}
for k, v := range b {
r[k] = v
}
return r
}

func testAccNewConfig(name string) (*template.Template, error) {
config := ""
if err := filepath.Walk(filepath.Join("../examples/", name), func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
r, err := filepath.Match("*.tf", f.Name())
if err != nil {
return err
}
if r {
c, err := ioutil.ReadFile(path)
if err != nil {
return err
}
config = config + string(c)
}
return nil
}); err != nil {
return nil, err
}
t, err := testAccConfigTemplates.New(name).Parse(config)
if err != nil {
return nil, err
}
return t, nil
}

type testAccConfig struct {
Config string
Name string
Step int
}

func (c testAccConfig) log() {
l := strings.Split(c.Config, "\n")
for i := range l {
l[i] = " " + l[i]
}
fmt.Printf("+++ CONFIG %q (step %d):\n%s\n", c.Name, c.Step, strings.Join(l, "\n"))
}

func newTestAccConfig(name string, values map[string]interface{}, step int) (*testAccConfig, error) {
var (
t *template.Template
err error
cfg = &strings.Builder{}
)
t = testAccConfigTemplates.Lookup(name)
if t == nil {
t, err = testAccNewConfig(name)
if err != nil {
return nil, err
}
}
if err := t.Execute(cfg, values); err != nil {
return nil, err
}
return &testAccConfig{Config: cfg.String(), Name: name, Step: step}, nil
}
83 changes: 0 additions & 83 deletions megaport/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ package megaport

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"text/template"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/utilitywarehouse/terraform-provider-megaport/megaport/api"
)

var (
testAccConfigTemplates = &template.Template{}
)

func TestValidateAWSBGPAuthKey(t *testing.T) {
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012346789"
short := acctest.RandStringFromCharSet(5, charset)
Expand All @@ -43,17 +35,6 @@ func TestValidateAWSBGPAuthKey(t *testing.T) {
}
}

func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
r := make(map[string]interface{}, len(a))
for k, v := range a {
r[k] = v
}
for k, v := range b {
r[k] = v
}
return r
}

func testAccCheckResourceExists(n string, o interface{}) resource.TestCheckFunc {
return func(s *terraform.State) error {
cfg := testAccProvider.Meta().(*Config)
Expand Down Expand Up @@ -112,67 +93,3 @@ func testAccCheckResourceDestroy(s *terraform.State) error {
}
return nil
}

func testAccNewConfig(name string) (*template.Template, error) {
config := ""
if err := filepath.Walk(filepath.Join("../examples/", name), func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
r, err := filepath.Match("*.tf", f.Name())
if err != nil {
return err
}
if r {
c, err := ioutil.ReadFile(path)
if err != nil {
return err
}
config = config + string(c)
}
return nil
}); err != nil {
return nil, err
}
t, err := testAccConfigTemplates.New(name).Parse(config)
if err != nil {
return nil, err
}
return t, nil
}

type testAccConfig struct {
Config string
Name string
Step int
}

func (c testAccConfig) log() {
l := strings.Split(c.Config, "\n")
for i := range l {
l[i] = " " + l[i]
}
fmt.Printf("+++ CONFIG %q (step %d):\n%s\n", c.Name, c.Step, strings.Join(l, "\n"))
}

func newTestAccConfig(name string, values map[string]interface{}, step int) (*testAccConfig, error) {
var (
t *template.Template
err error
cfg = &strings.Builder{}
)
t = testAccConfigTemplates.Lookup(name)
if t == nil {
t, err = testAccNewConfig(name)
if err != nil {
return nil, err
}
}
if err := t.Execute(cfg, values); err != nil {
return nil, err
}
return &testAccConfig{Config: cfg.String(), Name: name, Step: step}, nil
}

0 comments on commit 4b1bef8

Please sign in to comment.