|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Amazon.CodeBuild; |
| 4 | +using Amazon.CodeBuild.Model; |
| 5 | +using Testcontainers.Floci; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Testcontainers.Floci.Tests; |
| 9 | + |
| 10 | +public sealed class CodeBuildServiceTest : IAsyncLifetime |
| 11 | +{ |
| 12 | + private const string ProjectName = "floci-test"; |
| 13 | + |
| 14 | + private readonly FlociContainer _floci = new FlociBuilder(TestImages.Floci) |
| 15 | + .WithCodeBuild(new CodeBuildConfig()) |
| 16 | + .Build(); |
| 17 | + |
| 18 | + public Task InitializeAsync() => _floci.StartAsync(); |
| 19 | + |
| 20 | + public async Task DisposeAsync() |
| 21 | + { |
| 22 | + try |
| 23 | + { |
| 24 | + using var cb = CreateClient(); |
| 25 | + await cb.DeleteProjectAsync(new DeleteProjectRequest { Name = ProjectName }); |
| 26 | + } |
| 27 | + catch (AmazonCodeBuildException) |
| 28 | + { |
| 29 | + // The container is being disposed anyway; nothing actionable here. |
| 30 | + } |
| 31 | + |
| 32 | + await _floci.DisposeAsync(); |
| 33 | + } |
| 34 | + |
| 35 | + private AmazonCodeBuildClient CreateClient() |
| 36 | + { |
| 37 | + return new AmazonCodeBuildClient( |
| 38 | + _floci.AccessKey, |
| 39 | + _floci.SecretKey, |
| 40 | + new AmazonCodeBuildConfig |
| 41 | + { |
| 42 | + ServiceURL = _floci.GetEndpoint(), |
| 43 | + AuthenticationRegion = _floci.Region, |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task RunsABuildToCompletion() |
| 49 | + { |
| 50 | + using var cb = CreateClient(); |
| 51 | + |
| 52 | + // Unlike the upstream Java test (which only lists projects), we run a real build: Floci |
| 53 | + // spawns a build container, executes the buildspec, and reports the terminal status. A |
| 54 | + // tiny alpine image keeps the build container pull cheap. (Verified separately that a |
| 55 | + // failing buildspec yields FAILED, so SUCCEEDED genuinely reflects buildspec execution.) |
| 56 | + await cb.CreateProjectAsync(new CreateProjectRequest |
| 57 | + { |
| 58 | + Name = ProjectName, |
| 59 | + Source = new ProjectSource |
| 60 | + { |
| 61 | + Type = SourceType.NO_SOURCE, |
| 62 | + Buildspec = "version: 0.2\nphases:\n build:\n commands:\n - echo hello-from-floci", |
| 63 | + }, |
| 64 | + Artifacts = new ProjectArtifacts { Type = ArtifactsType.NO_ARTIFACTS }, |
| 65 | + Environment = new ProjectEnvironment |
| 66 | + { |
| 67 | + Type = EnvironmentType.LINUX_CONTAINER, |
| 68 | + Image = "public.ecr.aws/docker/library/alpine:3.20", |
| 69 | + ComputeType = ComputeType.BUILD_GENERAL1_SMALL, |
| 70 | + }, |
| 71 | + ServiceRole = "arn:aws:iam::000000000000:role/codebuild-role", |
| 72 | + }); |
| 73 | + |
| 74 | + var started = await cb.StartBuildAsync(new StartBuildRequest { ProjectName = ProjectName }); |
| 75 | + var buildId = started.Build.Id; |
| 76 | + |
| 77 | + var status = await WaitForTerminalStatusAsync(cb, buildId); |
| 78 | + |
| 79 | + Assert.Equal(StatusType.SUCCEEDED, status); |
| 80 | + } |
| 81 | + |
| 82 | + private static async Task<StatusType> WaitForTerminalStatusAsync(AmazonCodeBuildClient cb, string buildId) |
| 83 | + { |
| 84 | + for (var attempt = 0; attempt < 60; attempt++) |
| 85 | + { |
| 86 | + var builds = await cb.BatchGetBuildsAsync(new BatchGetBuildsRequest |
| 87 | + { |
| 88 | + Ids = new List<string> { buildId }, |
| 89 | + }); |
| 90 | + |
| 91 | + var status = builds.Builds[0].BuildStatus; |
| 92 | + if (status != StatusType.IN_PROGRESS) |
| 93 | + { |
| 94 | + return status; |
| 95 | + } |
| 96 | + |
| 97 | + await Task.Delay(2000); |
| 98 | + } |
| 99 | + |
| 100 | + throw new Xunit.Sdk.XunitException("CodeBuild build did not reach a terminal status within the timeout."); |
| 101 | + } |
| 102 | +} |
0 commit comments