Skip to content

Commit

Permalink
Yet more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ebozduman committed Jul 27, 2022
1 parent 1f68384 commit 17fd7a1
Showing 1 changed file with 0 additions and 356 deletions.
356 changes: 0 additions & 356 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2748,362 +2748,6 @@ internal static async Task ObjectRetentionAsync_Test1(MinioClient minio)

#endregion


#region Bucket Notifications

internal static async Task ListenBucketNotificationsAsync_Test1(MinioClient minio)
{
var startTime = DateTime.Now;
var bucketName = GetRandomName(15);
var objectName = GetRandomName(10);
var contentType = "application/octet-stream";
IDisposable subscription = null;
var args = new Dictionary<string, string>
{
{ "bucketName", bucketName },
{ "objectName", objectName },
{ "contentType", contentType },
{ "size", "1KB" }
};
try
{
await Setup_Test(minio, bucketName);

var received = new List<MinioNotificationRaw>();

var eventsList = new List<EventType>();
eventsList.Add(EventType.ObjectCreatedAll);

var listenArgs = new ListenBucketNotificationsArgs()
.WithBucket(bucketName)
.WithEvents(eventsList);
var events = minio.ListenBucketNotificationsAsync(listenArgs);
subscription = events.Subscribe(
ev => received.Add(ev),
ex => { },
() => { }
);

await PutObject_Tester(minio, bucketName, objectName, null, contentType,
0, null, rsg.GenerateStreamFromSeed(1 * KB));

// wait for notifications
var eventDetected = false;
for (var attempt = 0; attempt < 10; attempt++)
if (received.Count > 0)
{
// Check if there is any unexpected error returned
// and captured in the receivedJson list, like
// "NotImplemented" api error. If so, we throw an exception
// and skip running this test
if (received.Count > 1 && received[1].json.StartsWith("<Error><Code>"))
{
// Although the attribute is called "json",
// returned data in list "received" is in xml
// format and it is an error.Here, we convert xml
// into json format.
var receivedJson = XmlStrToJsonStr(received[1].json);


// Cleanup the "Error" key encapsulating "receivedJson"
// data. This is required to match and convert json data
// "receivedJson" into class "ErrorResponse"
var len = "{'Error':".Length;
var trimmedFront = receivedJson.Substring(len);
var trimmedFull = trimmedFront.Substring(0, trimmedFront.Length - 1);

var err = JsonConvert.DeserializeObject<ErrorResponse>(trimmedFull);

Exception ex = new UnexpectedMinioException(err.Message);
if (err.Code == "NotImplemented")
ex = new NotImplementedException(err.Message);

throw ex;
}

var notification = JsonConvert.DeserializeObject<MinioNotification>(received[0].json);

if (notification.Records != null)
{
Assert.AreEqual(1, notification.Records.Length);
Assert.IsTrue(notification.Records[0].eventName.Contains("s3:ObjectCreated:Put"));
Assert.IsTrue(
objectName.Contains(HttpUtility.UrlDecode(notification.Records[0].s3.objectMeta.key)));
Assert.IsTrue(contentType.Contains(notification.Records[0].s3.objectMeta.contentType));
eventDetected = true;
break;
}
}

// subscription.Dispose();
if (!eventDetected)
throw new UnexpectedMinioException("Failed to detect the expected bucket notification event.");

new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
}
catch (NotImplementedException ex)
{
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.NA, DateTime.Now - startTime, ex.Message,
ex.ToString(), args: args).Log();
}
catch (Exception ex)
{
if (ex.Message == "Listening for bucket notification is specific" +
" only to `minio` server endpoints")
{
// This is expected when bucket notification
// is requested against AWS.
// Check if endPoint is AWS
bool isAWS(string endPoint)
{
var rgx = new Regex("^s3\\.?.*\\.amazonaws\\.com", RegexOptions.IgnoreCase);
var matches = rgx.Matches(endPoint);
return matches.Count > 0;
}

if (Environment.GetEnvironmentVariable("AWS_ENDPOINT") != null ||
isAWS(Environment.GetEnvironmentVariable("SERVER_ENDPOINT")))
// This is a PASS
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
}
else
{
new MintLogger(nameof(ListenBucketNotificationsAsync_Test1),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.FAIL, DateTime.Now - startTime, ex.Message,
ex.ToString(), args: args).Log();
throw;
}
}
finally
{
await TearDown(minio, bucketName);
if (subscription != null)
subscription.Dispose();
}
}


