Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
support add deployment history api
Browse files Browse the repository at this point in the history
  • Loading branch information
suwatch committed Oct 30, 2015
1 parent ed830cb commit 90a1e58
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 8 deletions.
11 changes: 10 additions & 1 deletion Kudu.Client/Deployment/RemoteDeploymentManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Kudu.Client.Infrastructure;
using Kudu.Core.Deployment;
using Newtonsoft.Json.Linq;

namespace Kudu.Client.Deployment
{
Expand Down Expand Up @@ -53,6 +53,15 @@ public Task DeleteAsync(string id)
return Client.DeleteSafeAsync(BuildUrl(id));
}

public async Task<DeployResult> PutAsync(string id, JObject payload)
{
using (var response = await Client.PutAsJsonAsync(BuildUrl(id), payload))
{
response.EnsureSuccessful();
return await response.Content.ReadAsAsync<DeployResult>();
}
}

public Task<HttpResponseMessage> DeployAsync(string id)
{
return Client.PutAsync(BuildUrl(id));
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Core/Deployment/DeploymentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DeploymentManager : IDeploymentManager
private readonly IWebHooksManager _hooksManager;

private const string XmlLogFile = "log.xml";
private const string TextLogFile = "log.log";
public const string TextLogFile = "log.log";
private const string TemporaryDeploymentIdPrefix = "temp-";
public const int MaxSuccessDeploymentResults = 10;

Expand Down
42 changes: 41 additions & 1 deletion Kudu.FunctionalTests/DeploymentManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public class DeploymentApisTests
public async Task DeploymentApis()
{
// Arrange

string appName = "DeploymentApis";

using (var repo = Git.Clone("HelloWorld"))
Expand Down Expand Up @@ -171,6 +170,47 @@ await ApplicationManager.RunAsync(appName, async appManager =>
// Make sure running this again doesn't throw an exception
await appManager.DeploymentManager.DeployAsync(null);
// Create new deployment test
var id = Guid.NewGuid().ToString();
var payload = new JObject();
var endtime = DateTime.UtcNow;
payload["status"] = (int)DeployStatus.Success;
payload["message"] = "this is commit message";
payload["deployer"] = "kudu";
payload["author"] = "tester";
payload["end_time"] = endtime.ToString("o");
payload["details"] = "http://kudu.com/deployments/details";
// add new deployment
result = await appManager.DeploymentManager.PutAsync(id, payload);
Assert.Equal(id, result.Id);
Assert.Equal(DeployStatus.Success, result.Status);
Assert.Equal("this is commit message", result.Message);
Assert.Equal("kudu", result.Deployer);
Assert.Equal("tester", result.Author);
Assert.Equal(endtime, result.EndTime);
Assert.Equal(true, result.Current);
// check result
results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.True(results.Any(r => r.Id == id));
result = results[0];
Assert.Equal(id, result.Id);
Assert.Equal(DeployStatus.Success, result.Status);
Assert.Equal("this is commit message", result.Message);
Assert.Equal("kudu", result.Deployer);
Assert.Equal("tester", result.Author);
Assert.Equal(endtime, result.EndTime);
Assert.Equal(true, result.Current);
entries = (await appManager.DeploymentManager.GetLogEntriesAsync(result.Id)).ToList();
Assert.Equal(1, entries.Count);
Assert.Equal("Deployment successful.", entries[0].Message);
entries = (await appManager.DeploymentManager.GetLogEntryDetailsAsync(result.Id, entries[0].Id)).ToList();
Assert.Equal(1, entries.Count);
Assert.Equal(payload["details"], entries[0].Message);
});
}
}
Expand Down
79 changes: 76 additions & 3 deletions Kudu.Services.Test/DeploymentControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
using Moq;
using Xunit;
using System.Linq;
using Kudu.Core;
using Kudu.Core.Tracing;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Kudu.Services.Test
{
Expand All @@ -27,7 +31,7 @@ public async Task RedeployReturns404IfRepoDoesNotExist()
repoFactory.Setup(r => r.GetRepository()).Returns((IRepository)null);
var opLock = new Mock<IOperationLock>();
opLock.Setup(f => f.Lock()).Returns(true);
var controller = new DeploymentController(Mock.Of<ITracer>(), Mock.Of<IDeploymentManager>(), Mock.Of<IDeploymentStatusManager>(),
var controller = new DeploymentController(Mock.Of<ITracer>(), Mock.Of<IEnvironment>(), Mock.Of<IAnalytics>(), Mock.Of<IDeploymentManager>(), Mock.Of<IDeploymentStatusManager>(),
opLock.Object, repoFactory.Object, Mock.Of<IAutoSwapHandler>());
controller.Request = GetRequest();

Expand All @@ -50,7 +54,7 @@ public async Task RedeployReturns404IfCommitDoesNotExist()
repoFactory.Setup(r => r.GetRepository()).Returns(repository.Object);
var opLock = new Mock<IOperationLock>();
opLock.Setup(f => f.Lock()).Returns(true);
var controller = new DeploymentController(Mock.Of<ITracer>(), Mock.Of<IDeploymentManager>(), Mock.Of<IDeploymentStatusManager>(),
var controller = new DeploymentController(Mock.Of<ITracer>(), Mock.Of<IEnvironment>(), Mock.Of<IAnalytics>(), Mock.Of<IDeploymentManager>(), Mock.Of<IDeploymentStatusManager>(),
opLock.Object, repoFactory.Object, Mock.Of<IAutoSwapHandler>());
controller.Request = GetRequest();

Expand All @@ -70,7 +74,7 @@ public async Task GetLogEntryDetailsReturns404IfLogEntryDetailsDoesNotExist()
deploymentManager
.Setup(d => d.GetLogEntryDetails(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Enumerable.Empty<LogEntry>());
var controller = new DeploymentController(Mock.Of<ITracer>(), deploymentManager.Object, Mock.Of<IDeploymentStatusManager>(),
var controller = new DeploymentController(Mock.Of<ITracer>(), Mock.Of<IEnvironment>(), Mock.Of<IAnalytics>(), deploymentManager.Object, Mock.Of<IDeploymentStatusManager>(),
Mock.Of<IOperationLock>(), Mock.Of<IRepositoryFactory>(), Mock.Of<IAutoSwapHandler>());
controller.Request = GetRequest();

Expand All @@ -91,5 +95,74 @@ private static HttpRequestMessage GetRequest()

return request;
}

[Theory]
[MemberData("ParseDeployResultScenarios")]
public void TryParseDeployResultTests(string id, JObject payload, bool expected)
{
// Arrange
var controller = new DeploymentController(Mock.Of<ITracer>(), Mock.Of<IEnvironment>(), Mock.Of<IAnalytics>(), Mock.Of<IDeploymentManager>(), Mock.Of<IDeploymentStatusManager>(), Mock.Of<IOperationLock>(), Mock.Of<IRepositoryFactory>(), Mock.Of<IAutoSwapHandler>());

// Act
DeployResult result;
var actual = controller.TryParseDeployResult(id, payload, out result);

// Assert
Assert.Equal(expected, actual);
Assert.True(actual ? result != null : result == null);
if (result != null)
{
Assert.Equal(id, result.Id);
Assert.Equal((DeployStatus)payload.Value<int>("status"), result.Status);
Assert.Equal(payload.Value<string>("message"), result.Message);
Assert.Equal(payload.Value<string>("deployer"), result.Deployer);
Assert.Equal(payload.Value<string>("author"), result.Author);
Assert.Equal(payload.Value<string>("author_email"), result.AuthorEmail);
Assert.NotNull(result.StartTime);
Assert.NotNull(result.EndTime);

var startTime = payload.Value<DateTime?>("start_time");
if (startTime != null)
{
Assert.Equal(startTime, result.StartTime);
}

var endTime = payload.Value<DateTime?>("end_time");
if (endTime != null)
{
Assert.Equal(endTime, result.EndTime);
}

var active = payload.Value<bool?>("active");
if (active == null)
{
Assert.Equal(result.Status == DeployStatus.Success, result.Current);
}
else
{
Assert.Equal(active, result.Current);
}
}
}

public static IEnumerable<object[]> ParseDeployResultScenarios
{
get
{
yield return new object[] { null, null, false };
yield return new object[] { "deploy_id", null, false };
yield return new object[] { "deploy_id", new JObject(), false };
yield return new object[] { "deploy_id", JObject.Parse("{status:2,message:'msg',deployer:'kudu',author:'me'}"), false };
yield return new object[] { "deploy_id", JObject.Parse("{status:3,message:'',deployer:'kudu',author:'me'}"), false };
yield return new object[] { "deploy_id", JObject.Parse("{status:4,message:'msg',deployer:'',author:'me'}"), false };
yield return new object[] { "deploy_id", JObject.Parse("{status:3,message:'msg',deployer:'kudu',author:''}"), false };
yield return new object[] { "deploy_id", JObject.Parse("{status:4,message:'msg',deployer:'kudu',author:'me'}"), true };

var now = DateTime.UtcNow;
yield return new object[] { "deploy_id", JObject.Parse("{status:3,message:'msg',deployer:'kudu',author:'me',author_email:'me@foo.com',start_time:'" + now.ToString("o") + "',end_time:'" + now.AddSeconds(25).ToString("o") + "'}"), true };
yield return new object[] { "deploy_id", JObject.Parse("{status:4,message:'msg',deployer:'kudu',author:'me',author_email:'me@foo.com',start_time:'" + now.ToString("o") + "',end_time:'" + now.AddSeconds(25).ToString("o") + "'}"), true };
yield return new object[] { "deploy_id", JObject.Parse("{status:4,message:'msg',deployer:'kudu',author:'me',author_email:'me@foo.com',start_time:'" + now.ToString("o") + "',end_time:'" + now.AddSeconds(25).ToString("o") + "',active:false}"), true };
}
}
}
}
Loading

0 comments on commit 90a1e58

Please sign in to comment.