forked from 22Acacia/terraform-provider-googlecli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_dataflow_test.go
134 lines (114 loc) · 3.46 KB
/
resource_dataflow_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
126
127
128
129
130
131
132
133
134
package main
import (
"os"
"fmt"
"time"
"testing"
"math/rand"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDataflowCreate(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDataflowDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataflow,
Check: resource.ComposeTestCheckFunc(
testAccDataflowExists(
"googlecli_dataflow.foobar"),
),
},
},
})
}
func TestAccDataflowCreateRepeat(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDataflowDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataflow,
Check: resource.ComposeTestCheckFunc(
testAccDataflowExists(
"googlecli_dataflow.foobar"),
),
},
},
})
}
var disallowedDeletedStates = map[string]bool {
"JOB_STATE_RUNNING": true,
"JOB_STATE_UNKNOWN": true,
"JOB_STATE_FAILED": true,
"JOB_STATE_UPDATED": true,
}
func testAccCheckDataflowDestroy(s *terraform.State) error {
projectName := os.Getenv("GOOGLE_PROJECT")
for _, rs := range s.RootModule().Resources {
if rs.Type != "googlecli_dataflow" {
continue
}
jobdesc, err := ReadDataflow(rs.Primary.Attributes["jobids.0"], projectName)
if err != nil {
return fmt.Errorf("Failed to read dataflow list: %q", err)
}
if jobdesc.CurrentState == "" {
return fmt.Errorf("Dataflow jobs never started ")
}
_, disallowedCurrent := disallowedDeletedStates[jobdesc.CurrentState]
_, disallowedRequested := disallowedDeletedStates[jobdesc.RequestedState]
if disallowedCurrent && disallowedRequested {
return fmt.Errorf("Dataflow job in disallowed state: %q with requested state in: %q", jobdesc.CurrentState, jobdesc.RequestedState)
}
}
return nil
}
var disallowedCreatedStates = map[string] bool {
"JOB_STATE_FAILED": true,
"JOB_STATE_STOPPED": true,
"JOB_STATE_UPDATED": true,
"JOB_STATE_UNKNOWN": true,
}
func testAccDataflowExists(n string) resource.TestCheckFunc {
projectName := os.Getenv("GOOGLE_PROJECT")
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
jobdesc, err := ReadDataflow(rs.Primary.Attributes["jobids.0"], projectName)
if err != nil {
return fmt.Errorf("In test: Command line read has errored: %q with rs.Primary hash: %q", err, rs.Primary)
}
if jobdesc.CurrentState == "" {
return fmt.Errorf("Dataflow jobs never started")
}
if _, ok := disallowedCreatedStates[jobdesc.CurrentState]; ok {
return fmt.Errorf("Dataflow job in starting in disallowed state: %q", jobdesc.CurrentState)
}
return nil
}
}
var randDataflowInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int()
var testAccDataflow = fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "tf-test-bucket-%d"
force_destroy = true
}
resource "googlecli_dataflow" "foobar" {
name = "foobar-%d"
classpath = "./test-fixtures/google-cloud-dataflow-java-examples-all-bundled-1.1.1-SNAPSHOT.jar"
class = "com.google.cloud.dataflow.examples.WordCount"
optional_args = {
stagingLocation = "gs://${google_storage_bucket.bucket.name}"
runner = "DataflowPipelineRunner"
}
}
`, randDataflowInt, randDataflowInt)