github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/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  	"os"
     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  	var (
    25  		fixtures []f
    26  		imgName  string
    27  	)
    28  
    29  	// FIXME (thaJeztah): update fixtures for more current versions.
    30  	if runtime.GOOS != "windows" {
    31  		imgName = "ubuntu"
    32  		fixtures = []f{
    33  			{"fixtures/unix/container_config_1_19.json", strslice.StrSlice{"bash"}},
    34  		}
    35  	} else {
    36  		imgName = "windows"
    37  		fixtures = []f{
    38  			{"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}},
    39  		}
    40  	}
    41  
    42  	for _, f := range fixtures {
    43  		f := f
    44  		t.Run(f.file, func(t *testing.T) {
    45  			b, err := os.ReadFile(f.file)
    46  			if err != nil {
    47  				t.Fatal(err)
    48  			}
    49  
    50  			c, h, _, err := decodeContainerConfig(bytes.NewReader(b), sysinfo.New())
    51  			if err != nil {
    52  				t.Fatal(err)
    53  			}
    54  
    55  			if c.Image != imgName {
    56  				t.Fatalf("Expected %s image, found %s", imgName, c.Image)
    57  			}
    58  
    59  			if len(c.Entrypoint) != len(f.entrypoint) {
    60  				t.Fatalf("Expected %v, found %v", f.entrypoint, c.Entrypoint)
    61  			}
    62  
    63  			if h != nil && h.Memory != 1000 {
    64  				t.Fatalf("Expected memory to be 1000, found %d", h.Memory)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  // TestDecodeContainerConfigIsolation validates isolation passed
    71  // to the daemon in the hostConfig structure. Note this is platform specific
    72  // as to what level of container isolation is supported.
    73  func TestDecodeContainerConfigIsolation(t *testing.T) {
    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  	}
   132  	if b, err = json.Marshal(w); err != nil {
   133  		return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
   134  	}
   135  	return decodeContainerConfig(bytes.NewReader(b), sysinfo.New())
   136  }