Skip to content

Commit adca28b

Browse files
committed
Do nothing when MoveObject is called with the same src and dest
1 parent ce183af commit adca28b

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/corelib/Providers/Rackspace/CloudFilesProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,13 @@ public ExtractArchiveResponse ExtractArchive(Stream stream, string uploadPath, A
15411541
/// <inheritdoc />
15421542
public void MoveObject(string sourceContainer, string sourceObjectName, string destinationContainer, string destinationObjectName, string destinationContentType = null, Dictionary<string, string> headers = null, string region = null, bool useInternalUrl = false, CloudIdentity identity = null)
15431543
{
1544+
// Do nothing if the source and destination locations are the same. Prevents the object from being deleted accidentally.
1545+
var prefix = GetServiceEndpointCloudFiles(identity, region, useInternalUrl);
1546+
var src = new Uri($"{prefix}/{_encodeDecodeProvider.UrlEncode(sourceContainer)}/{_encodeDecodeProvider.UrlEncode(sourceObjectName)}");
1547+
var dest = new Uri($"{prefix}/{_encodeDecodeProvider.UrlEncode(destinationContainer)}/{_encodeDecodeProvider.UrlEncode(destinationObjectName)}");
1548+
if (src == dest)
1549+
return;
1550+
15441551
CopyObject(sourceContainer, sourceObjectName, destinationContainer, destinationObjectName, destinationContentType, headers, region, useInternalUrl, identity);
15451552
DeleteObject(sourceContainer, sourceObjectName, headers, true, region, useInternalUrl, identity);
15461553
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.Remoting;
6+
using System.Text;
7+
using net.openstack.Core.Exceptions.Response;
8+
using OpenStack;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
12+
namespace net.openstack.Providers.Rackspace
13+
{
14+
public class CloudFilesTests
15+
{
16+
private const string region = "RegionOne";
17+
private readonly CloudFilesProvider _cloudFiles;
18+
19+
public CloudFilesTests(ITestOutputHelper testLog)
20+
{
21+
var testOutput = new XunitTraceListener(testLog);
22+
Trace.Listeners.Add(testOutput);
23+
OpenStackNet.Tracing.Http.Listeners.Add(testOutput);
24+
25+
var authenticationProvider = TestIdentityProvider.GetIdentityProvider();
26+
authenticationProvider.Authenticate();
27+
28+
_cloudFiles = new CloudFilesProvider(null, authenticationProvider);
29+
}
30+
31+
public void Dispose()
32+
{
33+
Trace.Listeners.Clear();
34+
OpenStackNet.Tracing.Http.Listeners.Clear();
35+
}
36+
37+
[Fact]
38+
public void MoveObject()
39+
{
40+
const string container = "test";
41+
const string fileName = "helloworld.html";
42+
const string backupName = "helloworld.bak";
43+
44+
// Remove old test data
45+
try
46+
{
47+
_cloudFiles.DeleteObject(container, backupName, region: region);
48+
}
49+
catch (ItemNotFoundException)
50+
{
51+
}
52+
53+
// Create test data
54+
_cloudFiles.CreateContainer(container, region: region);
55+
var testObject = new MemoryStream(Encoding.UTF8.GetBytes("hello world"));
56+
_cloudFiles.CreateObject(container, testObject, fileName, region: region);
57+
58+
// Backup the file
59+
_cloudFiles.MoveObject(container, fileName, container, backupName, region: region);
60+
61+
// Verify the file moved
62+
var files = _cloudFiles.ListObjects(container, region: region);
63+
Assert.False(files.Any(f => f.Name == fileName));
64+
Assert.True(files.Any(f => f.Name == backupName));
65+
}
66+
67+
[Fact]
68+
public void MoveObject_ShouldDoNothing_WhenSourceAndDestinationAreTheSame()
69+
{
70+
const string container = "test";
71+
const string filename = "helloworld.html";
72+
73+
// Create test data
74+
_cloudFiles.CreateContainer(container, region: region);
75+
var testObject = new MemoryStream(Encoding.UTF8.GetBytes("hello world"));
76+
_cloudFiles.CreateObject(container, testObject, filename, region: region);
77+
78+
// Move the file to the same location
79+
_cloudFiles.MoveObject(container, filename, container, filename, region: region);
80+
81+
// Verify the file wasn't removed
82+
var files = _cloudFiles.ListObjects(container, region: region);
83+
Assert.True(files.Any(f => f.Name == filename));
84+
}
85+
}
86+
}

src/testing/integration/OpenStack.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<Compile Include="Identity\v2\IdentityTests.cs" />
124124
<Compile Include="Networking\v2\NetworkingTestDataManager.cs" />
125125
<Compile Include="Networking\v2\NetworkingServiceTests.cs" />
126+
<Compile Include="ObjectStorage\CloudFilesProviderTests.cs" />
126127
<Compile Include="Properties\AssemblyInfo.cs" />
127128
<Compile Include="Providers\Rackspace\CloudNetworksTests.cs" />
128129
<Compile Include="Providers\Rackspace\CloudServersTests.cs" />

0 commit comments

Comments
 (0)