-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathresources_test.go
More file actions
165 lines (142 loc) · 4.05 KB
/
resources_test.go
File metadata and controls
165 lines (142 loc) · 4.05 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package resource_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
resource "github.com/ahume/github-deployment-resource"
)
func file(path, contents string) {
Ω(ioutil.WriteFile(path, []byte(contents), 0644)).Should(Succeed())
}
var _ = Describe("Resources", func() {
var (
p resource.OutRequest
sourceDir string
)
BeforeEach(func() {
var err error
sourceDir, err = ioutil.TempDir("", "github-deployment")
Ω(err).ShouldNot(HaveOccurred())
// Um.
os.Args = []string{"out", sourceDir}
})
AfterEach(func() {
Ω(os.RemoveAll(sourceDir)).Should(Succeed())
})
Context("Params is unmarshalled", func() {
BeforeEach(func() {
p = resource.NewOutRequest()
})
It("adds default type", func() {
r := bytes.NewReader([]byte(`{
"params": {}
}`))
_ = json.NewDecoder(r).Decode(&p)
Ω(*p.Params.Type).Should(Equal("status"))
})
It("gets values from strings", func() {
r := bytes.NewReader([]byte(`{
"params": {
"type": "deployment",
"ref": "ref-string",
"state": "state-string",
"task": "task-string",
"environment": "environment-string",
"description": "description-string"
}
}`))
err := json.NewDecoder(r).Decode(&p)
Ω(err).ShouldNot(HaveOccurred())
Ω(*p.Params.Type).Should(Equal("deployment"))
Ω(*p.Params.Ref).Should(Equal("ref-string"))
Ω(*p.Params.State).Should(Equal("state-string"))
Ω(*p.Params.Task).Should(Equal("task-string"))
Ω(*p.Params.Environment).Should(Equal("environment-string"))
Ω(*p.Params.Description).Should(Equal("description-string"))
})
It("gets values from files", func() {
idPath := filepath.Join(sourceDir, "id")
refPath := filepath.Join(sourceDir, "ref")
statePath := filepath.Join(sourceDir, "state")
taskPath := filepath.Join(sourceDir, "task")
environmentPath := filepath.Join(sourceDir, "environment")
descriptionPath := filepath.Join(sourceDir, "description")
file(idPath, "id-from-file")
file(refPath, "ref-from-file")
file(statePath, "state-from-file")
file(taskPath, "task-from-file")
file(environmentPath, "environment-from-file")
file(descriptionPath, "description-from-file")
r := bytes.NewReader([]byte(`{
"params": {
"type": "deployment",
"id": {
"file": "id"
},
"ref": {
"file": "ref"
},
"state": {
"file": "state"
},
"task": {
"file": "task"
},
"environment": {
"file": "environment"
},
"description": {
"file": "description"
}
}
}`))
err := json.NewDecoder(r).Decode(&p)
Ω(err).ShouldNot(HaveOccurred())
Ω(*p.Params.Type).Should(Equal("deployment"))
Ω(*p.Params.ID).Should(Equal("id-from-file"))
Ω(*p.Params.Ref).Should(Equal("ref-from-file"))
Ω(*p.Params.State).Should(Equal("state-from-file"))
Ω(*p.Params.Task).Should(Equal("task-from-file"))
Ω(*p.Params.Environment).Should(Equal("environment-from-file"))
Ω(*p.Params.Description).Should(Equal("description-from-file"))
})
It("gets raw payload", func() {
r := bytes.NewReader([]byte(`{
"params": {
"type": "deployment",
"payload": {
"one":"two",
"three":"four"
}
}
}`))
err := json.NewDecoder(r).Decode(&p)
payload := *p.Params.Payload
Ω(err).ShouldNot(HaveOccurred())
Ω(*p.Params.Type).Should(Equal("deployment"))
Ω(payload["one"]).Should(Equal("two"))
Ω(payload["three"]).Should(Equal("four"))
})
It("merges raw payload into file payload", func() {
payloadPath := filepath.Join(sourceDir, "payload")
file(payloadPath, `{"three":"four"}`)
r := bytes.NewReader([]byte(`{
"params": {
"type": "deployment",
"payload": {"one":"two"},
"payload_path": "payload"
}
}`))
err := json.NewDecoder(r).Decode(&p)
payload := *p.Params.Payload
Ω(err).ShouldNot(HaveOccurred())
Ω(*p.Params.Type).Should(Equal("deployment"))
Ω(payload["one"]).Should(Equal("two"))
Ω(payload["three"]).Should(Equal("four"))
})
})
})