Skip to content

Commit

Permalink
feature: add rules for type validation
Browse files Browse the repository at this point in the history
Signed-off-by: zhuangqh <[email protected]>
  • Loading branch information
zhuangqh authored and allencloud committed Nov 5, 2018
1 parent 69b5035 commit 2b5fad7
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 14 deletions.
6 changes: 0 additions & 6 deletions apis/opts/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,3 @@ func parseLabel(label string) ([]string, error) {
}
return fields, nil
}

// ValidateLabels verifies the correct of labels
func ValidateLabels(map[string]string) error {
// TODO
return nil
}
4 changes: 4 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ func (s *Server) updateContainer(ctx context.Context, rw http.ResponseWriter, re
if err := json.NewDecoder(reader).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

name := mux.Vars(req)["name"]

Expand Down
15 changes: 13 additions & 2 deletions apis/server/system_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alibaba/pouch/pkg/utils"

"github.com/docker/docker/pkg/ioutils"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -41,20 +42,30 @@ func (s *Server) version(ctx context.Context, rw http.ResponseWriter, req *http.

func (s *Server) updateDaemon(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
cfg := &types.DaemonUpdateConfig{}

// decode request body
if err := json.NewDecoder(req.Body).Decode(cfg); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

// TODO: validate cfg in details
// validate request body
if err := cfg.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

return s.SystemMgr.UpdateDaemon(cfg)
}

func (s *Server) auth(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
auth := types.AuthConfig{}

// decode request body
if err := json.NewDecoder(req.Body).Decode(&auth); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := auth.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

token, err := s.SystemMgr.Auth(&auth)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,7 @@ definitions:
StopTimeout:
description: "Timeout to stop a container in seconds."
type: "integer"
minimum: 0
default: 10
Shell:
description: "Shell for when `RUN`, `CMD`, and `ENTRYPOINT` uses a shell."
Expand Down Expand Up @@ -3222,6 +3223,7 @@ definitions:
ExecCreateConfig:
type: "object"
description: is a small subset of the Config struct that holds the configuration.
required: [Cmd]
properties:
User:
type: "string"
Expand Down Expand Up @@ -3917,8 +3919,9 @@ definitions:
type: "string"
example: "127.0.0.1"
HostPort:
description: "Host port number that the container's port is mapped to."
description: "Host port number that the container's port is mapped to. range (0,65535]"
type: "string"
pattern: ^([1-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$
example: "4443"

RestartPolicy:
Expand Down
18 changes: 18 additions & 0 deletions apis/types/container_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions apis/types/exec_create_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion apis/types/port_binding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/api_container_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,29 @@ func (suite *APIContainerCreateSuite) TestBadParam(c *check.C) {
helpwantedForMissingCase(c, "container api create with bad request")
}

func testCreateContainerWithBadParam(c *check.C, cname string, obj map[string]interface{}) {
q := url.Values{}
q.Add("name", cname)
query := request.WithQuery(q)
body := request.WithJSONBody(obj)

resp, err := request.Post("/containers/create", query, body)
defer DelContainerForceMultyTime(c, cname)
c.Assert(err, check.IsNil)
defer resp.Body.Close()
CheckRespStatus(c, resp, 400)
}

// TestCreateWithBadStopTimeout using bad stopTimeout to create container.
func (suite *APIContainerCreateSuite) TestCreateWithBadStopTimeout(c *check.C) {
testCreateContainerWithBadParam(c,
"TestCreateWithBadStopTimeout",
map[string]interface{}{
"Image": busyboxImage,
"StopTimeout": -1,
})
}

func (suite *APIContainerCreateSuite) TestCreateNvidiaConfig(c *check.C) {
cname := "TestCreateNvidiaConfig"
q := url.Values{}
Expand Down
16 changes: 14 additions & 2 deletions test/api_container_exec_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (suite *APIContainerExecSuite) TestContainerCreateExecWithEnvs(c *check.C)
CheckRespStatus(c, resp, 201)
}

// TestContainerCreateExecNoCmd tests execing containers is OK.
// TestContainerCreateExecNoCmd tests exec without cmd.
func (suite *APIContainerExecSuite) TestContainerCreateExecNoCmd(c *check.C) {
cname := "TestContainerCreateExecNoCmd"

Expand All @@ -71,14 +71,26 @@ func (suite *APIContainerExecSuite) TestContainerCreateExecNoCmd(c *check.C) {

StartContainerOk(c, cname)

// test no Cmd
obj := map[string]interface{}{
"Detach": true,
}
body := request.WithJSONBody(obj)

resp, err := request.Post("/containers/"+cname+"/exec", body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 201)
CheckRespStatus(c, resp, 400)

// test empty Cmd
obj = map[string]interface{}{
"Detach": true,
"Cmd": []string{},
}
body = request.WithJSONBody(obj)

resp, err = request.Post("/containers/"+cname+"/exec", body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 400)
}

// TestExecCreatedContainer tests creating exec on created container return error.
Expand Down
12 changes: 12 additions & 0 deletions test/cli_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func (suite *PouchExecSuite) TestExecCommand(c *check.C) {
}
}

func (suite *PouchExecSuite) TestExecNoCommand(c *check.C) {
cname := "TestExecNoCommand"
res := command.PouchRun("run", "-d", "--name", cname, busyboxImage, "sleep", "100000").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, cname)

res = command.PouchRun("exec", cname)
expectedError := "requires at least 2 arg(s), only received 1"
if out := res.Combined(); !strings.Contains(out, expectedError) {
c.Fatalf("unexpected output %s, expected %s", out, expectedError)
}
}

// TestExecMultiCommands is to verify the correctness of execing container with specified commands.
func (suite *PouchExecSuite) TestExecMultiCommands(c *check.C) {
name := "exec-normal2"
Expand Down

0 comments on commit 2b5fad7

Please sign in to comment.