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