Ejemplo n.º 1
0
// GetOverlay fetches an overlay from nanobox.io and untars it into dst
func GetOverlay(overlay, dst string) {

	// extract a user and archive (desired engine)
	user, archive := ExtractArchive(overlay)

	// extract an engine and version from the archive
	engine, version := ExtractEngine(archive)

	//
	res, err := GetEngine(user, engine, version)
	if err != nil {
		config.Fatal("[util/engine/engine] http.Get() failed", err.Error())
	}
	defer res.Body.Close()

	//
	switch res.StatusCode / 100 {
	case 2, 3:
		break
	case 4, 5:
		os.Stderr.WriteString(stylish.ErrBullet("Unable to fetch '%v' overlay, exiting...", engine))
		os.Exit(1)
	}

	//
	if err := fileutil.Untar(dst, res.Body); err != nil {
		config.Fatal("[util/engine/engine] file.Untar() failed", err.Error())
	}
}
Ejemplo n.º 2
0
// tests if Tar and Untar are working as intended; can't really test taring w/o
// also testing untaring
func TestTarUntar(t *testing.T) {

	// create tmp dirs
	src, dst, file, paths, err := setup()
	defer cleanup(src, dst)
	if err != nil {
		t.Error("Unexpected error - ", err.Error())
	}

	//
	tarball, err := os.Create(filepath.Join(dst, "file.tar.gz"))
	if err != nil {
		t.Error("Unexpected error - ", err.Error())
	}
	defer tarball.Close()

	// create a tarball from src
	if err := fileutil.Tar(src, tarball); err != nil {
		t.Error("Unexpected error - ", err.Error())
	}

	// varify that tarball was create at dst
	if _, err := os.Stat(filepath.Join(dst, "file.tar.gz")); err != nil {
		t.Error("Expected file, got nothing - ", err.Error())
	}

	//
	archive, err := os.Open(filepath.Join(dst, "file.tar.gz"))
	if err != nil {
		t.Error("Unexpected error - ", err.Error())
	}
	defer archive.Close()

	// untar tarball
	if err := fileutil.Untar(dst, archive); err != nil {
		t.Error("Unexpected error - ", err.Error())
	}

	// iterate through each path checking to see if file.txt was untared correctly;
	// the contents of the tarball should be the src directory (inside the dst dir)
	for _, path := range paths {
		if _, err := os.Stat(filepath.Join(dst, path, file)); err != nil {
			t.Error("Expected file, got nothing - ", err.Error())
		}
	}
}