Merge pull request #106 from mopemope/name

Add docker name parameter
This commit is contained in:
Aanand Prasad 2014-07-16 15:29:42 -07:00
commit 04b2a96ff8
3 changed files with 56 additions and 2 deletions

View File

@ -112,7 +112,24 @@ func (b *dockerClientBackend) createContainer(cmd ...string) (libswarm.Sender, e
return nil, fmt.Errorf("dockerclient: spawn takes exactly 1 argument, got %d", len(cmd))
}
resp, err := b.client.call("POST", "/containers/create", cmd[0])
param := cmd[0]
containerValues := url.Values{}
var postParam map[string]interface{}
if err := json.Unmarshal([]byte(param), &postParam); err != nil {
return nil, err
}
if name, ok := postParam["name"]; ok {
containerValues.Set("name", name.(string))
delete(postParam, "name")
tmp, err := json.Marshal(postParam)
if err != nil {
return nil, err
}
param = string(tmp)
}
resp, err := b.client.call("POST", "/containers/create?"+containerValues.Encode(), param)
if err != nil {
return nil, err
}

View File

@ -79,6 +79,25 @@ func TestSpawn(t *testing.T) {
server.Check()
}
func TestSpawnWithName(t *testing.T) {
name := "foo"
server := makeServer(t, &requestStub{
reqMethod: "POST",
reqPath: "/containers/create?name=foo",
reqBody: "{}",
resStatus: 201,
resBody: "{}",
},
)
i := instance(t, server)
_, err := i.Spawn(fmt.Sprintf("{\"name\":\"%s\"}", name))
if err != nil {
t.Fatal(err)
}
server.Check()
}
func TestAttachToChild(t *testing.T) {
name := "foo"
server := makeServer(t, &requestStub{
@ -210,7 +229,12 @@ func (s *stubServer) Check() {
func (s *stubServer) ServeRequest(w http.ResponseWriter, r *http.Request) {
for _, record := range s.stubs {
stub := record.stub
if r.Method == stub.reqMethod && r.URL.Path == stub.reqPath {
path := r.URL.Path
q := r.URL.RawQuery
if q != "" {
path += "?" + q
}
if r.Method == stub.reqMethod && path == stub.reqPath {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
s.t.Fatal(err)

View File

@ -225,6 +225,19 @@ func postContainersCreate(out libswarm.Sender, version version.Version, w http.R
return err
}
name := r.Form.Get("name")
if name != "" {
var reqJson map[string]interface{}
if err = json.Unmarshal(body, &reqJson); err != nil {
return err
}
reqJson["name"] = name
body, err = json.Marshal(reqJson)
if err != nil {
return err
}
}
container, err := libswarm.AsClient(out).Spawn(string(body))
if err != nil {
return err