Skip to content

Commit

Permalink
Merge pull request containers#9414 from edigaryev/fix-wait-api-condition
Browse files Browse the repository at this point in the history
API: fix libpod's container wait endpoint condition conversion
  • Loading branch information
openshift-merge-robot authored Feb 18, 2021
2 parents c3419d2 + e022c19 commit b2bb05d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pkg/api/handlers/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"syscall"
"time"

"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/pkg/util"
"github.com/gorilla/schema"
"github.com/sirupsen/logrus"
Expand All @@ -19,6 +20,7 @@ func NewAPIDecoder() *schema.Decoder {
d.IgnoreUnknownKeys(true)
d.RegisterConverter(map[string][]string{}, convertURLValuesString)
d.RegisterConverter(time.Time{}, convertTimeString)
d.RegisterConverter(define.ContainerStatus(0), convertContainerStatusString)

var Signal syscall.Signal
d.RegisterConverter(Signal, convertSignal)
Expand Down Expand Up @@ -46,6 +48,19 @@ func convertURLValuesString(query string) reflect.Value {
return reflect.ValueOf(f)
}

func convertContainerStatusString(query string) reflect.Value {
result, err := define.StringToContainerStatus(query)
if err != nil {
logrus.Infof("convertContainerStatusString: Failed to parse %s: %s", query, err.Error())

// We return nil here instead of result because reflect.ValueOf().IsValid() will be true
// in github.com/gorilla/schema's decoder, which means there's no parsing error
return reflect.ValueOf(nil)
}

return reflect.ValueOf(result)
}

// isZero() can be used to determine if parsing failed.
func convertTimeString(query string) reflect.Value {
var (
Expand Down
11 changes: 11 additions & 0 deletions pkg/bindings/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"errors"
"fmt"
"net/url"
"reflect"
"strconv"
Expand All @@ -11,14 +12,23 @@ import (
)

func IsSimpleType(f reflect.Value) bool {
if _, ok := f.Interface().(fmt.Stringer); ok {
return true
}

switch f.Kind() {
case reflect.Bool, reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.String:
return true
}

return false
}

func SimpleTypeToParam(f reflect.Value) string {
if s, ok := f.Interface().(fmt.Stringer); ok {
return s.String()
}

switch f.Kind() {
case reflect.Bool:
return strconv.FormatBool(f.Bool())
Expand All @@ -31,6 +41,7 @@ func SimpleTypeToParam(f reflect.Value) string {
case reflect.String:
return f.String()
}

panic("the input parameter is not a simple type")
}

Expand Down
2 changes: 1 addition & 1 deletion test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cid=$(jq -r '.Id' <<<"$output")
# Prior to the fix in #6835, this would fail 500 "args must not be empty"
t POST libpod/containers/${cid}/start '' 204
# Container should exit almost immediately. Wait for it, confirm successful run
t POST libpod/containers/${cid}/wait '' 200 '0'
t POST "libpod/containers/${cid}/wait?condition=stopped&condition=exited" '' 200 '0'
t GET libpod/containers/${cid}/json 200 \
.Id=$cid \
.State.Status~\\\(exited\\\|stopped\\\) \
Expand Down

0 comments on commit b2bb05d

Please sign in to comment.