2015-06-05 16:09:53 -04:00
|
|
|
// +build experimental
|
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/daemon/graphdriver/vfs"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2016-04-20 08:38:34 -04:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-06-05 16:09:53 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
check.Suite(&DockerExternalGraphdriverSuite{
|
|
|
|
ds: &DockerSuite{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type DockerExternalGraphdriverSuite struct {
|
2016-04-20 08:38:34 -04:00
|
|
|
server *httptest.Server
|
|
|
|
jserver *httptest.Server
|
|
|
|
ds *DockerSuite
|
|
|
|
d *Daemon
|
|
|
|
ec map[string]*graphEventsCounter
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type graphEventsCounter struct {
|
|
|
|
activations int
|
|
|
|
creations int
|
|
|
|
removals int
|
|
|
|
gets int
|
|
|
|
puts int
|
|
|
|
stats int
|
|
|
|
cleanups int
|
|
|
|
exists int
|
|
|
|
init int
|
|
|
|
metadata int
|
|
|
|
diff int
|
|
|
|
applydiff int
|
|
|
|
changes int
|
|
|
|
diffsize int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) SetUpTest(c *check.C) {
|
|
|
|
s.d = NewDaemon(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) TearDownTest(c *check.C) {
|
|
|
|
s.d.Stop()
|
|
|
|
s.ds.TearDownTest(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) SetUpSuite(c *check.C) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec = make(map[string]*graphEventsCounter)
|
|
|
|
s.setUpPluginViaSpecFile(c)
|
|
|
|
s.setUpPluginViaJSONFile(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) setUpPluginViaSpecFile(c *check.C) {
|
2015-06-05 16:09:53 -04:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
s.server = httptest.NewServer(mux)
|
|
|
|
|
2016-04-20 08:38:34 -04:00
|
|
|
s.setUpPlugin(c, "test-external-graph-driver", "spec", mux, []byte(s.server.URL))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) setUpPluginViaJSONFile(c *check.C) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
s.jserver = httptest.NewServer(mux)
|
|
|
|
|
|
|
|
p := plugins.Plugin{Name: "json-external-graph-driver", Addr: s.jserver.URL}
|
|
|
|
b, err := json.Marshal(p)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
s.setUpPlugin(c, "json-external-graph-driver", "json", mux, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) setUpPlugin(c *check.C, name string, ext string, mux *http.ServeMux, b []byte) {
|
2015-06-05 16:09:53 -04:00
|
|
|
type graphDriverRequest struct {
|
|
|
|
ID string `json:",omitempty"`
|
|
|
|
Parent string `json:",omitempty"`
|
|
|
|
MountLabel string `json:",omitempty"`
|
2016-02-18 20:24:59 -05:00
|
|
|
ReadOnly bool `json:",omitempty"`
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type graphDriverResponse struct {
|
|
|
|
Err error `json:",omitempty"`
|
|
|
|
Dir string `json:",omitempty"`
|
|
|
|
Exists bool `json:",omitempty"`
|
|
|
|
Status [][2]string `json:",omitempty"`
|
|
|
|
Metadata map[string]string `json:",omitempty"`
|
|
|
|
Changes []archive.Change `json:",omitempty"`
|
|
|
|
Size int64 `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
respond := func(w http.ResponseWriter, data interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
|
|
|
|
switch t := data.(type) {
|
|
|
|
case error:
|
2015-10-08 11:51:41 -04:00
|
|
|
fmt.Fprintln(w, fmt.Sprintf(`{"Err": %q}`, t.Error()))
|
2015-06-05 16:09:53 -04:00
|
|
|
case string:
|
|
|
|
fmt.Fprintln(w, t)
|
|
|
|
default:
|
|
|
|
json.NewEncoder(w).Encode(&data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
decReq := func(b io.ReadCloser, out interface{}, w http.ResponseWriter) error {
|
|
|
|
defer b.Close()
|
|
|
|
if err := json.NewDecoder(b).Decode(&out); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("error decoding json: %s", err.Error()), 500)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-20 08:38:34 -04:00
|
|
|
base, err := ioutil.TempDir("", name)
|
2015-06-05 16:09:53 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-10-08 11:51:41 -04:00
|
|
|
vfsProto, err := vfs.Init(base, []string{}, nil, nil)
|
2015-10-25 05:41:41 -04:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("error initializing graph driver"))
|
2015-10-08 11:51:41 -04:00
|
|
|
driver := graphdriver.NewNaiveDiffDriver(vfsProto, nil, nil)
|
2015-06-05 16:09:53 -04:00
|
|
|
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext] = &graphEventsCounter{}
|
2015-06-05 16:09:53 -04:00
|
|
|
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].activations++
|
2015-06-05 16:09:53 -04:00
|
|
|
respond(w, `{"Implements": ["GraphDriver"]}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Init", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].init++
|
2015-06-05 16:09:53 -04:00
|
|
|
respond(w, "{}")
|
|
|
|
})
|
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
mux.HandleFunc("/GraphDriver.CreateReadWrite", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].creations++
|
2016-02-18 20:24:59 -05:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := driver.CreateReadWrite(req.ID, req.Parent, "", nil); err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, "{}")
|
|
|
|
})
|
|
|
|
|
2015-06-05 16:09:53 -04:00
|
|
|
mux.HandleFunc("/GraphDriver.Create", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].creations++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
2016-03-20 00:42:58 -04:00
|
|
|
if err := driver.Create(req.ID, req.Parent, "", nil); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, "{}")
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].removals++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Remove(req.ID); err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, "{}")
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Get", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].gets++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
|
|
|
return
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := driver.Get(req.ID, req.MountLabel)
|
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, &graphDriverResponse{Dir: dir})
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Put", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].puts++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Put(req.ID); err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, "{}")
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Exists", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].exists++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, &graphDriverResponse{Exists: driver.Exists(req.ID)})
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Status", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].stats++
|
2015-10-08 11:51:41 -04:00
|
|
|
respond(w, &graphDriverResponse{Status: driver.Status()})
|
2015-06-05 16:09:53 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Cleanup", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].cleanups++
|
2015-06-05 16:09:53 -04:00
|
|
|
err := driver.Cleanup()
|
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, `{}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.GetMetadata", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].metadata++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := driver.GetMetadata(req.ID)
|
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, &graphDriverResponse{Metadata: data})
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Diff", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].diff++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := driver.Diff(req.ID, req.Parent)
|
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
io.Copy(w, diff)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.Changes", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].changes++
|
2015-06-05 16:09:53 -04:00
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
changes, err := driver.Changes(req.ID, req.Parent)
|
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, &graphDriverResponse{Changes: changes})
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.ApplyDiff", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].applydiff++
|
2015-10-08 11:51:41 -04:00
|
|
|
var diff archive.Reader = r.Body
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2015-06-05 16:09:53 -04:00
|
|
|
id := r.URL.Query().Get("id")
|
|
|
|
parent := r.URL.Query().Get("parent")
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
if id == "" {
|
|
|
|
http.Error(w, fmt.Sprintf("missing id"), 409)
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := driver.ApplyDiff(id, parent, diff)
|
2015-06-05 16:09:53 -04:00
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, &graphDriverResponse{Size: size})
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/GraphDriver.DiffSize", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.ec[ext].diffsize++
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
var req graphDriverRequest
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := decReq(r.Body, &req, w); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := driver.DiffSize(req.ID, req.Parent)
|
|
|
|
if err != nil {
|
|
|
|
respond(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, &graphDriverResponse{Size: size})
|
|
|
|
})
|
|
|
|
|
2015-10-25 05:41:41 -04:00
|
|
|
err = os.MkdirAll("/etc/docker/plugins", 0755)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("error creating /etc/docker/plugins"))
|
2015-06-05 16:09:53 -04:00
|
|
|
|
2016-04-20 08:38:34 -04:00
|
|
|
specFile := "/etc/docker/plugins/" + name + "." + ext
|
|
|
|
err = ioutil.WriteFile(specFile, b, 0644)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("error writing to %s", specFile))
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) TearDownSuite(c *check.C) {
|
|
|
|
s.server.Close()
|
2016-04-20 08:38:34 -04:00
|
|
|
s.jserver.Close()
|
2015-06-05 16:09:53 -04:00
|
|
|
|
2015-10-25 05:41:41 -04:00
|
|
|
err := os.RemoveAll("/etc/docker/plugins")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("error removing /etc/docker/plugins"))
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) TestExternalGraphDriver(c *check.C) {
|
2016-04-20 08:38:34 -04:00
|
|
|
s.testExternalGraphDriver("test-external-graph-driver", "spec", c)
|
|
|
|
s.testExternalGraphDriver("json-external-graph-driver", "json", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) testExternalGraphDriver(name string, ext string, c *check.C) {
|
|
|
|
if err := s.d.StartWithBusybox("-s", name); err != nil {
|
2016-01-28 13:33:35 -05:00
|
|
|
b, _ := ioutil.ReadFile(s.d.LogFileName())
|
2015-10-08 11:51:41 -04:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("\n%s", string(b)))
|
|
|
|
}
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
out, err := s.d.Cmd("run", "-d", "--name=graphtest", "busybox", "sh", "-c", "echo hello > /hello")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
2016-04-20 08:38:34 -04:00
|
|
|
err = s.d.Restart("-s", name)
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
out, err = s.d.Cmd("inspect", "--format='{{.GraphDriver.Name}}'", "graphtest")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
2016-04-20 08:38:34 -04:00
|
|
|
c.Assert(strings.TrimSpace(out), check.Equals, name)
|
2015-06-05 16:09:53 -04:00
|
|
|
|
|
|
|
out, err = s.d.Cmd("diff", "graphtest")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
c.Assert(strings.Contains(out, "A /hello"), check.Equals, true)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("rm", "-f", "graphtest")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("info")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
|
|
|
err = s.d.Stop()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
// Don't check s.ec.exists, because the daemon no longer calls the
|
|
|
|
// Exists function.
|
2016-04-20 08:38:34 -04:00
|
|
|
c.Assert(s.ec[ext].activations, check.Equals, 2)
|
|
|
|
c.Assert(s.ec[ext].init, check.Equals, 2)
|
|
|
|
c.Assert(s.ec[ext].creations >= 1, check.Equals, true)
|
|
|
|
c.Assert(s.ec[ext].removals >= 1, check.Equals, true)
|
|
|
|
c.Assert(s.ec[ext].gets >= 1, check.Equals, true)
|
|
|
|
c.Assert(s.ec[ext].puts >= 1, check.Equals, true)
|
|
|
|
c.Assert(s.ec[ext].stats, check.Equals, 3)
|
|
|
|
c.Assert(s.ec[ext].cleanups, check.Equals, 2)
|
|
|
|
c.Assert(s.ec[ext].applydiff >= 1, check.Equals, true)
|
|
|
|
c.Assert(s.ec[ext].changes, check.Equals, 1)
|
|
|
|
c.Assert(s.ec[ext].diffsize, check.Equals, 0)
|
|
|
|
c.Assert(s.ec[ext].diff, check.Equals, 0)
|
|
|
|
c.Assert(s.ec[ext].metadata, check.Equals, 1)
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerExternalGraphdriverSuite) TestExternalGraphDriverPull(c *check.C) {
|
|
|
|
testRequires(c, Network)
|
|
|
|
c.Assert(s.d.Start(), check.IsNil)
|
|
|
|
|
|
|
|
out, err := s.d.Cmd("pull", "busybox:latest")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("run", "-d", "busybox", "top")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
}
|