/
acc.go
106 lines (93 loc) · 3.11 KB
/
acc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package atf
import (
"fmt"
"net/http"
"os"
"strings"
"testing"
pkgutils "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
// GetAPIFunc accepts terraform states attribures as params and
// expects response and error as return values
type GetAPIFunc func(attr map[string]string) (interface{}, error)
type Acc struct {
PreCheck func(t *testing.T)
Providers map[string]*schema.Provider
GetAPI GetAPIFunc
ResourceName string
Version string
}
// RunResourcePlanTest to run resource plan only test case. This will take first
// config from specific resource
func (a *Acc) RunResourcePlanTest(t *testing.T) {
checkSkip(t)
a.runPlanTest(t, true)
}
// RunDataSourceTests to run data source plan only test case. This will take first
// config from specific data source
func (a *Acc) RunDataSourceTests(t *testing.T) {
r := newReader(t, false, a.ResourceName)
checkSkip(t)
testSteps := r.getTestCases(a.Version, a.GetAPI)
resource.ParallelTest(t, resource.TestCase{
IsUnitTest: false,
PreCheck: func() { a.PreCheck(t) },
Providers: a.Providers,
Steps: testSteps,
})
}
// RunResourceTests creates test cases and run tests which includes create/update/delete/read
func (a *Acc) RunResourceTests(t *testing.T) {
checkSkip(t)
// populate test cases
r := newReader(t, true, a.ResourceName)
testSteps := r.getTestCases(a.Version, a.GetAPI)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { a.PreCheck(t) },
Providers: a.Providers,
CheckDestroy: resource.ComposeTestCheckFunc(
a.checkResourceDestroy,
),
Steps: testSteps,
})
}
// checkResourceDestroy checks resource destroy conditions. This function will parse error
// and check status code is 404 or not
func (a *Acc) checkResourceDestroy(s *terraform.State) error {
rs, ok := s.RootModule().Resources[fmt.Sprintf("%s.tf_%s", a.ResourceName, getLocalName(a.ResourceName))]
if !ok {
return fmt.Errorf("[Check Destroy] resource %s not found", a.ResourceName)
}
_, err := a.GetAPI(rs.Primary.Attributes)
statusCode := pkgutils.GetStatusCode(err)
if statusCode != http.StatusNotFound {
return fmt.Errorf("expected %d statuscode, but got %d", 404, statusCode)
}
return nil
}
// runs plan test for resource or data source. only first config from test case
// will considered on plan test
func (a *Acc) runPlanTest(t *testing.T, isResource bool) {
r := newReader(t, isResource, a.ResourceName)
testSteps := r.getTestCases(a.Version, a.GetAPI)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { a.PreCheck(t) },
Providers: a.Providers,
Steps: []resource.TestStep{
{
Config: testSteps[0].Config,
PlanOnly: true,
ExpectNonEmptyPlan: true,
Check: testSteps[0].Check,
},
},
})
}
func checkSkip(t *testing.T) {
if strings.ToLower(os.Getenv("TF_ACC")) != "true" && os.Getenv("TF_ACC") != "1" {
t.Skip("acceptance test is skipped since TF_ACC is not set")
}
}