-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support prefix parameter in lakectl diff command (#7832)
* support prefix parameter in lakectl diff command * update docs * write first test * rename * use string builder * add more tests * rearrange args * suggest builder pattern for lakectl command in tests (#7836) * suggest builder pattern for lakectl command in tests * fix lint * add Flag api * use Arg in Flag * review fixes * update docs
- Loading branch information
1 parent
237aaac
commit 773f2d0
Showing
5 changed files
with
245 additions
and
7 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Left ref: lakefs://${REPO}/${LEFT_BRANCH} | ||
Right ref: lakefs://${REPO}/${RIGHT_BRANCH} | ||
${DIFF_LIST} |
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,37 @@ | ||
package esti | ||
|
||
import "strings" | ||
|
||
type LakeCtlCmd struct { | ||
rawCmd string | ||
} | ||
|
||
func NewLakeCtl() *LakeCtlCmd { | ||
return &LakeCtlCmd{ | ||
rawCmd: Lakectl(), | ||
} | ||
} | ||
|
||
func (l *LakeCtlCmd) Arg(arg string) *LakeCtlCmd { | ||
l.rawCmd += " " + arg | ||
return l | ||
} | ||
|
||
// Flag Same as Arg, added for usage clarity. | ||
func (l *LakeCtlCmd) Flag(arg string) *LakeCtlCmd { | ||
return l.Arg(arg) | ||
} | ||
|
||
func (l *LakeCtlCmd) PathArg(components ...string) *LakeCtlCmd { | ||
l.rawCmd += " " + strings.Join(components, "/") | ||
return l | ||
} | ||
|
||
func (l *LakeCtlCmd) URLArg(schemaPrefix string, components ...string) *LakeCtlCmd { | ||
l.rawCmd += " " + schemaPrefix + strings.Join(components, "/") | ||
return l | ||
} | ||
|
||
func (l *LakeCtlCmd) Get() string { | ||
return l.rawCmd | ||
} |
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,189 @@ | ||
package esti | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const ( | ||
filePath1 = "path/to/file1.txt" | ||
filePath2 = "path/to/other/file2.txt" | ||
testBranch = "test" | ||
) | ||
|
||
var filesForDiffTest = map[string]string{ | ||
filePath1: "ro_1k", | ||
filePath2: "ro_1k_other", | ||
} | ||
|
||
func TestLakectlDiffAddedFiles(t *testing.T) { | ||
repoName := generateUniqueRepositoryName() | ||
storage := generateUniqueStorageNamespace(repoName) | ||
|
||
createRepo(t, repoName, storage) | ||
createBranch(t, repoName, storage, testBranch) | ||
|
||
uploadFiles(t, repoName, testBranch, filesForDiffTest) | ||
commit(t, repoName, testBranch, "adding test files to "+testBranch) | ||
|
||
expectedDiff := &ExpectedDiff{ | ||
added: []string{filePath1, filePath2}, | ||
deleted: []string{}, | ||
} | ||
runDiffAndExpect(t, repoName, testBranch, "", expectedDiff) | ||
} | ||
|
||
func TestLakectlDiffDeletedFiles(t *testing.T) { | ||
repoName := generateUniqueRepositoryName() | ||
storage := generateUniqueStorageNamespace(repoName) | ||
|
||
createRepo(t, repoName, storage) | ||
uploadFiles(t, repoName, mainBranch, filesForDiffTest) | ||
commit(t, repoName, mainBranch, "adding test files to "+mainBranch) | ||
|
||
createBranch(t, repoName, storage, testBranch) | ||
deleteFiles(t, repoName, testBranch, filePath1, filePath2) | ||
commit(t, repoName, testBranch, "deleting test files from "+testBranch) | ||
|
||
expectedDiff := &ExpectedDiff{ | ||
added: []string{}, | ||
deleted: []string{filePath1, filePath2}, | ||
} | ||
runDiffAndExpect(t, repoName, testBranch, "", expectedDiff) | ||
} | ||
|
||
func TestLakectlDiffPrefix(t *testing.T) { | ||
repoName := generateUniqueRepositoryName() | ||
storage := generateUniqueStorageNamespace(repoName) | ||
|
||
createRepo(t, repoName, storage) | ||
createBranch(t, repoName, storage, testBranch) | ||
|
||
uploadFiles(t, repoName, testBranch, filesForDiffTest) | ||
commit(t, repoName, testBranch, "adding test files to "+testBranch) | ||
|
||
runDiffAndExpect(t, repoName, testBranch, "path/to/", &ExpectedDiff{ | ||
added: []string{filePath1, filePath2}, | ||
deleted: []string{}, | ||
}) | ||
runDiffAndExpect(t, repoName, testBranch, "path/to/o", &ExpectedDiff{ | ||
added: []string{filePath2}, | ||
deleted: []string{}, | ||
}) | ||
runDiffAndExpect(t, repoName, testBranch, "path/to/f", &ExpectedDiff{ | ||
added: []string{filePath1}, | ||
deleted: []string{}, | ||
}) | ||
runDiffAndExpect(t, repoName, testBranch, "path/to/x", &ExpectedDiff{ | ||
added: []string{}, | ||
deleted: []string{}, | ||
}) | ||
} | ||
|
||
type ExpectedDiff struct { | ||
added []string | ||
deleted []string | ||
} | ||
|
||
func (a ExpectedDiff) buildAssertionString() string { | ||
type PrefixedFile struct { | ||
prefix string | ||
path string | ||
} | ||
var added []PrefixedFile | ||
var deleted []PrefixedFile | ||
for _, file := range a.added { | ||
added = append(added, PrefixedFile{"+ added ", file}) | ||
} | ||
for _, file := range a.deleted { | ||
deleted = append(deleted, PrefixedFile{"- removed ", file}) | ||
} | ||
var all = append(added, deleted...) | ||
sort.Slice(all, func(i, j int) bool { | ||
return all[i].path < all[j].path | ||
}) | ||
|
||
var sb strings.Builder | ||
for _, file := range all { | ||
sb.WriteString(file.prefix) | ||
sb.WriteString(file.path) | ||
sb.WriteString("\n") | ||
} | ||
return sb.String() | ||
} | ||
|
||
func runDiffAndExpect(t *testing.T, repoName string, testBranch string, prefix string, diffArgs *ExpectedDiff) { | ||
diffVars := map[string]string{ | ||
"REPO": repoName, | ||
"LEFT_BRANCH": mainBranch, | ||
"RIGHT_BRANCH": testBranch, | ||
"DIFF_LIST": diffArgs.buildAssertionString(), | ||
} | ||
|
||
cmd := NewLakeCtl(). | ||
Arg("diff"). | ||
URLArg("lakefs://", repoName, mainBranch). | ||
URLArg("lakefs://", repoName, testBranch) | ||
if prefix != "" { | ||
cmd.Flag("--prefix").Arg(prefix) | ||
} | ||
|
||
RunCmdAndVerifySuccessWithFile(t, cmd.Get(), false, "lakectl_diff", diffVars) | ||
} | ||
|
||
func uploadFiles(t *testing.T, repoName string, branch string, files map[string]string) { | ||
for filePath, contentPath := range files { | ||
cmd := NewLakeCtl(). | ||
Arg("fs upload"). | ||
Flag("-s"). | ||
PathArg("files", contentPath). | ||
URLArg("lakefs://", repoName, branch, filePath) | ||
RunCmdAndVerifyContainsText(t, cmd.Get(), false, filePath, nil) | ||
} | ||
} | ||
|
||
func commit(t *testing.T, repoName string, branch string, commitMessage string) { | ||
cmd := NewLakeCtl(). | ||
Arg("commit"). | ||
URLArg("lakefs://", repoName, branch). | ||
Flag("-m"). | ||
Arg("\"" + commitMessage + "\"") | ||
RunCmdAndVerifyContainsText(t, cmd.Get(), false, commitMessage, nil) | ||
} | ||
|
||
func deleteFiles(t *testing.T, repoName string, branch string, files ...string) { | ||
for _, filePath := range files { | ||
cmd := NewLakeCtl(). | ||
Arg("fs rm"). | ||
URLArg("lakefs://", repoName, branch, filePath) | ||
RunCmdAndVerifySuccess(t, cmd.Get(), false, "", nil) | ||
} | ||
} | ||
|
||
func createRepo(t *testing.T, repoName string, storage string) { | ||
cmd := NewLakeCtl(). | ||
Arg("repo create"). | ||
URLArg("lakefs://", repoName). | ||
Arg(storage) | ||
RunCmdAndVerifySuccessWithFile(t, cmd.Get(), false, "lakectl_repo_create", map[string]string{ | ||
"REPO": repoName, | ||
"STORAGE": storage, | ||
"BRANCH": mainBranch, | ||
}) | ||
} | ||
|
||
func createBranch(t *testing.T, repoName string, storage string, branch string) { | ||
branchVars := map[string]string{ | ||
"REPO": repoName, | ||
"STORAGE": storage, | ||
"SOURCE_BRANCH": mainBranch, | ||
"DEST_BRANCH": branch, | ||
} | ||
cmd := NewLakeCtl(). | ||
Arg("branch create"). | ||
URLArg("lakefs://", repoName, branch). | ||
Flag("--source"). | ||
URLArg("lakefs://", repoName, mainBranch) | ||
RunCmdAndVerifySuccessWithFile(t, cmd.Get(), false, "lakectl_branch_create", branchVars) | ||
} |