Skip to content

Commit

Permalink
support install local archive
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Sep 5, 2017
1 parent 7cde85e commit 5c5dc74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func Wget(ctx context.Context, rawURL string, dest string) (string, error) {

// InstallArchive downloads the URL and extracts the archive to the dest diretory.
// Supports zip, and tarball.
// TODO: support local file
func InstallArchive(ctx context.Context, rawURL string, dest string) error {
err := os.MkdirAll("/tmp/chaos", 0755)
if err != nil {
Expand All @@ -69,8 +68,12 @@ func InstallArchive(ctx context.Context, rawURL string, dest string) error {
defer os.RemoveAll(tmpDir)

var name string
if name, err = Wget(ctx, rawURL, tmpDir); err != nil {
return err
if strings.HasPrefix(rawURL, "file://") {
name = strings.Trim(rawURL, "file://")
} else {
if name, err = Wget(ctx, rawURL, "/tmp/chaos"); err != nil {
return err
}
}

if strings.HasSuffix(name, ".zip") {
Expand All @@ -83,9 +86,6 @@ func InstallArchive(ctx context.Context, rawURL string, dest string) error {
return err
}

// Remove the archive file
os.Remove(name)

if dest, err = filepath.Abs(dest); err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"testing"
Expand Down Expand Up @@ -41,6 +42,27 @@ func TestInstallArchive(t *testing.T) {
if err != nil {
t.Fatalf("install archive failed %v", err)
}

archFile := path.Join(tmpDir, "a.tar.gz")
testCreateArichive(t, path.Join(tmpDir, "test"), archFile)
err = InstallArchive(context.Background(), "file://"+archFile, path.Join(tmpDir, "3"))
if err != nil {
t.Fatalf("install archive failed %v", err)
}
}

func testCreateArichive(t *testing.T, srcDir string, name string) {
os.MkdirAll(srcDir, 0755)
f, err := os.Create(path.Join(srcDir, "a.log"))
if err != nil {
t.Fatalf("create file failed %v", err)
}
f.WriteString("hello world")
f.Close()

if err = exec.Command("tar", "-cf", name, "-C", srcDir, ".").Run(); err != nil {
t.Fatalf("tar %f to %f failed %v", srcDir, name, err)
}
}

func TestDaemon(t *testing.T) {
Expand Down

0 comments on commit 5c5dc74

Please sign in to comment.