diff --git a/pkg/util/util.go b/pkg/util/util.go index 2039cec..e949bba 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -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 { @@ -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") { @@ -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 } diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index a6f7b81..091d2f6 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -4,6 +4,7 @@ import ( "context" "io/ioutil" "os" + "os/exec" "path" "strconv" "testing" @@ -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) {