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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
// +build !windows
package continuity
import (
"bytes"
_ "crypto/sha256"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"sort"
"syscall"
"testing"
"github.com/containerd/continuity/devices"
"github.com/opencontainers/go-digest"
)
// Hard things:
// 1. Groups/gid - no standard library support.
// 2. xattrs - must choose package to provide this.
// 3. ADS - no clue where to start.
func TestWalkFS(t *testing.T) {
rand.Seed(1)
// Testing:
// 1. Setup different files:
// - links
// - sibling directory - relative
// - sibling directory - absolute
// - parent directory - absolute
// - parent directory - relative
// - illegal links
// - parent directory - relative, out of root
// - parent directory - absolute, out of root
// - regular files
// - character devices
// - what about sticky bits?
// 2. Build the manifest.
// 3. Verify expected result.
testResources := []dresource{
{
path: "a",
mode: 0644,
},
{
kind: rhardlink,
path: "a-hardlink",
target: "a",
},
{
kind: rdirectory,
path: "b",
mode: 0755,
},
{
kind: rhardlink,
path: "b/a-hardlink",
target: "a",
},
{
path: "b/a",
mode: 0600 | os.ModeSticky,
},
{
kind: rdirectory,
path: "c",
mode: 0755,
},
{
path: "c/a",
mode: 0644,
},
{
kind: rrelsymlink,
path: "c/ca-relsymlink",
mode: 0600,
target: "a",
},
{
kind: rrelsymlink,
path: "c/a-relsymlink",
mode: 0600,
target: "../a",
},
{
kind: rabssymlink,
path: "c/a-abssymlink",
mode: 0600,
target: "a",
},
// TODO(stevvooe): Make sure we can test this case and get proper
// errors when it is encountered.
// {
// // create a bad symlink and make sure we don't include it.
// kind: relsymlink,
// path: "c/a-badsymlink",
// mode: 0600,
// target: "../../..",
// },
// TODO(stevvooe): Must add tests for xattrs, with symlinks,
// directorys and regular files.
{
kind: rnamedpipe,
path: "fifo",
mode: 0666 | os.ModeNamedPipe,
},
{
kind: rdirectory,
path: "/dev",
mode: 0755,
},
// NOTE(stevvooe): Below here, we add a few simple character devices.
// Block devices are untested but should be nearly the same as
// character devices.
// devNullResource,
// devZeroResource,
}
root, err := ioutil.TempDir("", "continuity-test-")
if err != nil {
t.Fatalf("error creating temporary directory: %v", err)
}
defer os.RemoveAll(root)
generateTestFiles(t, root, testResources)
ctx, err := NewContext(root)
if err != nil {
t.Fatalf("error getting context: %v", err)
}
m, err := BuildManifest(ctx)
if err != nil {
t.Fatalf("error building manifest: %v", err)
}
var b bytes.Buffer
MarshalText(&b, m)
t.Log(b.String())
// TODO(dmcgowan): always verify, currently hard links not supported
//if err := VerifyManifest(ctx, m); err != nil {
// t.Fatalf("error verifying manifest: %v")
//}
expectedResources, err := expectedResourceList(root, testResources)
if err != nil {
// TODO(dmcgowan): update function to panic, this would mean test setup error
t.Fatalf("error creating resource list: %v", err)
}
// Diff resources
diff := diffResourceList(expectedResources, m.Resources)
if diff.HasDiff() {
t.Log("Resource list difference")
for _, a := range diff.Additions {
t.Logf("Unexpected resource: %#v", a)
}
for _, d := range diff.Deletions {
t.Logf("Missing resource: %#v", d)
}
for _, u := range diff.Updates {
t.Logf("Changed resource:\n\tExpected: %#v\n\tActual: %#v", u.Original, u.Updated)
}
t.FailNow()
}
}
// TODO(stevvooe): At this time, we have a nice testing framework to define
// and build resources. This will likely be a pre-cursor to the packages
// public interface.
type kind int
func (k kind) String() string {
switch k {
case rfile:
return "file"
case rdirectory:
return "directory"
case rhardlink:
return "hardlink"
case rchardev:
return "chardev"
case rnamedpipe:
return "namedpipe"
}
panic(fmt.Sprintf("unknown kind: %v", int(k)))
}
const (
rfile kind = iota
rdirectory
rhardlink
rrelsymlink
rabssymlink
rchardev
rnamedpipe
)
type dresource struct {
kind kind
path string
mode os.FileMode
target string // hard/soft link target
digest digest.Digest
size int
uid int64
gid int64
major, minor int
}
func generateTestFiles(t *testing.T, root string, resources []dresource) {
for i, resource := range resources {
p := filepath.Join(root, resource.path)
switch resource.kind {
case rfile:
size := rand.Intn(4 << 20)
d := make([]byte, size)
randomBytes(d)
dgst := digest.FromBytes(d)
resources[i].digest = dgst
resources[i].size = size
// this relies on the proper directory parent being defined.
if err := ioutil.WriteFile(p, d, resource.mode); err != nil {
t.Fatalf("error writing %q: %v", p, err)
}
case rdirectory:
if err := os.Mkdir(p, resource.mode); err != nil {
t.Fatalf("error creating directory %q: %v", p, err)
}
case rhardlink:
target := filepath.Join(root, resource.target)
if err := os.Link(target, p); err != nil {
t.Fatalf("error creating hardlink: %v", err)
}
case rrelsymlink:
if err := os.Symlink(resource.target, p); err != nil {
t.Fatalf("error creating symlink: %v", err)
}
case rabssymlink:
// for absolute links, we join with root.
target := filepath.Join(root, resource.target)
if err := os.Symlink(target, p); err != nil {
t.Fatalf("error creating symlink: %v", err)
}
case rchardev, rnamedpipe:
if err := devices.Mknod(p, resource.mode, resource.major, resource.minor); err != nil {
t.Fatalf("error creating device %q: %v", p, err)
}
default:
t.Fatalf("unknown resource type: %v", resource.kind)
}
st, err := os.Lstat(p)
if err != nil {
t.Fatalf("error statting after creation: %v", err)
}
resources[i].uid = int64(st.Sys().(*syscall.Stat_t).Uid)
resources[i].gid = int64(st.Sys().(*syscall.Stat_t).Gid)
resources[i].mode = st.Mode()
// TODO: Readback and join xattr
}
// log the test root for future debugging
if err := filepath.Walk(root, func(p string, fi os.FileInfo, err error) error {
if fi.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(p)
if err != nil {
return err
}
t.Log(fi.Mode(), p, "->", target)
} else {
t.Log(fi.Mode(), p)
}
return nil
}); err != nil {
t.Fatalf("error walking created root: %v", err)
}
var b bytes.Buffer
if err := tree(&b, root); err != nil {
t.Fatalf("error running tree: %v", err)
}
t.Logf("\n%s", b.String())
}
func randomBytes(p []byte) {
for i := range p {
p[i] = byte(rand.Intn(1<<8 - 1))
}
}
// expectedResourceList sorts the set of resources into the order
// expected in the manifest and collapses hardlinks
func expectedResourceList(root string, resources []dresource) ([]Resource, error) {
resourceMap := map[string]Resource{}
paths := []string{}
for _, r := range resources {
absPath := r.path
if !filepath.IsAbs(absPath) {
absPath = "/" + absPath
}
switch r.kind {
case rfile:
f := ®ularFile{
resource: resource{
paths: []string{absPath},
mode: r.mode,
uid: r.uid,
gid: r.gid,
},
size: int64(r.size),
digests: []digest.Digest{r.digest},
}
resourceMap[absPath] = f
paths = append(paths, absPath)
case rdirectory:
d := &directory{
resource: resource{
paths: []string{absPath},
mode: r.mode,
uid: r.uid,
gid: r.gid,
},
}
resourceMap[absPath] = d
paths = append(paths, absPath)
case rhardlink:
targetPath := r.target
if !filepath.IsAbs(targetPath) {
targetPath = "/" + targetPath
}
target, ok := resourceMap[targetPath]
if !ok {
return nil, errors.New("must specify target before hardlink for test resources")
}
rf, ok := target.(*regularFile)
if !ok {
return nil, errors.New("hardlink target must be regular file")
}
// TODO(dmcgowan): full merge
rf.paths = append(rf.paths, absPath)
// TODO(dmcgowan): check if first path is now different, changes source order and should update
// resource map key, to avoid canonically ordered first should be regular file
sort.Stable(sort.StringSlice(rf.paths))
case rrelsymlink, rabssymlink:
targetPath := r.target
if r.kind == rabssymlink && !filepath.IsAbs(r.target) {
// for absolute links, we join with root.
targetPath = filepath.Join(root, targetPath)
}
s := &symLink{
resource: resource{
paths: []string{absPath},
mode: r.mode,
uid: r.uid,
gid: r.gid,
},
target: targetPath,
}
resourceMap[absPath] = s
paths = append(paths, absPath)
case rchardev:
d := &device{
resource: resource{
paths: []string{absPath},
mode: r.mode,
uid: r.uid,
gid: r.gid,
},
major: uint64(r.major),
minor: uint64(r.minor),
}
resourceMap[absPath] = d
paths = append(paths, absPath)
case rnamedpipe:
p := &namedPipe{
resource: resource{
paths: []string{absPath},
mode: r.mode,
uid: r.uid,
gid: r.gid,
},
}
resourceMap[absPath] = p
paths = append(paths, absPath)
default:
return nil, fmt.Errorf("unknown resource type: %v", r.kind)
}
}
if len(resourceMap) < len(paths) {
return nil, errors.New("resource list has duplicated paths")
}
sort.Strings(paths)
manifestResources := make([]Resource, len(paths))
for i, p := range paths {
manifestResources[i] = resourceMap[p]
}
return manifestResources, nil
}
|