This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API to return available versions of Node and NPM
Fixes #784
- Loading branch information
Showing
8 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Kudu.Services.Diagnostics | ||
{ | ||
[DataContract(Name = "runtime")] | ||
public class RuntimeInfo | ||
{ | ||
[DataMember(Name = "nodejs")] | ||
public IEnumerable<Dictionary<string, string>> NodeVerions { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Abstractions; | ||
using System.Linq; | ||
using System.Text; | ||
using Kudu.Contracts.Tracing; | ||
using Kudu.Services.Diagnostics; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Kudu.Services.Test | ||
{ | ||
public class RuntimeControllerFacts | ||
{ | ||
private static readonly string _programFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
private static readonly string _nodeDir = Path.Combine(_programFilesDir, "nodejs"); | ||
|
||
[Fact] | ||
public void RuntimeControllerReturnsEmptyListIfDirectoryDoesNotExist() | ||
{ | ||
// Arrange | ||
var nodeDir = new Mock<DirectoryInfoBase>(); | ||
nodeDir.Setup(d => d.Exists).Returns(false); | ||
var directory = new Mock<IDirectoryInfoFactory>(); | ||
directory.Setup(d => d.FromDirectoryName(_nodeDir)).Returns(nodeDir.Object); | ||
var fileSystem = new Mock<IFileSystem>(); | ||
fileSystem.Setup(f => f.DirectoryInfo).Returns(directory.Object); | ||
|
||
var controller = new RuntimeController(Mock.Of<ITracer>(), fileSystem.Object); | ||
|
||
// Act | ||
var runtimeInfo = controller.GetRuntimeVersions(); | ||
|
||
// Assert | ||
Assert.Empty(runtimeInfo.NodeVerions); | ||
} | ||
|
||
[Fact] | ||
public void RuntimeControllerReturnsNodeVersions() | ||
{ | ||
// Arrange | ||
var nodeDir = new Mock<DirectoryInfoBase>(); | ||
nodeDir.Setup(d => d.Exists).Returns(true); | ||
nodeDir.Setup(d => d.GetDirectories()).Returns(new[] { | ||
CreateDirectory("0.8.19", CreateFile("npm.txt", "1.2.8")), | ||
CreateDirectory("0.10.5", CreateFile("npm.txt", "1.3.11")), | ||
CreateDirectory("0.10.18"), | ||
CreateDirectory("node_modules"), | ||
CreateDirectory("docs") | ||
}); | ||
var directoryInfo = new Mock<IDirectoryInfoFactory>(); | ||
directoryInfo.Setup(d => d.FromDirectoryName(_nodeDir)).Returns(nodeDir.Object); | ||
var fileSystem = new Mock<IFileSystem>(); | ||
fileSystem.Setup(f => f.DirectoryInfo).Returns(directoryInfo.Object); | ||
|
||
var controller = new RuntimeController(Mock.Of<ITracer>(), fileSystem.Object); | ||
|
||
// Act | ||
var nodeVersions = controller.GetRuntimeVersions().NodeVerions.ToList(); | ||
|
||
// Assert | ||
Assert.Equal(new[] { "0.8.19", "0.10.5", "0.10.18" }, nodeVersions.Select(v => v["version"])); | ||
Assert.Equal(new[] { "1.2.8", "1.3.11", null }, nodeVersions.Select(v => v["npm"])); | ||
} | ||
|
||
private DirectoryInfoBase CreateDirectory(string name, params FileInfoBase[] files) | ||
{ | ||
var dir = new Mock<DirectoryInfoBase>(); | ||
dir.SetupGet(d => d.Name).Returns(name); | ||
dir.Setup(d => d.GetFiles(It.IsAny<string>())).Returns(files); | ||
return dir.Object; | ||
} | ||
|
||
private FileInfoBase CreateFile(string fileName, string content) | ||
{ | ||
var file = new Mock<FileInfoBase>(); | ||
file.SetupGet(f => f.Name).Returns(fileName); | ||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(content)); | ||
file.Setup(f => f.OpenRead()).Returns(memoryStream); | ||
return file.Object; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Abstractions; | ||
using System.Linq; | ||
using System.Net.Http.Formatting; | ||
using System.Text.RegularExpressions; | ||
using System.Web.Http; | ||
using System.Web.Http.Controllers; | ||
using Kudu.Contracts.Tracing; | ||
using Newtonsoft.Json; | ||
|
||
namespace Kudu.Services.Diagnostics | ||
{ | ||
public class RuntimeController : ApiController | ||
{ | ||
private const string VersionKey = "version"; | ||
private static readonly Regex _versionRegex = new Regex(@"^\d+\.\d+", RegexOptions.ExplicitCapture); | ||
private readonly ITracer _tracer; | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public RuntimeController(ITracer tracer, IFileSystem fileSystem) | ||
{ | ||
_tracer = tracer; | ||
_fileSystem = fileSystem; | ||
} | ||
|
||
protected override void Initialize(HttpControllerContext controllerContext) | ||
{ | ||
controllerContext.Configuration.Formatters.Clear(); | ||
|
||
var settings = new JsonSerializerSettings | ||
{ | ||
ContractResolver = new LowerCasePropertyNamesContractResolver() | ||
}; | ||
controllerContext.Configuration.Formatters.Add(new JsonMediaTypeFormatter { SerializerSettings = settings }); | ||
} | ||
|
||
[HttpGet] | ||
public RuntimeInfo GetRuntimeVersions() | ||
{ | ||
using (_tracer.Step("RuntimeController.GetRuntimeVersions")) | ||
{ | ||
return new RuntimeInfo | ||
{ | ||
NodeVerions = GetNodeVersions() | ||
}; | ||
} | ||
} | ||
|
||
private IEnumerable<Dictionary<string, string>> GetNodeVersions() | ||
{ | ||
string nodeRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "nodejs"); | ||
var directoryInfo = _fileSystem.DirectoryInfo.FromDirectoryName(nodeRoot); | ||
if (directoryInfo.Exists) | ||
{ | ||
return directoryInfo.GetDirectories() | ||
.Where(dir => _versionRegex.IsMatch(dir.Name)) | ||
.Select(dir => new Dictionary<string, string> | ||
{ | ||
{ VersionKey, dir.Name }, | ||
{ "npm", TryReadNpmVersion(dir) } | ||
}); | ||
} | ||
return Enumerable.Empty<Dictionary<string, string>>(); | ||
} | ||
|
||
private string TryReadNpmVersion(DirectoryInfoBase nodeDir) | ||
{ | ||
var npmRedirectionFile = nodeDir.GetFiles("npm.txt").FirstOrDefault(); | ||
if (npmRedirectionFile == null) | ||
{ | ||
return null; | ||
} | ||
using (StreamReader reader = new StreamReader(npmRedirectionFile.OpenRead())) | ||
{ | ||
return reader.ReadLine(); | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Kudu.Services/Infrastructure/LowerCasePropertyNamesContractResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace Kudu.Services | ||
{ | ||
public class LowerCasePropertyNamesContractResolver : DefaultContractResolver | ||
{ | ||
protected override string ResolvePropertyName(string propertyName) | ||
{ | ||
return propertyName.ToLowerInvariant(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters