Skip to content

Commit 8d98ecd

Browse files
fix(*): Handling of unavailable services
Send 503 - Service Temporarily Unavailable for not running services Fixes #2161
1 parent 4be44ac commit 8d98ecd

3 files changed

Lines changed: 61 additions & 13 deletions

File tree

router/templates/nginx.conf

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ http {
3636
client_max_body_size {{ or (.deis_router_bodySize) "1m" }};
3737

3838
log_format upstreaminfo '[$time_local] - $remote_addr - $remote_user - $status - "$request" - $bytes_sent - "$http_referer" - "$http_user_agent" - "$server_name" - $upstream_addr';
39-
39+
4040
# send logs to STDOUT so they can be seen using 'docker logs'
4141
access_log /opt/nginx/logs/access.log upstreaminfo;
4242
error_log /opt/nginx/logs/error.log;
@@ -51,11 +51,13 @@ http {
5151
upstream deis-controller {
5252
server {{ .deis_controller_host }}:{{ .deis_controller_port }};
5353
}
54+
{{ end }}
5455

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

60+
{{ if .deis_controller_host }}
5961
location / {
6062
proxy_buffering off;
6163
proxy_set_header Host $host;
@@ -67,21 +69,28 @@ http {
6769

6870
proxy_pass http://deis-controller;
6971
}
70-
}{{ end }}
72+
{{ else }}
73+
location / {
74+
return 503;
75+
}
76+
{{ end }}
77+
}
7178
## end deis-controller
7279

7380
## start deis-store-gateway
7481
{{ if .deis_store_gateway_host }}
7582
upstream deis-store-gateway {
7683
server {{ .deis_store_gateway_host }}:{{ .deis_store_gateway_port }};
7784
}
85+
{{ end }}
7886

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

8391
client_max_body_size 0;
8492

93+
{{ if .deis_store_gateway_host }}
8594
location / {
8695
proxy_buffering off;
8796
proxy_set_header Host $host;
@@ -93,7 +102,12 @@ http {
93102

94103
proxy_pass http://deis-store-gateway;
95104
}
96-
}{{ end }}
105+
{{ else }}
106+
location / {
107+
return 503;
108+
}
109+
{{ end }}
110+
}
97111
## end deis-store-gateway
98112

99113
## start service definitions for each application
@@ -103,11 +117,13 @@ http {
103117
{{ range $upstream := $service.Nodes }}server {{ $upstream.Value }};
104118
{{ end }}
105119
}
120+
{{ end }}
106121

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

126+
{{ if $service.Nodes }}
111127
location / {
112128
proxy_buffering off;
113129
proxy_set_header Host $host;
@@ -129,8 +145,13 @@ http {
129145

130146
proxy_pass http://{{ Base $service.Key }};
131147
}
148+
{{ else }}
149+
location / {
150+
return 503;
151+
}
152+
{{ end }}
132153
}
133-
{{ end }}{{ end }}
154+
{{ end }}
134155
## end service definitions for each application
135156

136157
# healthcheck

tests/ps_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import (
1212
)
1313

1414
var (
15-
psListCmd = "ps:list --app={{.AppName}}"
16-
psScaleCmd = "ps:scale web={{.ProcessNum}} --app={{.AppName}}"
15+
psListCmd = "ps:list --app={{.AppName}}"
16+
psScaleCmd = "ps:scale web={{.ProcessNum}} --app={{.AppName}}"
17+
psDownScaleCmd = "ps:scale web=0 --app={{.AppName}}"
1718
)
1819

1920
func TestPs(t *testing.T) {
2021
params := psSetup(t)
21-
psScaleTest(t, params)
22+
psScaleTest(t, params, psScaleCmd)
2223
appsOpenTest(t, params)
2324
psListTest(t, params, false)
25+
psScaleTest(t, params, psDownScaleCmd)
26+
utils.CurlWithFail(t, params, true, "503")
27+
2428
utils.AppsDestroyTest(t, params)
2529
utils.Execute(t, psScaleCmd, params, true, "404 NOT FOUND")
2630
// ensure we can choose our preferred beverage
@@ -55,8 +59,7 @@ func psListTest(t *testing.T, params *utils.DeisTestConfig, notflag bool) {
5559
utils.CheckList(t, psListCmd, params, output, notflag)
5660
}
5761

58-
func psScaleTest(t *testing.T, params *utils.DeisTestConfig) {
59-
cmd := psScaleCmd
62+
func psScaleTest(t *testing.T, params *utils.DeisTestConfig, cmd string) {
6063
if strings.Contains(params.ExampleApp, "dockerfile") {
6164
cmd = strings.Replace(cmd, "web", "cmd", 1)
6265
}

tests/utils/itutils.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,19 @@ func doCurl(url string) ([]byte, error) {
9292
body, err := ioutil.ReadAll(response.Body)
9393

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

9898
return body, nil
9999
}
100100

101101
// Curl connects to a Deis endpoint to see if the example app is running.
102102
func Curl(t *testing.T, params *DeisTestConfig) {
103+
CurlWithFail(t, params, false, "")
104+
}
105+
106+
// Curl connects to a Deis endpoint to see if the example app is running.
107+
func CurlWithFail(t *testing.T, params *DeisTestConfig, failFlag bool, expect string) {
103108
url := "http://" + params.AppName + "." + params.Domain
104109

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

115120
// once more to fail with an error
116121
body, err := doCurl(url)
117-
if err != nil {
118-
t.Fatal(err)
122+
123+
switch failFlag {
124+
case true:
125+
if err != nil {
126+
if strings.Contains(string(err.Error()), expect) {
127+
fmt.Println("(Error expected...ok) " + expect)
128+
} else {
129+
t.Fatal(err)
130+
}
131+
} else {
132+
if strings.Contains(string(body), expect) {
133+
fmt.Println("(Error expected...ok) " + expect)
134+
} else {
135+
t.Fatal(err)
136+
}
137+
}
138+
case false:
139+
if err != nil {
140+
t.Fatal(err)
141+
} else {
142+
fmt.Println(string(body))
143+
}
119144
}
120-
fmt.Println(string(body))
121145
}
122146

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

0 commit comments

Comments
 (0)