gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/pkg/katautils/hook_test.go (about)

     1  // Copyright (c) 2018 Intel Corporation
     2  // Copyright (c) 2018 HyperHQ Inc.
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  //
     6  
     7  package katautils
     8  
     9  import (
    10  	"context"
    11  	"os"
    12  	"testing"
    13  
    14  	ktu "github.com/kata-containers/runtime/pkg/katatestutils"
    15  	. "github.com/kata-containers/runtime/virtcontainers/pkg/mock"
    16  	"github.com/opencontainers/runtime-spec/specs-go"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  // Important to keep these values in sync with hook test binary
    21  var testKeyHook = "test-key"
    22  var testContainerIDHook = "test-container-id"
    23  var testControllerIDHook = "test-controller-id"
    24  var testBinHookPath = "/usr/bin/virtcontainers/bin/test/hook"
    25  var testBundlePath = "/test/bundle"
    26  
    27  func getMockHookBinPath() string {
    28  	if DefaultMockHookBinPath == "" {
    29  		return testBinHookPath
    30  	}
    31  
    32  	return DefaultMockHookBinPath
    33  }
    34  
    35  func createHook(timeout int) specs.Hook {
    36  	to := &timeout
    37  	if timeout == 0 {
    38  		to = nil
    39  	}
    40  
    41  	return specs.Hook{
    42  		Path:    getMockHookBinPath(),
    43  		Args:    []string{testKeyHook, testContainerIDHook, testControllerIDHook},
    44  		Env:     os.Environ(),
    45  		Timeout: to,
    46  	}
    47  }
    48  
    49  func createWrongHook() specs.Hook {
    50  	return specs.Hook{
    51  		Path: getMockHookBinPath(),
    52  		Args: []string{"wrong-args"},
    53  		Env:  os.Environ(),
    54  	}
    55  }
    56  
    57  func TestRunHook(t *testing.T) {
    58  	if tc.NotValid(ktu.NeedRoot()) {
    59  		t.Skip(ktu.TestDisabledNeedRoot)
    60  	}
    61  
    62  	assert := assert.New(t)
    63  
    64  	ctx := context.Background()
    65  
    66  	// Run with timeout 0
    67  	hook := createHook(0)
    68  	err := runHook(ctx, hook, testSandboxID, testBundlePath)
    69  	assert.NoError(err)
    70  
    71  	// Run with timeout 1
    72  	hook = createHook(1)
    73  	err = runHook(ctx, hook, testSandboxID, testBundlePath)
    74  	assert.NoError(err)
    75  
    76  	// Run timeout failure
    77  	hook = createHook(1)
    78  	hook.Args = append(hook.Args, "2")
    79  	err = runHook(ctx, hook, testSandboxID, testBundlePath)
    80  	assert.Error(err)
    81  
    82  	// Failure due to wrong hook
    83  	hook = createWrongHook()
    84  	err = runHook(ctx, hook, testSandboxID, testBundlePath)
    85  	assert.Error(err)
    86  }
    87  
    88  func TestPreStartHooks(t *testing.T) {
    89  	if tc.NotValid(ktu.NeedRoot()) {
    90  		t.Skip(ktu.TestDisabledNeedRoot)
    91  	}
    92  
    93  	assert := assert.New(t)
    94  
    95  	ctx := context.Background()
    96  
    97  	// Hooks field is nil
    98  	spec := specs.Spec{}
    99  	err := PreStartHooks(ctx, spec, "", "")
   100  	assert.NoError(err)
   101  
   102  	// Hooks list is empty
   103  	spec = specs.Spec{
   104  		Hooks: &specs.Hooks{},
   105  	}
   106  	err = PreStartHooks(ctx, spec, "", "")
   107  	assert.NoError(err)
   108  
   109  	// Run with timeout 0
   110  	hook := createHook(0)
   111  	spec = specs.Spec{
   112  		Hooks: &specs.Hooks{
   113  			Prestart: []specs.Hook{hook},
   114  		},
   115  	}
   116  	err = PreStartHooks(ctx, spec, testSandboxID, testBundlePath)
   117  	assert.NoError(err)
   118  
   119  	// Failure due to wrong hook
   120  	hook = createWrongHook()
   121  	spec = specs.Spec{
   122  		Hooks: &specs.Hooks{
   123  			Prestart: []specs.Hook{hook},
   124  		},
   125  	}
   126  	err = PreStartHooks(ctx, spec, testSandboxID, testBundlePath)
   127  	assert.Error(err)
   128  }
   129  
   130  func TestPostStartHooks(t *testing.T) {
   131  	if tc.NotValid(ktu.NeedRoot()) {
   132  		t.Skip(ktu.TestDisabledNeedRoot)
   133  	}
   134  
   135  	assert := assert.New(t)
   136  
   137  	ctx := context.Background()
   138  
   139  	// Hooks field is nil
   140  	spec := specs.Spec{}
   141  	err := PostStartHooks(ctx, spec, "", "")
   142  	assert.NoError(err)
   143  
   144  	// Hooks list is empty
   145  	spec = specs.Spec{
   146  		Hooks: &specs.Hooks{},
   147  	}
   148  	err = PostStartHooks(ctx, spec, "", "")
   149  	assert.NoError(err)
   150  
   151  	// Run with timeout 0
   152  	hook := createHook(0)
   153  	spec = specs.Spec{
   154  		Hooks: &specs.Hooks{
   155  			Poststart: []specs.Hook{hook},
   156  		},
   157  	}
   158  	err = PostStartHooks(ctx, spec, testSandboxID, testBundlePath)
   159  	assert.NoError(err)
   160  
   161  	// Failure due to wrong hook
   162  	hook = createWrongHook()
   163  	spec = specs.Spec{
   164  		Hooks: &specs.Hooks{
   165  			Poststart: []specs.Hook{hook},
   166  		},
   167  	}
   168  	err = PostStartHooks(ctx, spec, testSandboxID, testBundlePath)
   169  	assert.Error(err)
   170  }
   171  
   172  func TestPostStopHooks(t *testing.T) {
   173  	if tc.NotValid(ktu.NeedRoot()) {
   174  		t.Skip(ktu.TestDisabledNeedRoot)
   175  	}
   176  
   177  	assert := assert.New(t)
   178  
   179  	ctx := context.Background()
   180  
   181  	// Hooks field is nil
   182  	spec := specs.Spec{}
   183  	err := PostStopHooks(ctx, spec, "", "")
   184  	assert.NoError(err)
   185  
   186  	// Hooks list is empty
   187  	spec = specs.Spec{
   188  		Hooks: &specs.Hooks{},
   189  	}
   190  	err = PostStopHooks(ctx, spec, "", "")
   191  	assert.NoError(err)
   192  
   193  	// Run with timeout 0
   194  	hook := createHook(0)
   195  	spec = specs.Spec{
   196  		Hooks: &specs.Hooks{
   197  			Poststop: []specs.Hook{hook},
   198  		},
   199  	}
   200  	err = PostStopHooks(ctx, spec, testSandboxID, testBundlePath)
   201  	assert.NoError(err)
   202  
   203  	// Failure due to wrong hook
   204  	hook = createWrongHook()
   205  	spec = specs.Spec{
   206  		Hooks: &specs.Hooks{
   207  			Poststop: []specs.Hook{hook},
   208  		},
   209  	}
   210  	err = PostStopHooks(ctx, spec, testSandboxID, testBundlePath)
   211  	assert.Error(err)
   212  }