-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_cf_stack_test.go
76 lines (62 loc) · 1.77 KB
/
data_source_cf_stack_test.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
package cloudfoundry
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
"fmt"
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
const stackDataResource = `
data "cloudfoundry_stack" "s" {
name = "%s"
}
`
func TestAccDataSourceStack_normal(t *testing.T) {
defaultStacks, _, err := testSession().ClientV2.GetStacks()
if err != nil {
panic(err)
}
ref := "data.cloudfoundry_stack.s"
resource.ParallelTest(t,
resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(stackDataResource, defaultStacks[0].Name),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStackExists(ref),
resource.TestCheckResourceAttr(
ref, "name", defaultStacks[0].Name),
),
},
},
})
}
func checkDataSourceStackExists(resource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
session := testAccProvider.Meta().(*managers.Session)
rs, ok := s.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("stack '%s' not found in terraform state", resource)
}
id := rs.Primary.ID
name := rs.Primary.Attributes["name"]
description := rs.Primary.Attributes["description"]
stacks, _, err := session.ClientV2.GetStacks(ccv2.FilterByName(name))
if err != nil {
return err
}
if len(stacks) == 0 {
return NotFound
}
if err := assertSame(id, stacks[0].GUID); err != nil {
return err
}
if err := assertSame(description, stacks[0].Description); err != nil {
return err
}
return nil
}
}