gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/vm_test.go (about)

     1  // Copyright (c) 2018 HyperHQ Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"context"
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/kata-containers/runtime/virtcontainers/utils"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestNewVM(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
    22  	assert.Nil(err)
    23  	defer os.RemoveAll(testDir)
    24  
    25  	config := VMConfig{
    26  		HypervisorType: MockHypervisor,
    27  		AgentType:      NoopAgentType,
    28  		ProxyType:      NoopProxyType,
    29  	}
    30  	hyperConfig := HypervisorConfig{
    31  		KernelPath: testDir,
    32  		ImagePath:  testDir,
    33  	}
    34  
    35  	ctx := context.Background()
    36  
    37  	var vm *VM
    38  	_, err = NewVM(ctx, config)
    39  	assert.Error(err)
    40  
    41  	config.HypervisorConfig = hyperConfig
    42  	vm, err = NewVM(ctx, config)
    43  	assert.Nil(err)
    44  
    45  	// VM operations
    46  	err = vm.Pause()
    47  	assert.Nil(err)
    48  	err = vm.Resume()
    49  	assert.Nil(err)
    50  	err = vm.Start()
    51  	assert.Nil(err)
    52  	err = vm.Disconnect()
    53  	assert.Nil(err)
    54  	err = vm.Save()
    55  	assert.Nil(err)
    56  	err = vm.Stop()
    57  	assert.Nil(err)
    58  	err = vm.AddCPUs(2)
    59  	assert.Nil(err)
    60  	err = vm.AddMemory(128)
    61  	assert.Nil(err)
    62  	err = vm.OnlineCPUMemory()
    63  	assert.Nil(err)
    64  	err = vm.ReseedRNG()
    65  	assert.Nil(err)
    66  
    67  	// template VM
    68  	config.HypervisorConfig.BootFromTemplate = true
    69  	_, err = NewVM(ctx, config)
    70  	assert.Error(err)
    71  
    72  	config.HypervisorConfig.MemoryPath = testDir
    73  	_, err = NewVM(ctx, config)
    74  	assert.Error(err)
    75  
    76  	config.HypervisorConfig.DevicesStatePath = testDir
    77  	_, err = NewVM(ctx, config)
    78  	assert.Nil(err)
    79  }
    80  
    81  func TestVMConfigValid(t *testing.T) {
    82  	assert := assert.New(t)
    83  
    84  	config := VMConfig{}
    85  
    86  	err := config.Valid()
    87  	assert.Error(err)
    88  
    89  	testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
    90  	assert.Nil(err)
    91  	defer os.RemoveAll(testDir)
    92  
    93  	config.HypervisorConfig = HypervisorConfig{
    94  		KernelPath: testDir,
    95  		InitrdPath: testDir,
    96  	}
    97  	err = config.Valid()
    98  	assert.Nil(err)
    99  }
   100  
   101  func TestSetupProxy(t *testing.T) {
   102  	assert := assert.New(t)
   103  
   104  	config := VMConfig{
   105  		HypervisorType: MockHypervisor,
   106  		AgentType:      NoopAgentType,
   107  	}
   108  
   109  	hypervisor := &mockHypervisor{}
   110  	agent := &noopAgent{}
   111  
   112  	// wrong proxy type
   113  	config.ProxyType = ProxyType("invalidProxyType")
   114  	_, _, _, err := setupProxy(hypervisor, agent, config, "foobar")
   115  	assert.NotNil(err)
   116  
   117  	config.ProxyType = NoopProxyType
   118  	_, _, _, err = setupProxy(hypervisor, agent, config, "foobar")
   119  	assert.Nil(err)
   120  }
   121  
   122  func TestVMConfigGrpc(t *testing.T) {
   123  	assert := assert.New(t)
   124  	config := VMConfig{
   125  		HypervisorType:   QemuHypervisor,
   126  		HypervisorConfig: newQemuConfig(),
   127  		AgentType:        KataContainersAgent,
   128  		AgentConfig:      KataAgentConfig{false, true, false, false, 0, "", "", []string{}},
   129  		ProxyType:        NoopProxyType,
   130  	}
   131  
   132  	p, err := config.ToGrpc()
   133  	assert.Nil(err)
   134  
   135  	config2, err := GrpcToVMConfig(p)
   136  	assert.Nil(err)
   137  
   138  	assert.True(utils.DeepCompare(config, *config2))
   139  }