File: check_test.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (100 lines) | stat: -rw-r--r-- 2,916 bytes parent folder | download | duplicates (3)
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
package storage

import (
	"archive/tar"
	"testing"

	"github.com/containers/storage/pkg/archive"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCheckDirectory(t *testing.T) {
	vectors := []struct {
		description string
		headers     []tar.Header
		expected    []string
	}{
		{
			description: "basic",
			headers: []tar.Header{
				{Name: "a", Typeflag: tar.TypeDir},
			},
			expected: []string{
				"a/",
			},
		},
		{
			description: "whiteout",
			headers: []tar.Header{
				{Name: "a", Typeflag: tar.TypeDir},
				{Name: "a/b", Typeflag: tar.TypeDir},
				{Name: "a/b/c", Typeflag: tar.TypeReg},
				{Name: "a/b/d", Typeflag: tar.TypeReg},
				{Name: "a/b/" + archive.WhiteoutPrefix + "c", Typeflag: tar.TypeReg},
			},
			expected: []string{
				"a/",
				"a/b/",
				"a/b/d",
			},
		},
		{
			description: "opaque",
			headers: []tar.Header{
				{Name: "a", Typeflag: tar.TypeDir},
				{Name: "a/b", Typeflag: tar.TypeDir},
				{Name: "a/b/c", Typeflag: tar.TypeReg},
				{Name: "a/b/d", Typeflag: tar.TypeReg},
				{Name: "a/b/" + archive.WhiteoutOpaqueDir, Typeflag: tar.TypeReg},
			},
			expected: []string{
				"a/",
				"a/b/",
			},
		},
	}
	for i := range vectors {
		t.Run(vectors[i].description, func(t *testing.T) {
			cd := newCheckDirectoryDefaults()
			for _, hdr := range vectors[i].headers {
				cd.header(&hdr)
			}
			actual := cd.names()
			assert.ElementsMatch(t, vectors[i].expected, actual)
		})
	}
}

func TestCheckDetectWriteable(t *testing.T) {
	var sawRWlayers, sawRWimages bool
	stoar, err := GetStore(StoreOptions{
		RunRoot:         t.TempDir(),
		GraphRoot:       t.TempDir(),
		GraphDriverName: "vfs",
	})
	if (err != nil) {
		t.Skip("Debian-Local: Skipping test: %w", err)
	}
	require.NoError(t, err, "unexpected error initializing test store")
	s, ok := stoar.(*store)
	require.True(t, ok, "unexpected error making type assertion")
	_, done, err := readAllLayerStores(s, func(store roLayerStore) (struct{}, bool, error) {
		if roLayerStoreIsReallyReadWrite(store) { // implicitly checking that the type assertion in this function doesn't panic
			sawRWlayers = true
		}
		return struct{}{}, false, nil
	})
	assert.False(t, done, "unexpected error from readAllLayerStores")
	assert.NoError(t, err, "unexpected error from readAllLayerStores")
	assert.True(t, sawRWlayers, "unexpected error detecting which layer store is writeable")
	_, done, err = readAllImageStores(s, func(store roImageStore) (struct{}, bool, error) {
		if roImageStoreIsReallyReadWrite(store) { // implicitly checking that the type assertion in this function doesn't panic
			sawRWimages = true
		}
		return struct{}{}, false, nil
	})
	assert.False(t, done, "unexpected error from readAllImageStores")
	assert.NoError(t, err, "unexpected error from readAllImageStores")
	assert.True(t, sawRWimages, "unexpected error detecting which image store is writeable")
}