@@ -119,6 +119,75 @@ def test_start_chaos(self):
119119 states = set ([c ['state' ] for c in response .data ['results' ]])
120120 self .assertEqual (states , set (['crashed' , 'up' ]))
121121
122+ def test_restart_chaos (self ):
123+ url = '/v1/apps'
124+ response = self .client .post (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
125+ self .assertEqual (response .status_code , 201 )
126+ app_id = response .data ['id' ]
127+ # post a new build
128+ url = "/v1/apps/{app_id}/builds" .format (** locals ())
129+ body = {'image' : 'autotest/example' , 'sha' : 'a' * 40 ,
130+ 'procfile' : json .dumps ({'web' : 'node server.js' , 'worker' : 'node worker.js' })}
131+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
132+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
133+ self .assertEqual (response .status_code , 201 )
134+ url = "/v1/apps/{app_id}/containers" .format (** locals ())
135+ response = self .client .get (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
136+ self .assertEqual (response .status_code , 200 )
137+ self .assertEqual (len (response .data ['results' ]), 1 )
138+ # scale up, which will allow some crashed containers
139+ url = "/v1/apps/{app_id}/scale" .format (** locals ())
140+ body = {'web' : 20 , 'worker' : 20 }
141+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
142+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
143+ self .assertEqual (response .status_code , 204 )
144+ # let's get chaotic
145+ chaos .STOP_ERROR_RATE = 0.5
146+ chaos .START_ERROR_RATE = 0.5
147+ # reboot the web processes
148+ url = "/v1/apps/{app_id}/containers/web/restart" .format (** locals ())
149+ response = self .client .post (url ,
150+ content_type = 'application/json' ,
151+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
152+ self .assertEqual (response .status_code , 200 , response .data )
153+ # inspect broken containers
154+ url = "/v1/apps/{app_id}/containers" .format (** locals ())
155+ response = self .client .get (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
156+ self .assertEqual (response .status_code , 200 )
157+ self .assertEqual (response .data ['count' ], 40 )
158+ # make sure some failed
159+ states = set ([c ['state' ] for c in response .data ['results' ]])
160+ self .assertEqual (states , set (['crashed' , 'up' ]))
161+ # make sure that we only rebooted the web processes
162+ types = set ([c ['type' ] for c in response .data ['results' ] if c ['state' ] == 'crashed' ])
163+ self .assertEqual (types , set (['web' ]))
164+ # start fresh
165+ chaos .STOP_ERROR_RATE = 0.0
166+ chaos .START_ERROR_RATE = 0.0
167+ url = "/v1/apps/{app_id}/containers/web/restart" .format (** locals ())
168+ response = self .client .post (url ,
169+ content_type = 'application/json' ,
170+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
171+ # let the carnage continue
172+ chaos .STOP_ERROR_RATE = 0.5
173+ chaos .START_ERROR_RATE = 0.5
174+ # reboot ALL the containers!
175+ url = "/v1/apps/{app_id}/containers/restart" .format (** locals ())
176+ response = self .client .post (url ,
177+ content_type = 'application/json' ,
178+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
179+ self .assertEqual (response .status_code , 200 )
180+ # inspect broken containers
181+ url = "/v1/apps/{app_id}/containers" .format (** locals ())
182+ response = self .client .get (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
183+ self .assertEqual (response .status_code , 200 )
184+ self .assertEqual (len (response .data ['results' ]), 40 )
185+ # make sure some failed
186+ states = set ([c ['state' ] for c in response .data ['results' ]])
187+ self .assertEqual (states , set (['crashed' , 'up' ]))
188+ types = set ([c ['type' ] for c in response .data ['results' ]])
189+ self .assertEqual (types , set (['web' , 'worker' ]))
190+
122191 def test_destroy_chaos (self ):
123192 url = '/v1/apps'
124193 response = self .client .post (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
0 commit comments