Ejemplo n.º 1
0
func TestStaticSocket(t *testing.T) {
	tdir, err := ioutil.TempDir("", "schema-test-")
	if err != nil {
		t.Fatalf("ioutil.TempDir(): %v", err)
	}
	defer os.RemoveAll(tdir)

	sockPath := filepath.Join(tdir, "socket")
	err = osutil.Mksocket(sockPath)
	if err == osutil.ErrNotSupported {
		t.SkipNow()
	}
	if err != nil {
		t.Fatalf("osutil.Mksocket(): %v", err)
	}

	fi, err := os.Lstat(sockPath)
	if err != nil {
		t.Fatalf("os.Lstat(): %v", err)
	}

	bb := NewCommonFileMap(sockPath, fi)
	bb.SetType("socket")
	bb.SetFileName(sockPath)
	blob := bb.Blob()
	t.Logf("Got JSON for socket: %s\n", blob.JSON())

	sf, ok := blob.AsStaticFile()
	if !ok {
		t.Fatalf("Blob.AsStaticFile(): Expected true, got false")
	}
	_, ok = sf.AsStaticSocket()
	if !ok {
		t.Fatalf("StaticFile.AsStaticSocket(): Expected true, got false")
	}
}
Ejemplo n.º 2
0
func TestStaticFileAndStaticSymlink(t *testing.T) {
	// TODO (marete): Split this into two test functions.
	fd, err := ioutil.TempFile("", "schema-test-")
	if err != nil {
		t.Fatalf("io.TempFile(): %v", err)
	}
	defer os.Remove(fd.Name())
	defer fd.Close()

	fi, err := os.Lstat(fd.Name())
	if err != nil {
		t.Fatalf("os.Lstat(): %v", err)
	}

	bb := NewCommonFileMap(fd.Name(), fi)
	bb.SetType("file")
	bb.SetFileName(fd.Name())
	blob := bb.Blob()

	sf, ok := blob.AsStaticFile()
	if !ok {
		t.Fatalf("Blob.AsStaticFile(): Unexpected return value: false")
	}
	if want, got := filepath.Base(fd.Name()), sf.FileName(); want != got {
		t.Fatalf("StaticFile.FileName(): Expected %s, got %s",
			want, got)
	}

	_, ok = sf.AsStaticSymlink()
	if ok {
		t.Fatalf("StaticFile.AsStaticSymlink(): Unexpected return value: true")
	}

	dir, err := ioutil.TempDir("", "schema-test-")
	if err != nil {
		t.Fatalf("ioutil.TempDir(): %v", err)
	}
	defer os.RemoveAll(dir)

	target := "bar"
	src := filepath.Join(dir, "foo")
	err = os.Symlink(target, src)
	fi, err = os.Lstat(src)
	if err != nil {
		t.Fatalf("os.Lstat():  %v", err)
	}

	bb = NewCommonFileMap(src, fi)
	bb.SetType("symlink")
	bb.SetFileName(src)
	bb.SetSymlinkTarget(target)
	blob = bb.Blob()

	sf, ok = blob.AsStaticFile()
	if !ok {
		t.Fatalf("Blob.AsStaticFile(): Unexpected return value: false")
	}
	sl, ok := sf.AsStaticSymlink()
	if !ok {
		t.Fatalf("StaticFile.AsStaticSymlink(): Unexpected return value: false")
	}

	if want, got := filepath.Base(src), sl.FileName(); want != got {
		t.Fatalf("StaticSymlink.FileName(): Expected %s, got %s",
			want, got)
	}

	if want, got := target, sl.SymlinkTargetString(); got != want {
		t.Fatalf("StaticSymlink.SymlinkTargetString(): Expected %s, got %s", want, got)
	}
}