Skip to content

Commit 3ea44c3

Browse files
committed
Integration tests: fast-fail on missing credential env vars
Add TestHelper.AssertCredentialsPresent() that validates required environment variables are set and non-empty at test construction time. Each integration test class now calls this before making any API calls, producing an immediate, actionable error message identifying which GitHub Actions secret needs rotating.
1 parent 8e6a05a commit 3ea44c3

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

src/OctoshiftCLI.IntegrationTests/AdoToGithub.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ protected AdoToGithub(ITestOutputHelper output, string adoServerUrl = "https://d
2323
StartTime = DateTime.Now;
2424
_output = output;
2525

26+
TestHelper.AssertCredentialsPresent(
27+
(adoPatEnvVar, "Azure DevOps personal access token"),
28+
("GHEC_PAT", "GitHub Enterprise Cloud personal access token"));
29+
2630
var logger = new OctoLogger(x => { }, x => _output.WriteLine(x), x => { }, x => { });
2731

2832
_versionClient = new HttpClient();

src/OctoshiftCLI.IntegrationTests/BbsToGithub.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ public BbsToGithub(ITestOutputHelper output)
3939
_startTime = DateTime.Now;
4040
_output = output;
4141

42+
var azureStorageEnvVar = $"AZURE_STORAGE_CONNECTION_STRING_BBS_{TestHelper.GetOsName().ToUpper()}";
43+
TestHelper.AssertCredentialsPresent(
44+
("BBS_USERNAME", "Bitbucket Server username"),
45+
("BBS_PASSWORD", "Bitbucket Server password"),
46+
("GHEC_PAT", "GitHub Enterprise Cloud personal access token"),
47+
(azureStorageEnvVar, "Azure blob storage connection string for BBS migration archives"));
48+
4249
_logger = new OctoLogger(_ => { }, x => _output.WriteLine(x), _ => { }, _ => { });
4350

4451
var sourceBbsUsername = Environment.GetEnvironmentVariable("BBS_USERNAME");
4552
var sourceBbsPassword = Environment.GetEnvironmentVariable("BBS_PASSWORD");
4653
var targetGithubToken = Environment.GetEnvironmentVariable("GHEC_PAT");
47-
_azureStorageConnectionString = Environment.GetEnvironmentVariable($"AZURE_STORAGE_CONNECTION_STRING_BBS_{TestHelper.GetOsName().ToUpper()}");
54+
_azureStorageConnectionString = Environment.GetEnvironmentVariable(azureStorageEnvVar);
4855
_tokens = new Dictionary<string, string>
4956
{
5057
["BBS_USERNAME"] = sourceBbsUsername,

src/OctoshiftCLI.IntegrationTests/GhesToGithub.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ public GhesToGithub(ITestOutputHelper output)
3636
_startTime = DateTime.Now;
3737
_output = output;
3838

39+
var azureStorageEnvVar = $"AZURE_STORAGE_CONNECTION_STRING_GHES_{TestHelper.GetOsName().ToUpper()}";
40+
TestHelper.AssertCredentialsPresent(
41+
("GHES_PAT", "GitHub Enterprise Server personal access token"),
42+
("GHEC_PAT", "GitHub Enterprise Cloud personal access token"),
43+
(azureStorageEnvVar, "Azure blob storage connection string for GHES migration archives"));
44+
3945
var logger = new OctoLogger(_ => { }, x => _output.WriteLine(x), _ => { }, _ => { });
4046

4147
var sourceGithubToken = Environment.GetEnvironmentVariable("GHES_PAT");
4248
var targetGithubToken = Environment.GetEnvironmentVariable("GHEC_PAT");
43-
_azureStorageConnectionString = Environment.GetEnvironmentVariable($"AZURE_STORAGE_CONNECTION_STRING_GHES_{TestHelper.GetOsName().ToUpper()}");
49+
_azureStorageConnectionString = Environment.GetEnvironmentVariable(azureStorageEnvVar);
4450
_tokens = new Dictionary<string, string>
4551
{
4652
["GH_SOURCE_PAT"] = sourceGithubToken,

src/OctoshiftCLI.IntegrationTests/GithubToGithub.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public GithubToGithub(ITestOutputHelper output)
2626
_startTime = DateTime.Now;
2727
_output = output;
2828

29+
TestHelper.AssertCredentialsPresent(
30+
("GHEC_PAT", "GitHub Enterprise Cloud personal access token"));
31+
2932
var logger = new OctoLogger(x => { }, x => _output.WriteLine(x), x => { }, x => { });
3033

3134
var githubToken = Environment.GetEnvironmentVariable("GHEC_PAT");

src/OctoshiftCLI.IntegrationTests/TestHelper.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,36 @@ public static string GetOsName()
526526
: throw new InvalidOperationException("Could not determine OS");
527527
}
528528

529+
/// <summary>
530+
/// Validates that required environment variables for credentials are set and non-empty.
531+
/// Throws an InvalidOperationException with a clear message identifying which env var is missing.
532+
/// Call this at the start of integration test constructors for fast-fail with actionable error messages.
533+
/// </summary>
534+
public static void AssertCredentialsPresent(params (string envVarName, string description)[] credentials)
535+
{
536+
if (credentials is null)
537+
{
538+
throw new ArgumentNullException(nameof(credentials));
539+
}
540+
541+
var missing = new List<string>();
542+
foreach (var (envVarName, description) in credentials)
543+
{
544+
var value = Environment.GetEnvironmentVariable(envVarName);
545+
if (string.IsNullOrWhiteSpace(value))
546+
{
547+
missing.Add($" - {envVarName}: {description}");
548+
}
549+
}
550+
551+
if (missing.Any())
552+
{
553+
throw new InvalidOperationException(
554+
$"Missing or empty credential environment variable(s). " +
555+
$"If these are GitHub Actions secrets, they may need to be rotated:\n{string.Join("\n", missing)}");
556+
}
557+
}
558+
529559
public async Task RunCliMigration(string generateScriptCommand, string cliName, IReadOnlyDictionary<string, string> tokens)
530560
{
531561
await RunCliCommand(generateScriptCommand, cliName, tokens);

0 commit comments

Comments
 (0)