diff --git a/cmd/hdfs/main.go b/cmd/hdfs/main.go index da783d3c..801d839f 100755 --- a/cmd/hdfs/main.go +++ b/cmd/hdfs/main.go @@ -32,6 +32,7 @@ Valid commands: cat SOURCE... head [-n LINES | -c BYTES] SOURCE... tail [-n LINES | -c BYTES] SOURCE... + test [-defsz] FILE... du [-sh] FILE... checksum FILE... get SOURCE [DEST] @@ -57,6 +58,13 @@ Valid commands: mkdirOpts = getopt.New() mkdirp = mkdirOpts.Bool('p') + testOpts = getopt.New() + testIsDir = testOpts.Bool('d') + testPathExists = testOpts.Bool('e') + testIsFile = testOpts.Bool('f') + testIsNonEmpty = testOpts.Bool('s') + testIsEmpty = testOpts.Bool('z') + touchOpts = getopt.New() touchc = touchOpts.Bool('c') @@ -95,6 +103,7 @@ func init() { duOpts.SetUsage(printHelp) getmergeOpts.SetUsage(printHelp) dfOpts.SetUsage(printHelp) + testOpts.SetUsage(printHelp) } func main() { @@ -148,6 +157,14 @@ func main() { case "df": dfOpts.Parse(argv) df(*dfh) + case "test": + testOpts.Parse(argv) + test(testOpts.Args(), testCommandFlags{ + exists: *testPathExists, + isFile: *testIsFile, + isDir: *testIsDir, + isEmpty: *testIsEmpty, + isNonEmpty: *testIsNonEmpty}) case "truncate": truncate(argv[1:]) // it's a seeeeecret command diff --git a/cmd/hdfs/test.go b/cmd/hdfs/test.go new file mode 100644 index 00000000..9a1ed648 --- /dev/null +++ b/cmd/hdfs/test.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "os" +) + +type testCommandFlags struct { + exists bool + isDir bool + isFile bool + isNonEmpty bool + isEmpty bool +} + +func test(args []string, flags testCommandFlags) { + if len(args) == 0 { + fatalWithUsage() + } + + expanded, client, err := getClientAndExpandedPaths(args) + if err != nil { + fatal(err) + } + + for _, p := range expanded { + fi, err := client.Stat(p) + if err != nil { + //fmt.Fprintln(os.Stderr, err) + status = 1 + continue + } + + if !doTest(fi, flags) { + status = 1 + continue + } + } +} + +func doTest(fi os.FileInfo, flags testCommandFlags) bool { + var result bool + var flags_count int + + if flags.exists { + result = true + flags_count++ + } + if flags.isDir { + result = fi.IsDir() + flags_count++ + } + if flags.isFile { + result = !fi.IsDir() + flags_count++ + } + if flags.isNonEmpty { + result = fi.Size() > 0 + flags_count++ + } + if flags.isEmpty { + result = fi.Size() == 0 + flags_count++ + } + + if flags_count != 1 { + fmt.Fprintln(os.Stderr, "Only one test flag is allowed") + return false + } + + return result +} diff --git a/cmd/hdfs/test/test.bats b/cmd/hdfs/test/test.bats new file mode 100644 index 00000000..a822ba13 --- /dev/null +++ b/cmd/hdfs/test/test.bats @@ -0,0 +1,131 @@ +#!/usr/bin/env bats + +load helper + +setup() { + $HDFS mkdir -p /_test_cmd/test/dir1 + $HDFS mkdir -p /_test_cmd/test/dir2 + $HDFS mkdir -p /_test_cmd/test/dir3 + $HDFS put $ROOT_TEST_DIR/testdata/empty.txt /_test_cmd/test/empty.txt + $HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/test/foo.txt + $HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/test/dir1/foo1.txt +} + +@test "exists for existent file" { + run $HDFS test -e /_test_cmd/test/foo.txt + assert_success +} + +@test "exists for existent dir" { + run $HDFS test -e /_test_cmd/test/dir1 + assert_success +} + +@test "exists for non existent file" { + run $HDFS test -e /_test_cmd/test/bar.txt + assert_failure +} + +@test "exists for non existent dir" { + run $HDFS test -e /_test_cmd/test/dir9 + assert_failure +} + +@test "isfile for existent file" { + run $HDFS test -f /_test_cmd/test/foo.txt + assert_success +} + +@test "isfile for existent dir" { + run $HDFS test -f /_test_cmd/test/dir1 + assert_failure +} + +@test "isfile for non existent file" { + run $HDFS test -f /_test_cmd/test/bar.txt + assert_failure +} + +@test "isfile for non existent dir" { + run $HDFS test -f /_test_cmd/test/dir9 + assert_failure +} + +@test "isdir for existent file" { + run $HDFS test -d /_test_cmd/test/foo.txt + assert_failure +} + +@test "isdir for existent dir" { + run $HDFS test -d /_test_cmd/test/dir1 + assert_success +} + +@test "isdir for non existent file" { + run $HDFS test -d /_test_cmd/test/bar.txt + assert_failure +} + +@test "isdir for non existent dir" { + run $HDFS test -d /_test_cmd/test/dir9 + assert_failure +} + +@test "isnonempty for existent empty file" { + run $HDFS test -s /_test_cmd/test/empty.txt + assert_failure +} + +@test "isnonempty for existent non empty file" { + run $HDFS test -s /_test_cmd/test/foo.txt + assert_success +} + +@test "isnonempty for existent dir" { + run $HDFS test -s /_test_cmd/test/dir1 + assert_failure +} + +@test "isnonempty for non existent file" { + run $HDFS test -s /_test_cmd/test/bar.txt + assert_failure +} + +@test "isnonempty for non existent dir" { + run $HDFS test -s /_test_cmd/test/dir9 + assert_failure +} + +@test "isempty for existent empty file" { + run $HDFS test -z /_test_cmd/test/empty.txt + assert_success +} + +@test "isempty for existent non empty file" { + run $HDFS test -z /_test_cmd/test/foo.txt + assert_failure +} + +# TOOD: check this outcome is correct +@test "isempty for existent dir" { + run $HDFS test -z /_test_cmd/test/dir1 + assert_success +} + +@test "isempty for non existent file" { + run $HDFS test -z /_test_cmd/test/bar.txt + assert_failure +} + +@test "isempty for non existent dir" { + run $HDFS test -z /_test_cmd/test/dir9 + assert_failure +} + +teardown() { + $HDFS rm -r /_test_cmd/test +} + +teardown() { + $HDFS rm -r /_test_cmd/test +} diff --git a/testdata/empty.txt b/testdata/empty.txt new file mode 100644 index 00000000..e69de29b