-
Notifications
You must be signed in to change notification settings - Fork 3
/
overmount_test.go
205 lines (172 loc) · 4.8 KB
/
overmount_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package overmount
import (
"archive/tar"
"context"
"encoding/json"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
. "testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
. "gopkg.in/check.v1"
)
type mountSuite struct {
Repository *Repository
}
var _ = Suite(&mountSuite{})
func TestOvermount(t *T) {
TestingT(t)
}
func (m *mountSuite) SetUpTest(c *C) {
tmpdir, err := ioutil.TempDir("", "")
if err != nil {
panic(err)
}
repo, err := NewRepository(tmpdir, os.Getenv("VIRTUAL") != "")
if err != nil {
panic(err)
}
m.Repository = repo
}
func (m *mountSuite) TestRepositoryTempDir(c *C) {
t, err := m.Repository.TempDir()
c.Assert(err, IsNil)
p, err := filepath.Rel(m.Repository.baseDir, t)
c.Assert(err, IsNil)
first, _ := path.Split(p)
c.Assert(err, IsNil)
c.Assert(first, Equals, tmpdirBase+"/")
}
func (m *mountSuite) TestBasicImageMount(c *C) {
if m.Repository.IsVirtual() {
c.Skip("Cannot mount virtual layers")
return
}
layerNames := []string{"one", "two", "three"}
for i := 0; i < len(layerNames); i++ {
layers := []*Layer{}
for x, name := range layerNames[:i+1] {
// stack the layers as parents of each other, except for the first of
// course.
var parent *Layer
if x > 0 {
parent = layers[x-1]
}
child, err := m.Repository.NewLayer(name, parent)
c.Assert(err, IsNil)
layers = append(layers, child)
}
image := m.Repository.NewImage(layers[len(layers)-1])
if len(layers) == 1 {
c.Assert(image.Mount(), NotNil)
m.Repository.mkdirCheckRel(image.layer.Path())
} else {
c.Assert(image.Mount(), IsNil)
c.Assert(image.mount.Mounted(), Equals, true)
}
target := image.layer.MountPath()
if len(layers) == 1 {
target = image.layer.Path()
}
r, w, err := os.Pipe()
c.Assert(err, IsNil)
errChan := make(chan error, 1)
go func(target string) {
tw := tar.NewWriter(w)
defer w.Close()
defer tw.Close()
defer close(errChan)
err = tw.WriteHeader(&tar.Header{
Name: image.layer.ID(),
Mode: 0600,
Typeflag: tar.TypeReg,
})
if err != nil {
errChan <- err
return
}
if _, err := tw.Write([]byte{}); err != nil {
errChan <- err
return
}
}(target)
c.Assert(image.layer.asset.Unpack(r), IsNil)
fis, err := ioutil.ReadDir(target)
c.Assert(err, IsNil)
c.Assert(len(fis), Equals, len(layers)) // one file for each layer, one written to each layer
if len(layers) > 1 {
c.Assert(image.Unmount(), IsNil)
}
for _, layer := range layers {
m.Repository.RemoveLayer(layer)
}
}
}
func (m *mountSuite) TestImageUnpack(c *C) {
dockerClient, err := client.NewEnvClient()
c.Assert(err, IsNil)
reader, err := dockerClient.ImagePull(context.Background(), "docker.io/library/postgres:latest", types.ImagePullOptions{})
c.Assert(err, IsNil)
_, err = io.Copy(ioutil.Discard, reader)
c.Assert(err, IsNil)
reader, err = dockerClient.ImageSave(context.Background(), []string{"library/postgres:latest"})
c.Assert(err, IsNil)
layerMap := map[string]string{}
var manifest []map[string]interface{}
tr := tar.NewReader(reader)
tmpdir, err := ioutil.TempDir("", "")
c.Assert(err, IsNil)
for {
header, err := tr.Next()
if err != nil {
break
}
if path.Base(header.Name) == "layer.tar" {
layerID := path.Base(path.Dir(header.Name))
f, err := os.Create(path.Join(tmpdir, layerID+".tar"))
c.Assert(err, IsNil)
_, err = io.Copy(f, tr)
c.Assert(err, IsNil)
f.Close()
layerMap[layerID] = f.Name()
} else if path.Base(header.Name) == "manifest.json" {
content, err := ioutil.ReadAll(tr)
c.Assert(err, IsNil)
c.Assert(json.Unmarshal(content, &manifest), IsNil)
} else {
io.Copy(ioutil.Discard, tr)
}
}
reader.Close()
var parent *Layer // at the end of the loop, the parent will be the top-most layer
for _, tmp := range manifest[0]["Layers"].([]interface{}) {
layerID := path.Dir(tmp.(string))
tarfile, err := os.Open(layerMap[layerID])
c.Assert(err, IsNil)
layer, err := m.Repository.NewLayer(layerID, parent)
c.Assert(os.MkdirAll(layer.layerBase(), 0700), IsNil)
c.Assert(err, IsNil)
parent = layer
digest, err := layer.Unpack(tarfile)
c.Assert(err, IsNil)
c.Assert(digest, NotNil)
}
if !m.Repository.IsVirtual() {
image := m.Repository.NewImage(parent)
c.Assert(image.Mount(), IsNil)
_, err = os.Stat(path.Join(parent.MountPath(), "/usr/local/bin/docker-entrypoint.sh"))
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(parent.MountPath(), "/var/lib/postgresql"))
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(parent.MountPath(), "/etc/passwd"))
c.Assert(err, IsNil)
c.Assert(image.Unmount(), IsNil)
_, err = os.Stat(path.Join(parent.MountPath(), "/usr/local/bin/docker-entrypoint.sh"))
c.Assert(err, NotNil)
_, err = os.Stat(path.Join(parent.MountPath(), "/etc/passwd"))
c.Assert(err, NotNil)
}
}