This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
oci_test.go
89 lines (69 loc) · 1.69 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
//
// 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) {
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)
assert.Error(err)
// Write the spec file to create a valid OCI bundle
spec.Root.Path = rootfsPath
err = writeSpecToFile(spec)
assert.NoError(err)
cwd, err := changeToBundlePath(spec)
assert.NoError(err)
assert.Equal(cwd, originalCwd)
cwd, err = os.Getwd()
assert.NoError(err)
assert.Equal(bundlePath, cwd)
}
func TestWriteSpecToFile(t *testing.T) {
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)
assert.NoError(err)
file, err := os.Open(path.Join(bundlePath, ociConfigFile))
assert.NoError(err)
defer file.Close()
stat, err := file.Stat()
assert.NoError(err)
assert.True(stat.Size() > 0)
}