1
2
3
4 package overlay
5
6 import (
7 "context"
8 "errors"
9 "os"
10 "testing"
11 )
12
13 type undo struct {
14 osMkdirAll func(string, os.FileMode) error
15 osRemoveAll func(string) error
16 unixMount func(string, string, string, uintptr, string) error
17 }
18
19 func (u *undo) Close() {
20 osMkdirAll = u.osMkdirAll
21 unixMount = u.unixMount
22 }
23
24
25
26
27 func captureTestMethods() *undo {
28 u := &undo{
29 osMkdirAll: osMkdirAll,
30 osRemoveAll: osRemoveAll,
31 unixMount: unixMount,
32 }
33 osMkdirAll = nil
34 osRemoveAll = nil
35 unixMount = nil
36 return u
37 }
38
39 func Test_Mount_Success(t *testing.T) {
40 undo := captureTestMethods()
41 defer undo.Close()
42
43 var upperCreated, workCreated, rootCreated bool
44 osMkdirAll = func(path string, perm os.FileMode) error {
45 if perm != 0755 {
46 t.Errorf("os.MkdirAll at: %s, perm: %v expected perm: 0755", path, perm)
47 }
48 switch path {
49 case "/upper":
50 upperCreated = true
51 return nil
52 case "/work":
53 workCreated = true
54 return nil
55 case "/root":
56 rootCreated = true
57 return nil
58 }
59 return errors.New("unexpected os.MkdirAll path")
60 }
61 unixMount = func(source string, target string, fstype string, flags uintptr, data string) error {
62 if source != "overlay" {
63 t.Errorf("expected source: 'overlay' got: %v", source)
64 }
65 if target != "/root" {
66 t.Errorf("expected target: '/root' got: %v", target)
67 }
68 if fstype != "overlay" {
69 t.Errorf("expected fstype: 'overlay' got: %v", fstype)
70 }
71 if flags != 0 {
72 t.Errorf("expected flags: '0' got: %v", flags)
73 }
74 if data != "lowerdir=/layer1:/layer2,upperdir=/upper,workdir=/work" {
75 t.Errorf("expected data: 'lowerdir=/layer1:/layer2,upperdir=/upper,workdir=/work' got: %v", data)
76 }
77 return nil
78 }
79
80 err := MountLayer(context.Background(), []string{"/layer1", "/layer2"}, "/upper", "/work", "/root", false)
81 if err != nil {
82 t.Fatalf("expected no error got: %v", err)
83 }
84 if !upperCreated || !workCreated || !rootCreated {
85 t.Fatalf("expected all upper: %v, work: %v, root: %v to be created", upperCreated, workCreated, rootCreated)
86 }
87 }
88
89 func Test_Mount_Readonly_Success(t *testing.T) {
90 undo := captureTestMethods()
91 defer undo.Close()
92
93 var rootCreated bool
94 osMkdirAll = func(path string, perm os.FileMode) error {
95 if perm != 0755 {
96 t.Errorf("os.MkdirAll at: %s, perm: %v expected perm: 0755", path, perm)
97 }
98 switch path {
99 case "/root":
100 rootCreated = true
101 return nil
102 }
103 return errors.New("unexpected os.MkdirAll path")
104 }
105 unixMount = func(source string, target string, fstype string, flags uintptr, data string) error {
106 if source != "overlay" {
107 t.Errorf("expected source: 'overlay' got: %v", source)
108 }
109 if target != "/root" {
110 t.Errorf("expected target: '/root' got: %v", target)
111 }
112 if fstype != "overlay" {
113 t.Errorf("expected fstype: 'overlay' got: %v", fstype)
114 }
115 if flags != 0 {
116 t.Errorf("expected flags: '0' got: %v", flags)
117 }
118 if data != "lowerdir=/layer1:/layer2" {
119 t.Errorf("expected data: 'lowerdir=/layer1:/layer2' got: %v", data)
120 }
121 return nil
122 }
123
124 err := MountLayer(context.Background(), []string{"/layer1", "/layer2"}, "", "", "/root", false)
125 if err != nil {
126 t.Fatalf("expected no error got: %v", err)
127 }
128 if !rootCreated {
129 t.Fatal("expected root to be created")
130 }
131 }
132
View as plain text