github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_api_inspect_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/Prakhar-Agarwal-byte/moby/api/types"
     9  	"github.com/Prakhar-Agarwal-byte/moby/api/types/versions/v1p20"
    10  	"github.com/Prakhar-Agarwal-byte/moby/client"
    11  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/cli"
    12  	"github.com/Prakhar-Agarwal-byte/moby/testutil"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func (s *DockerAPISuite) TestInspectAPIContainerResponse(c *testing.T) {
    18  	out := cli.DockerCmd(c, "run", "-d", "busybox", "true").Stdout()
    19  	cleanedContainerID := strings.TrimSpace(out)
    20  
    21  	keysBase := []string{
    22  		"Id", "State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings",
    23  		"ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "MountLabel", "ProcessLabel", "GraphDriver",
    24  	}
    25  
    26  	type acase struct {
    27  		version string
    28  		keys    []string
    29  	}
    30  
    31  	var cases []acase
    32  
    33  	if testEnv.DaemonInfo.OSType == "windows" {
    34  		cases = []acase{
    35  			{"v1.25", append(keysBase, "Mounts")},
    36  		}
    37  	} else {
    38  		cases = []acase{
    39  			{"v1.20", append(keysBase, "Mounts")},
    40  			{"v1.19", append(keysBase, "Volumes", "VolumesRW")},
    41  		}
    42  	}
    43  
    44  	for _, cs := range cases {
    45  		body := getInspectBody(c, cs.version, cleanedContainerID)
    46  
    47  		var inspectJSON map[string]interface{}
    48  		err := json.Unmarshal(body, &inspectJSON)
    49  		assert.NilError(c, err, "Unable to unmarshal body for version %s", cs.version)
    50  
    51  		for _, key := range cs.keys {
    52  			_, ok := inspectJSON[key]
    53  			assert.Check(c, ok, "%s does not exist in response for version %s", key, cs.version)
    54  		}
    55  
    56  		// Issue #6830: type not properly converted to JSON/back
    57  		_, ok := inspectJSON["Path"].(bool)
    58  		assert.Assert(c, !ok, "Path of `true` should not be converted to boolean `true` via JSON marshalling")
    59  	}
    60  }
    61  
    62  func (s *DockerAPISuite) TestInspectAPIContainerVolumeDriverLegacy(c *testing.T) {
    63  	// No legacy implications for Windows
    64  	testRequires(c, DaemonIsLinux)
    65  	out := cli.DockerCmd(c, "run", "-d", "busybox", "true").Stdout()
    66  	cleanedContainerID := strings.TrimSpace(out)
    67  
    68  	cases := []string{"v1.19", "v1.20"}
    69  	for _, version := range cases {
    70  		body := getInspectBody(c, version, cleanedContainerID)
    71  
    72  		var inspectJSON map[string]interface{}
    73  		err := json.Unmarshal(body, &inspectJSON)
    74  		assert.NilError(c, err, "Unable to unmarshal body for version %s", version)
    75  
    76  		config, ok := inspectJSON["Config"]
    77  		assert.Assert(c, ok, "Unable to find 'Config'")
    78  		cfg := config.(map[string]interface{})
    79  		_, ok = cfg["VolumeDriver"]
    80  		assert.Assert(c, ok, "API version %s expected to include VolumeDriver in 'Config'", version)
    81  	}
    82  }
    83  
    84  func (s *DockerAPISuite) TestInspectAPIContainerVolumeDriver(c *testing.T) {
    85  	out := cli.DockerCmd(c, "run", "-d", "--volume-driver", "local", "busybox", "true").Stdout()
    86  	cleanedContainerID := strings.TrimSpace(out)
    87  
    88  	body := getInspectBody(c, "v1.25", cleanedContainerID)
    89  
    90  	var inspectJSON map[string]interface{}
    91  	err := json.Unmarshal(body, &inspectJSON)
    92  	assert.NilError(c, err, "Unable to unmarshal body for version 1.25")
    93  
    94  	config, ok := inspectJSON["Config"]
    95  	assert.Assert(c, ok, "Unable to find 'Config'")
    96  	cfg := config.(map[string]interface{})
    97  	_, ok = cfg["VolumeDriver"]
    98  	assert.Assert(c, !ok, "API version 1.25 expected to not include VolumeDriver in 'Config'")
    99  
   100  	config, ok = inspectJSON["HostConfig"]
   101  	assert.Assert(c, ok, "Unable to find 'HostConfig'")
   102  	cfg = config.(map[string]interface{})
   103  	_, ok = cfg["VolumeDriver"]
   104  	assert.Assert(c, ok, "API version 1.25 expected to include VolumeDriver in 'HostConfig'")
   105  }
   106  
   107  func (s *DockerAPISuite) TestInspectAPIImageResponse(c *testing.T) {
   108  	cli.DockerCmd(c, "tag", "busybox:latest", "busybox:mytag")
   109  	apiClient, err := client.NewClientWithOpts(client.FromEnv)
   110  	assert.NilError(c, err)
   111  	defer apiClient.Close()
   112  
   113  	imageJSON, _, err := apiClient.ImageInspectWithRaw(testutil.GetContext(c), "busybox")
   114  	assert.NilError(c, err)
   115  
   116  	assert.Check(c, len(imageJSON.RepoTags) == 2)
   117  	assert.Check(c, is.Contains(imageJSON.RepoTags, "busybox:latest"))
   118  	assert.Check(c, is.Contains(imageJSON.RepoTags, "busybox:mytag"))
   119  }
   120  
   121  // #17131, #17139, #17173
   122  func (s *DockerAPISuite) TestInspectAPIEmptyFieldsInConfigPre121(c *testing.T) {
   123  	// Not relevant on Windows
   124  	testRequires(c, DaemonIsLinux)
   125  	out := cli.DockerCmd(c, "run", "-d", "busybox", "true").Stdout()
   126  	cleanedContainerID := strings.TrimSpace(out)
   127  
   128  	cases := []string{"v1.19", "v1.20"}
   129  	for _, version := range cases {
   130  		body := getInspectBody(c, version, cleanedContainerID)
   131  
   132  		var inspectJSON map[string]interface{}
   133  		err := json.Unmarshal(body, &inspectJSON)
   134  		assert.NilError(c, err, "Unable to unmarshal body for version %s", version)
   135  		config, ok := inspectJSON["Config"]
   136  		assert.Assert(c, ok, "Unable to find 'Config'")
   137  		cfg := config.(map[string]interface{})
   138  		for _, f := range []string{"MacAddress", "NetworkDisabled", "ExposedPorts"} {
   139  			_, ok := cfg[f]
   140  			assert.Check(c, ok, "API version %s expected to include %s in 'Config'", version, f)
   141  		}
   142  	}
   143  }
   144  
   145  func (s *DockerAPISuite) TestInspectAPIBridgeNetworkSettings120(c *testing.T) {
   146  	// Not relevant on Windows, and besides it doesn't have any bridge network settings
   147  	testRequires(c, DaemonIsLinux)
   148  	out := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
   149  	containerID := strings.TrimSpace(out)
   150  	cli.WaitRun(c, containerID)
   151  
   152  	body := getInspectBody(c, "v1.20", containerID)
   153  
   154  	var inspectJSON v1p20.ContainerJSON
   155  	err := json.Unmarshal(body, &inspectJSON)
   156  	assert.NilError(c, err)
   157  
   158  	settings := inspectJSON.NetworkSettings
   159  	assert.Assert(c, len(settings.IPAddress) != 0)
   160  }
   161  
   162  func (s *DockerAPISuite) TestInspectAPIBridgeNetworkSettings121(c *testing.T) {
   163  	// Windows doesn't have any bridge network settings
   164  	testRequires(c, DaemonIsLinux)
   165  	out := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
   166  	containerID := strings.TrimSpace(out)
   167  	cli.WaitRun(c, containerID)
   168  
   169  	body := getInspectBody(c, "v1.21", containerID)
   170  
   171  	var inspectJSON types.ContainerJSON
   172  	err := json.Unmarshal(body, &inspectJSON)
   173  	assert.NilError(c, err)
   174  
   175  	settings := inspectJSON.NetworkSettings
   176  	assert.Assert(c, len(settings.IPAddress) != 0)
   177  	assert.Assert(c, settings.Networks["bridge"] != nil)
   178  	assert.Equal(c, settings.IPAddress, settings.Networks["bridge"].IPAddress)
   179  }