Skip to content

Commit cad623c

Browse files
committed
change upload to return the destination path
1 parent 8d61839 commit cad623c

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

src/main/java/io/hyperfoil/tools/qdup/cmd/impl/Upload.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.hyperfoil.tools.qdup.cmd.Cmd;
44
import io.hyperfoil.tools.qdup.cmd.Context;
55

6+
import java.io.File;
7+
68
public class Upload extends Cmd {
79
private String path;
810
private String destination;
@@ -19,15 +21,14 @@ public Upload(String path){
1921
public String getDestination(){return destination;}
2022
@Override
2123
public void run(String input, Context context) {
22-
2324
populatedPath = populateStateVariables(path,this, context);
2425
populatedDestination = populateStateVariables(destination ,this, context);
2526

2627
//create remote directory
2728
if(populatedDestination.endsWith("/")) {
28-
context.getShell().sh("mkdir -p " + populatedDestination);
29+
context.getShell().shSync("mkdir -p " + populatedDestination);
30+
populatedDestination+=(new File(populatedPath)).getName();
2931
}
30-
3132
boolean worked = context.getLocal().upload(
3233
populatedPath,
3334
populatedDestination,
@@ -37,6 +38,9 @@ public void run(String input, Context context) {
3738
context.error("failed to upload "+populatedPath+" to "+populatedDestination);
3839
context.abort(false);
3940
} else {
41+
if(context.getHost().isShell()){
42+
43+
}
4044
context.next(populatedDestination);
4145
}
4246
}

src/test/java/io/hyperfoil/tools/qdup/cmd/impl/UploadTest.java

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package io.hyperfoil.tools.qdup.cmd.impl;
22

3+
import io.hyperfoil.tools.qdup.Host;
34
import io.hyperfoil.tools.qdup.Run;
4-
import io.hyperfoil.tools.qdup.cmd.Cmd;
5-
import io.hyperfoil.tools.qdup.cmd.Dispatcher;
6-
import io.hyperfoil.tools.qdup.cmd.Script;
5+
import io.hyperfoil.tools.qdup.SecretFilter;
6+
import io.hyperfoil.tools.qdup.cmd.*;
77
import io.hyperfoil.tools.qdup.config.RunConfig;
88
import io.hyperfoil.tools.qdup.config.RunConfigBuilder;
99
import io.hyperfoil.tools.qdup.config.yaml.Parser;
10+
import io.hyperfoil.tools.qdup.shell.AbstractShell;
11+
import io.hyperfoil.tools.yaup.time.SystemTimer;
1012
import org.junit.Test;
1113
import io.hyperfoil.tools.qdup.SshTestBase;
1214

1315
import java.io.File;
1416
import java.io.IOException;
1517
import java.nio.file.Files;
18+
import java.nio.file.Paths;
19+
import java.util.Arrays;
1620
import java.util.HashMap;
21+
import java.util.concurrent.ScheduledThreadPoolExecutor;
1722
import java.util.stream.Collectors;
1823

19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.*;
2125

2226
public class UploadTest extends SshTestBase {
2327

@@ -49,7 +53,6 @@ public void uploadFile(){
4953

5054
assertFalse("unexpected errors:\n"+config.getErrorStrings().stream().collect(Collectors.joining("\n")),config.hasErrors());
5155

52-
5356
Dispatcher dispatcher = new Dispatcher();
5457
Run run = new Run(tmpDir.toString(),config,dispatcher);
5558
run.run();
@@ -62,4 +65,87 @@ public void uploadFile(){
6265
e.printStackTrace();
6366
}
6467
}
68+
69+
@Test
70+
public void return_remote_path_new_name() throws IOException {
71+
String wrote = "bizbuz";
72+
File source = Files.createTempFile("qdup","upload.txt").toFile();
73+
Files.write(source.toPath(),wrote.getBytes());
74+
File runFolder = Files.createTempDirectory("qdup").toFile();
75+
76+
Upload upload = new Upload(source.getPath(),"/tmp/found.txt");
77+
78+
AbstractShell shell = AbstractShell.getShell(
79+
Host.parse(Host.LOCAL),
80+
new ScheduledThreadPoolExecutor(2),
81+
new SecretFilter(),
82+
false
83+
);
84+
Run run = new Run(runFolder.getPath(),new RunConfigBuilder().buildConfig(),new Dispatcher());
85+
ScriptContext scriptContext = new ScriptContext(shell,run.getConfig().getState(),run,new SystemTimer("download"),upload,true);
86+
SpyContext spyContext = new SpyContext(scriptContext,run.getConfig().getState(), run.getCoordinator());
87+
upload.run("",spyContext);
88+
89+
assertNotNull("upload should call next",spyContext.getNext());
90+
String response = spyContext.getNext();
91+
assertEquals("response should match the destination","/tmp/found.txt",response);
92+
String readContent = Files.readString(Paths.get(response));
93+
assertEquals(wrote,readContent);
94+
}
95+
96+
@Test
97+
public void return_remote_path_target_folder() throws IOException {
98+
String wrote = "bizbuz";
99+
File source = Files.createTempFile("qdup.",".upload.txt").toFile();
100+
Files.write(source.toPath(),wrote.getBytes());
101+
File runFolder = Files.createTempDirectory("qdup_").toFile();
102+
103+
File tempFolder = Files.createTempDirectory("qdup_").toFile();
104+
//tempFolder.mkdirs();
105+
Upload upload = new Upload(source.getPath(),tempFolder.getAbsolutePath()+File.separator);
106+
107+
AbstractShell shell = AbstractShell.getShell(
108+
Host.parse(Host.LOCAL),
109+
new ScheduledThreadPoolExecutor(2),
110+
new SecretFilter(),
111+
false
112+
);
113+
Run run = new Run(runFolder.getPath(),new RunConfigBuilder().buildConfig(),new Dispatcher());
114+
ScriptContext scriptContext = new ScriptContext(shell,run.getConfig().getState(),run,new SystemTimer("download"),upload,true);
115+
SpyContext spyContext = new SpyContext(scriptContext,run.getConfig().getState(), run.getCoordinator());
116+
upload.run("",spyContext);
117+
118+
assertNotNull("upload should call next",spyContext.getNext());
119+
String response = spyContext.getNext();
120+
assertTrue("response should end with source name",response.endsWith(source.getName()));
121+
String readContent = Files.readString(Paths.get(response));
122+
assertEquals(wrote,readContent);
123+
}
124+
125+
@Test
126+
public void return_remote_path_target_file() throws IOException {
127+
String wrote = "bizbuz";
128+
File source = Files.createTempFile("qdup.",".upload.txt").toFile();
129+
Files.write(source.toPath(),wrote.getBytes());
130+
File runFolder = Files.createTempDirectory("qdup_").toFile();
131+
132+
Upload upload = new Upload(source.getPath(),"/tmp/renamed.txt");
133+
AbstractShell shell = AbstractShell.getShell(
134+
Host.parse(Host.LOCAL),
135+
new ScheduledThreadPoolExecutor(2),
136+
new SecretFilter(),
137+
false
138+
);
139+
Run run = new Run(runFolder.getPath(),new RunConfigBuilder().buildConfig(),new Dispatcher());
140+
ScriptContext scriptContext = new ScriptContext(shell,run.getConfig().getState(),run,new SystemTimer("download"),upload,true);
141+
SpyContext spyContext = new SpyContext(scriptContext,run.getConfig().getState(), run.getCoordinator());
142+
upload.run("",spyContext);
143+
144+
assertNotNull("upload should call next",spyContext.getNext());
145+
String response = spyContext.getNext();
146+
assertEquals("response should be the specified path","/tmp/renamed.txt",response);
147+
String readContent = Files.readString(Paths.get(response));
148+
assertEquals(wrote,readContent);
149+
}
150+
65151
}

0 commit comments

Comments
 (0)