This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
oci_test.go
100 lines (78 loc) · 1.97 KB
/
oci_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
//
// Copyright (c) 2018 NVIDIA CORPORATION
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)
func TestChangeToBundlePath(t *testing.T) {
skipUnlessRoot(t)
containerId := "1"
assert := assert.New(t)
originalCwd, err := os.Getwd()
assert.NoError(err)
defer os.Chdir(originalCwd)
bundlePath, err := ioutil.TempDir("", "bundle")
assert.NoError(err)
defer os.RemoveAll(bundlePath)
rootfsPath := path.Join(bundlePath, "rootfs")
err = os.Mkdir(rootfsPath, 0750)
assert.NoError(err)
spec := &specs.Spec{}
spec.Root = &specs.Root{
Path: "",
Readonly: false,
}
_, err = changeToBundlePath(spec, containerId)
assert.Error(err)
// Write the spec file to create a valid OCI bundle
spec.Root.Path = rootfsPath
err = writeSpecToFile(spec, containerId)
assert.NoError(err)
cwd, err := changeToBundlePath(spec, containerId)
assert.NoError(err)
assert.Equal(cwd, originalCwd)
cwd, err = os.Getwd()
assert.NoError(err)
assert.Equal(bundlePath, cwd)
}
func TestWriteSpecToFile(t *testing.T) {
skipUnlessRoot(t)
containerId := "1"
assert := assert.New(t)
bundlePath, err := ioutil.TempDir("", "bundle")
assert.NoError(err)
defer os.RemoveAll(bundlePath)
originalCwd, err := os.Getwd()
assert.NoError(err)
defer os.Chdir(originalCwd)
err = os.Chdir(bundlePath)
assert.NoError(err)
rootfsPath := path.Join(bundlePath, "rootfs")
spec := &specs.Spec{
Root: &specs.Root{
Path: rootfsPath,
Readonly: false,
},
}
err = writeSpecToFile(spec, containerId)
assert.NoError(err)
file, err := os.Open(path.Join(bundlePath, ociConfigFile))
assert.Error(err)
defer file.Close()
_, err = file.Stat()
assert.Error(err)
file, err = os.Open(path.Join("/run/libcontainer/", containerId, ociConfigFile))
assert.NoError(err)
defer file.Close()
stat, err := file.Stat()
assert.NoError(err)
assert.True(stat.Size() > 0)
}