forked from KevinSnyderCodes/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_command_test.go
152 lines (120 loc) · 4.32 KB
/
in_command_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package resource_test
import (
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/shipt/go-github/v32/github"
resource "github.com/linkyard/github-deployment-resource"
"github.com/linkyard/github-deployment-resource/fakes"
)
var _ = Describe("In Command", func() {
var (
command *resource.InCommand
githubClient *fakes.FakeGitHub
inRequest resource.InRequest
inResponse resource.InResponse
inErr error
tmpDir string
destDir string
)
BeforeEach(func() {
var err error
githubClient = &fakes.FakeGitHub{}
command = resource.NewInCommand(githubClient, ioutil.Discard)
tmpDir, err = ioutil.TempDir("", "github-deployment")
Ω(err).ShouldNot(HaveOccurred())
destDir = filepath.Join(tmpDir, "destination")
inRequest = resource.InRequest{}
})
AfterEach(func() {
Ω(os.RemoveAll(tmpDir)).Should(Succeed())
})
buildDeployment := func(ID int64, env string, task string) *github.Deployment {
return &github.Deployment{
ID: github.Int64(ID),
Environment: github.String(env),
Task: github.String(task),
Ref: github.String("master"),
SHA: github.String("12345"),
Description: github.String("One more"),
Creator: &github.User{
Login: github.String("Something"),
},
CreatedAt: &github.Timestamp{time.Date(2016, 01, 20, 15, 15, 15, 0, time.UTC)},
}
}
buildDeploymentStatus := func(ID int64, state string) *github.DeploymentStatus {
return &github.DeploymentStatus{
ID: github.Int64(ID),
State: github.String(state),
CreatedAt: &github.Timestamp{time.Date(2016, 01, 20, 15, 15, 15, 0, time.UTC)},
}
}
Context("when there is a deployment found", func() {
disaster := errors.New("no deployment")
BeforeEach(func() {
githubClient.GetDeploymentReturns(&github.Deployment{}, disaster)
inRequest.Version = resource.Version{
ID: "1",
}
})
It("returns an appropriate error", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Expect(inErr).To(Equal(disaster))
})
})
Context("when there is a deployment found", func() {
BeforeEach(func() {
githubClient.GetDeploymentReturns(buildDeployment(1, "production", "deploy"), nil)
githubClient.ListDeploymentStatusesReturns([]*github.DeploymentStatus{
buildDeploymentStatus(1, "success"),
}, nil)
inRequest.Version = resource.Version{
ID: "1",
}
})
It("creates the correct data files", func() {
inResponse, inErr = command.Run(destDir, inRequest)
contents, err := ioutil.ReadFile(path.Join(destDir, "id"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("1"))
contents, err = ioutil.ReadFile(path.Join(destDir, "ref"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("master"))
contents, err = ioutil.ReadFile(path.Join(destDir, "sha"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("12345"))
contents, err = ioutil.ReadFile(path.Join(destDir, "task"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("deploy"))
contents, err = ioutil.ReadFile(path.Join(destDir, "environment"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("production"))
contents, err = ioutil.ReadFile(path.Join(destDir, "description"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("One more"))
})
It("outputs the correct metadata", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Ω(inResponse.Metadata).Should(ConsistOf(
resource.MetadataPair{Name: "id", Value: "1"},
resource.MetadataPair{Name: "ref", Value: "master"},
resource.MetadataPair{Name: "sha", Value: "12345"},
resource.MetadataPair{Name: "task", Value: "deploy"},
resource.MetadataPair{Name: "description", Value: "One more"},
resource.MetadataPair{Name: "environment", Value: "production"},
resource.MetadataPair{Name: "creator", Value: "Something"},
resource.MetadataPair{Name: "created_at", Value: "2016-01-20 15:15:15"},
resource.MetadataPair{Name: "status_id", Value: "1"},
resource.MetadataPair{Name: "status", Value: "success"},
resource.MetadataPair{Name: "status_created_at", Value: "2016-01-20 15:15:15"},
resource.MetadataPair{Name: "status_count", Value: "1"},
))
})
})
})