Skip to content

Commit

Permalink
Add AWS arguments to generate-script commands (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
tambling authored Oct 25, 2022
1 parent 0e7883f commit 54c46b8
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 109 deletions.
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Added additional retry logic covering the case when polling for migration status fails for any reason (along with a few other situations)
- Added `--aws-bucket-name` to `gh gei generate-script` and removed `--azure-storage-connection-string`.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void Should_Have_Options()
TestHelpers.VerifyCommandOption(command.Options, "ado-team-project", false, true);
TestHelpers.VerifyCommandOption(command.Options, "github-target-org", true);
TestHelpers.VerifyCommandOption(command.Options, "ghes-api-url", false);
TestHelpers.VerifyCommandOption(command.Options, "azure-storage-connection-string", false);
TestHelpers.VerifyCommandOption(command.Options, "no-ssl-verify", false);
TestHelpers.VerifyCommandOption(command.Options, "skip-releases", false);
TestHelpers.VerifyCommandOption(command.Options, "lock-source-repo", false);
Expand All @@ -56,6 +55,7 @@ public void Should_Have_Options()
TestHelpers.VerifyCommandOption(command.Options, "github-source-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "ado-pat", false, true);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "aws-bucket-name", false);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Octoshift.Models;
using OctoshiftCLI.Contracts;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.GithubEnterpriseImporter;
using OctoshiftCLI.GithubEnterpriseImporter.Commands;
using OctoshiftCLI.GithubEnterpriseImporter.Handlers;
using Xunit;
Expand All @@ -19,7 +18,6 @@ public class GenerateScriptCommandHandlerTests
{
private readonly Mock<GithubApi> _mockGithubApi = TestHelpers.CreateMock<GithubApi>();
private readonly Mock<AdoApi> _mockAdoApi = TestHelpers.CreateMock<AdoApi>();
private readonly Mock<EnvironmentVariableProvider> _mockEnvironmentVariableProvider = TestHelpers.CreateMock<EnvironmentVariableProvider>();
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>();
private readonly Mock<IVersionProvider> _mockVersionProvider = new Mock<IVersionProvider>();

Expand All @@ -28,6 +26,7 @@ public class GenerateScriptCommandHandlerTests
private const string SOURCE_ORG = "FOO-SOURCE-ORG";
private const string TARGET_ORG = "FOO-TARGET-ORG";
private const string REPO = "REPO";
private const string AWS_BUCKET_NAME = "AWS_BUCKET_NAME";
private string _script;

public GenerateScriptCommandHandlerTests()
Expand All @@ -36,7 +35,6 @@ public GenerateScriptCommandHandlerTests()
_mockOctoLogger.Object,
_mockGithubApi.Object,
_mockAdoApi.Object,
_mockEnvironmentVariableProvider.Object,
_mockVersionProvider.Object
)
{
Expand All @@ -61,6 +59,14 @@ await FluentActions
.Should().ThrowAsync<OctoshiftCliException>();
}

[Fact]
public async Task No_Github_Source_Org_Or_Ado_Source_Org_Throws()
{
await _handler.Invoking(async handler => await handler.Handle(new GenerateScriptCommandArgs { GithubTargetOrg = TARGET_ORG }))
.Should()
.ThrowAsync<OctoshiftCliException>();
}

[Fact]
public async Task Sequential_Github_No_Data()
{
Expand Down Expand Up @@ -295,13 +301,12 @@ public async Task Sequential_Github_Ghes_Repo()
{
// Arrange
const string ghesApiUrl = "https://foo.com/api/v3";
const string azureStorageConnectionString = "FOO-STORAGE-CONNECTION-STRING";

_mockGithubApi
.Setup(m => m.GetRepos(SOURCE_ORG))
.ReturnsAsync(new[] { REPO });

var expected = $"Exec {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --azure-storage-connection-string \"{azureStorageConnectionString}\" --wait }}";
var expected = $"Exec {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --wait }}";

// Act
var args = new GenerateScriptCommandArgs
Expand All @@ -310,7 +315,6 @@ public async Task Sequential_Github_Ghes_Repo()
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
GhesApiUrl = ghesApiUrl,
AzureStorageConnectionString = azureStorageConnectionString,
Sequential = true
};
await _handler.Handle(args);
Expand All @@ -319,7 +323,6 @@ public async Task Sequential_Github_Ghes_Repo()

// Assert
_script.Should().Be(expected);
_mockOctoLogger.Verify(m => m.LogInformation("AZURE STORAGE CONNECTION STRING: ***"));
_mockOctoLogger.Verify(m => m.LogInformation($"GHES API URL: {ghesApiUrl}"));
}

Expand Down Expand Up @@ -658,7 +661,6 @@ public async Task Parallel_Github_Ghes_Single_Repo()
{
// Arrange
const string ghesApiUrl = "https://foo.com/api/v3";
const string azureStorageConnectionString = "FOO-STORAGE-CONNECTION-STRING";

_mockGithubApi
.Setup(m => m.GetRepos(SOURCE_ORG))
Expand Down Expand Up @@ -699,7 +701,7 @@ function ExecAndGetMigrationID {
expected.AppendLine($"# =========== Organization: {SOURCE_ORG} ===========");
expected.AppendLine();
expected.AppendLine("# === Queuing repo migrations ===");
expected.AppendLine($"$MigrationID = ExecAndGetMigrationID {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --azure-storage-connection-string \"{azureStorageConnectionString}\" }}");
expected.AppendLine($"$MigrationID = ExecAndGetMigrationID {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" }}");
expected.AppendLine($"$RepoMigrations[\"{REPO}\"] = $MigrationID");
expected.AppendLine();
expected.AppendLine();
Expand All @@ -725,14 +727,12 @@ exit 1
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
GhesApiUrl = ghesApiUrl,
AzureStorageConnectionString = azureStorageConnectionString
GhesApiUrl = ghesApiUrl
};
await _handler.Handle(args);

// Assert
_script.Should().Be(expected.ToString());
_mockOctoLogger.Verify(m => m.LogInformation("AZURE STORAGE CONNECTION STRING: ***"));
_mockOctoLogger.Verify(m => m.LogInformation($"GHES API URL: {ghesApiUrl}"));
}

Expand Down Expand Up @@ -1026,7 +1026,6 @@ public async Task Parallel_Github_Ghes_Single_Repo_With_Download_Migration_Logs(
{
// Arrange
const string ghesApiUrl = "https://foo.com/api/v3";
const string azureStorageConnectionString = "FOO-STORAGE-CONNECTION-STRING";

_mockGithubApi
.Setup(m => m.GetRepos(SOURCE_ORG))
Expand Down Expand Up @@ -1067,7 +1066,7 @@ function ExecAndGetMigrationID {
expected.AppendLine($"# =========== Organization: {SOURCE_ORG} ===========");
expected.AppendLine();
expected.AppendLine("# === Queuing repo migrations ===");
expected.AppendLine($"$MigrationID = ExecAndGetMigrationID {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --azure-storage-connection-string \"{azureStorageConnectionString}\" }}");
expected.AppendLine($"$MigrationID = ExecAndGetMigrationID {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" }}");
expected.AppendLine($"$RepoMigrations[\"{REPO}\"] = $MigrationID");
expected.AppendLine();
expected.AppendLine();
Expand Down Expand Up @@ -1095,7 +1094,6 @@ exit 1
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
GhesApiUrl = ghesApiUrl,
AzureStorageConnectionString = azureStorageConnectionString,
DownloadMigrationLogs = true
};
await _handler.Handle(args);
Expand All @@ -1109,7 +1107,6 @@ public async Task Parallel_Github_Ghes_Single_Repo_No_Ssl()
{
// Arrange
const string ghesApiUrl = "https://foo.com/api/v3";
const string azureStorageConnectionString = "FOO-STORAGE-CONNECTION-STRING";

_mockGithubApi
.Setup(m => m.GetRepos(SOURCE_ORG))
Expand Down Expand Up @@ -1150,7 +1147,7 @@ function ExecAndGetMigrationID {
expected.AppendLine($"# =========== Organization: {SOURCE_ORG} ===========");
expected.AppendLine();
expected.AppendLine("# === Queuing repo migrations ===");
expected.AppendLine($"$MigrationID = ExecAndGetMigrationID {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --azure-storage-connection-string \"{azureStorageConnectionString}\" --no-ssl-verify }}");
expected.AppendLine($"$MigrationID = ExecAndGetMigrationID {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --no-ssl-verify }}");
expected.AppendLine($"$RepoMigrations[\"{REPO}\"] = $MigrationID");
expected.AppendLine();
expected.AppendLine();
Expand All @@ -1177,7 +1174,6 @@ exit 1
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
GhesApiUrl = ghesApiUrl,
AzureStorageConnectionString = azureStorageConnectionString,
NoSslVerify = true
};
await _handler.Handle(args);
Expand Down Expand Up @@ -1402,6 +1398,77 @@ public async Task Parallel_Ado_Contains_Cli_Version()
_script.Should().Contain(expectedCliVersionComment);
}

[Fact]
public async Task Sequential_Ghes_Single_Repo_Aws_S3()
{
// Arrange
const string ghesApiUrl = "https://foo.com/api/v3";

_mockGithubApi
.Setup(m => m.GetRepos(SOURCE_ORG))
.ReturnsAsync(new[] { REPO });

var expected = $"Exec {{ gh gei migrate-repo --github-source-org \"{SOURCE_ORG}\" --source-repo \"{REPO}\" --github-target-org \"{TARGET_ORG}\" --target-repo \"{REPO}\" --ghes-api-url \"{ghesApiUrl}\" --aws-bucket-name \"{AWS_BUCKET_NAME}\" --wait }}";

// Act
var args = new GenerateScriptCommandArgs
{
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
GhesApiUrl = ghesApiUrl,
AwsBucketName = AWS_BUCKET_NAME,
Sequential = true
};
await _handler.Handle(args);

_script = TrimNonExecutableLines(_script);

// Assert
_script.Should().Be(expected);
_mockOctoLogger.Verify(m => m.LogInformation($"AWS BUCKET NAME: {AWS_BUCKET_NAME}"));
}

[Fact]
public async Task It_Throws_When_Aws_Bucket_Name_Is_Provided_But_Ghes_Api_Url_Is_Not()
{
// Arrange
var args = new GenerateScriptCommandArgs
{
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
AwsBucketName = AWS_BUCKET_NAME,
Sequential = true
};

// Act, Assert
await _handler
.Invoking(async handler => await handler.Handle(args))
.Should()
.ThrowAsync<OctoshiftCliException>();
}

[Fact]
public async Task It_Throws_When_No_Ssl_Verify_Is_Set_But_Ghes_Api_Url_Is_Not()
{
// Arrange
var args = new GenerateScriptCommandArgs
{
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
Output = new FileInfo("unit-test-output"),
NoSslVerify = true,
Sequential = true
};

// Act, Assert
await _handler
.Invoking(async handler => await handler.Handle(args))
.Should()
.ThrowAsync<OctoshiftCliException>();
}

private string TrimNonExecutableLines(string script, int skipFirst = 9, int skipLast = 0)
{
var lines = script.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable();
Expand Down
Loading

0 comments on commit 54c46b8

Please sign in to comment.