Skip to content

Commit

Permalink
Fix flaky tests by removing the need to create physical files and doi…
Browse files Browse the repository at this point in the history
…ng everything in memory
  • Loading branch information
ArinGhazarian committed Nov 21, 2024
1 parent ac7ae2f commit ede7b84
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Octoshift/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OctoshiftCLI.Extensions
Expand All @@ -23,5 +24,7 @@ public static async Task<int> Sum<T>(this IEnumerable<T> list, Func<T, Task<int>
}

public static IEnumerable<T> ToEmptyEnumerableIfNull<T>(this IEnumerable<T> enumerable) => enumerable ?? Enumerable.Empty<T>();

public static string GetString(this byte[] bytes) => Encoding.UTF8.GetString(bytes.ToArray());
}
}
2 changes: 2 additions & 0 deletions src/Octoshift/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public static class StringExtensions
public static string ToUnixPath(this string path) => path?.Replace("\\", "/");

public static string EscapeDataString(this string value) => Uri.EscapeDataString(value);

public static byte[] ToBytes(this string s) => Encoding.UTF8.GetBytes(s);
}
}
2 changes: 1 addition & 1 deletion src/Octoshift/Services/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FileSystemProvider

public virtual FileStream Open(string path, FileMode mode) => File.Open(path, mode);

public virtual FileStream OpenRead(string path) => File.OpenRead(path);
public virtual Stream OpenRead(string path) => File.OpenRead(path);

public virtual async Task WriteAllTextAsync(string path, string contents) => await File.WriteAllTextAsync(path, contents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Moq;
using OctoshiftCLI.BbsToGithub.Commands.MigrateRepo;
using OctoshiftCLI.BbsToGithub.Services;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;
using Xunit;

Expand Down Expand Up @@ -350,17 +351,20 @@ public async Task Happy_Path_Uploads_To_Github_Storage()
var githubOrgDatabaseId = Guid.NewGuid().ToString();
const string gitArchiveFilePath = "./gitdata_archive";
const string gitArchiveUrl = "gei://archive/1";
const string gitArchiveContents = "I am git archive";

await File.WriteAllTextAsync(gitArchiveFilePath, "I am git archive");
await using var gitContentStream = File.OpenRead(gitArchiveFilePath);
await using var gitContentStream = new MemoryStream(gitArchiveContents.ToBytes());

_mockFileSystemProvider.Setup(m => m.OpenRead(gitArchiveFilePath)).Returns(gitContentStream);

_mockGithubApi.Setup(x => x.GetOrganizationId(GITHUB_ORG).Result).Returns(GITHUB_ORG_ID);
_mockGithubApi.Setup(x => x.CreateBbsMigrationSource(GITHUB_ORG_ID).Result).Returns(MIGRATION_SOURCE_ID);
_mockGithubApi.Setup(x => x.GetOrganizationDatabaseId(GITHUB_ORG).Result).Returns(githubOrgDatabaseId);
_mockGithubApi
.Setup(x => x.UploadArchiveToGithubStorage(githubOrgDatabaseId, It.IsAny<string>(), gitContentStream).Result)
.Setup(x => x.UploadArchiveToGithubStorage(
githubOrgDatabaseId,
It.IsAny<string>(),
It.Is<Stream>(s => (s as MemoryStream).ToArray().GetString() == gitArchiveContents)).Result)
.Returns(gitArchiveUrl);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.GithubEnterpriseImporter.Commands.MigrateRepo;
using OctoshiftCLI.GithubEnterpriseImporter.Services;
using OctoshiftCLI.Services;
Expand Down Expand Up @@ -345,16 +346,13 @@ public async Task Happy_Path_UseGithubStorage(bool useGhesBlobCredentials)
var metadataArchiveUrl = $"https://example.com/{metadataArchiveId}";
var uploadedGitArchiveUrl = "gei://archive/1";
var uploadedMetadataArchiveUrl = "gei://archive/2";
var gitArchiveFilePath = "./gitdata_archive";
var metadataArchiveFilePath = "./metadata_archive";
var gitArchiveDownloadFilePath = "git_archive_downaloded.tmp";
var metadataArchiveDownloadFilePath = "metadata_archive_downloaded.tmp";
var gitArchiveContents = "I am git archive";
var metadataArchiveContents = "I am metadata archive";

File.WriteAllText(gitArchiveFilePath, "I am git archive");
File.WriteAllText(metadataArchiveFilePath, "I am metadata archive");

using var gitContentStream = File.OpenRead(gitArchiveFilePath);
using var metaContentStream = File.OpenRead(metadataArchiveFilePath);
using var gitContentStream = new MemoryStream(gitArchiveContents.ToBytes());
using var metaContentStream = new MemoryStream(metadataArchiveContents.ToBytes());

_mockFileSystemProvider
.SetupSequence(m => m.GetTempFileName())
Expand All @@ -372,7 +370,6 @@ public async Task Happy_Path_UseGithubStorage(bool useGhesBlobCredentials)
_mockGhesVersionChecker.Setup(m => m.AreBlobCredentialsRequired(GHES_API_URL)).ReturnsAsync(useGhesBlobCredentials);

_mockTargetGithubApi.Setup(x => x.DoesOrgExist(TARGET_ORG).Result).Returns(true);

_mockTargetGithubApi.Setup(x => x.GetOrganizationId(TARGET_ORG).Result).Returns(githubOrgId);
_mockTargetGithubApi.Setup(x => x.GetOrganizationDatabaseId(TARGET_ORG).Result).Returns(githubOrgDatabaseId);
_mockTargetGithubApi.Setup(x => x.CreateGhecMigrationSource(githubOrgId).Result).Returns(migrationSourceId);
Expand Down Expand Up @@ -402,11 +399,17 @@ public async Task Happy_Path_UseGithubStorage(bool useGhesBlobCredentials)
_mockSourceGithubApi.Setup(x => x.GetArchiveMigrationUrl(SOURCE_ORG, metadataArchiveId).Result).Returns(metadataArchiveUrl);

_mockTargetGithubApi
.Setup(x => x.UploadArchiveToGithubStorage(githubOrgDatabaseId, It.Is<string>(a => a.EndsWith("git_archive.tar.gz")), gitContentStream).Result)
.Setup(x => x.UploadArchiveToGithubStorage(
githubOrgDatabaseId,
It.Is<string>(a => a.EndsWith("git_archive.tar.gz")),
It.Is<Stream>(s => (s as MemoryStream).ToArray().GetString() == gitArchiveContents)).Result)
.Returns(uploadedGitArchiveUrl);

_mockTargetGithubApi
.Setup(x => x.UploadArchiveToGithubStorage(githubOrgDatabaseId, It.Is<string>(a => a.EndsWith("metadata_archive.tar.gz")), metaContentStream).Result)
.Setup(x => x.UploadArchiveToGithubStorage(
githubOrgDatabaseId,
It.Is<string>(a => a.EndsWith("metadata_archive.tar.gz")),
It.Is<Stream>(s => (s as MemoryStream).ToArray().GetString() == metadataArchiveContents)).Result)
.Returns(uploadedMetadataArchiveUrl);

var args = new MigrateRepoCommandArgs
Expand Down

0 comments on commit ede7b84

Please sign in to comment.