-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_exec_test.go
125 lines (115 loc) · 2.92 KB
/
resource_exec_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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"exec": testAccProvider,
}
}
func TestResourceExecCreate(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccResourceExecDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceExecConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("exec.foo", "output", "success\n"),
),
},
},
})
}
func TestResourceExecUpdate(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccResourceExecDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceExecConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("exec.foo", "output", "success\n"),
),
},
resource.TestStep{
Config: testAccResourceExecConfig_basic_2,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("exec.foo", "output", "success2\n"),
),
},
},
})
}
func TestResourceExecCreateTestFail(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccResourceExecDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceExecConfig_test_fail,
Check: resource.ComposeTestCheckFunc(
testAccCheckExecResourceIsNil("exec.foo"),
),
},
resource.TestStep{
Config: testAccResourceExecConfig_test_pass,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("exec.foo", "output", "success\n"),
),
},
},
})
}
func testAccCheckExecResourceIsNil(r string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[r]
if ok {
return fmt.Errorf("Resource exists: %s", r)
}
return nil
}
}
func testAccResourceExecDestroy(s *terraform.State) error {
return nil
}
const testAccResourceExecConfig_basic = `
resource "exec" "foo" {
command = "echo 'success'"
}
`
const testAccResourceExecConfig_basic_2 = `
resource "exec" "foo" {
command = "echo 'success2'"
}
`
const testAccResourceExecConfig_test_pass = `
resource "exec" "foo" {
command = "echo 'success'"
only_if = "true"
}
`
const testAccResourceExecConfig_test_fail = `
resource "exec" "foo" {
command = "echo 'success'"
only_if = "false"
}
`
const testAccResourceExecConfig_timeout = `
resource "exec" "foo" {
command = "sleep 2 && echo 'success'"
timeout = 1
}
`
const testAccResourceExecConfig_fail = `
resource "exec" "foo" {
command = "echo 'failure' >&2 && exit 1"
}
`