|
| 1 | +// Copyright 2016 The Linux Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package image |
| 16 | + |
| 17 | +import ( |
| 18 | + "archive/tar" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +func TestCreateFilesystemChangeset(t *testing.T) { |
| 30 | + // create base fileystem |
| 31 | + tmp1, err := ioutil.TempDir("", "test-layer") |
| 32 | + if err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + //defer os.RemoveAll(tmp1) |
| 36 | + basepath := filepath.Join(tmp1, "base") |
| 37 | + err = os.MkdirAll(basepath, 0700) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + // get the file list of base layer |
| 43 | + expectedFiles := map[string]bool{ |
| 44 | + "bin/": true, |
| 45 | + "bin/app": true, |
| 46 | + "bin/test": true, |
| 47 | + "bin/tool": true, |
| 48 | + "etc/": true, |
| 49 | + "etc/app.cfg": true, |
| 50 | + "etc/tool.cfg.d/": true, |
| 51 | + "etc/tool.cfg.d/tool.cfg": true, |
| 52 | + } |
| 53 | + err = createFilesystem(basepath, expectedFiles, nil) |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + // create base layer |
| 58 | + tarfile := filepath.Join(tmp1, "base.tar") |
| 59 | + err = CreateLayer(basepath, "", tarfile) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + files, err := listTarFiles(tarfile) |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + // verify base layer has packed all the file |
| 69 | + // and no expected file are packed. |
| 70 | + err = verify(expectedFiles, files) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + // create a identical copy of base |
| 76 | + snapshot1path := filepath.Join(tmp1, "base.s1") |
| 77 | + cpCmd := exec.Command("cp", "-a", basepath, snapshot1path) |
| 78 | + err = cpCmd.Run() |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("fail to cp: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + // delete some file and add some file in base.s1 |
| 84 | + layer1FilesDiff := map[string]bool{ |
| 85 | + "bin/tool": false, |
| 86 | + "bin/app.tool": true, |
| 87 | + "etc/app.cfg": false, |
| 88 | + "etc/tool.cfg.d/": false, |
| 89 | + "etc/app.cfg.d/": true, |
| 90 | + "etc/app.tool.cfg": true, |
| 91 | + "etc/app.cfg.d/app.cfg": true, |
| 92 | + } |
| 93 | + modifiedfiles := map[string]func(string) error{ |
| 94 | + "bin/app": func(path string) error { |
| 95 | + err := ioutil.WriteFile(path, []byte(fmt.Sprintf("Hello world")), 0755) |
| 96 | + return err |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + err = createFilesystem(snapshot1path, layer1FilesDiff, modifiedfiles) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + |
| 105 | + // create layer diff |
| 106 | + tarfile1 := filepath.Join(tmp1, "base.s1.tar") |
| 107 | + err = CreateLayer(snapshot1path, basepath, tarfile1) |
| 108 | + if err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + |
| 112 | + execptedfiles := map[string]bool{ |
| 113 | + "bin/": true, |
| 114 | + "bin/app": true, |
| 115 | + "bin/app.tool": true, |
| 116 | + "bin/.wh.tool": true, |
| 117 | + "etc/": true, |
| 118 | + "etc/.wh.app.cfg": true, |
| 119 | + "etc/app.cfg.d/": true, |
| 120 | + "etc/app.cfg.d/app.cfg": true, |
| 121 | + "etc/app.tool.cfg": true, |
| 122 | + "etc/.wh.tool.cfg.d": true, |
| 123 | + } |
| 124 | + |
| 125 | + actualfile, err := listTarFiles(tarfile1) |
| 126 | + if err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + |
| 130 | + // verify if CreateLayer has created expected filesystem changeset |
| 131 | + err = verify(execptedfiles, actualfile) |
| 132 | + if err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func createFilesystem(path string, files map[string]bool, modify map[string]func(string) error) error { |
| 138 | + for f, add := range files { |
| 139 | + // add file |
| 140 | + if add { |
| 141 | + // create a directory |
| 142 | + if strings.HasSuffix(f, "/") { |
| 143 | + err := os.MkdirAll(filepath.Join(path, f), 0700) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } else { // create file |
| 148 | + file := filepath.Join(path, f) |
| 149 | + err := os.MkdirAll(filepath.Dir(file), 0700) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + _, err = os.Create(file) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + } |
| 158 | + } else { // remove file |
| 159 | + file := filepath.Join(path, f) |
| 160 | + err := os.RemoveAll(file) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // apply file modify |
| 168 | + for f, fun := range modify { |
| 169 | + err := fun(filepath.Join(path, f)) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func verify(m1 map[string]bool, m2 map[string]bool) error { |
| 178 | + for f, _ := range m1 { |
| 179 | + if _, ok := m2[f]; !ok { |
| 180 | + return fmt.Errorf("expected file %v not exist", f) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + for f, _ := range m2 { |
| 185 | + if _, ok := m1[f]; !ok { |
| 186 | + return fmt.Errorf("%v is not an expected file", f) |
| 187 | + } |
| 188 | + } |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +func listTarFiles(path string) (map[string]bool, error) { |
| 193 | + var files = make(map[string]bool) |
| 194 | + file, err := os.Open(path) |
| 195 | + if err != nil { |
| 196 | + return nil, err |
| 197 | + } |
| 198 | + |
| 199 | + tr := tar.NewReader(file) |
| 200 | + for { |
| 201 | + hdr, err := tr.Next() |
| 202 | + if err == io.EOF { |
| 203 | + break |
| 204 | + } |
| 205 | + if err != nil { |
| 206 | + return nil, err |
| 207 | + } |
| 208 | + files[hdr.Name] = true |
| 209 | + } |
| 210 | + return files, nil |
| 211 | +} |
0 commit comments