-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathadditional_layer_test.go
More file actions
83 lines (66 loc) · 2.55 KB
/
additional_layer_test.go
File metadata and controls
83 lines (66 loc) · 2.55 KB
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
//go:build linux
package storage
import (
"encoding/base64"
"os"
"path/filepath"
"testing"
digest "github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupAdditionalLayerStore creates a temporary additional layer store directory
// with the expected structure for the given tocDigest and image reference.
// The info file contains the provided content.
func setupAdditionalLayerStore(t *testing.T, tocDigest digest.Digest, imageRef string, infoContent string) string {
t.Helper()
alsRoot := t.TempDir()
refDir := base64.StdEncoding.EncodeToString([]byte(imageRef))
layerDir := filepath.Join(alsRoot, refDir, tocDigest.String())
require.NoError(t, os.MkdirAll(layerDir, 0o755))
require.NoError(t, os.MkdirAll(filepath.Join(layerDir, "diff"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(layerDir, "info"), []byte(infoContent), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(layerDir, "blob"), []byte{}, 0o644))
return alsRoot
}
func TestLookupAdditionalLayerSuccess(t *testing.T) {
prevLevel := logrus.GetLevel()
logrus.SetLevel(logrus.ErrorLevel)
t.Cleanup(func() { logrus.SetLevel(prevLevel) })
tocDigest := digest.FromString("test-layer")
imageRef := "fedora"
info := Layer{
ID: "test-id",
CompressedSize: 42,
TOCDigest: tocDigest,
}
infoJSON, err := json.Marshal(info)
require.NoError(t, err)
alsPath := setupAdditionalLayerStore(t, tocDigest, imageRef, string(infoJSON))
store := newTestStore(t, StoreOptions{
GraphDriverName: "overlay",
GraphDriverOptions: []string{"additionallayerstore=" + alsPath + ":ref"},
})
t.Cleanup(func() { _, _ = store.Shutdown(true) })
al, err := store.LookupAdditionalLayer(tocDigest, imageRef)
require.NoError(t, err)
defer al.Release()
assert.Equal(t, tocDigest, al.TOCDigest())
assert.Equal(t, int64(42), al.CompressedSize())
}
func TestLookupAdditionalLayerDecodeError(t *testing.T) {
prevLevel := logrus.GetLevel()
logrus.SetLevel(logrus.ErrorLevel)
t.Cleanup(func() { logrus.SetLevel(prevLevel) })
tocDigest := digest.FromString("test-layer")
imageRef := "fedora"
alsPath := setupAdditionalLayerStore(t, tocDigest, imageRef, "not valid json")
store := newTestStore(t, StoreOptions{
GraphDriverName: "overlay",
GraphDriverOptions: []string{"additionallayerstore=" + alsPath + ":ref"},
})
t.Cleanup(func() { _, _ = store.Shutdown(true) })
_, err := store.LookupAdditionalLayer(tocDigest, imageRef)
assert.Error(t, err, "should fail on invalid JSON in info file")
}