github.com/brianfoshee/docker@v1.6.0/integration-cli/docker_api_inspect_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"os/exec"
     6  	"testing"
     7  )
     8  
     9  func TestInspectApiContainerResponse(t *testing.T) {
    10  	defer deleteAllContainers()
    11  
    12  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
    13  	out, _, err := runCommandWithOutput(runCmd)
    14  	if err != nil {
    15  		t.Fatalf("failed to create a container: %s, %v", out, err)
    16  	}
    17  
    18  	cleanedContainerID := stripTrailingCharacters(out)
    19  
    20  	// test on json marshal version
    21  	// and latest version
    22  	testVersions := []string{"v1.11", "latest"}
    23  
    24  	for _, testVersion := range testVersions {
    25  		endpoint := "/containers/" + cleanedContainerID + "/json"
    26  		if testVersion != "latest" {
    27  			endpoint = "/" + testVersion + endpoint
    28  		}
    29  		body, err := sockRequest("GET", endpoint, nil)
    30  		if err != nil {
    31  			t.Fatalf("sockRequest failed for %s version: %v", testVersion, err)
    32  		}
    33  
    34  		var inspectJSON map[string]interface{}
    35  		if err = json.Unmarshal(body, &inspectJSON); err != nil {
    36  			t.Fatalf("unable to unmarshal body for %s version: %v", testVersion, err)
    37  		}
    38  
    39  		keys := []string{"State", "Created", "Path", "Args", "Config", "Image", "NetworkSettings", "ResolvConfPath", "HostnamePath", "HostsPath", "LogPath", "Name", "Driver", "ExecDriver", "MountLabel", "ProcessLabel", "Volumes", "VolumesRW"}
    40  
    41  		if testVersion == "v1.11" {
    42  			keys = append(keys, "ID")
    43  		} else {
    44  			keys = append(keys, "Id")
    45  		}
    46  
    47  		for _, key := range keys {
    48  			if _, ok := inspectJSON[key]; !ok {
    49  				t.Fatalf("%s does not exist in reponse for %s version", key, testVersion)
    50  			}
    51  		}
    52  		//Issue #6830: type not properly converted to JSON/back
    53  		if _, ok := inspectJSON["Path"].(bool); ok {
    54  			t.Fatalf("Path of `true` should not be converted to boolean `true` via JSON marshalling")
    55  		}
    56  	}
    57  
    58  	logDone("container json - check keys in container json response")
    59  }