Skip to content

Commit

Permalink
Allow go template to work properly with inspect
Browse files Browse the repository at this point in the history
Closes moby#11641

Signed-off-by: Srini Brahmaroutu <srbrahma@us.ibm.com>
  • Loading branch information
brahmaroutu committed Apr 1, 2015
1 parent 3f77f62 commit c1c033d
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 72 deletions.
34 changes: 25 additions & 9 deletions api/client/inspect.go
Expand Up @@ -9,12 +9,14 @@ import (
"text/template"

flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/utils"
)

// CmdInspect displays low-level information on one or more containers or images.
//
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

func (cli *DockerCli) CmdInspect(args ...string) error {
cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true)
tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
Expand All @@ -35,11 +37,13 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
indented := new(bytes.Buffer)
indented.WriteByte('[')
status := 0
isImage := false

for _, name := range cmd.Args() {
obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
if err != nil {
obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
isImage = true
if err != nil {
if strings.Contains(err.Error(), "No such") {
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
Expand All @@ -58,16 +62,28 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
continue
}
} else {
// Has template, will render
var value interface{}
if err := json.Unmarshal(obj, &value); err != nil {
fmt.Fprintf(cli.err, "%s\n", err)
status = 1
continue
}
if err := tmpl.Execute(cli.out, value); err != nil {
return err
if isImage {
inspPtr := runconfig.InspectImage{}
if err := json.Unmarshal(obj, &inspPtr); err != nil {
fmt.Fprintf(cli.err, "Failed to unmarshal data into a data structure. %s\n", err)
status = 1
continue
}
if err := tmpl.Execute(cli.out, inspPtr); err != nil {
return fmt.Errorf("Failed when applying the formatter on the incoming data. %s", err.Error())
}
} else {
inspPtr := runconfig.InspectContainer{}
if err := json.Unmarshal(obj, &inspPtr); err != nil {
fmt.Fprintf(cli.err, "Failed to unmarshal data into a data structure. %s\n", err)
status = 1
continue
}
if err := tmpl.Execute(cli.out, inspPtr); err != nil {
return fmt.Errorf("Failed when applying the formatter on the incoming data. %s", err.Error())
}
}

cli.out.Write([]byte{'\n'})
}
indented.WriteString(",")
Expand Down
77 changes: 53 additions & 24 deletions daemon/inspect.go
Expand Up @@ -32,30 +32,54 @@ func (daemon *Daemon) ContainerInspect(job *engine.Job) error {
return nil
}

out := &engine.Env{}
out.SetJson("Id", container.ID)
out.SetAuto("Created", container.Created)
out.SetJson("Path", container.Path)
out.SetList("Args", container.Args)
out.SetJson("Config", container.Config)
out.SetJson("State", container.State)
out.Set("Image", container.ImageID)
out.SetJson("NetworkSettings", container.NetworkSettings)
out.Set("ResolvConfPath", container.ResolvConfPath)
out.Set("HostnamePath", container.HostnamePath)
out.Set("HostsPath", container.HostsPath)
out.Set("LogPath", container.LogPath)
out.SetJson("Name", container.Name)
out.SetInt("RestartCount", container.RestartCount)
out.Set("Driver", container.Driver)
out.Set("ExecDriver", container.ExecDriver)
out.Set("MountLabel", container.MountLabel)
out.Set("ProcessLabel", container.ProcessLabel)
out.SetJson("Volumes", container.Volumes)
out.SetJson("VolumesRW", container.VolumesRW)
out.SetJson("AppArmorProfile", container.AppArmorProfile)
var value runconfig.InspectContainer

out.SetList("ExecIDs", container.GetExecIDs())
value.Id = container.ID
value.Created = container.Created
{
val, err := json.Marshal(container.Path)
if err != nil {
return err
}
value.Path = string(val)
}
value.Args = container.Args
value.Config = *container.Config

value.State = (*container.State).State
value.Image = container.ImageID
{
val, err := json.Marshal(container.Path)
if err != nil {
return err
}
value.Path = string(val)
}
value.NetworkSettings = runconfig.NetworkSettings(*container.NetworkSettings)
value.ResolvConfPath = container.ResolvConfPath
value.HostnamePath = container.HostnamePath
value.HostsPath = container.HostsPath
value.LogPath = container.LogPath
value.Name = container.Name
value.RestartCount = container.RestartCount
value.Driver = container.Driver
value.ExecDriver = container.ExecDriver
value.MountLabel = container.MountLabel
value.ProcessLabel = container.ProcessLabel
value.Volumes = map[string]string{}
value.VolumesRW = map[string]bool{}
if len(container.Volumes) > 0 {
value.Volumes = container.Volumes
}
if len(container.VolumesRW) > 0 {
value.VolumesRW = container.VolumesRW
}
value.AppArmorProfile = container.AppArmorProfile
if len(container.GetExecIDs()) > 0 {
value.ExecIDs = container.GetExecIDs()
} else {
value.ExecIDs = nil
}

if children, err := daemon.Children(container.Name); err == nil {
for linkAlias, child := range children {
Expand All @@ -71,9 +95,14 @@ func (daemon *Daemon) ContainerInspect(job *engine.Job) error {
}()
}

out.SetJson("HostConfig", container.hostConfig)
value.HostConfig = *container.hostConfig

container.hostConfig.Links = nil
out := &engine.Env{}
if err := out.Import(value); err != nil {
return err
}

if _, err := out.WriteTo(job.Stdout); err != nil {
return err
}
Expand Down
16 changes: 2 additions & 14 deletions daemon/network_settings.go
Expand Up @@ -3,25 +3,13 @@ package daemon
import (
"github.com/docker/docker/engine"
"github.com/docker/docker/nat"
"github.com/docker/docker/runconfig"
)

// FIXME: move deprecated port stuff to nat to clean up the core.
type PortMapping map[string]string // Deprecated

type NetworkSettings struct {
IPAddress string
IPPrefixLen int
MacAddress string
LinkLocalIPv6Address string
LinkLocalIPv6PrefixLen int
GlobalIPv6Address string
GlobalIPv6PrefixLen int
Gateway string
IPv6Gateway string
Bridge string
PortMapping map[string]PortMapping // Deprecated
Ports nat.PortMap
}
type NetworkSettings runconfig.NetworkSettings

func (settings *NetworkSettings) PortMappingAPI() *engine.Table {
var outs = engine.NewTable("", 0)
Expand Down
14 changes: 2 additions & 12 deletions daemon/state.go
Expand Up @@ -2,26 +2,16 @@ package daemon

import (
"fmt"
"sync"
"time"

"github.com/docker/docker/daemon/execdriver"
"github.com/docker/docker/pkg/units"
"github.com/docker/docker/runconfig"
)

type State struct {
sync.Mutex
Running bool
Paused bool
Restarting bool
OOMKilled bool
runconfig.State
removalInProgress bool // Not need for this to be persistent on disk.
Dead bool
Pid int
ExitCode int
Error string // contains last known error when starting the container
StartedAt time.Time
FinishedAt time.Time
waitChan chan struct{}
}

Expand Down
15 changes: 8 additions & 7 deletions integration-cli/docker_cli_build_test.go
Expand Up @@ -2436,7 +2436,7 @@ func TestBuildCmd(t *testing.T) {

func TestBuildExpose(t *testing.T) {
name := "testbuildexpose"
expected := "map[2375/tcp:map[]]"
expected := "map[2375/tcp:{}]"
defer deleteImages(name)
_, err := buildImage(name,
`FROM scratch
Expand Down Expand Up @@ -2536,7 +2536,7 @@ func TestBuildExposeOrder(t *testing.T) {

func TestBuildExposeUpperCaseProto(t *testing.T) {
name := "testbuildexposeuppercaseproto"
expected := "map[5678/udp:map[]]"
expected := "map[5678/udp:{}]"
defer deleteImages(name)
_, err := buildImage(name,
`FROM scratch
Expand All @@ -2558,7 +2558,7 @@ func TestBuildExposeUpperCaseProto(t *testing.T) {
func TestBuildExposeHostPort(t *testing.T) {
// start building docker file with ip:hostPort:containerPort
name := "testbuildexpose"
expected := "map[5678/tcp:map[]]"
expected := "map[5678/tcp:{}]"
defer deleteImages(name)
_, out, err := buildImageWithOut(name,
`FROM scratch
Expand Down Expand Up @@ -3390,8 +3390,8 @@ func TestBuildEntrypointRunCleanup(t *testing.T) {
t.Fatal(err)
}
// Cmd must be cleaned up
if expected := "<no value>"; res != expected {
t.Fatalf("Cmd %s, expected %s", res, expected)
if res != "[]" {
t.Fatalf("Cmd %s, expected []", res)
}
logDone("build - cleanup cmd after RUN")
}
Expand Down Expand Up @@ -4346,9 +4346,10 @@ func TestBuildCleanupCmdOnEntrypoint(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if expected := "<no value>"; res != expected {
t.Fatalf("Cmd %s, expected %s", res, expected)
if res != "[]" {
t.Fatalf("Cmd %s, expected []", res)
}

res, err = inspectField(name, "Config.Entrypoint")
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion integration-cli/docker_cli_commit_test.go
Expand Up @@ -263,7 +263,7 @@ func TestCommitChange(t *testing.T) {
defer deleteImages(imageId)

expected := map[string]string{
"Config.ExposedPorts": "map[8080/tcp:map[]]",
"Config.ExposedPorts": "map[8080/tcp:{}]",
"Config.Env": "[DEBUG=true test=1 PATH=/foo]",
}

Expand Down
6 changes: 3 additions & 3 deletions integration-cli/docker_cli_exec_test.go
Expand Up @@ -474,8 +474,8 @@ func TestInspectExecID(t *testing.T) {
if err != nil {
t.Fatalf("failed to inspect container: %s, %v", out, err)
}
if out != "<no value>" {
t.Fatalf("ExecIDs should be empty, got: %s", out)
if out != "[]" {
t.Fatalf("ExecIDs should be [], got: %s", out)
}

exitCode, err = runCommand(exec.Command(dockerBinary, "exec", "-d", id, "ls", "/"))
Expand All @@ -490,7 +490,7 @@ func TestInspectExecID(t *testing.T) {

out = strings.TrimSuffix(out, "\n")
if out == "[]" || out == "<no value>" {
t.Fatalf("ExecIDs should not be empty, got: %s", out)
t.Fatalf("ExecIDs should not be empty, got: '%s'", out)
}

logDone("inspect - inspect a container with ExecIDs")
Expand Down
63 changes: 63 additions & 0 deletions integration-cli/docker_cli_inspect_test.go
@@ -1,7 +1,9 @@
package main

import (
"fmt"
"os/exec"
"strconv"
"strings"
"testing"
)
Expand All @@ -21,3 +23,64 @@ func TestInspectImage(t *testing.T) {

logDone("inspect - inspect an image")
}

func TestInspectImageFilterInt(t *testing.T) {
imageTest := "emptyfs"
imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Size}}'", imageTest)
out, exitCode, err := runCommandWithOutput(imagesCmd)
if exitCode != 0 || err != nil {
t.Fatalf("failed to inspect image: %s, %v", out, err)
}
size, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
if err != nil {
t.Fatalf("failed to inspect size of the image: %s, %v", out, err)
}

//now see if the size turns out to be the same
formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size)
imagesCmd = exec.Command(dockerBinary, "inspect", formatStr, imageTest)
out, exitCode, err = runCommandWithOutput(imagesCmd)
if exitCode != 0 || err != nil {
t.Fatalf("failed to inspect image: %s, %v", out, err)
}
if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result {
t.Fatalf("Expected size: %s for image: %s but received size: %s", size, imageTest, strings.TrimSuffix(out, "\n"))
}

logDone("inspect - inspect an image test filter integer")
}

func TestInspectContainerFilterInt(t *testing.T) {
defer deleteAllContainers()

runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil {
t.Fatalf("failed to run container: %v, output: %q", err, out)
}

id := stripTrailingCharacters(out)

runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.ExitCode}}'", id)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf("failed to inspect container: %s, %v", out, err)
}
exitCode, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
if err != nil {
t.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err)
}

//now get the exit code to verify
formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode)
runCmd = exec.Command(dockerBinary, "inspect", formatStr, id)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf("failed to inspect container: %s, %v", out, err)
}
if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result {
t.Fatalf("Expected exitcode: %d for container: %s", exitCode, id)
}

logDone("inspect - inspect a container test filter integer")
}
4 changes: 2 additions & 2 deletions integration-cli/docker_cli_run_test.go
Expand Up @@ -2818,7 +2818,7 @@ func TestRunVolumesCleanPaths(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if out != "<no value>" {
if out != "" {
t.Fatalf("Found unexpected volume entry for '/foo/' in volumes\n%q", out)
}

Expand All @@ -2834,7 +2834,7 @@ func TestRunVolumesCleanPaths(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if out != "<no value>" {
if out != "" {
t.Fatalf("Found unexpected volume entry for '/bar/' in volumes\n%q", out)
}
out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar")
Expand Down

0 comments on commit c1c033d

Please sign in to comment.