|
| 1 | +/* |
| 2 | + * umoci: Umoci Modifies Open Containers' Images |
| 3 | + * Copyright (C) 2017 SUSE LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package mtreefilter |
| 19 | + |
| 20 | +import ( |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/vbatts/go-mtree" |
| 27 | +) |
| 28 | + |
| 29 | +func TestIsParent(t *testing.T) { |
| 30 | + for _, test := range []struct { |
| 31 | + parent, path string |
| 32 | + expected bool |
| 33 | + }{ |
| 34 | + {"/", "/a", true}, |
| 35 | + {"/", "/a/b/c", true}, |
| 36 | + {"/", "/", true}, |
| 37 | + {"/a path/", "/a path", true}, |
| 38 | + {"/a nother path", "/a nother path/test", true}, |
| 39 | + {"/a nother path", "/a nother path/test/1 2/ 33 /", true}, |
| 40 | + {"/path1", "/path2", false}, |
| 41 | + {"/pathA", "/PATHA", false}, |
| 42 | + {"/pathC", "/path", false}, |
| 43 | + {"/path9", "/", false}, |
| 44 | + // Make sure it's not the same as filepath.HasPrefix. |
| 45 | + {"/a/b/c", "/a/b/c/d", true}, |
| 46 | + {"/a/b/c", "/a/b/cd", false}, |
| 47 | + {"/a/b/c", "/a/bcd", false}, |
| 48 | + {"/a/bc", "/a/bcd", false}, |
| 49 | + {"/abc", "/abcd", false}, |
| 50 | + } { |
| 51 | + got := isParent(test.parent, test.path) |
| 52 | + if got != test.expected { |
| 53 | + t.Errorf("isParent(%q, %q) got %v expected %v", test.parent, test.path, got, test.expected) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestMaskDeltas(t *testing.T) { |
| 59 | + dir, err := ioutil.TempDir("", "TestMaskDeltas-") |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + defer os.RemoveAll(dir) |
| 64 | + |
| 65 | + var mtreeKeywords []mtree.Keyword = append(mtree.DefaultKeywords, "sha256digest") |
| 66 | + |
| 67 | + // Create some files. |
| 68 | + if err != ioutil.WriteFile(filepath.Join(dir, "file1"), []byte("contents"), 0644) { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + if err != ioutil.WriteFile(filepath.Join(dir, "file2"), []byte("another content"), 0644) { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + if err != os.MkdirAll(filepath.Join(dir, "dir", "child"), 0755) { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + if err != os.MkdirAll(filepath.Join(dir, "dir", "child2"), 0755) { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + if err != ioutil.WriteFile(filepath.Join(dir, "dir", "file 3"), []byte("more content"), 0644) { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + if err != ioutil.WriteFile(filepath.Join(dir, "dir", "child2", "4 files"), []byte("very content"), 0644) { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + // Generate a diff. |
| 88 | + originalDh, err := mtree.Walk(dir, nil, mtreeKeywords, nil) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + // Modify the root. |
| 94 | + if err := os.RemoveAll(filepath.Join(dir, "file2")); err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + if err := ioutil.WriteFile(filepath.Join(dir, "dir", "new"), []byte("more content"), 0755); err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + if err := ioutil.WriteFile(filepath.Join(dir, "file1"), []byte("different contents"), 0666); err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + |
| 104 | + // Generate the set of diffs. |
| 105 | + newDh, err := mtree.Walk(dir, nil, mtreeKeywords, nil) |
| 106 | + if err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + diff, err := mtree.Compare(originalDh, newDh, mtreeKeywords) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + for _, test := range []struct { |
| 115 | + paths []string |
| 116 | + len int |
| 117 | + }{ |
| 118 | + {nil, len(diff)}, |
| 119 | + {[]string{"/"}, 0}, |
| 120 | + {[]string{"dir"}, 3}, |
| 121 | + {[]string{filepath.Join("dir", "child2")}, 5}, |
| 122 | + {[]string{"file2"}, 4}, |
| 123 | + {[]string{"/", "file2"}, 0}, |
| 124 | + {[]string{"file2", filepath.Join("dir", "child2")}, 4}, |
| 125 | + } { |
| 126 | + newDiff := FilterDeltas(diff, MaskFilter(test.paths)) |
| 127 | + if len(newDiff) != test.len { |
| 128 | + t.Errorf("diff %v expected %d got %d", test.paths, test.len, len(newDiff)) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments