forked from cloudfoundry/cf-acceptance-tests
/
lifecycle.go
194 lines (159 loc) · 6.13 KB
/
lifecycle.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package windows
import (
"regexp"
"strconv"
"time"
. "github.com/cloudfoundry-incubator/cf-test-helpers/workflowhelpers"
. "github.com/cloudfoundry/cf-acceptance-tests/cats_suite_helpers"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/assets"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/random_name"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
"github.com/cloudfoundry-incubator/cf-test-helpers/helpers"
)
var _ = WindowsDescribe("Application Lifecycle", func() {
var appName string
BeforeEach(func() {
appName = random_name.CATSRandomName("APP")
})
It("exercises the app through its lifecycle", func() {
By("pushing it", func() {
Expect(cf.Cf("push",
appName,
"-s", Config.GetWindowsStack(),
"-b", Config.GetHwcBuildpackName(),
"-m", DEFAULT_MEMORY_LIMIT,
"-p", assets.NewAssets().Nora,
"-d", Config.GetAppsDomain()).Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
})
By("checking the 'started' event", func() {
found, _ := lastAppUsageEvent(appName, "STARTED")
Expect(found).To(BeTrue())
})
By("verifying it's up", func() {
Eventually(helpers.CurlingAppRoot(Config, appName)).Should(ContainSubstring("hello i am nora"))
})
By("verifying reported disk/memory usage", func() {
// #0 running 2015-06-10 02:22:39 PM 0.0% 48.7M of 2G 14M of 1G
var metrics = regexp.MustCompile(`running.*(?:[\d\.]+)%\s+([\d\.]+)[MG]? of (?:[\d\.]+)[MG]\s+([\d\.]+)[MG]? of (?:[\d\.]+)[MG]`)
memdisk := func() (float64, float64) {
app := cf.Cf("app", appName)
Expect(app.Wait()).To(Exit(0))
arr := metrics.FindStringSubmatch(string(app.Out.Contents()))
Expect(arr).NotTo(BeNil())
mem, err := strconv.ParseFloat(arr[1], 64)
Expect(err).ToNot(HaveOccurred())
disk, err := strconv.ParseFloat(arr[2], 64)
Expect(err).ToNot(HaveOccurred())
return mem, disk
}
Eventually(func() float64 { m, _ := memdisk(); return m }, Config.CfPushTimeoutDuration()).Should(BeNumerically(">", 0.0))
Eventually(func() float64 { _, d := memdisk(); return d }, Config.CfPushTimeoutDuration()).Should(BeNumerically(">", 0.0))
})
By("makes system environment variables available", func() {
Eventually(func() string {
return helpers.CurlApp(Config, appName, "/env")
}).Should(ContainSubstring(`"INSTANCE_GUID"`))
})
By("stopping it", func() {
Expect(cf.Cf("stop", appName).Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(helpers.CurlingAppRoot(Config, appName)).Should(ContainSubstring("404"))
})
By("setting an environment variable", func() {
Expect(cf.Cf("set-env", appName, "FOO", "bar").Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
})
By("starting it", func() {
Expect(cf.Cf("start", appName).Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(helpers.CurlingAppRoot(Config, appName)).Should(ContainSubstring("hello i am nora"))
})
By("checking custom env variables are available", func() {
Eventually(func() string {
return helpers.CurlAppWithTimeout(Config, appName, "/env/FOO", 30*time.Second)
}).Should(ContainSubstring("bar"))
})
By("scaling it", func() {
Expect(cf.Cf("scale", appName, "-i", "2").Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(cf.Cf("apps")).Should(gbytes.Say("2/2"))
Expect(cf.Cf("app", appName).Wait()).ToNot(gbytes.Say("insufficient resources"))
})
By("restarting an instance", func() {
idsBefore := reportedIDs(2, appName)
Expect(len(idsBefore)).To(Equal(2))
Expect(cf.Cf("restart-app-instance", appName, "1").Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(func() []string {
return differentIDsFrom(idsBefore, appName)
}, Config.CfPushTimeoutDuration(), 2*time.Second).Should(HaveLen(1))
})
By("updating, is reflected through another push", func() {
Eventually(helpers.CurlingAppRoot(Config, appName)).Should(ContainSubstring("hello i am nora"))
Expect(cf.Cf(
"push", appName,
"-p", assets.NewAssets().WindowsWebapp,
"-c", ".\\webapp.exe",
"-b", Config.GetBinaryBuildpackName(),
).Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(helpers.CurlingAppRoot(Config, appName)).Should(ContainSubstring("hi i am a standalone webapp"))
})
By("removing it", func() {
Expect(cf.Cf("delete", appName, "-f").Wait()).To(Exit(0))
app := cf.Cf("app", appName).Wait()
Expect(app).To(Exit(1))
Expect(app.Err).To(gbytes.Say("not found"))
Eventually(func() string {
return helpers.CurlAppRoot(Config, appName)
}).Should(ContainSubstring("404"))
found, _ := lastAppUsageEvent(appName, "STOPPED")
Expect(found).To(BeTrue())
})
})
})
type AppUsageEvent struct {
Entity struct {
AppName string `json:"app_name"`
State string `json:"state"`
BuildpackName string `json:"buildpack_name"`
BuildpackGuid string `json:"buildpack_guid"`
} `json:"entity"`
}
type AppUsageEvents struct {
Resources []AppUsageEvent `struct:"resources"`
}
func lastAppUsageEvent(appName string, state string) (bool, AppUsageEvent) {
var response AppUsageEvents
AsUser(TestSetup.AdminUserContext(), Config.DefaultTimeoutDuration(), func() {
ApiRequest("GET", "/v2/app_usage_events?order-direction=desc&page=1&results-per-page=150", &response, Config.DefaultTimeoutDuration())
})
for _, event := range response.Resources {
if event.Entity.AppName == appName && event.Entity.State == state {
return true, event
}
}
return false, AppUsageEvent{}
}
func reportedIDs(instances int, appName string) map[string]bool {
timer := time.NewTimer(time.Second * 120)
defer timer.Stop()
run := true
go func() {
<-timer.C
run = false
}()
seenIDs := map[string]bool{}
for len(seenIDs) != instances && run == true {
seenIDs[helpers.CurlApp(Config, appName, "/id")] = true
time.Sleep(time.Second)
}
return seenIDs
}
func differentIDsFrom(idsBefore map[string]bool, appName string) []string {
differentIDs := []string{}
for id := range reportedIDs(len(idsBefore), appName) {
if !idsBefore[id] {
differentIDs = append(differentIDs, id)
}
}
return differentIDs
}