public static String putObject(SwiftClientCfg cfg, File srcFile, String container, String fileName) { if (fileName == null) { fileName = srcFile.getName(); } Script command = new Script("/bin/bash", logger); command.add("-c"); command.add(String.format(CD_SRC, srcFile.getParent())+getUploadObjectCommand(cfg, getSwiftCLIPath(), container,fileName, srcFile.length())); OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); String result = command.execute(parser); if (result != null) { throw new CloudRuntimeException("Failed to upload file: " + result); } if (parser.getLines() != null) { String[] lines = parser.getLines().split("\\n"); for (String line : lines) { if (line.contains("Errno") || line.contains("failed") || line.contains("not found")) { throw new CloudRuntimeException("Failed to upload file: " + Arrays.toString(lines)); } } } return container + File.separator + srcFile.getName(); }
@Test public void testGetUploadCmdWithSegmentsBecauseOfSize() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn(null); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 5368709121L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd -S 5368709120"; assertThat(uploadCmd, is(equalTo(expected))); }
@Test public void testGetUploadCmd() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn(null); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 1024); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd"; assertThat(uploadCmd, is(equalTo(expected))); }
@Test public void testGetUploadCmdWithStoragePolicy() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn("policy1"); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 1024L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd --storage-policy \"policy1\""; assertThat(uploadCmd, is(equalTo(expected))); }
@Test public void testGetUploadCmdWithSegmentsAndStoragePolicy() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn("policy1"); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 5368709121L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd --storage-policy \"policy1\" -S 5368709120"; assertThat(uploadCmd, is(equalTo(expected))); }