Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add acceptance test for crashed app instance stats #1239

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions apps/crashing.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package apps

import (
"encoding/json"
"fmt"
. "github.com/cloudfoundry/cf-acceptance-tests/cats_suite_helpers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"

"github.com/cloudfoundry/cf-acceptance-tests/helpers/app_helpers"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/assets"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/random_name"
"github.com/cloudfoundry/cf-test-helpers/v2/cf"
"github.com/cloudfoundry/cf-test-helpers/v2/helpers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
"time"
)

var _ = AppsDescribe("Crashing", func() {
Expand Down Expand Up @@ -45,6 +47,69 @@ var _ = AppsDescribe("Crashing", func() {
})
})

Describe("an app with three instances, two running and one crashing", func() {
It("keeps two instances running while another crashes", func() {
By("Pushing the app with three instances")
Expect(cf.Cf(
"push", appName,
"-b", "python_buildpack",
"-m", DEFAULT_MEMORY_LIMIT,
"-p", assets.NewAssets().PythonCrashApp,
"-i", "3", // Setting three instances
).Wait(Config.CfPushTimeoutDuration())).To(Exit(0))

By("Checking that the app is up and running")
Eventually(cf.Cf("app", appName)).Should(Say("running"))

By("Waiting until one instance crashes")
appGuid := app_helpers.GetAppGuid(appName)
processStatsPath := fmt.Sprintf("/v3/apps/%s/processes/web/stats", appGuid)

// Poll until at least one instance has crashed
Eventually(func() bool {
session := cf.Cf("curl", processStatsPath).Wait()
instancesJson := struct {
Resources []struct {
Type string `json:"type"`
State string `json:"state"`
} `json:"resources"`
}{}

bytes := session.Wait().Out.Contents()
err := json.Unmarshal(bytes, &instancesJson)
Expect(err).ToNot(HaveOccurred())

for _, instance := range instancesJson.Resources {
if instance.State == "CRASHED" {
return true
}
}
return false
}, 60*time.Second, 5*time.Second).Should(BeTrue(), "At least one instance should be in the CRASHED state")

By("Verifying at least one instance is still running")
session := cf.Cf("curl", processStatsPath).Wait()
instancesJson := struct {
Resources []struct {
Type string `json:"type"`
State string `json:"state"`
} `json:"resources"`
}{}

bytes := session.Wait().Out.Contents()
json.Unmarshal(bytes, &instancesJson)

foundRunning := false
for _, instance := range instancesJson.Resources {
if instance.State == "RUNNING" {
foundRunning = true
break
}
}
Expect(foundRunning).To(BeTrue(), "At least one instance should still be in the RUNNING state")
})
})

Context("the app crashes", func() {
BeforeEach(func() {
Expect(cf.Cf(app_helpers.CatnipWithArgs(
Expand Down
1 change: 1 addition & 0 deletions assets/python-crash-app/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python app.py
29 changes: 29 additions & 0 deletions assets/python-crash-app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import http.server
import socketserver
import sys

class CustomHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = f"Hello, you've reached the instance {os.getenv('CF_INSTANCE_INDEX', 'not defined')}!"
self.wfile.write(message.encode('utf-8'))

def main():
port = int(os.getenv('PORT', 8080))
instance_index = int(os.getenv('CF_INSTANCE_INDEX', -1))

if instance_index > 1:
print(f"Instance {instance_index} is quitting!")
sys.exit(1)

handler = CustomHandler
httpd = socketserver.TCPServer(("", port), handler)

print(f"Serving on port {port} from instance {instance_index}")
httpd.serve_forever()

if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions helpers/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Assets struct {
LoggregatorLoadGenerator string
LoggregatorLoadGeneratorGo string
Python string
PythonCrashApp string
Nginx string
Node string
CNBNode string
Expand Down Expand Up @@ -75,6 +76,7 @@ func NewAssets() Assets {
Php: "assets/php",
Proxy: "assets/proxy",
Python: "assets/python",
PythonCrashApp: "assets/python-crash-app",
R: "assets/r",
RubySimple: "assets/ruby_simple",
SecurityGroupBuildpack: "assets/security_group_buildpack.zip",
Expand Down