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

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