From 33806baf8efc16baaa35f8fc90cbd080d1f08234 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Thu, 2 Feb 2017 13:34:59 +0900 Subject: [PATCH 1/4] Rename to AbstractFileTransferSpec --- ...erSpecification.groovy => AbstractFileTransferSpec.groovy} | 2 +- .../groovy/org/hidetake/groovy/ssh/test/server/ScpSpec.groovy | 2 +- .../org/hidetake/groovy/ssh/test/server/SftpSpec.groovy | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/{AbstractFileTransferSpecification.groovy => AbstractFileTransferSpec.groovy} (99%) diff --git a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpecification.groovy b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy similarity index 99% rename from server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpecification.groovy rename to server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy index 618720b9..46f8a6b4 100644 --- a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpecification.groovy +++ b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy @@ -17,7 +17,7 @@ import static org.hidetake.groovy.ssh.test.server.FileDivCategory.DirectoryType. import static org.hidetake.groovy.ssh.test.server.FilenameUtils.toUnixPath @Use(FileDivCategory) -abstract class AbstractFileTransferSpecification extends Specification { +abstract class AbstractFileTransferSpec extends Specification { @Shared SshServer server diff --git a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/ScpSpec.groovy b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/ScpSpec.groovy index 1775254c..d13bee4a 100644 --- a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/ScpSpec.groovy +++ b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/ScpSpec.groovy @@ -5,7 +5,7 @@ import org.hidetake.groovy.ssh.session.transfer.FileTransferMethod import spock.lang.Timeout @Timeout(10) -class ScpSpec extends AbstractFileTransferSpecification { +class ScpSpec extends AbstractFileTransferSpec { def setupSpec() { server.commandFactory = new ScpCommandFactory() diff --git a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/SftpSpec.groovy b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/SftpSpec.groovy index f6bd6377..f55922af 100644 --- a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/SftpSpec.groovy +++ b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/SftpSpec.groovy @@ -6,7 +6,7 @@ import org.hidetake.groovy.ssh.operation.SftpException import static org.hidetake.groovy.ssh.test.server.FilenameUtils.toUnixPath -class SftpSpec extends AbstractFileTransferSpecification { +class SftpSpec extends AbstractFileTransferSpec { def setupSpec() { server.subsystemFactories = [new SftpSubsystemFactory()] @@ -14,7 +14,7 @@ class SftpSpec extends AbstractFileTransferSpecification { } - //FIXME: should be in AbstractFileTransferSpecification but put here due to bug of Apache SSHD + //FIXME: should be in AbstractFileTransferSpec but put here due to bug of Apache SSHD def "put(dir) should throw IOException if destination does not exist"() { given: def sourceDir = temporaryFolder.newFolder('source') From df8d0c10e760520f2f6a8671fc46f189c662cecf Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Fri, 3 Feb 2017 23:37:33 +0900 Subject: [PATCH 2/4] Migrate complex file transfer specs to os-integration-test (put) --- .../groovy/ssh/test/os/FileDivCategory.groovy | 31 ++ .../groovy/ssh/test/os/RemoteFixture.groovy | 108 +++++++ .../test/os/AbstractFileTransferSpec.groovy | 286 +++++++++++------- .../server/AbstractFileTransferSpec.groovy | 225 -------------- 4 files changed, 320 insertions(+), 330 deletions(-) create mode 100644 os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy create mode 100644 os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy diff --git a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy new file mode 100644 index 00000000..3fb38262 --- /dev/null +++ b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy @@ -0,0 +1,31 @@ +package org.hidetake.groovy.ssh.test.os + +@Category(File) +class FileDivCategory { + + File div(String child) { + new File(this as File, child) + } + + File div(DirectoryType type) { + switch (type) { + case DirectoryType.DIRECTORY: + assert mkdir() + break + + case DirectoryType.DIRECTORIES: + assert mkdirs() + break + + default: + throw new IllegalArgumentException("Unknown directory type: $type") + } + this + } + + static enum DirectoryType { + DIRECTORY, + DIRECTORIES + } + +} diff --git a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy new file mode 100644 index 00000000..a1fdce80 --- /dev/null +++ b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy @@ -0,0 +1,108 @@ +package org.hidetake.groovy.ssh.test.os + +import groovy.transform.Canonical +import groovy.util.logging.Slf4j +import org.hidetake.groovy.ssh.Ssh +import org.hidetake.groovy.ssh.core.Service +import org.hidetake.groovy.ssh.operation.SftpException +import org.junit.rules.ExternalResource + +@Slf4j +class RemoteFixture extends ExternalResource { + + private final Service ssh = Ssh.newService() + + @Override + protected void before() { + Fixture.createRemotes(ssh) + } + + @Override + protected void after() { + } + + static enum Mkdir { + remoteDir + } + + @Canonical + static class RemotePath { + Service ssh + String path + + RemotePath div(String child) { + new RemotePath(ssh, "$path/$child") + } + + RemotePath div(Mkdir ignore) { + ssh.run { + session(ssh.remotes.Default) { + execute("mkdir $path") + } + } + this + } + + String getText() { + ssh.run { + session(ssh.remotes.Default) { + get from: path + } + } + } + + RemotePath leftShift(String text) { + ssh.run { + session(ssh.remotes.Default) { + put text: text, into: path + } + } + this + } + + List list() { + ssh.run { + session(ssh.remotes.Default) { + sftp { + ls(path).findAll { !(it.filename in ['.', '..']) } + } + } + } as List + } + + boolean exists() { + try { + ssh.run { + session(ssh.remotes.Default) { + sftp { + stat(path) + } + } + } as List + true + } catch (SftpException e) { + false + } + } + + @Override + String toString() { + path + } + } + + RemotePath newFolder() { + def path = new RemotePath(ssh, Fixture.remoteTmpPath()) + path / Mkdir.remoteDir + path + } + + String content(String... pathElements) { + ssh.run { + session(ssh.remotes.Default) { + get from: pathElements.join('/') + } + } + } + +} diff --git a/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy b/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy index 0dbdb828..016eb5e9 100644 --- a/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy +++ b/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy @@ -1,19 +1,25 @@ package org.hidetake.groovy.ssh.test.os -import groovy.io.FileType import org.hidetake.groovy.ssh.Ssh import org.hidetake.groovy.ssh.core.Service import org.junit.Rule import org.junit.rules.TemporaryFolder import spock.lang.Specification +import spock.lang.Unroll +import spock.util.mop.Use -import static org.hidetake.groovy.ssh.test.os.Fixture.* +import static org.hidetake.groovy.ssh.test.os.FileDivCategory.DirectoryType.DIRECTORIES +import static org.hidetake.groovy.ssh.test.os.FileDivCategory.DirectoryType.DIRECTORY +import static org.hidetake.groovy.ssh.test.os.Fixture.createRemotes +import static org.hidetake.groovy.ssh.test.os.Fixture.remoteTmpPath +import static org.hidetake.groovy.ssh.test.os.RemoteFixture.Mkdir.remoteDir /** * Check if file transfer works with OpenSSH. * * @author Hidetake Iwata */ +@Use(FileDivCategory) abstract class AbstractFileTransferSpec extends Specification { Service ssh @@ -21,186 +27,256 @@ abstract class AbstractFileTransferSpec extends Specification { @Rule TemporaryFolder temporaryFolder + @Rule + RemoteFixture remoteFixture + def setup() { ssh = Ssh.newService() createRemotes(ssh) } - def 'should put, compute and get files'() { - given: - def x = randomInt() - def y = randomInt() - - def localX = temporaryFolder.newFile() << x - def localY = temporaryFolder.newFile() << y - def localA = temporaryFolder.newFile() - def localB = temporaryFolder.newFile() - def remoteX = remoteTmpPath() - def remoteY = remoteTmpPath() - def remoteA = remoteTmpPath() - def remoteB = remoteTmpPath() - - when: + def newRemoteTemporaryFolder() { + def folder = remoteTmpPath() ssh.run { session(ssh.remotes.Default) { - put from: localX, into: remoteX - put from: localY, into: remoteY - execute "expr `cat $remoteX` + `cat $remoteY` > $remoteA" - execute "expr `cat $remoteX` - `cat $remoteY` > $remoteB" - get from: remoteA, into: localA - get from: remoteB, into: localB + execute("mkdir -vp $folder") } } + folder + } - then: - localA.text as int == (x + y) - localB.text as int == (x - y) + def getRemoteContent(String path) { + ssh.run { + session(ssh.remotes.Default) { + get from: path + } + } } - def 'should put contents and compute'() { - given: - def x = randomInt() - def y = randomInt() - def remoteX = remoteTmpPath() - def remoteY = remoteTmpPath() + // + // [PUT] file transfer + // + + def "put(file) should create a file if destination is a non-existent file in a directory"() { + given: + def sourceFile = temporaryFolder.newFile() << 'Source Content' + def destinationDir = remoteFixture.newFolder() + def destinationFile = destinationDir / 'file1' when: - def result = ssh.run { + ssh.run { session(ssh.remotes.Default) { - put text: x, into: remoteX - put bytes: y.toString().bytes, into: remoteY - [a: execute("expr `cat $remoteX` + `cat $remoteY`"), - b: execute("expr `cat $remoteX` - `cat $remoteY`")] + put from: sourceFile.path, into: destinationFile.path } } then: - result.a as int == (x + y) - result.b as int == (x - y) + destinationFile.text == 'Source Content' } - def 'should merge and overwrite a directory recursively on put'() { + def "put(file) should create a file if destination is an existent directory"() { given: - def x = randomInt() - def y = randomInt() - def z = randomInt() - - and: 'prepare the local folder' - def localFolder = temporaryFolder.newFolder() - new File(localFolder, 'Y/Z').mkdirs() - new File(localFolder, 'xfile') << x - new File(localFolder, 'Y/yfile') << y - new File(localFolder, 'Y/Z/zfile') << z - - and: 'prepare the remote folder' - def remoteFolder = remoteTmpPath() + def sourceFile = temporaryFolder.newFile() << 'Source Content' + def destinationDir = remoteFixture.newFolder() + destinationDir / (sourceFile.name) << 'Destination Content 1' + destinationDir / 'file2' << 'Destination Content 2' + + when: ssh.run { session(ssh.remotes.Default) { - execute "mkdir -vp $remoteFolder/${localFolder.name}/Y" - execute "echo -n dummy1 > $remoteFolder/${localFolder.name}/Y/yfile" - execute "echo -n dummy2 > $remoteFolder/${localFolder.name}/Y/yfile2" + put from: sourceFile, into: destinationDir } } + then: 'destination should be overwrote' + (destinationDir / sourceFile.name).text == 'Source Content' + + and: 'destination should be kept as-is' + (destinationDir / 'file2').text == 'Destination Content 2' + } + + def "put(file) should overwrite a file if destination is an existent file"() { + given: + def sourceFile = temporaryFolder.newFile('file1') << 'Source Content' + def destinationDir = remoteFixture.newFolder() + destinationDir / 'file1' << 'Destination Content' + when: ssh.run { session(ssh.remotes.Default) { - put from: localFolder, into: remoteFolder + put from: sourceFile.path, into: "$destinationDir/file1" } } - and: - def result = ssh.run { + then: + (destinationDir / 'file1').text == 'Source Content' + } + + def "put(file) should throw IOException if destination and its parent do not exist"() { + given: + def sourceFile = temporaryFolder.newFile() << 'Source Content' + def destinationFile = remoteFixture.newFolder() / 'dir1' / 'file1' + + when: + ssh.run { session(ssh.remotes.Default) { - [x: get(from: "$remoteFolder/${localFolder.name}/xfile"), - y: get(from: "$remoteFolder/${localFolder.name}/Y/yfile"), - y2: get(from: "$remoteFolder/${localFolder.name}/Y/yfile2"), - z: get(from: "$remoteFolder/${localFolder.name}/Y/Z/zfile")] + put from: sourceFile, into: destinationFile.path } } then: - result.x as int == x - result.y as int == y - result.y2 == 'dummy2' - result.z as int == z + IOException e = thrown() + e.message.contains(destinationFile.path) } - def 'should merge and overwrite a directory recursively on get'() { + + + // + // [PUT] directory transfer + // + + def "put(dir) should create a directory if destination is an existent directory"() { given: - def x = randomInt() - def y = randomInt() - def z = randomInt() - - and: 'prepare the local folder' - def localFolder = temporaryFolder.newFolder() - new File(localFolder, 'X/Y').mkdirs() - new File(localFolder, 'X/Y/yfile') << 'dummy1' - new File(localFolder, 'X/Y/yfile2') << 'dummy2' - - and: 'prepare the remote folder' - def remoteFolder = remoteTmpPath() + def sourceDir = temporaryFolder.newFolder() + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / DIRECTORY + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / DIRECTORY + + def destinationDir = remoteFixture.newFolder() + + when: ssh.run { session(ssh.remotes.Default) { - execute "mkdir -vp $remoteFolder/X/Y/Z" - execute "echo $x > $remoteFolder/X/xfile" - execute "echo $y > $remoteFolder/X/Y/yfile" - execute "echo $z > $remoteFolder/X/Y/Z/zfile" + put from: sourceDir, into: destinationDir.path } } + then: + (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' + (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' + (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] + } + + def "put(dir) should create a directory even if source is empty"() { + given: + def sourceDir = temporaryFolder.newFolder() + def destinationDir = remoteFixture.newFolder() + when: ssh.run { session(ssh.remotes.Default) { - get from: "$remoteFolder/X", into: localFolder + put from: sourceDir, into: destinationDir.path } } then: - new File(localFolder, 'X/xfile').text as int == x - new File(localFolder, 'X/Y/yfile').text as int == y - new File(localFolder, 'X/Y/yfile2').text == 'dummy2' - new File(localFolder, 'X/Y/Z/zfile').text as int == z + (destinationDir / sourceDir.name).list() == [] + } + + def "put(dir) should overwrite a directory if destination already exists"() { + given: + def sourceDir = temporaryFolder.newFolder() + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / DIRECTORY + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / DIRECTORY + + and: + def destinationDir = remoteFixture.newFolder() + destinationDir / sourceDir.name / remoteDir + destinationDir / sourceDir.name / 'file1' << 'Destination Content 1' + destinationDir / sourceDir.name / 'dir2' / remoteDir + destinationDir / sourceDir.name / 'dir2' / 'file2' << 'Destination Content 2' + destinationDir / sourceDir.name / 'dir2' / 'dir3' / remoteDir when: - def filesInLocalFolder = [] - localFolder.traverse(type: FileType.FILES) { filesInLocalFolder << it } + ssh.run { + session(ssh.remotes.Default) { + put from: sourceDir, into: destinationDir.path + } + } then: - filesInLocalFolder.size() == 4 + (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' + (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' + (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] } - def 'should get a large file and put it back'() { + //FIXME: put into SftpSpec due to bug of Apache SSHD + //def "put(dir) should throw IOException if destination does not exist"() { + + def "put(dir) should put a directory recursively"() { given: - def sizeKB = 1024 * 8 + def sourceDir = temporaryFolder.newFolder() + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / DIRECTORIES + + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / 'file3' << 'Source Content 3' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'file4' << 'Source Content 4' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5' << 'Source Content 5' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6' << 'Source Content 6' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7' << 'Source Content 7' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8' << 'Source Content 8' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9' << 'Source Content 9' - def localX = temporaryFolder.newFile() - def remoteX = remoteTmpPath() - def remoteY = remoteTmpPath() + def destinationDir = remoteFixture.newFolder() when: ssh.run { session(ssh.remotes.Default) { - execute "dd if=/dev/zero of=$remoteX bs=1024 count=$sizeKB" - get from: remoteX, into: localX - remove remoteX + put from: sourceDir.path, into: destinationDir } } then: - localX.size() == 1024 * sizeKB + (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' + (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'file3').text == 'Source Content 3' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'file4').text == 'Source Content 4' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5').text == 'Source Content 5' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6').text == 'Source Content 6' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7').text == 'Source Content 7' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8').text == 'Source Content 8' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9').text == 'Source Content 9' + } + + @Unroll + def "put(dir) should put filtered files with regex #regex"() { + given: + def sourceDir = temporaryFolder.newFolder() + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / DIRECTORY + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / DIRECTORY + + def destinationDir = remoteFixture.newFolder() when: - def actualSize = ssh.run { + ssh.run { session(ssh.remotes.Default) { - put from: localX, into: remoteY - execute("wc -c < $remoteY") as int + put from: sourceDir, into: destinationDir.path, filter: { it.name =~ regex } } } then: - actualSize == 1024 * sizeKB + (destinationDir / sourceDir.name).exists() == d1 + (destinationDir / sourceDir.name / 'file1').exists() == f1 + (destinationDir / sourceDir.name / 'dir2').exists() == d2 + (destinationDir / sourceDir.name / 'dir2' / 'file2').exists() == f2 + + and: 'empty directory should not be put' + !(destinationDir / sourceDir.name / 'dir2' / 'dir3').exists() + + where: + regex | d1 | f1 | d2 | f2 + /0$/ | false | false | false | false + /1$/ | true | true | false | false + /2$/ | true | false | true | true } + + } diff --git a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy index 46f8a6b4..c79bf3ca 100644 --- a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy +++ b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy @@ -247,231 +247,6 @@ abstract class AbstractFileTransferSpec extends Specification { - // - // [PUT] file transfer - // - - def "put(file) should create a file if destination is a non-existent file in a directory"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - def destinationFile = temporaryFolder.newFolder() / 'file1' - assert !destinationFile.exists() - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceFile.path, into: toUnixPath(destinationFile.path) - } - } - - then: - destinationFile.text == 'Source Content' - } - - def "put(file) should create a file if destination is an existent directory"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - - def destinationDir = temporaryFolder.newFolder() - def destination1File = destinationDir / sourceFile.name << 'Destination Content 1' - def destination2File = destinationDir / 'file2' << 'Destination Content 2' - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceFile, into: toUnixPath(destinationDir.path) - } - } - - then: 'destination should be overwrote' - destination1File.text == 'Source Content' - - and: 'destination should be kept as-is' - destination2File.text == 'Destination Content 2' - } - - def "put(file) should overwrite a file if destination is an existent file"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - def destinationFile = temporaryFolder.newFile() << 'Destination Content' - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceFile.path, into: toUnixPath(destinationFile.path) - } - } - - then: - destinationFile.text == 'Source Content' - } - - def "put(file) should throw IOException if destination and its parent do not exist"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - def destinationFile = temporaryFolder.newFolder() / 'dir1' / 'file1' - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceFile, into: toUnixPath(destinationFile.path) - } - } - - then: - IOException e = thrown() - e.message.contains(toUnixPath(destinationFile.path)) - } - - - - // - // [PUT] directory transfer - // - - def "put(dir) should create a directory if destination is an existent directory"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / DIRECTORY - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / DIRECTORY - - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceDir, into: toUnixPath(destinationDir.path) - } - } - - then: - (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' - (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' - (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] - } - - def "put(dir) should create a directory even if source is empty"() { - given: - def sourceDir = temporaryFolder.newFolder() - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceDir, into: toUnixPath(destinationDir.path) - } - } - - then: - (destinationDir / sourceDir.name).list() == [] - } - - def "put(dir) should overwrite a directory if destination already exists"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / DIRECTORY - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / DIRECTORY - - and: - def destinationDir = temporaryFolder.newFolder() - destinationDir / sourceDir.name / DIRECTORY - destinationDir / sourceDir.name / 'file1' << 'Destination Content 1' - destinationDir / sourceDir.name / 'dir2' / DIRECTORY - destinationDir / sourceDir.name / 'dir2' / 'file2' << 'Destination Content 2' - destinationDir / sourceDir.name / 'dir2' / 'dir3' / DIRECTORY - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceDir, into: toUnixPath(destinationDir.path) - } - } - - then: - (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' - (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' - (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] - } - - //FIXME: put into SftpSpec due to bug of Apache SSHD - //def "put(dir) should throw IOException if destination does not exist"() { - - def "put(dir) should put a directory recursively"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / DIRECTORIES - - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / 'file3' << 'Source Content 3' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'file4' << 'Source Content 4' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5' << 'Source Content 5' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6' << 'Source Content 6' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7' << 'Source Content 7' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8' << 'Source Content 8' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9' << 'Source Content 9' - - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: toUnixPath(sourceDir.path), into: destinationDir - } - } - - then: - (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' - (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'file3').text == 'Source Content 3' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'file4').text == 'Source Content 4' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5').text == 'Source Content 5' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6').text == 'Source Content 6' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7').text == 'Source Content 7' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8').text == 'Source Content 8' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9').text == 'Source Content 9' - } - - @Unroll - def "put(dir) should put filtered files with regex #regex"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / DIRECTORY - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / DIRECTORY - - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - put from: sourceDir, into: toUnixPath(destinationDir.path), filter: { it.name =~ regex } - } - } - - then: - (destinationDir / sourceDir.name).exists() == d1 - (destinationDir / sourceDir.name / 'file1').exists() == f1 - (destinationDir / sourceDir.name / 'dir2').exists() == d2 - (destinationDir / sourceDir.name / 'dir2' / 'file2').exists() == f2 - - and: 'empty directory should not be put' - !(destinationDir / sourceDir.name / 'dir2' / 'dir3').exists() - - where: - regex | d1 | f1 | d2 | f2 - /0$/ | false | false | false | false - /1$/ | true | true | false | false - /2$/ | true | false | true | true - } - - - // // [GET] arguments // From a0095ddbd250b081d5f82f5b3c552462d3336578 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Fri, 10 Feb 2017 20:07:17 +0900 Subject: [PATCH 3/4] Migrate complex file transfer specs to os-integration-test (get) --- .../groovy/ssh/test/os/RemoteFixture.groovy | 13 +- .../test/os/AbstractFileTransferSpec.groovy | 238 +++++++++++++++++ .../server/AbstractFileTransferSpec.groovy | 243 ------------------ 3 files changed, 245 insertions(+), 249 deletions(-) diff --git a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy index a1fdce80..cc17a4ce 100644 --- a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy +++ b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy @@ -43,6 +43,10 @@ class RemoteFixture extends ExternalResource { this } + String getName() { + path.substring(path.lastIndexOf('/')) + } + String getText() { ssh.run { session(ssh.remotes.Default) { @@ -97,12 +101,9 @@ class RemoteFixture extends ExternalResource { path } - String content(String... pathElements) { - ssh.run { - session(ssh.remotes.Default) { - get from: pathElements.join('/') - } - } + RemotePath newFile() { + def folder = newFolder() + folder / "${UUID.randomUUID()}" } } diff --git a/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy b/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy index 016eb5e9..6fae1981 100644 --- a/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy +++ b/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy @@ -279,4 +279,242 @@ abstract class AbstractFileTransferSpec extends Specification { + // + // [GET] file transfer + // + + def "get(file) should create a file if destination is a non-existent file in a directory"() { + given: + def sourceFile = remoteFixture.newFile() << 'Source Content' + def destinationFile = temporaryFolder.newFolder() / 'file1' + assert !destinationFile.exists() + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceFile.path, into: destinationFile + } + } + + then: + destinationFile.text == 'Source Content' + } + + def "get(file) should create a file if destination is an existent directory"() { + given: + def sourceFile = remoteFixture.newFile() << 'Source Content' + + def destinationDir = temporaryFolder.newFolder() + def destination1File = destinationDir / sourceFile.name << 'Destination Content 1' + def destination2File = destinationDir / 'file2' << 'Destination Content 2' + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceFile.path, into: destinationDir + } + } + + then: 'destination should be overwrote' + destination1File.text == 'Source Content' + + and: 'destination should be kept as-is' + destination2File.text == 'Destination Content 2' + } + + def "get(file) should overwrite a file if destination is an existent file"() { + given: + def sourceFile = remoteFixture.newFile() << 'Source Content' + def destinationFile = temporaryFolder.newFile() << 'Destination Content' + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceFile.path, into: destinationFile.path + } + } + + then: + sourceFile.text == 'Source Content' + destinationFile.text == 'Source Content' + } + + def "get(file) should throw IOException if destination and its parent do not exist"() { + given: + def sourceFile = remoteFixture.newFile() << 'Source Content' + def destinationFile = temporaryFolder.newFolder() / 'dir1' / 'file1' + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceFile.path, into: destinationFile + } + } + + then: + IOException e = thrown() + e.message.contains(destinationFile.path) + } + + + // + // [GET] directory transfer + // + + def "get(dir) should create a directory if destination is an existent directory"() { + given: + def sourceDir = remoteFixture.newFolder() + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / remoteDir + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / remoteDir + + def destinationDir = temporaryFolder.newFolder() + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceDir.path, into: destinationDir + } + } + + then: + (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' + (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' + (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] + } + + def "get(dir) should create a directory even if source is empty"() { + given: + def sourceDir = remoteFixture.newFolder() + def destinationDir = temporaryFolder.newFolder() + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceDir.path, into: destinationDir + } + } + + then: + (destinationDir / sourceDir.name).list() == [] + } + + def "get(dir) should overwrite a directory if destination already exists"() { + given: + def sourceDir = remoteFixture.newFolder() + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / remoteDir + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / remoteDir + + and: + def destinationDir = temporaryFolder.newFolder() + destinationDir / sourceDir.name / DIRECTORY + destinationDir / sourceDir.name / 'file1' << 'Destination Content 1' + destinationDir / sourceDir.name / 'dir2' / DIRECTORY + destinationDir / sourceDir.name / 'dir2' / 'file2' << 'Destination Content 2' + destinationDir / sourceDir.name / 'dir2' / 'dir3' / DIRECTORY + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceDir.path, into: destinationDir + } + } + + then: + (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' + (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' + (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] + } + + def "get(dir) should throw IOException if destination does not exist"() { + given: + def sourceDir = remoteFixture.newFolder() + def destinationDir = temporaryFolder.newFolder() / 'dir1' + assert !destinationDir.exists() + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceDir.path, into: destinationDir + } + } + + then: + IOException e = thrown() + e.message.contains(destinationDir.path) + } + + def "get(dir) should get a directory recursively"() { + given: + def sourceDir = remoteFixture.newFolder() + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / remoteDir + + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / 'file3' << 'Source Content 3' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'file4' << 'Source Content 4' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5' << 'Source Content 5' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6' << 'Source Content 6' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7' << 'Source Content 7' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8' << 'Source Content 8' + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9' << 'Source Content 9' + + def destinationDir = temporaryFolder.newFolder() + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceDir.path, into: destinationDir + } + } + + then: + (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' + (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'file3').text == 'Source Content 3' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'file4').text == 'Source Content 4' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5').text == 'Source Content 5' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6').text == 'Source Content 6' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7').text == 'Source Content 7' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8').text == 'Source Content 8' + (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9').text == 'Source Content 9' + } + + @Unroll + def "get(dir) should get filtered files with regex #regex"() { + given: + def sourceDir = remoteFixture.newFolder() + sourceDir / 'file1' << 'Source Content 1' + sourceDir / 'dir2' / remoteDir + sourceDir / 'dir2' / 'file2' << 'Source Content 2' + sourceDir / 'dir2' / 'dir3' / remoteDir + + def destinationDir = temporaryFolder.newFolder() + + when: + ssh.run { + session(ssh.remotes.Default) { + get from: sourceDir.path, into: destinationDir, filter: { it.name =~ regex } + } + } + + then: + (destinationDir / sourceDir.name).exists() == d1 + (destinationDir / sourceDir.name / 'file1').exists() == f1 + (destinationDir / sourceDir.name / 'dir2').exists() == d2 + (destinationDir / sourceDir.name / 'dir2' / 'file2').exists() == f2 + + and: 'empty directory should not be get' + !(destinationDir / sourceDir.name / 'dir2' / 'dir3').exists() + + where: + regex | d1 | f1 | d2 | f2 + /0$/ | false | false | false | false + /1$/ | true | true | false | false + /2$/ | true | false | true | true + } + } diff --git a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy index c79bf3ca..d31de6cd 100644 --- a/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy +++ b/server-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/server/AbstractFileTransferSpec.groovy @@ -12,8 +12,6 @@ import spock.lang.Unroll import spock.util.concurrent.PollingConditions import spock.util.mop.Use -import static org.hidetake.groovy.ssh.test.server.FileDivCategory.DirectoryType.DIRECTORIES -import static org.hidetake.groovy.ssh.test.server.FileDivCategory.DirectoryType.DIRECTORY import static org.hidetake.groovy.ssh.test.server.FilenameUtils.toUnixPath @Use(FileDivCategory) @@ -404,245 +402,4 @@ abstract class AbstractFileTransferSpec extends Specification { e.message.contains(toUnixPath(sourceFile.path)) } - - - // - // [GET] file transfer - // - - def "get(file) should create a file if destination is a non-existent file in a directory"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - def destinationFile = temporaryFolder.newFolder() / 'file1' - assert !destinationFile.exists() - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceFile.path), into: destinationFile - } - } - - then: - destinationFile.text == 'Source Content' - } - - def "get(file) should create a file if destination is an existent directory"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - - def destinationDir = temporaryFolder.newFolder() - def destination1File = destinationDir / sourceFile.name << 'Destination Content 1' - def destination2File = destinationDir / 'file2' << 'Destination Content 2' - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceFile.path), into: destinationDir - } - } - - then: 'destination should be overwrote' - destination1File.text == 'Source Content' - - and: 'destination should be kept as-is' - destination2File.text == 'Destination Content 2' - } - - def "get(file) should overwrite a file if destination is an existent file"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - def destinationFile = temporaryFolder.newFile() << 'Destination Content' - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceFile.path), into: destinationFile.path - } - } - - then: - sourceFile.text == 'Source Content' - destinationFile.text == 'Source Content' - } - - def "get(file) should throw IOException if destination and its parent do not exist"() { - given: - def sourceFile = temporaryFolder.newFile() << 'Source Content' - def destinationFile = temporaryFolder.newFolder() / 'dir1' / 'file1' - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceFile.path), into: destinationFile - } - } - - then: - IOException e = thrown() - e.message.contains(toUnixPath(destinationFile.path)) - } - - - - // - // [GET] directory transfer - // - - def "get(dir) should create a directory if destination is an existent directory"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / DIRECTORY - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / DIRECTORY - - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceDir.path), into: destinationDir - } - } - - then: - (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' - (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' - (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] - } - - def "get(dir) should create a directory even if source is empty"() { - given: - def sourceDir = temporaryFolder.newFolder() - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceDir.path), into: destinationDir - } - } - - then: - (destinationDir / sourceDir.name).list() == [] - } - - def "get(dir) should overwrite a directory if destination already exists"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / DIRECTORY - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / DIRECTORY - - and: - def destinationDir = temporaryFolder.newFolder() - destinationDir / sourceDir.name / DIRECTORY - destinationDir / sourceDir.name / 'file1' << 'Destination Content 1' - destinationDir / sourceDir.name / 'dir2' / DIRECTORY - destinationDir / sourceDir.name / 'dir2' / 'file2' << 'Destination Content 2' - destinationDir / sourceDir.name / 'dir2' / 'dir3' / DIRECTORY - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceDir.path), into: destinationDir - } - } - - then: - (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' - (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' - (destinationDir / sourceDir.name / 'dir2' / 'dir3').list() == [] - } - - def "get(dir) should throw IOException if destination does not exist"() { - given: - def sourceDir = temporaryFolder.newFolder() - def destinationDir = temporaryFolder.newFolder() / 'dir1' - assert !destinationDir.exists() - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceDir.path), into: destinationDir - } - } - - then: - IOException e = thrown() - e.message.contains(toUnixPath(destinationDir.path)) - } - - def "get(dir) should get a directory recursively"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / DIRECTORIES - - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / 'file3' << 'Source Content 3' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'file4' << 'Source Content 4' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5' << 'Source Content 5' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6' << 'Source Content 6' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7' << 'Source Content 7' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8' << 'Source Content 8' - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9' << 'Source Content 9' - - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceDir.path), into: destinationDir - } - } - - then: - (destinationDir / sourceDir.name / 'file1').text == 'Source Content 1' - (destinationDir / sourceDir.name / 'dir2' / 'file2').text == 'Source Content 2' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'file3').text == 'Source Content 3' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'file4').text == 'Source Content 4' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'file5').text == 'Source Content 5' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'file6').text == 'Source Content 6' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'file7').text == 'Source Content 7' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'file8').text == 'Source Content 8' - (destinationDir / sourceDir.name / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / 'file9').text == 'Source Content 9' - } - - @Unroll - def "get(dir) should get filtered files with regex #regex"() { - given: - def sourceDir = temporaryFolder.newFolder() - sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / DIRECTORY - sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / DIRECTORY - - def destinationDir = temporaryFolder.newFolder() - - when: - ssh.run { - session(ssh.remotes.testServer) { - get from: toUnixPath(sourceDir.path), into: destinationDir, filter: { it.name =~ regex } - } - } - - then: - (destinationDir / sourceDir.name).exists() == d1 - (destinationDir / sourceDir.name / 'file1').exists() == f1 - (destinationDir / sourceDir.name / 'dir2').exists() == d2 - (destinationDir / sourceDir.name / 'dir2' / 'file2').exists() == f2 - - and: 'empty directory should not be get' - !(destinationDir / sourceDir.name / 'dir2' / 'dir3').exists() - - where: - regex | d1 | f1 | d2 | f2 - /0$/ | false | false | false | false - /1$/ | true | true | false | false - /2$/ | true | false | true | true - } - } From bef3bc30803864e81d08ef2d92dce7b70a21ff05 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Fri, 10 Feb 2017 20:21:26 +0900 Subject: [PATCH 4/4] Refactor MkdirType --- .../groovy/ssh/test/os/FileDivCategory.groovy | 13 +++----- .../groovy/ssh/test/os/MkdirType.groovy | 6 ++++ .../groovy/ssh/test/os/RemoteFixture.groovy | 31 +++++++++++++------ .../test/os/AbstractFileTransferSpec.groovy | 25 +++++++-------- 4 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/MkdirType.groovy diff --git a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy index 3fb38262..92106b33 100644 --- a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy +++ b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/FileDivCategory.groovy @@ -7,25 +7,20 @@ class FileDivCategory { new File(this as File, child) } - File div(DirectoryType type) { + File div(MkdirType type) { switch (type) { - case DirectoryType.DIRECTORY: + case MkdirType.DIRECTORY: assert mkdir() break - case DirectoryType.DIRECTORIES: + case MkdirType.DIRECTORIES: assert mkdirs() break default: - throw new IllegalArgumentException("Unknown directory type: $type") + throw new IllegalArgumentException("Unknown mkdir type: $type") } this } - static enum DirectoryType { - DIRECTORY, - DIRECTORIES - } - } diff --git a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/MkdirType.groovy b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/MkdirType.groovy new file mode 100644 index 00000000..4c0b4e71 --- /dev/null +++ b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/MkdirType.groovy @@ -0,0 +1,6 @@ +package org.hidetake.groovy.ssh.test.os + +enum MkdirType { + DIRECTORY, + DIRECTORIES +} diff --git a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy index cc17a4ce..1bf3ff44 100644 --- a/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy +++ b/os-integration-test/src/main/groovy/org/hidetake/groovy/ssh/test/os/RemoteFixture.groovy @@ -21,10 +21,6 @@ class RemoteFixture extends ExternalResource { protected void after() { } - static enum Mkdir { - remoteDir - } - @Canonical static class RemotePath { Service ssh @@ -34,11 +30,26 @@ class RemoteFixture extends ExternalResource { new RemotePath(ssh, "$path/$child") } - RemotePath div(Mkdir ignore) { - ssh.run { - session(ssh.remotes.Default) { - execute("mkdir $path") - } + RemotePath div(MkdirType type) { + switch (type) { + case MkdirType.DIRECTORY: + ssh.run { + session(ssh.remotes.Default) { + execute("mkdir $path") + } + } + break + + case MkdirType.DIRECTORIES: + ssh.run { + session(ssh.remotes.Default) { + execute("mkdir -p $path") + } + } + break + + default: + throw new IllegalArgumentException("Unknown mkdir type: $type") } this } @@ -97,7 +108,7 @@ class RemoteFixture extends ExternalResource { RemotePath newFolder() { def path = new RemotePath(ssh, Fixture.remoteTmpPath()) - path / Mkdir.remoteDir + path / MkdirType.DIRECTORY path } diff --git a/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy b/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy index 6fae1981..39acd946 100644 --- a/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy +++ b/os-integration-test/src/test/groovy/org/hidetake/groovy/ssh/test/os/AbstractFileTransferSpec.groovy @@ -8,11 +8,10 @@ import spock.lang.Specification import spock.lang.Unroll import spock.util.mop.Use -import static org.hidetake.groovy.ssh.test.os.FileDivCategory.DirectoryType.DIRECTORIES -import static org.hidetake.groovy.ssh.test.os.FileDivCategory.DirectoryType.DIRECTORY import static org.hidetake.groovy.ssh.test.os.Fixture.createRemotes import static org.hidetake.groovy.ssh.test.os.Fixture.remoteTmpPath -import static org.hidetake.groovy.ssh.test.os.RemoteFixture.Mkdir.remoteDir +import static org.hidetake.groovy.ssh.test.os.MkdirType.DIRECTORIES +import static org.hidetake.groovy.ssh.test.os.MkdirType.DIRECTORY /** * Check if file transfer works with OpenSSH. @@ -185,11 +184,11 @@ abstract class AbstractFileTransferSpec extends Specification { and: def destinationDir = remoteFixture.newFolder() - destinationDir / sourceDir.name / remoteDir + destinationDir / sourceDir.name / DIRECTORY destinationDir / sourceDir.name / 'file1' << 'Destination Content 1' - destinationDir / sourceDir.name / 'dir2' / remoteDir + destinationDir / sourceDir.name / 'dir2' / DIRECTORY destinationDir / sourceDir.name / 'dir2' / 'file2' << 'Destination Content 2' - destinationDir / sourceDir.name / 'dir2' / 'dir3' / remoteDir + destinationDir / sourceDir.name / 'dir2' / 'dir3' / DIRECTORY when: ssh.run { @@ -365,9 +364,9 @@ abstract class AbstractFileTransferSpec extends Specification { given: def sourceDir = remoteFixture.newFolder() sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / remoteDir + sourceDir / 'dir2' / DIRECTORY sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / remoteDir + sourceDir / 'dir2' / 'dir3' / DIRECTORY def destinationDir = temporaryFolder.newFolder() @@ -404,9 +403,9 @@ abstract class AbstractFileTransferSpec extends Specification { given: def sourceDir = remoteFixture.newFolder() sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / remoteDir + sourceDir / 'dir2' / DIRECTORY sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / remoteDir + sourceDir / 'dir2' / 'dir3' / DIRECTORY and: def destinationDir = temporaryFolder.newFolder() @@ -450,7 +449,7 @@ abstract class AbstractFileTransferSpec extends Specification { def "get(dir) should get a directory recursively"() { given: def sourceDir = remoteFixture.newFolder() - sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / remoteDir + sourceDir / 'dir2' / 'dir3' / 'dir4' / 'dir5' / 'dir6' / 'dir7' / 'dir8' / 'dir9' / DIRECTORIES sourceDir / 'file1' << 'Source Content 1' sourceDir / 'dir2' / 'file2' << 'Source Content 2' @@ -488,9 +487,9 @@ abstract class AbstractFileTransferSpec extends Specification { given: def sourceDir = remoteFixture.newFolder() sourceDir / 'file1' << 'Source Content 1' - sourceDir / 'dir2' / remoteDir + sourceDir / 'dir2' / DIRECTORY sourceDir / 'dir2' / 'file2' << 'Source Content 2' - sourceDir / 'dir2' / 'dir3' / remoteDir + sourceDir / 'dir2' / 'dir3' / DIRECTORY def destinationDir = temporaryFolder.newFolder()