-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasset_test.go
136 lines (117 loc) · 4.37 KB
/
asset_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
package overmount
import (
"io/ioutil"
"os"
"path"
"github.com/docker/docker/pkg/archive"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
. "gopkg.in/check.v1"
)
func (m *mountSuite) TestAssetBasic(c *C) {
if os.Getenv("VIRTUAL") != "" {
c.Skip("Cannot run this test with virtual layers")
return
}
tmpdir, err := ioutil.TempDir("", "")
c.Assert(err, IsNil)
dispatchTable := map[string]Checker{
tmpdir: IsNil,
"overmount.go": NotNil,
}
for dir, check := range dispatchTable {
if check == IsNil { // cheating a bit to ensure we're not removing overmount.go
c.Assert(os.Remove(dir), IsNil)
c.Assert(os.Symlink("/etc", dir), IsNil)
asset, _ := NewAsset(dir, digest.SHA256.Digester(), false)
_, err := asset.LoadDigest()
c.Assert(errors.Cause(err), Equals, ErrInvalidAsset)
c.Assert(os.Remove(dir), IsNil)
asset, err = NewAsset(dir, digest.SHA256.Digester(), false)
c.Assert(err, IsNil)
_, err = asset.LoadDigest()
c.Assert(errors.Cause(err), Equals, ErrInvalidAsset)
c.Assert(os.Mkdir(dir, 0700), IsNil)
}
asset, err := NewAsset(dir, digest.SHA256.Digester(), false)
c.Assert(err, IsNil)
c.Assert(asset.Path(), Equals, dir)
c.Assert(asset.Digest(), Equals, emptyDigest)
reader, err := archive.Tar("/go/src/github.com/box-builder/overmount", archive.Uncompressed)
c.Assert(err, IsNil)
c.Assert(asset.Unpack(reader), check)
dg := asset.Digest()
_, err = os.Stat(path.Join(asset.Path(), "overmount.go"))
c.Assert(err, check)
digester := digest.SHA256.Digester()
c.Assert(asset.Pack(digester.Hash()), check)
c.Assert(dg, Equals, digester.Digest())
c.Assert(dg, Equals, asset.Digest())
}
// Testing LoadDigest
dir := tmpdir
asset, err := NewAsset(dir, digest.SHA256.Digester(), false)
c.Assert(err, IsNil)
digester := digest.SHA256.Digester()
c.Assert(asset.Pack(digester.Hash()), IsNil)
asset.resetDigest()
dg, err := asset.LoadDigest()
c.Assert(err, IsNil)
c.Assert(dg, Equals, digester.Digest())
c.Assert(asset.Digest(), Equals, digester.Digest())
tf, err := ioutil.TempDir("", "overmount-temp-symlink-target-")
c.Assert(os.RemoveAll(dir), IsNil)
c.Assert(os.Symlink(tf, dir), IsNil)
reader, err := archive.Tar("/go/src/github.com/box-builder/overmount", archive.Uncompressed)
c.Assert(err, IsNil)
c.Assert(errors.Cause(asset.Unpack(reader)), Equals, ErrInvalidAsset)
c.Assert(errors.Cause(asset.Pack(ioutil.Discard)), Equals, ErrInvalidAsset)
}
func (m *mountSuite) TestAssetVirtual(c *C) {
if os.Getenv("VIRTUAL") == "" {
c.Skip("This test is for virtual layers only")
return
}
dir, err := ioutil.TempDir("", "overmount-asset-test-")
c.Assert(err, IsNil)
layerFile := path.Join(dir, "layer.tar")
asset, err := NewAsset(layerFile, digest.SHA256.Digester(), true)
c.Assert(err, IsNil)
c.Assert(os.Symlink("/etc/passwd", layerFile), IsNil)
_, err = asset.LoadDigest()
c.Assert(errors.Cause(err), Equals, ErrInvalidAsset)
c.Assert(os.Remove(layerFile), IsNil)
asset, err = NewAsset(layerFile, digest.SHA256.Digester(), true)
c.Assert(err, IsNil)
c.Assert(asset.Path(), Equals, layerFile)
c.Assert(asset.Digest(), Equals, emptyDigest)
reader, err := archive.Tar("/go/src/github.com/box-builder/overmount", archive.Uncompressed)
c.Assert(err, IsNil)
c.Assert(asset.Unpack(reader), IsNil)
c.Assert(asset.Digest(), Not(Equals), emptyDigest)
fi, err := os.Stat(asset.Path())
c.Assert(err, IsNil)
c.Assert(fi.Size(), Not(Equals), 0)
dg := asset.Digest()
asset.resetDigest()
c.Assert(asset.Digest(), Not(Equals), dg)
_, err = asset.LoadDigest()
c.Assert(err, IsNil)
c.Assert(asset.Digest(), Equals, dg)
errdir, err := ioutil.TempDir("", "overmount-asset-test-")
c.Assert(err, IsNil)
asset, err = NewAsset(errdir, digest.SHA256.Digester(), true)
c.Assert(err, IsNil)
reader, err = archive.Tar("/go/src/github.com/box-builder/overmount", archive.Uncompressed)
c.Assert(err, IsNil)
c.Assert(asset.Unpack(reader), NotNil)
c.Assert(asset.Pack(ioutil.Discard), NotNil)
tf, err := ioutil.TempFile("", "box-symlink-test-")
c.Assert(err, IsNil)
c.Assert(os.Remove(layerFile), IsNil)
c.Assert(os.Symlink(tf.Name(), layerFile), IsNil)
reader, err = archive.Tar("/go/src/github.com/box-builder/overmount", archive.Uncompressed)
c.Assert(err, IsNil)
c.Assert(errors.Cause(asset.Unpack(reader)), Equals, ErrInvalidAsset)
c.Assert(errors.Cause(asset.Pack(ioutil.Discard)), Equals, ErrInvalidAsset)
}