internal static async Task ListenBucketNotificationsAsync_Test2(MinioClient minio)
{
var startTime = DateTime.Now;
var events = new List<EventType>();
events.Add(EventType.ObjectCreatedAll);
var rxEvents = new List<NotificationEvent>();
IDisposable subscription = null;
var bucketName = GetRandomName(15);
var contentType = "application/json";
var args = new Dictionary<string, string>
{
{ "bucketName", bucketName },
{ "contentType", contentType },
{ "size", "16B" }
};

try
{
static Stream ToStream(string input)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(input);
writer.Flush();
stream.Position = 0;

return stream;
}

var bucketExistsArgs = new BucketExistsArgs()
.WithBucket(bucketName);
var found = await minio.BucketExistsAsync(bucketExistsArgs).ConfigureAwait(false);
if (!found)
{
var makeBucketArgs = new MakeBucketArgs()
.WithBucket(bucketName);
await minio.MakeBucketAsync(makeBucketArgs).ConfigureAwait(false);
}

void Notify(MinioNotificationRaw data)
{
var notification = JsonConvert.DeserializeObject<MinioNotification>(data.json);
if (notification is not { Records: { } }) return;

foreach (var @event in notification.Records) rxEvents.Add(@event);
}

var listenArgs = new ListenBucketNotificationsArgs()
.WithBucket(bucketName)
.WithEvents(events);
var observable = minio.ListenBucketNotificationsAsync(listenArgs);

subscription = observable.Subscribe(
ev => Notify(ev),
ex => throw new Exception($"OnError: {ex.Message}"),
() => throw new Exception("STOPPED LISTENING FOR BUCKET NOTIFICATIONS\n"));

var modelJson = "{\"test\": \"test\"}";
await using var stream = ToStream(modelJson);
var putObjectArgs = new PutObjectArgs()
.WithObject("test.json")
.WithBucket(bucketName)
.WithContentType(contentType)
.WithStreamData(stream)
.WithObjectSize(stream.Length);

await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);

// Waits until the Put event is detected
// Times out if the event is not caught in 3 seconds
var timeoutDuration = 3; // seconds
var stTime = DateTime.UtcNow;
var timeout = TimeSpan.FromSeconds(timeoutDuration);
while (rxEvents.Count < 1)
{
await Task.Delay(25);
if (DateTime.UtcNow - stTime >= timeout)
throw new Exception("Timeout: while waiting for events");
}

foreach (var ev in rxEvents) Assert.AreEqual("s3:ObjectCreated:Put", ev.eventName);

new MintLogger(nameof(ListenBucketNotificationsAsync_Test2),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for longer event processing",
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
}
catch (Exception ex)
{
new MintLogger(nameof(ListenBucketNotificationsAsync_Test2),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for longer event processing",
TestStatus.FAIL, DateTime.Now - startTime, ex.Message,
ex.ToString(), args: args).Log();
throw;
}

finally
{
await TearDown(minio, bucketName);
if (subscription != null)
subscription.Dispose();
}
}

internal static async Task ListenBucketNotificationsAsync_Test3(MinioClient minio)
{
var startTime = DateTime.Now;
var events = new List<EventType>();
events.Add(EventType.ObjectCreatedAll);
var rxEventsData = new MinioNotificationRaw("");
IDisposable disposable = null;
var bucketName = GetRandomName(15);
var suffix = ".json";
var contentType = "application/json";
var args = new Dictionary<string, string>
{
{ "bucketName", bucketName },
{ "contentType", contentType },
{ "suffix", suffix },
{ "size", "16B" }
};

try
{
var bucketExistsArgs = new BucketExistsArgs()
.WithBucket(bucketName);
if (!minio.BucketExistsAsync(bucketExistsArgs).Result)
{
var makeBucketArgs = new MakeBucketArgs()
.WithBucket(bucketName);
minio.MakeBucketAsync(makeBucketArgs).Wait();
}

var notificationsArgs = new ListenBucketNotificationsArgs()
.WithBucket(bucketName)
.WithSuffix(suffix)
.WithEvents(events);

var notifications = minio.ListenBucketNotificationsAsync(notificationsArgs);

var testState = "fail";
Exception exception = null;
disposable = notifications.Subscribe(
x =>
{
rxEventsData = x;
testState = "pass";
},
ex =>
{
exception = ex;
testState = "fail";
},
() => { testState = "completed"; });

var modelJson = "{\"test\": \"test\"}";
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(modelJson);
writer.Flush();
stream.Position = 0;

var putObjectArgs = new PutObjectArgs()
.WithObject("test.json")
.WithBucket(bucketName)
.WithContentType(contentType)
.WithStreamData(stream)
.WithObjectSize(stream.Length);

await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
Thread.Sleep(1000);

if (testState == "pass")
{
Assert.IsTrue(rxEventsData.json.Contains("\"eventName\":\"s3:ObjectCreated:Put\""));
new MintLogger(nameof(ListenBucketNotificationsAsync_Test3),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for no event processing",
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
}
else if (testState == "fail")
{
throw exception;
}
else if (testState == "completed")
{
throw new Exception("Bucket notification completed without catching the event");
}
}
catch (Exception ex)
{
new MintLogger(nameof(ListenBucketNotificationsAsync_Test3),
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for no event processing",
TestStatus.FAIL, DateTime.Now - startTime, ex.Message,
ex.ToString(), args: args).Log();
throw;
}

finally
{
await TearDown(minio, bucketName);
if (disposable != null)
disposable.Dispose();
}
}

#endregion

#region Make Bucket

internal static async Task MakeBucket_Test1(MinioClient minio)
Expand Down

0 comments on commit 17fd7a1

Please sign in to comment.