github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/store/vc_test.go (about)

     1  // Copyright (c) 2019 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package store
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestStoreVCRoots(t *testing.T) {
    19  	rootURL := filesystemScheme + "://" + ConfigStoragePath()
    20  	sandboxID := "sandbox"
    21  	containerID := "container"
    22  	sConfigRoot := rootURL + "/" + sandboxID
    23  	cConfigRoot := rootURL + "/" + sandboxID + "/" + containerID
    24  
    25  	assert.Equal(t, SandboxConfigurationRoot(sandboxID), sConfigRoot)
    26  	assert.Equal(t, ContainerConfigurationRoot(sandboxID, containerID), cConfigRoot)
    27  }
    28  
    29  func testStoreVCSandboxDir(t *testing.T, item Item, expected string) error {
    30  	var dir string
    31  	if item == Configuration {
    32  		dir = SandboxConfigurationRootPath(testSandboxID)
    33  	} else {
    34  		dir = SandboxRuntimeRootPath(testSandboxID)
    35  	}
    36  
    37  	if dir != expected {
    38  		return fmt.Errorf("Unexpected sandbox directory %s vs %s", dir, expected)
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  func testStoreVCSandboxFile(t *testing.T, item Item, expected string) error {
    45  	var file string
    46  	var err error
    47  
    48  	if item == Configuration {
    49  		file, err = SandboxConfigurationItemPath(testSandboxID, item)
    50  	} else {
    51  		file, err = SandboxRuntimeItemPath(testSandboxID, item)
    52  	}
    53  
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	if file != expected {
    59  		return fmt.Errorf("Unexpected sandbox file %s vs %s", file, expected)
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func TestStoreVCSandboxDirConfig(t *testing.T) {
    66  	err := testStoreVCSandboxDir(t, Configuration, sandboxDirConfig)
    67  	assert.Nil(t, err)
    68  }
    69  
    70  func TestStoreVCSandboxDirState(t *testing.T) {
    71  	err := testStoreVCSandboxDir(t, State, sandboxDirState)
    72  	assert.Nil(t, err)
    73  }
    74  
    75  func TestStoreVCSandboxDirLock(t *testing.T) {
    76  	err := testStoreVCSandboxDir(t, Lock, sandboxDirLock)
    77  	assert.Nil(t, err)
    78  }
    79  
    80  func TestStoreVCSandboxFileConfig(t *testing.T) {
    81  	err := testStoreVCSandboxFile(t, Configuration, sandboxFileConfig)
    82  	assert.Nil(t, err)
    83  }
    84  
    85  func TestStoreVCSandboxFileState(t *testing.T) {
    86  	err := testStoreVCSandboxFile(t, State, sandboxFileState)
    87  	assert.Nil(t, err)
    88  }
    89  
    90  func TestStoreVCSandboxFileLock(t *testing.T) {
    91  	err := testStoreVCSandboxFile(t, Lock, sandboxFileLock)
    92  	assert.Nil(t, err)
    93  }
    94  
    95  func TestStoreVCSandboxFileNegative(t *testing.T) {
    96  	_, err := SandboxConfigurationItemPath("", State)
    97  	assert.NotNil(t, err)
    98  
    99  	_, err = SandboxRuntimeItemPath("", State)
   100  	assert.NotNil(t, err)
   101  }
   102  
   103  func TestStoreVCNewVCSandboxStore(t *testing.T) {
   104  	testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
   105  	defer func() {
   106  		os.RemoveAll(testDir)
   107  	}()
   108  
   109  	var savedStorePath = VCStorePrefix
   110  	VCStorePrefix = testDir
   111  	defer func() {
   112  		VCStorePrefix = savedStorePath
   113  	}()
   114  
   115  	_, err := NewVCSandboxStore(context.Background(), testSandboxID)
   116  	assert.Nil(t, err)
   117  
   118  	_, err = NewVCSandboxStore(context.Background(), "")
   119  	assert.NotNil(t, err)
   120  }
   121  
   122  func TestStoreVCNewVCContainerStore(t *testing.T) {
   123  	testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
   124  	defer func() {
   125  		os.RemoveAll(testDir)
   126  	}()
   127  
   128  	var savedStorePath = VCStorePrefix
   129  	VCStorePrefix = testDir
   130  	defer func() {
   131  		VCStorePrefix = savedStorePath
   132  	}()
   133  
   134  	_, err := NewVCContainerStore(context.Background(), testSandboxID, "foobar")
   135  	assert.Nil(t, err)
   136  
   137  	_, err = NewVCContainerStore(context.Background(), "", "foobar")
   138  	assert.NotNil(t, err)
   139  
   140  	_, err = NewVCContainerStore(context.Background(), "", "foobar")
   141  	assert.NotNil(t, err)
   142  }