forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersist_test.go
118 lines (98 loc) · 3.08 KB
/
persist_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/kata-containers/runtime/virtcontainers/device/manager"
exp "github.com/kata-containers/runtime/virtcontainers/experimental"
"github.com/kata-containers/runtime/virtcontainers/persist"
"github.com/kata-containers/runtime/virtcontainers/types"
)
func testCreateExpSandbox() (*Sandbox, error) {
sconfig := SandboxConfig{
ID: "test-exp",
HypervisorType: MockHypervisor,
HypervisorConfig: newHypervisorConfig(nil, nil),
AgentType: NoopAgentType,
NetworkConfig: NetworkConfig{},
Volumes: nil,
Containers: nil,
Experimental: []exp.Feature{persist.NewStoreFeature},
}
// support experimental
sandbox, err := createSandbox(context.Background(), sconfig, nil)
if err != nil {
return nil, fmt.Errorf("Could not create sandbox: %s", err)
}
if err := sandbox.agent.startSandbox(sandbox); err != nil {
return nil, err
}
return sandbox, nil
}
func TestSupportNewStore(t *testing.T) {
assert := assert.New(t)
hConfig := newHypervisorConfig(nil, nil)
sandbox, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil)
assert.NoError(err)
defer cleanUp()
// not support experimental
assert.False(sandbox.supportNewStore())
// support experimental
sandbox, err = testCreateExpSandbox()
assert.NoError(err)
assert.True(sandbox.supportNewStore())
}
func TestSandboxRestore(t *testing.T) {
var err error
assert := assert.New(t)
sconfig := SandboxConfig{
ID: "test-exp",
Experimental: []exp.Feature{persist.NewStoreFeature},
}
container := make(map[string]*Container)
container["test-exp"] = &Container{}
sandbox := Sandbox{
id: "test-exp",
containers: container,
devManager: manager.NewDeviceManager(manager.VirtioSCSI, nil),
hypervisor: &mockHypervisor{},
ctx: context.Background(),
config: &sconfig,
}
sandbox.newStore, err = persist.GetDriver("fs")
assert.NoError(err)
assert.NotNil(sandbox.newStore)
// if we don't call Save(), we can get nothing from disk
err = sandbox.Restore()
assert.NotNil(t, err)
assert.True(os.IsNotExist(err))
// disk data are empty
err = sandbox.Save()
assert.NoError(err)
err = sandbox.Restore()
assert.NoError(err)
assert.Equal(sandbox.state.State, types.StateString(""))
assert.Equal(sandbox.state.GuestMemoryBlockSizeMB, uint32(0))
assert.Equal(sandbox.state.BlockIndex, 0)
// set state data and save again
sandbox.state.State = types.StateString("running")
sandbox.state.GuestMemoryBlockSizeMB = uint32(1024)
sandbox.state.BlockIndex = 2
// flush data to disk
err = sandbox.Save()
assert.Nil(err)
// empty the sandbox
sandbox.state = types.SandboxState{}
// restore data from disk
err = sandbox.Restore()
assert.Nil(err)
assert.Equal(sandbox.state.State, types.StateString("running"))
assert.Equal(sandbox.state.GuestMemoryBlockSizeMB, uint32(1024))
assert.Equal(sandbox.state.BlockIndex, 2)
}