本文整理匯總了Golang中github.com/dotcloud/docker/engine.NewTable函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTable函數的具體用法?Golang NewTable怎麽用?Golang NewTable使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewTable函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGetImagesJSONLegacyFormat
func TestGetImagesJSONLegacyFormat(t *testing.T) {
eng := engine.New()
var called bool
eng.Register("images", func(job *engine.Job) engine.Status {
called = true
outsLegacy := engine.NewTable("Created", 0)
outsLegacy.Add(createEnvFromGetImagesJSONStruct(sampleImage))
if _, err := outsLegacy.WriteListTo(job.Stdout); err != nil {
return job.Error(err)
}
return engine.StatusOK
})
r := serveRequestUsingVersion("GET", "/images/json", "1.6", nil, eng, t)
if !called {
t.Fatal("handler was not called")
}
assertHttpNotError(r, t)
assertContentType(r, "application/json", t)
images := engine.NewTable("Created", 0)
if _, err := images.ReadListFrom(r.Body.Bytes()); err != nil {
t.Fatal(err)
}
if images.Len() != 1 {
t.Fatalf("Expected 1 image, %d found", images.Len())
}
image := images.Data[0]
if image.Get("Tag") != "test-tag" {
t.Errorf("Expected tag 'test-tag', found '%s'", image.Get("Tag"))
}
if image.Get("Repository") != "test-name" {
t.Errorf("Expected repository 'test-name', found '%s'", image.Get("Repository"))
}
}
示例2: TestImageTagImageDelete
func TestImageTagImageDelete(t *testing.T) {
eng := NewTestEngine(t)
defer mkRuntimeFromEngine(eng, t).Nuke()
srv := mkServerFromEngine(eng, t)
initialImages := getAllImages(eng, t)
if err := eng.Job("tag", unitTestImageName, "utest", "tag1").Run(); err != nil {
t.Fatal(err)
}
if err := eng.Job("tag", unitTestImageName, "utest/docker", "tag2").Run(); err != nil {
t.Fatal(err)
}
if err := eng.Job("tag", unitTestImageName, "utest:5000/docker", "tag3").Run(); err != nil {
t.Fatal(err)
}
images := getAllImages(eng, t)
nExpected := len(initialImages.Data[0].GetList("RepoTags")) + 3
nActual := len(images.Data[0].GetList("RepoTags"))
if nExpected != nActual {
t.Errorf("Expected %d images, %d found", nExpected, nActual)
}
if err := srv.DeleteImage("utest/docker:tag2", engine.NewTable("", 0), true, false, false); err != nil {
t.Fatal(err)
}
images = getAllImages(eng, t)
nExpected = len(initialImages.Data[0].GetList("RepoTags")) + 2
nActual = len(images.Data[0].GetList("RepoTags"))
if nExpected != nActual {
t.Errorf("Expected %d images, %d found", nExpected, nActual)
}
if err := srv.DeleteImage("utest:5000/docker:tag3", engine.NewTable("", 0), true, false, false); err != nil {
t.Fatal(err)
}
images = getAllImages(eng, t)
nExpected = len(initialImages.Data[0].GetList("RepoTags")) + 1
nActual = len(images.Data[0].GetList("RepoTags"))
if err := srv.DeleteImage("utest:tag1", engine.NewTable("", 0), true, false, false); err != nil {
t.Fatal(err)
}
images = getAllImages(eng, t)
if images.Len() != initialImages.Len() {
t.Errorf("Expected %d image, %d found", initialImages.Len(), images.Len())
}
}
示例3: getImagesJSON
func getImagesJSON(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
var (
buffer *bytes.Buffer
job = srv.Eng.Job("images")
)
job.Setenv("filter", r.Form.Get("filter"))
job.Setenv("all", r.Form.Get("all"))
if version >= 1.9 {
job.Stdout.Add(w)
} else {
buffer = bytes.NewBuffer(nil)
job.Stdout.Add(buffer)
}
if err := job.Run(); err != nil {
return err
}
if version < 1.9 { // Send as a valid JSON array
outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(buffer); err != nil {
return err
}
if version < 1.8 { // Convert to legacy format
outsLegacy := engine.NewTable("Created", 0)
for _, out := range outs.Data {
for _, repoTag := range out.GetList("RepoTags") {
parts := strings.Split(repoTag, ":")
outLegacy := &engine.Env{}
outLegacy.Set("Repository", parts[0])
outLegacy.Set("Tag", parts[1])
outLegacy.Set("ID", out.Get("ID"))
outLegacy.SetInt64("Created", out.GetInt64("Created"))
outLegacy.SetInt64("Size", out.GetInt64("Size"))
outLegacy.SetInt64("VirtualSize", out.GetInt64("VirtualSize"))
outsLegacy.Add(outLegacy)
}
}
if _, err := outsLegacy.WriteListTo(w); err != nil {
return err
}
} else if _, err := outs.WriteListTo(w); err != nil {
return err
}
}
return nil
}
示例4: Search
// Search queries the public registry for images matching the specified
// search terms, and returns the results.
//
// Argument syntax: search TERM
//
// Option environment:
// 'authConfig': json-encoded credentials to authenticate against the registry.
// The search extends to images only accessible via the credentials.
//
// 'metaHeaders': extra HTTP headers to include in the request to the registry.
// The headers should be passed as a json-encoded dictionary.
//
// Output:
// Results are sent as a collection of structured messages (using engine.Table).
// Each result is sent as a separate message.
// Results are ordered by number of stars on the public registry.
func (s *Service) Search(job *engine.Job) engine.Status {
if n := len(job.Args); n != 1 {
return job.Errorf("Usage: %s TERM", job.Name)
}
var (
term = job.Args[0]
metaHeaders = map[string][]string{}
authConfig = &AuthConfig{}
)
job.GetenvJson("authConfig", authConfig)
job.GetenvJson("metaHeaders", metaHeaders)
r, err := NewRegistry(authConfig, HTTPRequestFactory(metaHeaders), IndexServerAddress())
if err != nil {
return job.Error(err)
}
results, err := r.SearchRepositories(term)
if err != nil {
return job.Error(err)
}
outs := engine.NewTable("star_count", 0)
for _, result := range results.Results {
out := &engine.Env{}
out.Import(result)
outs.Add(out)
}
outs.ReverseSort()
if _, err := outs.WriteListTo(job.Stdout); err != nil {
return job.Error(err)
}
return engine.StatusOK
}
示例5: getImagesSearch
func getImagesSearch(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
var (
buffer *bytes.Buffer
job = srv.Eng.Job("search", r.Form.Get("term"))
)
if version >= 1.9 {
job.Stdout.Add(w)
} else {
buffer = bytes.NewBuffer(nil)
job.Stdout.Add(buffer)
}
if err := job.Run(); err != nil {
return err
}
if version < 1.9 { // Send as a valid JSON array
outs := engine.NewTable("", 0)
if _, err := outs.ReadFrom(buffer); err != nil {
return err
}
if _, err := outs.WriteListTo(w); err != nil {
return err
}
}
return nil
}
示例6: getImagesHistory
func getImagesHistory(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if vars == nil {
return fmt.Errorf("Missing parameter")
}
var (
buffer *bytes.Buffer
job = srv.Eng.Job("history", vars["name"])
)
if version >= 1.9 {
job.Stdout.Add(w)
} else {
buffer = bytes.NewBuffer(nil)
job.Stdout.Add(buffer)
}
if err := job.Run(); err != nil {
return err
}
if version < 1.9 { // Send as a valid JSON array
outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(buffer); err != nil {
return err
}
if _, err := outs.WriteListTo(w); err != nil {
return err
}
}
return nil
}
示例7: TestDeleteImages
func TestDeleteImages(t *testing.T) {
eng := NewTestEngine(t)
//we expect errors, so we disable stderr
eng.Stderr = ioutil.Discard
defer mkRuntimeFromEngine(eng, t).Nuke()
initialImages := getImages(eng, t, true, "")
if err := eng.Job("tag", unitTestImageName, "test", "test").Run(); err != nil {
t.Fatal(err)
}
images := getImages(eng, t, true, "")
if len(images.Data[0].GetList("RepoTags")) != len(initialImages.Data[0].GetList("RepoTags"))+1 {
t.Errorf("Expected %d images, %d found", len(initialImages.Data[0].GetList("RepoTags"))+1, len(images.Data[0].GetList("RepoTags")))
}
req, err := http.NewRequest("DELETE", "/images/"+unitTestImageID, nil)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
t.Fatal(err)
}
if r.Code != http.StatusConflict {
t.Fatalf("Expected http status 409-conflict, got %v", r.Code)
}
req2, err := http.NewRequest("DELETE", "/images/test:test", nil)
if err != nil {
t.Fatal(err)
}
r2 := httptest.NewRecorder()
if err := api.ServeRequest(eng, api.APIVERSION, r2, req2); err != nil {
t.Fatal(err)
}
assertHttpNotError(r2, t)
if r2.Code != http.StatusOK {
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
}
outs := engine.NewTable("Created", 0)
if _, err := outs.ReadListFrom(r2.Body.Bytes()); err != nil {
t.Fatal(err)
}
if len(outs.Data) != 1 {
t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs.Data))
}
images = getImages(eng, t, false, "")
if images.Len() != initialImages.Len() {
t.Errorf("Expected %d image, %d found", initialImages.Len(), images.Len())
}
}
示例8: dockerContainers
// docker主機上的容器
func dockerContainers(c *utils.Connection, data []byte) {
dst := engine.NewTable("", 0)
if _, err := dst.ReadListFrom(data); err != nil {
log.Println("Read table error:", err)
}
if content, err := dst.ToListString(); err != nil {
log.Println("Table to string error:", err)
} else {
log.Println("Containers:", content)
}
}
示例9: TestDeleteTagWithExistingContainers
// Regression test for being able to untag an image with an existing
// container
func TestDeleteTagWithExistingContainers(t *testing.T) {
eng := NewTestEngine(t)
defer nuke(mkRuntimeFromEngine(eng, t))
srv := mkServerFromEngine(eng, t)
// Tag the image
if err := eng.Job("tag", unitTestImageID, "utest", "tag1").Run(); err != nil {
t.Fatal(err)
}
// Create a container from the image
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil)
if err != nil {
t.Fatal(err)
}
id := createNamedTestContainer(eng, config, t, "testingtags")
if id == "" {
t.Fatal("No id returned")
}
job := srv.Eng.Job("containers")
job.SetenvBool("all", true)
outs, err := job.Stdout.AddListTable()
if err != nil {
t.Fatal(err)
}
if err := job.Run(); err != nil {
t.Fatal(err)
}
if len(outs.Data) != 1 {
t.Fatalf("Expected 1 container got %d", len(outs.Data))
}
// Try to remove the tag
imgs := engine.NewTable("", 0)
if err := srv.DeleteImage("utest:tag1", imgs, true, false, false); err != nil {
t.Fatal(err)
}
if len(imgs.Data) != 1 {
t.Fatalf("Should only have deleted one untag %d", len(imgs.Data))
}
if untag := imgs.Data[0].Get("Untagged"); untag != "utest:tag1" {
t.Fatalf("Expected %s got %s", unitTestImageID, untag)
}
}
示例10: getImagesJSON
func getImagesJSON(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
var (
err error
outs *engine.Table
job = eng.Job("images")
)
job.Setenv("filters", r.Form.Get("filters"))
// FIXME this parameter could just be a match filter
job.Setenv("filter", r.Form.Get("filter"))
job.Setenv("all", r.Form.Get("all"))
if version.GreaterThanOrEqualTo("1.7") {
streamJSON(job, w, false)
} else if outs, err = job.Stdout.AddListTable(); err != nil {
return err
}
if err := job.Run(); err != nil {
return err
}
if version.LessThan("1.7") && outs != nil { // Convert to legacy format
outsLegacy := engine.NewTable("Created", 0)
for _, out := range outs.Data {
for _, repoTag := range out.GetList("RepoTags") {
parts := strings.Split(repoTag, ":")
outLegacy := &engine.Env{}
outLegacy.Set("Repository", parts[0])
outLegacy.Set("Tag", parts[1])
outLegacy.Set("Id", out.Get("Id"))
outLegacy.SetInt64("Created", out.GetInt64("Created"))
outLegacy.SetInt64("Size", out.GetInt64("Size"))
outLegacy.SetInt64("VirtualSize", out.GetInt64("VirtualSize"))
outsLegacy.Add(outLegacy)
}
}
w.Header().Set("Content-Type", "application/json")
if _, err := outsLegacy.WriteListTo(w); err != nil {
return err
}
}
return nil
}
示例11: dockerImages
// docker主機上的鏡像
func dockerImages(c *utils.Connection, data []byte) {
dst := engine.NewTable("", 0)
if _, err := dst.ReadListFrom(data); err != nil {
log.Println("Read table error:", err)
}
for _, env := range dst.Data {
created, err := strconv.ParseInt(env.Get("Created"), 10, 64)
if err == nil {
image := &resource.Image{Host: c.Src, Created: created}
registry.RegistryServer.RegisterImage(env.Get("Id"), image)
log.Printf("Register image id:%s host:%s", env.Get("Id"), c.Src)
} else {
log.Printf("Parse image created time error:%s", err)
}
}
}
示例12: TestGetContainersJSON
func TestGetContainersJSON(t *testing.T) {
eng := NewTestEngine(t)
defer mkRuntimeFromEngine(eng, t).Nuke()
srv := mkServerFromEngine(eng, t)
job := eng.Job("containers")
job.SetenvBool("all", true)
outs, err := job.Stdout.AddTable()
if err != nil {
t.Fatal(err)
}
if err := job.Run(); err != nil {
t.Fatal(err)
}
beginLen := len(outs.Data)
containerID := createTestContainer(eng, &docker.Config{
Image: unitTestImageID,
Cmd: []string{"echo", "test"},
}, t)
if containerID == "" {
t.Fatalf("Received empty container ID")
}
req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
if err := docker.ServeRequest(srv, docker.APIVERSION, r, req); err != nil {
t.Fatal(err)
}
assertHttpNotError(r, t)
containers := engine.NewTable("", 0)
if _, err := containers.ReadListFrom(r.Body.Bytes()); err != nil {
t.Fatal(err)
}
if len(containers.Data) != beginLen+1 {
t.Fatalf("Expected %d container, %d found (started with: %d)", beginLen+1, len(containers.Data), beginLen)
}
if id := containers.Data[0].Get("ID"); id != containerID {
t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", containerID, id)
}
}
示例13: getImagesJSON
func getImagesJSON(eng *engine.Engine, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
var (
err error
outs *engine.Table
job = eng.Job("images")
)
job.Setenv("filter", r.Form.Get("filter"))
job.Setenv("all", r.Form.Get("all"))
if version >= 1.7 {
job.Stdout.Add(w)
} else if outs, err = job.Stdout.AddListTable(); err != nil {
return err
}
if err := job.Run(); err != nil {
return err
}
if version < 1.7 && outs != nil { // Convert to legacy format
outsLegacy := engine.NewTable("Created", 0)
for _, out := range outs.Data {
for _, repoTag := range out.GetList("RepoTags") {
parts := strings.Split(repoTag, ":")
outLegacy := &engine.Env{}
outLegacy.Set("Repository", parts[0])
outLegacy.Set("Tag", parts[1])
outLegacy.Set("Id", out.Get("Id"))
outLegacy.SetInt64("Created", out.GetInt64("Created"))
outLegacy.SetInt64("Size", out.GetInt64("Size"))
outLegacy.SetInt64("VirtualSize", out.GetInt64("VirtualSize"))
outsLegacy.Add(outLegacy)
}
}
if _, err := outsLegacy.WriteListTo(w); err != nil {
return err
}
}
return nil
}
示例14: ls
func (b *dockerClientBackend) ls() ([]string, error) {
resp, err := b.client.call("GET", "/containers/json", "")
if err != nil {
return nil, fmt.Errorf("get: %v", err)
}
// FIXME: check for response error
c := engine.NewTable("Created", 0)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read body: %v", err)
}
if _, err := c.ReadListFrom(body); err != nil {
return nil, fmt.Errorf("readlist: %v", err)
}
names := []string{}
for _, env := range c.Data {
names = append(names, env.GetList("Names")[0][1:])
}
return names, nil
}
示例15: TestGetContainersChanges
func TestGetContainersChanges(t *testing.T) {
eng := NewTestEngine(t)
defer mkRuntimeFromEngine(eng, t).Nuke()
srv := mkServerFromEngine(eng, t)
// Create a container and remove a file
containerID := createTestContainer(eng,
&docker.Config{
Image: unitTestImageID,
Cmd: []string{"/bin/rm", "/etc/passwd"},
},
t,
)
containerRun(eng, containerID, t)
r := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/containers/"+containerID+"/changes", nil)
if err != nil {
t.Fatal(err)
}
if err := docker.ServeRequest(srv, docker.APIVERSION, r, req); err != nil {
t.Fatal(err)
}
assertHttpNotError(r, t)
outs := engine.NewTable("", 0)
if _, err := outs.ReadListFrom(r.Body.Bytes()); err != nil {
t.Fatal(err)
}
// Check the changelog
success := false
for _, elem := range outs.Data {
if elem.Get("Path") == "/etc/passwd" && elem.GetInt("Kind") == 2 {
success = true
}
}
if !success {
t.Fatalf("/etc/passwd as been removed but is not present in the diff")
}
}