-
Notifications
You must be signed in to change notification settings - Fork 239
/
mock.go
85 lines (76 loc) · 1.63 KB
/
mock.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
package image
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"sync"
)
var _ Registry = &MockRegistry{}
type MockRegistry struct {
RemoteImages map[Reference]*MockImage
localImages map[Reference]*MockImage
m sync.RWMutex
}
type MockImage struct {
Labels map[string]string
FS fs.FS
}
func (i *MockImage) unpack(dir string) error {
return fs.WalkDir(i.FS, ".", func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
data, err := fs.ReadFile(i.FS, path)
if err != nil {
return err
}
path = filepath.Join(dir, path)
pathDir := filepath.Dir(path)
if err := os.MkdirAll(pathDir, 0777); err != nil {
return err
}
return os.WriteFile(path, data, 0666)
})
}
func (m *MockRegistry) Pull(_ context.Context, ref Reference) error {
image, ok := m.RemoteImages[ref]
if !ok {
return errors.New("not found")
}
m.m.Lock()
defer m.m.Unlock()
if m.localImages == nil {
m.localImages = map[Reference]*MockImage{}
}
m.localImages[ref] = image
return nil
}
func (m *MockRegistry) Unpack(_ context.Context, ref Reference, dir string) error {
m.m.RLock()
defer m.m.RUnlock()
image, ok := m.localImages[ref]
if !ok {
return errors.New("not found")
}
return image.unpack(dir)
}
func (m *MockRegistry) Labels(_ context.Context, ref Reference) (map[string]string, error) {
m.m.RLock()
defer m.m.RUnlock()
image, ok := m.localImages[ref]
if !ok {
return nil, errors.New("not found")
}
return image.Labels, nil
}
func (m *MockRegistry) Destroy() error {
m.m.Lock()
defer m.m.Unlock()
m.localImages = nil
return nil
}