Skip to content

Commit

Permalink
fix(*): Handling of unavailable services
Browse files Browse the repository at this point in the history
Send 503 - Service Temporarily Unavailable for not running services

Fixes deis#2161
  • Loading branch information
johanneswuerbach committed Nov 24, 2014
1 parent 4be44ac commit 8d98ecd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
29 changes: 25 additions & 4 deletions router/templates/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ http {
client_max_body_size {{ or (.deis_router_bodySize) "1m" }};

log_format upstreaminfo '[$time_local] - $remote_addr - $remote_user - $status - "$request" - $bytes_sent - "$http_referer" - "$http_user_agent" - "$server_name" - $upstream_addr';

# send logs to STDOUT so they can be seen using 'docker logs'
access_log /opt/nginx/logs/access.log upstreaminfo;
error_log /opt/nginx/logs/error.log;
Expand All @@ -51,11 +51,13 @@ http {
upstream deis-controller {
server {{ .deis_controller_host }}:{{ .deis_controller_port }};
}
{{ end }}

server {
server_name ~^deis\.(?<domain>.+)$;
include deis.conf;

{{ if .deis_controller_host }}
location / {
proxy_buffering off;
proxy_set_header Host $host;
Expand All @@ -67,21 +69,28 @@ http {

proxy_pass http://deis-controller;
}
}{{ end }}
{{ else }}
location / {
return 503;
}
{{ end }}
}
## end deis-controller

## start deis-store-gateway
{{ if .deis_store_gateway_host }}
upstream deis-store-gateway {
server {{ .deis_store_gateway_host }}:{{ .deis_store_gateway_port }};
}
{{ end }}

server {
server_name ~^deis-store\.(?<domain>.+)$;
include deis.conf;

client_max_body_size 0;

{{ if .deis_store_gateway_host }}
location / {
proxy_buffering off;
proxy_set_header Host $host;
Expand All @@ -93,7 +102,12 @@ http {

proxy_pass http://deis-store-gateway;
}
}{{ end }}
{{ else }}
location / {
return 503;
}
{{ end }}
}
## end deis-store-gateway

## start service definitions for each application
Expand All @@ -103,11 +117,13 @@ http {
{{ range $upstream := $service.Nodes }}server {{ $upstream.Value }};
{{ end }}
}
{{ end }}

server {
server_name ~^{{ Base $service.Key }}\.(?<domain>.+)${{ range $app_domains := $domains }}{{ if eq (Base $service.Key) (Base $app_domains.Key) }} {{ $app_domains.Value }}{{ end }}{{ end }};
include deis.conf;

{{ if $service.Nodes }}
location / {
proxy_buffering off;
proxy_set_header Host $host;
Expand All @@ -129,8 +145,13 @@ http {

proxy_pass http://{{ Base $service.Key }};
}
{{ else }}
location / {
return 503;
}
{{ end }}
}
{{ end }}{{ end }}
{{ end }}
## end service definitions for each application

# healthcheck
Expand Down
13 changes: 8 additions & 5 deletions tests/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import (
)

var (
psListCmd = "ps:list --app={{.AppName}}"
psScaleCmd = "ps:scale web={{.ProcessNum}} --app={{.AppName}}"
psListCmd = "ps:list --app={{.AppName}}"
psScaleCmd = "ps:scale web={{.ProcessNum}} --app={{.AppName}}"
psDownScaleCmd = "ps:scale web=0 --app={{.AppName}}"
)

func TestPs(t *testing.T) {
params := psSetup(t)
psScaleTest(t, params)
psScaleTest(t, params, psScaleCmd)
appsOpenTest(t, params)
psListTest(t, params, false)
psScaleTest(t, params, psDownScaleCmd)
utils.CurlWithFail(t, params, true, "503")

utils.AppsDestroyTest(t, params)
utils.Execute(t, psScaleCmd, params, true, "404 NOT FOUND")
// ensure we can choose our preferred beverage
Expand Down Expand Up @@ -55,8 +59,7 @@ func psListTest(t *testing.T, params *utils.DeisTestConfig, notflag bool) {
utils.CheckList(t, psListCmd, params, output, notflag)
}

func psScaleTest(t *testing.T, params *utils.DeisTestConfig) {
cmd := psScaleCmd
func psScaleTest(t *testing.T, params *utils.DeisTestConfig, cmd string) {
if strings.Contains(params.ExampleApp, "dockerfile") {
cmd = strings.Replace(cmd, "web", "cmd", 1)
}
Expand Down
32 changes: 28 additions & 4 deletions tests/utils/itutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ func doCurl(url string) ([]byte, error) {
body, err := ioutil.ReadAll(response.Body)

if !strings.Contains(string(body), "Powered by Deis") {
return nil, fmt.Errorf("App not started (%d)", response.StatusCode)
return nil, fmt.Errorf("App not started (%d)\nBody: (%s)", response.StatusCode, string(body))
}

return body, nil
}

// Curl connects to a Deis endpoint to see if the example app is running.
func Curl(t *testing.T, params *DeisTestConfig) {
CurlWithFail(t, params, false, "")
}

// Curl connects to a Deis endpoint to see if the example app is running.
func CurlWithFail(t *testing.T, params *DeisTestConfig, failFlag bool, expect string) {
url := "http://" + params.AppName + "." + params.Domain

// FIXME: try the curl a few times
Expand All @@ -114,10 +119,29 @@ func Curl(t *testing.T, params *DeisTestConfig) {

// once more to fail with an error
body, err := doCurl(url)
if err != nil {
t.Fatal(err)

switch failFlag {
case true:
if err != nil {
if strings.Contains(string(err.Error()), expect) {
fmt.Println("(Error expected...ok) " + expect)
} else {
t.Fatal(err)
}
} else {
if strings.Contains(string(body), expect) {
fmt.Println("(Error expected...ok) " + expect)
} else {
t.Fatal(err)
}
}
case false:
if err != nil {
t.Fatal(err)
} else {
fmt.Println(string(body))
}
}
fmt.Println(string(body))
}

// AuthCancel tests whether `deis auth:cancel` destroys a user's account.
Expand Down

0 comments on commit 8d98ecd

Please sign in to comment.