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

     1  package configs_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/opencontainers/runc/libcontainer/configs"
    12  )
    13  
    14  func TestUnmarshalHooks(t *testing.T) {
    15  	timeout := time.Second
    16  
    17  	prestartCmd := configs.NewCommandHook(configs.Command{
    18  		Path:    "/var/vcap/hooks/prestart",
    19  		Args:    []string{"--pid=123"},
    20  		Env:     []string{"FOO=BAR"},
    21  		Dir:     "/var/vcap",
    22  		Timeout: &timeout,
    23  	})
    24  	prestart, err := json.Marshal(prestartCmd.Command)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	hook := configs.Hooks{}
    30  	err = hook.UnmarshalJSON([]byte(fmt.Sprintf(`{"Prestart" :[%s]}`, prestart)))
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	if !reflect.DeepEqual(hook.Prestart[0], prestartCmd) {
    36  		t.Errorf("Expected prestart to equal %+v but it was %+v",
    37  			prestartCmd, hook.Prestart[0])
    38  	}
    39  }
    40  
    41  func TestUnmarshalHooksWithInvalidData(t *testing.T) {
    42  	hook := configs.Hooks{}
    43  	err := hook.UnmarshalJSON([]byte(`{invalid-json}`))
    44  	if err == nil {
    45  		t.Error("Expected error to occur but it was nil")
    46  	}
    47  }
    48  
    49  func TestMarshalHooks(t *testing.T) {
    50  	timeout := time.Second
    51  
    52  	prestartCmd := configs.NewCommandHook(configs.Command{
    53  		Path:    "/var/vcap/hooks/prestart",
    54  		Args:    []string{"--pid=123"},
    55  		Env:     []string{"FOO=BAR"},
    56  		Dir:     "/var/vcap",
    57  		Timeout: &timeout,
    58  	})
    59  
    60  	hook := configs.Hooks{
    61  		Prestart: []configs.Hook{prestartCmd},
    62  	}
    63  	hooks, err := hook.MarshalJSON()
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	h := `{"poststart":null,"poststop":null,"prestart":[{"path":"/var/vcap/hooks/prestart","args":["--pid=123"],"env":["FOO=BAR"],"dir":"/var/vcap","timeout":1000000000}]}`
    69  	if string(hooks) != h {
    70  		t.Errorf("Expected hooks %s to equal %s", string(hooks), h)
    71  	}
    72  }
    73  
    74  func TestMarshalUnmarshalHooks(t *testing.T) {
    75  	timeout := time.Second
    76  
    77  	prestart := configs.NewCommandHook(configs.Command{
    78  		Path:    "/var/vcap/hooks/prestart",
    79  		Args:    []string{"--pid=123"},
    80  		Env:     []string{"FOO=BAR"},
    81  		Dir:     "/var/vcap",
    82  		Timeout: &timeout,
    83  	})
    84  
    85  	hook := configs.Hooks{
    86  		Prestart: []configs.Hook{prestart},
    87  	}
    88  	hooks, err := hook.MarshalJSON()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	umMhook := configs.Hooks{}
    94  	err = umMhook.UnmarshalJSON(hooks)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	if !reflect.DeepEqual(umMhook.Prestart[0], prestart) {
    99  		t.Errorf("Expected hooks to be equal after mashaling -> unmarshaling them: %+v, %+v", umMhook.Prestart[0], prestart)
   100  	}
   101  }
   102  
   103  func TestMarshalHooksWithUnexpectedType(t *testing.T) {
   104  	fHook := configs.NewFunctionHook(func(configs.HookState) error {
   105  		return nil
   106  	})
   107  	hook := configs.Hooks{
   108  		Prestart: []configs.Hook{fHook},
   109  	}
   110  	hooks, err := hook.MarshalJSON()
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	h := `{"poststart":null,"poststop":null,"prestart":null}`
   116  	if string(hooks) != h {
   117  		t.Errorf("Expected hooks %s to equal %s", string(hooks), h)
   118  	}
   119  }
   120  
   121  func TestFuncHookRun(t *testing.T) {
   122  	state := configs.HookState{
   123  		Version: "1",
   124  		ID:      "1",
   125  		Pid:     1,
   126  		Root:    "root",
   127  	}
   128  
   129  	fHook := configs.NewFunctionHook(func(s configs.HookState) error {
   130  		if !reflect.DeepEqual(state, s) {
   131  			t.Errorf("Expected state %+v to equal %+v", state, s)
   132  		}
   133  		return nil
   134  	})
   135  
   136  	fHook.Run(state)
   137  }
   138  
   139  func TestCommandHookRun(t *testing.T) {
   140  	state := configs.HookState{
   141  		Version: "1",
   142  		ID:      "1",
   143  		Pid:     1,
   144  		Root:    "root",
   145  	}
   146  	timeout := time.Second
   147  
   148  	cmdHook := configs.NewCommandHook(configs.Command{
   149  		Path:    os.Args[0],
   150  		Args:    []string{os.Args[0], "-test.run=TestHelperProcess"},
   151  		Env:     []string{"FOO=BAR"},
   152  		Dir:     "/",
   153  		Timeout: &timeout,
   154  	})
   155  
   156  	err := cmdHook.Run(state)
   157  	if err != nil {
   158  		t.Errorf(fmt.Sprintf("Expected error to not occur but it was %+v", err))
   159  	}
   160  }
   161  
   162  func TestCommandHookRunTimeout(t *testing.T) {
   163  	state := configs.HookState{
   164  		Version: "1",
   165  		ID:      "1",
   166  		Pid:     1,
   167  		Root:    "root",
   168  	}
   169  	timeout := (10 * time.Millisecond)
   170  
   171  	cmdHook := configs.NewCommandHook(configs.Command{
   172  		Path:    os.Args[0],
   173  		Args:    []string{os.Args[0], "-test.run=TestHelperProcessWithTimeout"},
   174  		Env:     []string{"FOO=BAR"},
   175  		Dir:     "/",
   176  		Timeout: &timeout,
   177  	})
   178  
   179  	err := cmdHook.Run(state)
   180  	if err == nil {
   181  		t.Error("Expected error to occur but it was nil")
   182  	}
   183  }
   184  
   185  func TestHelperProcess(*testing.T) {
   186  	fmt.Println("Helper Process")
   187  	os.Exit(0)
   188  }
   189  func TestHelperProcessWithTimeout(*testing.T) {
   190  	time.Sleep(time.Second)
   191  }