forked from project-stacker/stacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
130 lines (111 loc) · 3.29 KB
/
cache_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
package stacker
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/mitchellh/hashstructure"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/umoci"
"github.com/opencontainers/umoci/oci/casext"
"github.com/project-stacker/stacker/types"
"github.com/stretchr/testify/assert"
)
func TestLayerHashing(t *testing.T) {
dir, err := ioutil.TempDir("", "stacker_cache_test")
if err != nil {
t.Fatalf("couldn't create temp dir %v", err)
}
defer os.RemoveAll(dir)
config := types.StackerConfig{
StackerDir: dir,
RootFSDir: dir,
}
layerBases := path.Join(config.StackerDir, "layer-bases")
err = os.MkdirAll(layerBases, 0755)
if err != nil {
t.Fatalf("couldn't mkdir for layer bases %v", err)
}
oci, err := umoci.CreateLayout(path.Join(layerBases, "oci"))
if err != nil {
t.Fatalf("couldn't creat OCI layout: %v", err)
}
defer oci.Close()
err = umoci.NewImage(oci, "centos")
if err != nil {
t.Fatalf("couldn't create fake centos image %v", err)
}
stackerYaml := path.Join(dir, "stacker.yaml")
err = ioutil.WriteFile(stackerYaml, []byte(`
foo:
from:
type: docker
url: docker://centos:latest
run: zomg
build_only: true
`), 0644)
if err != nil {
t.Fatalf("couldn't write stacker yaml %v", err)
}
sf, err := types.NewStackerfile(stackerYaml, false, nil)
if err != nil {
t.Fatalf("couldn't read stacker file %v", err)
}
cache, err := OpenCache(config, casext.Engine{}, types.StackerFiles{"dummy": sf})
if err != nil {
t.Fatalf("couldn't open cache %v", err)
}
// fake a successful build for a build-only layer
err = os.MkdirAll(path.Join(dir, "foo"), 0755)
if err != nil {
t.Fatalf("couldn't fake successful bulid %v", err)
}
err = cache.Put("foo", map[types.LayerType]ispec.Descriptor{})
if err != nil {
t.Fatalf("couldn't put to cache %v", err)
}
// change the layer, but look it up under the same name, to make sure
// the layer itself is hashed
stackerYaml = path.Join(dir, "stacker.yaml")
err = ioutil.WriteFile(stackerYaml, []byte(`
foo:
from:
type: docker
url: docker://centos:latest
run: zomg meshuggah rocks
build_only: true
`), 0644)
if err != nil {
t.Fatalf("couldn't write stacker yaml %v", err)
}
sf, err = types.NewStackerfile(stackerYaml, false, nil)
if err != nil {
t.Fatalf("couldn't read stacker file %v", err)
}
// ok, now re-load the persisted cache
cache, err = OpenCache(config, casext.Engine{}, types.StackerFiles{"dummy": sf})
if err != nil {
t.Fatalf("couldn't re-load cache %v", err)
}
_, ok, err := cache.Lookup("foo")
if err != nil {
t.Errorf("lookup failed %v", err)
}
if ok {
t.Errorf("found cached entry when I shouldn't have?")
}
}
func TestCacheEntryChanged(t *testing.T) {
assert := assert.New(t)
h, err := hashstructure.Hash(CacheEntry{}, nil)
assert.NoError(err)
// The goal here is to make sure that the types of things in CacheEntry
// haven't changed; if they have (aka this test fails), you should do
// currentCacheVersion++, since stackers with an old cache will be
// invalid with your current patch.
//
// This test works because the type information is included in the
// hashstructure hash above, so using a zero valued CacheEntry is
// enough to capture changes in types.
assert.Equal(uint64(0x99dd94c023d7ccc6), h)
}