github.com/fntlnz/docker@v1.9.0-rc3/integration-cli/docker_api_inspect_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/pkg/stringutils"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  func (s *DockerSuite) TestInspectApiContainerResponse(c *check.C) {
    15  	testRequires(c, DaemonIsLinux)
    16  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
    17  
    18  	cleanedContainerID := strings.TrimSpace(out)
    19  	keysBase := []string{"Id", "State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings",
    20  		"ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "GraphDriver"}
    21  
    22  	cases := []struct {
    23  		version string
    24  		keys    []string
    25  	}{
    26  		{"1.20", append(keysBase, "Mounts")},
    27  		{"1.19", append(keysBase, "Volumes", "VolumesRW")},
    28  	}
    29  
    30  	for _, cs := range cases {
    31  		endpoint := fmt.Sprintf("/v%s/containers/%s/json", cs.version, cleanedContainerID)
    32  
    33  		status, body, err := sockRequest("GET", endpoint, nil)
    34  		c.Assert(err, check.IsNil)
    35  		c.Assert(status, check.Equals, http.StatusOK)
    36  
    37  		var inspectJSON map[string]interface{}
    38  		if err = json.Unmarshal(body, &inspectJSON); err != nil {
    39  			c.Fatalf("unable to unmarshal body for version %s: %v", cs.version, err)
    40  		}
    41  
    42  		for _, key := range cs.keys {
    43  			if _, ok := inspectJSON[key]; !ok {
    44  				c.Fatalf("%s does not exist in response for version %s", key, cs.version)
    45  			}
    46  		}
    47  
    48  		//Issue #6830: type not properly converted to JSON/back
    49  		if _, ok := inspectJSON["Path"].(bool); ok {
    50  			c.Fatalf("Path of `true` should not be converted to boolean `true` via JSON marshalling")
    51  		}
    52  	}
    53  }
    54  
    55  func (s *DockerSuite) TestInspectApiContainerVolumeDriverLegacy(c *check.C) {
    56  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
    57  
    58  	cleanedContainerID := strings.TrimSpace(out)
    59  
    60  	cases := []string{"1.19", "1.20"}
    61  	for _, version := range cases {
    62  		endpoint := fmt.Sprintf("/v%s/containers/%s/json", version, cleanedContainerID)
    63  		status, body, err := sockRequest("GET", endpoint, nil)
    64  		c.Assert(err, check.IsNil)
    65  		c.Assert(status, check.Equals, http.StatusOK)
    66  
    67  		var inspectJSON map[string]interface{}
    68  		if err = json.Unmarshal(body, &inspectJSON); err != nil {
    69  			c.Fatalf("unable to unmarshal body for version %s: %v", version, err)
    70  		}
    71  
    72  		config, ok := inspectJSON["Config"]
    73  		if !ok {
    74  			c.Fatal("Unable to find 'Config'")
    75  		}
    76  		cfg := config.(map[string]interface{})
    77  		if _, ok := cfg["VolumeDriver"]; !ok {
    78  			c.Fatalf("Api version %s expected to include VolumeDriver in 'Config'", version)
    79  		}
    80  	}
    81  }
    82  
    83  func (s *DockerSuite) TestInspectApiContainerVolumeDriver(c *check.C) {
    84  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
    85  
    86  	cleanedContainerID := strings.TrimSpace(out)
    87  
    88  	endpoint := fmt.Sprintf("/v1.21/containers/%s/json", cleanedContainerID)
    89  	status, body, err := sockRequest("GET", endpoint, nil)
    90  	c.Assert(err, check.IsNil)
    91  	c.Assert(status, check.Equals, http.StatusOK)
    92  
    93  	var inspectJSON map[string]interface{}
    94  	if err = json.Unmarshal(body, &inspectJSON); err != nil {
    95  		c.Fatalf("unable to unmarshal body for version 1.21: %v", err)
    96  	}
    97  
    98  	config, ok := inspectJSON["Config"]
    99  	if !ok {
   100  		c.Fatal("Unable to find 'Config'")
   101  	}
   102  	cfg := config.(map[string]interface{})
   103  	if _, ok := cfg["VolumeDriver"]; ok {
   104  		c.Fatal("Api version 1.21 expected to not include VolumeDriver in 'Config'")
   105  	}
   106  
   107  	config, ok = inspectJSON["HostConfig"]
   108  	if !ok {
   109  		c.Fatal("Unable to find 'HostConfig'")
   110  	}
   111  	cfg = config.(map[string]interface{})
   112  	if _, ok := cfg["VolumeDriver"]; !ok {
   113  		c.Fatal("Api version 1.21 expected to include VolumeDriver in 'HostConfig'")
   114  	}
   115  }
   116  
   117  func (s *DockerSuite) TestInspectApiImageResponse(c *check.C) {
   118  	dockerCmd(c, "tag", "busybox:latest", "busybox:mytag")
   119  
   120  	endpoint := "/images/busybox/json"
   121  	status, body, err := sockRequest("GET", endpoint, nil)
   122  
   123  	c.Assert(err, check.IsNil)
   124  	c.Assert(status, check.Equals, http.StatusOK)
   125  
   126  	var imageJSON types.ImageInspect
   127  	if err = json.Unmarshal(body, &imageJSON); err != nil {
   128  		c.Fatalf("unable to unmarshal body for latest version: %v", err)
   129  	}
   130  
   131  	c.Assert(len(imageJSON.RepoTags), check.Equals, 2)
   132  
   133  	c.Assert(stringutils.InSlice(imageJSON.RepoTags, "busybox:latest"), check.Equals, true)
   134  	c.Assert(stringutils.InSlice(imageJSON.RepoTags, "busybox:mytag"), check.Equals, true)
   135  }
   136  
   137  // #17131, #17139, #17173
   138  func (s *DockerSuite) TestInspectApiEmptyFieldsInConfigPre121(c *check.C) {
   139  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
   140  
   141  	cleanedContainerID := strings.TrimSpace(out)
   142  
   143  	cases := []string{"1.19", "1.20"}
   144  	for _, version := range cases {
   145  		endpoint := fmt.Sprintf("/v%s/containers/%s/json", version, cleanedContainerID)
   146  		status, body, err := sockRequest("GET", endpoint, nil)
   147  		c.Assert(err, check.IsNil)
   148  		c.Assert(status, check.Equals, http.StatusOK)
   149  
   150  		var inspectJSON map[string]interface{}
   151  		if err = json.Unmarshal(body, &inspectJSON); err != nil {
   152  			c.Fatalf("unable to unmarshal body for version %s: %v", version, err)
   153  		}
   154  
   155  		config, ok := inspectJSON["Config"]
   156  		if !ok {
   157  			c.Fatal("Unable to find 'Config'")
   158  		}
   159  		cfg := config.(map[string]interface{})
   160  		for _, f := range []string{"MacAddress", "NetworkDisabled", "ExposedPorts"} {
   161  			if _, ok := cfg[f]; !ok {
   162  				c.Fatalf("Api version %s expected to include %s in 'Config'", version, f)
   163  			}
   164  		}
   165  	}
   166  }