added tests in python rest api

Signed-off-by: cdoern <cbdoer23@g.holycross.edu>
This commit is contained in:
cdoern
2021-05-30 11:18:19 -04:00
parent a6f0ac229f
commit 2cc4535e1f
3 changed files with 26 additions and 13 deletions

View File

@ -22,7 +22,8 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
decoder := r.Context().Value("decoder").(*schema.Decoder) decoder := r.Context().Value("decoder").(*schema.Decoder)
query := struct { query := struct {
Stream bool `schema:"stream"` Stream bool `schema:"stream"`
OneShot bool `schema:"one-shot"` //added schema for one shot
}{ }{
Stream: true, Stream: true,
} }
@ -30,6 +31,10 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return return
} }
if query.Stream && query.OneShot { // mismatch. one-shot can only be passed with stream=false
utils.Error(w, "invalid combination of stream and one-shot", http.StatusBadRequest, define.ErrInvalidArg)
return
}
name := utils.GetName(r) name := utils.GetName(r)
ctnr, err := runtime.LookupContainer(name) ctnr, err := runtime.LookupContainer(name)
@ -56,6 +61,16 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
return return
} }
coder := json.NewEncoder(w)
// Write header and content type.
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
// Setup JSON encoder for streaming.
coder.SetEscapeHTML(true)
var preRead time.Time var preRead time.Time
var preCPUStats CPUStats var preCPUStats CPUStats
if query.Stream { if query.Stream {
@ -75,17 +90,6 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
} }
} }
// Write header and content type.
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
// Setup JSON encoder for streaming.
coder := json.NewEncoder(w)
coder.SetEscapeHTML(true)
streamLabel: // A label to flatten the scope streamLabel: // A label to flatten the scope
select { select {
case <-r.Context().Done(): case <-r.Context().Done():
@ -199,7 +203,7 @@ streamLabel: // A label to flatten the scope
flusher.Flush() flusher.Flush()
} }
if !query.Stream { if !query.Stream || query.OneShot {
return return
} }

View File

@ -375,6 +375,11 @@ func (s *APIServer) registerContainersHandlers(r *mux.Router) error {
// type: boolean // type: boolean
// default: true // default: true
// description: Stream the output // description: Stream the output
// - in: query
// name: one-shot
// type: boolean
// default: false
// description: Provide a one-shot response in which preCPU stats are blank, resulting in a single cycle return.
// produces: // produces:
// - application/json // - application/json
// responses: // responses:

View File

@ -30,6 +30,10 @@ class ContainerTestCase(APITestCase):
self.assertIn(r.status_code, (200, 409), r.text) self.assertIn(r.status_code, (200, 409), r.text)
if r.status_code == 200: if r.status_code == 200:
self.assertId(r.content) self.assertId(r.content)
r = requests.get(self.uri(self.resolve_container("/containers/{}/stats?stream=false&one-shot=true")))
self.assertIn(r.status_code, (200, 409), r.text)
if r.status_code == 200:
self.assertId(r.content)
def test_delete(self): def test_delete(self):
r = requests.delete(self.uri(self.resolve_container("/containers/{}"))) r = requests.delete(self.uri(self.resolve_container("/containers/{}")))