|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mount |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + // required for `-test.root` flag not to fail |
| 24 | + _ "github.com/containerd/continuity/testutil" |
| 25 | +) |
| 26 | + |
| 27 | +func TestReadonlyMounts(t *testing.T) { |
| 28 | + testCases := []struct { |
| 29 | + desc string |
| 30 | + input []Mount |
| 31 | + expected []Mount |
| 32 | + }{ |
| 33 | + { |
| 34 | + desc: "empty slice", |
| 35 | + input: []Mount{}, |
| 36 | + expected: []Mount{}, |
| 37 | + }, |
| 38 | + { |
| 39 | + desc: "removes `upperdir` and `workdir` from overlay mounts, appends upper layer to lower", |
| 40 | + input: []Mount{ |
| 41 | + { |
| 42 | + Type: "overlay", |
| 43 | + Source: "overlay", |
| 44 | + Options: []string{ |
| 45 | + "index=off", |
| 46 | + "workdir=/path/to/snapshots/4/work", |
| 47 | + "upperdir=/path/to/snapshots/4/fs", |
| 48 | + "lowerdir=/path/to/snapshots/1/fs", |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + Type: "overlay", |
| 53 | + Source: "overlay", |
| 54 | + Options: []string{ |
| 55 | + "index=on", |
| 56 | + "lowerdir=/another/path/to/snapshots/2/fs", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + expected: []Mount{ |
| 61 | + { |
| 62 | + Type: "overlay", |
| 63 | + Source: "overlay", |
| 64 | + Options: []string{ |
| 65 | + "index=off", |
| 66 | + "lowerdir=/path/to/snapshots/4/fs:/path/to/snapshots/1/fs", |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + Type: "overlay", |
| 71 | + Source: "overlay", |
| 72 | + Options: []string{ |
| 73 | + "index=on", |
| 74 | + "lowerdir=/another/path/to/snapshots/2/fs", |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + desc: "removes `rw` and appends `ro` (once) to other mount types", |
| 81 | + input: []Mount{ |
| 82 | + { |
| 83 | + Type: "mount-without-rw", |
| 84 | + Source: "", |
| 85 | + Options: []string{ |
| 86 | + "index=off", |
| 87 | + "workdir=/path/to/other/snapshots/work", |
| 88 | + "upperdir=/path/to/other/snapshots/2", |
| 89 | + "lowerdir=/path/to/other/snapshots/1", |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + Type: "mount-with-rw", |
| 94 | + Source: "", |
| 95 | + Options: []string{ |
| 96 | + "an-option=a-value", |
| 97 | + "another_opt=/another/value", |
| 98 | + "rw", |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + Type: "mount-with-ro", |
| 103 | + Source: "", |
| 104 | + Options: []string{ |
| 105 | + "an-option=a-value", |
| 106 | + "another_opt=/another/value", |
| 107 | + "ro", |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + expected: []Mount{ |
| 112 | + { |
| 113 | + Type: "mount-without-rw", |
| 114 | + Source: "", |
| 115 | + Options: []string{ |
| 116 | + "index=off", |
| 117 | + "workdir=/path/to/other/snapshots/work", |
| 118 | + "upperdir=/path/to/other/snapshots/2", |
| 119 | + "lowerdir=/path/to/other/snapshots/1", |
| 120 | + "ro", |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + Type: "mount-with-rw", |
| 125 | + Source: "", |
| 126 | + Options: []string{ |
| 127 | + "an-option=a-value", |
| 128 | + "another_opt=/another/value", |
| 129 | + "ro", |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + Type: "mount-with-ro", |
| 134 | + Source: "", |
| 135 | + Options: []string{ |
| 136 | + "an-option=a-value", |
| 137 | + "another_opt=/another/value", |
| 138 | + "ro", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + for _, tc := range testCases { |
| 146 | + if !reflect.DeepEqual(readonlyMounts(tc.input), tc.expected) { |
| 147 | + t.Fatalf("incorrectly modified mounts: %s", tc.desc) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments