-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move acceptance test helpers to their own file
Signed-off-by: Dimitrios Karagiannis <[email protected]>
- Loading branch information
Showing
2 changed files
with
89 additions
and
83 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,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 | ||
} |
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