github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/configs/config_unix_test.go (about)

     1  // +build linux freebsd
     2  
     3  package configs
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  func loadConfig(name string) (*Config, error) {
    14  	f, err := os.Open(filepath.Join("../sample_configs", name))
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  	defer f.Close()
    19  
    20  	var container *Config
    21  	if err := json.NewDecoder(f).Decode(&container); err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	// Check that a config doesn't contain extra fields
    26  	var configMap, abstractMap map[string]interface{}
    27  
    28  	if _, err := f.Seek(0, 0); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	if err := json.NewDecoder(f).Decode(&abstractMap); err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	configData, err := json.Marshal(&container)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	if err := json.Unmarshal(configData, &configMap); err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	for k := range configMap {
    46  		delete(abstractMap, k)
    47  	}
    48  
    49  	if len(abstractMap) != 0 {
    50  		return nil, fmt.Errorf("unknown fields: %s", abstractMap)
    51  	}
    52  
    53  	return container, nil
    54  }
    55  
    56  func TestRemoveNamespace(t *testing.T) {
    57  	ns := Namespaces{
    58  		{Type: NEWNET},
    59  	}
    60  	if !ns.Remove(NEWNET) {
    61  		t.Fatal("NEWNET was not removed")
    62  	}
    63  	if len(ns) != 0 {
    64  		t.Fatalf("namespaces should have 0 items but reports %d", len(ns))
    65  	}
    66  }
    67  
    68  func TestHostUIDNoUSERNS(t *testing.T) {
    69  	config := &Config{
    70  		Namespaces: Namespaces{},
    71  	}
    72  	uid, err := config.HostUID()
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if uid != 0 {
    77  		t.Fatalf("expected uid 0 with no USERNS but received %d", uid)
    78  	}
    79  }
    80  
    81  func TestHostUIDWithUSERNS(t *testing.T) {
    82  	config := &Config{
    83  		Namespaces: Namespaces{{Type: NEWUSER}},
    84  		UidMappings: []IDMap{
    85  			{
    86  				ContainerID: 0,
    87  				HostID:      1000,
    88  				Size:        1,
    89  			},
    90  		},
    91  	}
    92  	uid, err := config.HostUID()
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	if uid != 1000 {
    97  		t.Fatalf("expected uid 1000 with no USERNS but received %d", uid)
    98  	}
    99  }
   100  
   101  func TestHostGIDNoUSERNS(t *testing.T) {
   102  	config := &Config{
   103  		Namespaces: Namespaces{},
   104  	}
   105  	uid, err := config.HostGID()
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	if uid != 0 {
   110  		t.Fatalf("expected gid 0 with no USERNS but received %d", uid)
   111  	}
   112  }
   113  
   114  func TestHostGIDWithUSERNS(t *testing.T) {
   115  	config := &Config{
   116  		Namespaces: Namespaces{{Type: NEWUSER}},
   117  		GidMappings: []IDMap{
   118  			{
   119  				ContainerID: 0,
   120  				HostID:      1000,
   121  				Size:        1,
   122  			},
   123  		},
   124  	}
   125  	uid, err := config.HostGID()
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	if uid != 1000 {
   130  		t.Fatalf("expected gid 1000 with no USERNS but received %d", uid)
   131  	}
   132  }