-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from vladimirvivien/impl-archive-func
Implementation of the archive script function
- Loading branch information
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.starlark.net/starlark" | ||
|
||
"github.com/vmware-tanzu/crash-diagnostics/archiver" | ||
) | ||
|
||
// archiveFunc is a built-in starlark function that bundles specified directories into | ||
// an arhive format (i.e. tar.gz) | ||
// Starlark format: archive(file_name=<file name> ,source_paths=list) | ||
func archiveFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var outputFile string | ||
var paths *starlark.List | ||
|
||
if err := starlark.UnpackArgs( | ||
identifiers.archive, args, kwargs, | ||
"output_file?", &outputFile, | ||
"source_paths", &paths, | ||
); err != nil { | ||
return starlark.None, fmt.Errorf("%s: %s", identifiers.archive, err) | ||
} | ||
|
||
if len(outputFile) == 0 { | ||
outputFile = "archive.tar.gz" | ||
} | ||
|
||
if paths != nil && paths.Len() == 0 { | ||
return starlark.None, fmt.Errorf("%s: one or more paths required", identifiers.archive) | ||
} | ||
|
||
if err := archiver.Tar(outputFile, getPathElements(paths)...); err != nil { | ||
return starlark.None, fmt.Errorf("%s failed: %s", identifiers.archive, err) | ||
} | ||
|
||
return starlark.String(outputFile), nil | ||
} | ||
|
||
func getPathElements(paths *starlark.List) []string { | ||
pathElems := []string{} | ||
for i := 0; i < paths.Len(); i++ { | ||
if val, ok := paths.Index(i).(starlark.String); ok { | ||
pathElems = append(pathElems, string(val)) | ||
} | ||
} | ||
return pathElems | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
func TestArchiveFunc(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args func(t *testing.T) []starlark.Tuple | ||
eval func(t *testing.T, kwargs []starlark.Tuple) | ||
}{ | ||
{ | ||
name: "arhive single file", | ||
args: func(t *testing.T) []starlark.Tuple { | ||
return []starlark.Tuple{ | ||
{starlark.String("output_file"), starlark.String("/tmp/out.tar.gz")}, | ||
{starlark.String("source_paths"), starlark.NewList([]starlark.Value{starlark.String(defaults.workdir)})}, | ||
} | ||
}, | ||
eval: func(t *testing.T, kwargs []starlark.Tuple) { | ||
val, err := archiveFunc(newTestThreadLocal(t), nil, nil, kwargs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected := "/tmp/out.tar.gz" | ||
defer func() { | ||
os.RemoveAll(expected) | ||
os.RemoveAll(defaults.workdir) | ||
}() | ||
|
||
result := "" | ||
if r, ok := val.(starlark.String); ok { | ||
result = string(r) | ||
} | ||
if result != expected { | ||
t.Errorf("unexpected result: %s", result) | ||
} | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.eval(t, test.args(t)) | ||
}) | ||
} | ||
} | ||
|
||
func TestArchiveScript(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
script string | ||
eval func(t *testing.T, script string) | ||
}{ | ||
{ | ||
name: "archive defaults", | ||
script: ` | ||
result = archive(output_file="/tmp/archive.tar.gz", source_paths=["/tmp/crashd"]) | ||
`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expected := "/tmp/archive.tar.gz" | ||
var result string | ||
resultVal := exe.result["result"] | ||
if resultVal == nil { | ||
t.Fatal("archive() should be assigned to a variable for test") | ||
} | ||
res, ok := resultVal.(starlark.String) | ||
if !ok { | ||
t.Fatal("archive() should return a string") | ||
} | ||
result = string(res) | ||
defer func() { | ||
os.RemoveAll(result) | ||
os.RemoveAll(defaults.workdir) | ||
}() | ||
|
||
if result != expected { | ||
t.Errorf("unexpected result: %s", result) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.eval(t, test.script) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters