github.com/akerouanton/docker@v1.11.0-rc3/runconfig/config_test.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/engine-api/types/container"
    13  	networktypes "github.com/docker/engine-api/types/network"
    14  	"github.com/docker/engine-api/types/strslice"
    15  )
    16  
    17  type f struct {
    18  	file       string
    19  	entrypoint strslice.StrSlice
    20  }
    21  
    22  func TestDecodeContainerConfig(t *testing.T) {
    23  
    24  	var (
    25  		fixtures []f
    26  		image    string
    27  	)
    28  
    29  	if runtime.GOOS != "windows" {
    30  		image = "ubuntu"
    31  		fixtures = []f{
    32  			{"fixtures/unix/container_config_1_14.json", strslice.StrSlice{}},
    33  			{"fixtures/unix/container_config_1_17.json", strslice.StrSlice{"bash"}},
    34  			{"fixtures/unix/container_config_1_19.json", strslice.StrSlice{"bash"}},
    35  		}
    36  	} else {
    37  		image = "windows"
    38  		fixtures = []f{
    39  			{"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}},
    40  		}
    41  	}
    42  
    43  	for _, f := range fixtures {
    44  		b, err := ioutil.ReadFile(f.file)
    45  		if err != nil {
    46  			t.Fatal(err)
    47  		}
    48  
    49  		c, h, _, err := DecodeContainerConfig(bytes.NewReader(b))
    50  		if err != nil {
    51  			t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
    52  		}
    53  
    54  		if c.Image != image {
    55  			t.Fatalf("Expected %s image, found %s\n", image, c.Image)
    56  		}
    57  
    58  		if len(c.Entrypoint) != len(f.entrypoint) {
    59  			t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint)
    60  		}
    61  
    62  		if h != nil && h.Memory != 1000 {
    63  			t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory)
    64  		}
    65  	}
    66  }
    67  
    68  // TestDecodeContainerConfigIsolation validates isolation passed
    69  // to the daemon in the hostConfig structure. Note this is platform specific
    70  // as to what level of container isolation is supported.
    71  func TestDecodeContainerConfigIsolation(t *testing.T) {
    72  
    73  	// An invalid isolation level
    74  	if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil {
    75  		if !strings.Contains(err.Error(), `invalid --isolation: "invalid"`) {
    76  			t.Fatal(err)
    77  		}
    78  	}
    79  
    80  	// Blank isolation (== default)
    81  	if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil {
    82  		t.Fatal("Blank isolation should have succeeded")
    83  	}
    84  
    85  	// Default isolation
    86  	if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil {
    87  		t.Fatal("default isolation should have succeeded")
    88  	}
    89  
    90  	// Process isolation (Valid on Windows only)
    91  	if runtime.GOOS == "windows" {
    92  		if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil {
    93  			t.Fatal("process isolation should have succeeded")
    94  		}
    95  	} else {
    96  		if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil {
    97  			if !strings.Contains(err.Error(), `invalid --isolation: "process"`) {
    98  				t.Fatal(err)
    99  			}
   100  		}
   101  	}
   102  
   103  	// Hyper-V Containers isolation (Valid on Windows only)
   104  	if runtime.GOOS == "windows" {
   105  		if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
   106  			t.Fatal("hyperv isolation should have succeeded")
   107  		}
   108  	} else {
   109  		if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
   110  			if !strings.Contains(err.Error(), `invalid --isolation: "hyperv"`) {
   111  				t.Fatal(err)
   112  			}
   113  		}
   114  	}
   115  }
   116  
   117  // callDecodeContainerConfigIsolation is a utility function to call
   118  // DecodeContainerConfig for validating isolation
   119  func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
   120  	var (
   121  		b   []byte
   122  		err error
   123  	)
   124  	w := ContainerConfigWrapper{
   125  		Config: &container.Config{},
   126  		HostConfig: &container.HostConfig{
   127  			NetworkMode: "none",
   128  			Isolation:   container.Isolation(isolation)},
   129  	}
   130  	if b, err = json.Marshal(w); err != nil {
   131  		return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
   132  	}
   133  	return DecodeContainerConfig(bytes.NewReader(b))
   134  }