Skip to content

Commit

Permalink
Added Integration Test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
droyad committed Jul 17, 2017
1 parent 074a09a commit e97e6d1
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 24 deletions.
13 changes: 11 additions & 2 deletions AWS/DeleteCloudFormation.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
Param(
$tenantName = "MyTenant"
[Parameter(Mandatory=$True)] [string]$stackName
)

$ErrorActionPreference = "Stop"

$stackName = "IntegrationTests-$stackName"

if (-not $env:AWS_ACCESS_KEY_ID) {
$env:AWS_ACCESS_KEY_ID = $OctopusParameters["AWS_ACCESS_KEY_ID"]
}
if (-not $env:AWS_SECRET_ACCESS_KEY) {
$env:AWS_SECRET_ACCESS_KEY = $OctopusParameters["AWS_SECRET_ACCESS_KEY"]
}

$stacks = aws cloudformation list-stacks --region ap-southeast-2 | ConvertFrom-Json
$matching = $stacks.StackSummaries | Where-Object { $_.StackName -eq $stackName -and $_.StackStatus -ne "DELETE_COMPLETE" }
if($matching.length -eq $null)
if($matching -eq $null)
{
"Deleting stack $stackName"
aws cloudformation delete-stack --stack-name $stackName --region ap-southeast-2
Expand Down
22 changes: 16 additions & 6 deletions AWS/DeployCloudFormation.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
Param(
[Parameter(Mandatory=$True)] [string]$tenant,
[Parameter(Mandatory=$True)] [string]$tenantName
[string]$tenant,
[Parameter(Mandatory=$True)] [string]$stackName,
[Parameter(Mandatory=$True)] [string]$environment
)

$ErrorActionPreference = "Stop"

$stackName = "IntegrationTests-$tenantName"
$stackName = "IntegrationTests-$stackName"
$completedFormationName = ".\CompletedCloudFormation.json"

if (-not $env:AWS_ACCESS_KEY_ID) {
throw "AWS_ACCESS_KEY_ID has not been set"
$env:AWS_ACCESS_KEY_ID = $OctopusParameters["AWS_ACCESS_KEY_ID"]
}
if (-not $env:AWS_SECRET_ACCESS_KEY) {
throw "AWS_SECRET_ACCESS_KEY has not been set"
$env:AWS_SECRET_ACCESS_KEY = $OctopusParameters["AWS_SECRET_ACCESS_KEY"]
}

$registerRequest = Get-Content ".\Register.json" | ConvertFrom-Json
$formation = Get-Content ".\CloudFormation.json" | ConvertFrom-Json
$registerRequest.TenantIds = @($tenant)

if($tenant) {
$registerRequest.TenantIds = @($tenant)
$registerRequest.TenantedDeploymentParticipation = "Tenanted"
} else {
$registerRequest.TenantIds = @()
$registerRequest.TenantedDeploymentParticipation = "Untenanted"
}

$registerRequest.EnvironmentIds = @($environment)

function GetUserData($roles) {
$registerRequest.Roles = $roles
Expand Down
14 changes: 0 additions & 14 deletions AWS/MergeUserDataIntoCloudFormationTemplate.linq

This file was deleted.

3 changes: 1 addition & 2 deletions AzureFuctions/CreateTenant.csx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceW

var projects = repository.Projects.FindAll();
var environments = new ReferenceCollection(new [] {
repository.Environments.FindByName("Integration Test").Id,
repository.Environments.FindByName("Demo").Id
repository.Environments.FindByName("Integration Test").Id
});
var tenant = repository.Tenants.FindByName(branchName);
if (tenant == null)
Expand Down
96 changes: 96 additions & 0 deletions IntegrationTests/DeployProjects.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#r "C:\\Program Files\\Octopus Deploy\\Octopus\\Octopus.Client.dll"

using Octopus.Client;
using Octopus.Client.Model;
using System.Threading;
using System.Text.RegularExpressions;

var apiKey = Octopus.Parameters["ApiKey"];
var tenantId = Octopus.Parameters["Octopus.Deployment.Tenant.Id"];
var branch = Octopus.Parameters["Octopus.Deployment.Tenant.Name"];
var expectedMachineCount = int.Parse(Octopus.Parameters["ExpectedMachineCount"]);

var endpoint = new OctopusServerEndpoint("https://droyad.gq", apiKey);
var repository = new OctopusRepository(endpoint);

var production = repository.Environments.FindByName("Production").Id;
var integrationTest = repository.Environments.FindByName("Integration Test").Id;
var group = repository.ProjectGroups.FindByName("My Projects");
var projects = repository.ProjectGroups.GetProjects(group);

// Wait for Machines
int MachineCount()
{
return repository.Machines.FindAll()
.Count(m => m.HealthStatus == MachineModelHealthStatus.Healthy && m.TenantIds.Contains(tenantId));
}

var sw = Stopwatch.StartNew();

var count = MachineCount();
while (count < expectedMachineCount)
{
if(sw.Elapsed > TimeSpan.FromMinutes(5))
throw new Exception("Timed out waiting for machines");

Console.WriteLine($"{count} of {expectedMachineCount} machines");
Thread.Sleep(1000);
count = MachineCount();
}

// Deploy projects

var dashboard = repository.Dashboards.GetDashboard();

ReleaseResource GetReleaseToDeploy(ProjectResource project)
{
var release = repository.Projects.GetAllReleases(project).FirstOrDefault(r => Regex.IsMatch(r.Version, $"-{branch}[0-9]+$", RegexOptions.IgnoreCase));
if (release != null)
return release;

var item = dashboard.Items.FirstOrDefault(i => i.EnvironmentId == production && i.ProjectId == project.Id);
if (item != null)
return repository.Releases.Get(item.ReleaseId);

return null;
}

void CreateRelease(ProjectResource project, ReleaseResource release)
{
Console.WriteLine($"Deploying {project.Name} {release.Version}");
repository.Deployments.Create(new DeploymentResource()
{
ProjectId = release.ProjectId,
ReleaseId = release.Id,
ChannelId = release.ChannelId,
TenantId = tenantId,
EnvironmentId = integrationTest,
});
}

foreach (var project in projects)
{
var release = GetReleaseToDeploy(project);
if (release == null)
Console.WriteLine("Could not find a suitable release for " + project.Name);
else
CreateRelease(project, release);
}

// Wait for projects
string[] IncompleteDeployments()
{
var dash = repository.Dashboards.GetDynamicDashboard(projects.Select(p => p.Id).ToArray(), new[] { integrationTest });
return dash.Items
.Where(i => i.TenantId == tenantId && (i.State == TaskState.Queued || i.State == TaskState.Executing))
.Select(i => dash.Projects.First(p => p.Id == i.ProjectId).Name)
.ToArray();
}

var incomplete = IncompleteDeployments();
while (incomplete.Any())
{
Console.WriteLine($"Waiting for {string.Join(", ", incomplete)}");
Thread.Sleep(1000);
incomplete = IncompleteDeployments();
}
17 changes: 17 additions & 0 deletions IntegrationTests/RemoveMachines.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#r "C:\\Program Files\\Octopus Deploy\\Octopus\\Octopus.Client.dll"

using Octopus.Client;
using Octopus.Client.Model;

var apiKey = Octopus.Parameters["ApiKey"];
var tenantId = Octopus.Parameters["Octopus.Deployment.Tenant.Id"];

var endpoint = new OctopusServerEndpoint("https://droyad.gq", apiKey);
var repository = new OctopusRepository(endpoint);

var machines = from m in repository.Machines.FindAll()
where m.TenantIds.Contains(tenantId)
select m;

foreach (var machine in machines)
repository.Machines.Delete(machine);
30 changes: 30 additions & 0 deletions IntegrationTests/RunTests.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#r "C:\\Program Files\\Octopus Deploy\\Octopus\\Octopus.Client.dll"

using Octopus.Client;
using Octopus.Client.Model;
using Octopus.Client.Model.Endpoints;
using System.Net;
using System.IO;

var apiKey = Octopus.Parameters["ApiKey"];
var tenantId = Octopus.Parameters["Octopus.Deployment.Tenant.Id"];

var endpoint = new OctopusServerEndpoint("https://droyad.gq", apiKey);
var repository = new OctopusRepository(endpoint);

var integrationTest = repository.Environments.FindByName("Integration Test").Id;

var endpoints = from m in repository.Machines.FindAll()
where m.TenantIds.Contains(tenantId) &&
m.Roles.Contains("Web")
select (SshEndpointResource)m.Endpoint;

foreach (var e in endpoints)
{
var url = $"http://{e.Host}/20/50";
Console.WriteLine($"Requesting {url}");

using(var response = WebRequest.CreateHttp(url).GetResponse())
using(var s = new StreamReader(response.GetResponseStream()))
Console.WriteLine("Got " + s.ReadToEnd());
}
1 change: 1 addition & 0 deletions gitversion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mode: ContinuousDeployment

0 comments on commit e97e6d1

Please sign in to comment.