diff --git a/src/runtime/virtcontainers/factory/factory_test.go b/src/runtime/virtcontainers/factory/factory_test.go index 80cff101d..e7c40046b 100644 --- a/src/runtime/virtcontainers/factory/factory_test.go +++ b/src/runtime/virtcontainers/factory/factory_test.go @@ -8,6 +8,7 @@ package factory import ( "context" "os" + "strings" "testing" vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers" @@ -331,3 +332,126 @@ func TestDeepCompare(t *testing.T) { assert.Nil(err) assert.False(utils.DeepCompare(f1, f2)) } + +func TestFactoryConfig(t *testing.T) { + assert := assert.New(t) + + // Valid config + var config Config + config.VMConfig.HypervisorConfig = vc.HypervisorConfig{ + KernelPath: "foo", + ImagePath: "bar", + } + ctx := context.Background() + vf, err := NewFactory(ctx, config, false) + assert.Nil(err) + + f, ok := vf.(*factory) + assert.True(ok) + + vmc := f.Config() + + assert.Equal(config.VMConfig.HypervisorConfig.KernelPath, vmc.HypervisorConfig.KernelPath) + assert.Equal(config.VMConfig.HypervisorConfig.ImagePath, vmc.HypervisorConfig.ImagePath) +} + +func TestFactoryGetBaseVM(t *testing.T) { + assert := assert.New(t) + + // Set configs + var config Config + testDir := t.TempDir() + + hyperConfig := vc.HypervisorConfig{ + KernelPath: testDir, + ImagePath: testDir, + } + vmConfig := vc.VMConfig{ + HypervisorType: vc.MockHypervisor, + HypervisorConfig: hyperConfig, + } + config.VMConfig = vmConfig + config.TemplatePath = testDir + + err := vmConfig.Valid() + assert.Nil(err) + + ctx := context.Background() + + url, err := mock.GenerateKataMockHybridVSock() + assert.NoError(err) + defer mock.RemoveKataMockHybridVSock(url) + vc.MockHybridVSockPath = url + + hybridVSockTTRPCMock := mock.HybridVSockTTRPCMock{} + err = hybridVSockTTRPCMock.Start(url) + assert.NoError(err) + defer hybridVSockTTRPCMock.Stop() + + // New factory + vf, err := NewFactory(ctx, config, false) + assert.Nil(err) + + f, ok := vf.(*factory) + assert.True(ok) + + // Check VM Config + assert.Equal(f.Config(), vmConfig) + + // GetBaseVM + vm, err := f.GetBaseVM(ctx, vmConfig) + assert.Nil(err) + + // Get VM Status + defer func() { + r := recover() + assert.NotNil(r) + + // Close + err = vm.Stop(ctx) + assert.Nil(err) + }() + vmStatus := f.GetVMStatus() + assert.NotNil(vmStatus) // line of code to make golang happy. This is never executed. +} + +func TestNewFactoryWithCache(t *testing.T) { + assert := assert.New(t) + + // Config + var config Config + config.VMConfig.HypervisorConfig = vc.HypervisorConfig{ + KernelPath: "foo", + ImagePath: "bar", + } + ctx := context.Background() + + // cache>0 and fetch only should throw error + config.Cache = 1 + vf, err := NewFactory(ctx, config, true) + + assert.Nil(vf) + assert.Error(err) + b := err.Error() + assert.True(strings.Contains(b, "cache factory does not support fetch")) +} + +func TestNewFactoryWrongCacheEndpoint(t *testing.T) { + assert := assert.New(t) + + // Config + var config Config + config.VMConfig.HypervisorConfig = vc.HypervisorConfig{ + KernelPath: "foo", + ImagePath: "bar", + } + ctx := context.Background() + + config.VMCache = true + vf, err := NewFactory(ctx, config, false) + + assert.Nil(vf) + assert.Error(err) + b := err.Error() + assert.True(strings.Contains(b, "rpc error")) // sanity check +}