github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/runconfig/config_test.go (about)

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