From 05ac266138ddd0b9059a8054f501c841c45f94ab Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Fri, 2 Oct 2020 18:28:55 +0100 Subject: [PATCH 1/2] #57 Adding support for terraform validate command. --- .../TerraformValidateTests.cs | 97 +++++++++++++++++++ src/Cake.Terraform/TerraformAliases.cs | 14 +++ .../Validate/TerraformValidateRunner.cs | 32 ++++++ 3 files changed, 143 insertions(+) create mode 100644 src/Cake.Terraform.Tests/TerraformValidateTests.cs create mode 100644 src/Cake.Terraform/Validate/TerraformValidateRunner.cs diff --git a/src/Cake.Terraform.Tests/TerraformValidateTests.cs b/src/Cake.Terraform.Tests/TerraformValidateTests.cs new file mode 100644 index 0000000..745458d --- /dev/null +++ b/src/Cake.Terraform.Tests/TerraformValidateTests.cs @@ -0,0 +1,97 @@ +using System.Collections.Generic; +using Cake.Core; +using Cake.Terraform.Validate; +using Cake.Testing; +using Xunit; + +namespace Cake.Terraform.Tests +{ + public class TerraformValidateTests + { + class Fixture : TerraformFixture + { + public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { } + + protected override void RunTool() + { + ProcessRunner.Process.SetStandardOutput(new List { "default" }); + + var tool = new TerraformValidateRunner(FileSystem, Environment, ProcessRunner, Tools); + + tool.Run(Settings); + } + } + + public class TheExecutable + { + [Fact] + public void Should_throw_if_terraform_runner_was_not_found() + { + var fixture = new Fixture(); + fixture.GivenDefaultToolDoNotExist(); + + var result = Record.Exception(() => fixture.Run()); + + Assert.IsType(result); + Assert.Equal("Terraform: Could not locate executable.", result.Message); + } + + [Theory] + [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")] + [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")] + public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected) + { + var fixture = new Fixture() { Settings = { ToolPath = toolPath } }; + fixture.GivenSettingsToolPathExist(); + + var result = fixture.Run(); + + Assert.Equal(expected, result.Path.FullPath); + } + + [Fact] + public void Should_find_terraform_if_tool_path_not_provided() + { + var fixture = new Fixture(); + + var result = fixture.Run(); + + Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath); + } + + [Fact] + public void Should_throw_if_process_has_a_non_zero_exit_code() + { + var fixture = new Fixture(); + fixture.GivenProcessExitsWithCode(1); + + var result = Record.Exception(() => fixture.Run()); + + Assert.IsType(result); + Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message); + } + + [Fact] + public void Should_find_linux_executable() + { + var fixture = new Fixture(PlatformFamily.Linux); + fixture.Environment.Platform.Family = PlatformFamily.Linux; + + + var result = fixture.Run(); + + Assert.Equal("/Working/tools/terraform", result.Path.FullPath); + } + + [Fact] + public void Should_set_workspace_and_list_parameter() + { + var fixture = new Fixture(); + + var result = fixture.Run(); + + Assert.Contains("validate", result.Args); + } + } + } +} diff --git a/src/Cake.Terraform/TerraformAliases.cs b/src/Cake.Terraform/TerraformAliases.cs index 5fa31aa..43cab81 100644 --- a/src/Cake.Terraform/TerraformAliases.cs +++ b/src/Cake.Terraform/TerraformAliases.cs @@ -12,6 +12,7 @@ using Cake.Terraform.Plan; using Cake.Terraform.Refresh; using Cake.Terraform.Show; +using Cake.Terraform.Validate; namespace Cake.Terraform { @@ -149,6 +150,19 @@ public static void TerraformRefresh(this ICakeContext context, TerraformRefreshS runner.Run(settings); } + [CakeMethodAlias] + public static void TerraformValidate(this ICakeContext context) + { + TerraformValidate(context, new TerraformValidateSettings()); + } + + [CakeMethodAlias] + public static void TerraformValidate(this ICakeContext context, TerraformValidateSettings settings) + { + var runner = new TerraformValidateRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + runner.Run(settings); + } + [CakeMethodAlias] public static string TerraformOutput(this ICakeContext context, TerraformOutputSettings settings) { diff --git a/src/Cake.Terraform/Validate/TerraformValidateRunner.cs b/src/Cake.Terraform/Validate/TerraformValidateRunner.cs new file mode 100644 index 0000000..5774771 --- /dev/null +++ b/src/Cake.Terraform/Validate/TerraformValidateRunner.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Cake.Core; +using Cake.Core.IO; +using Cake.Core.Tooling; +using Cake.Terraform.Init; + +namespace Cake.Terraform.Validate +{ + public class TerraformValidateRunner : TerraformRunner + { + public TerraformValidateRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools) + : base(fileSystem, environment, processRunner, tools) + { + } + + public void Run(TerraformValidateSettings settings) + { + var builder = new ProcessArgumentBuilder().Append("validate"); + + + Run(settings, builder); + } + } + + public class TerraformValidateSettings : TerraformSettings + { + } +} From f63e8a20da1867fde49fa94c8c5b1b868d22eaba Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Fri, 2 Oct 2020 18:33:57 +0100 Subject: [PATCH 2/2] #57 Updating examples. --- docs/input/docs/usage/examples.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/input/docs/usage/examples.md b/docs/input/docs/usage/examples.md index 3f2d441..41a1c98 100644 --- a/docs/input/docs/usage/examples.md +++ b/docs/input/docs/usage/examples.md @@ -143,4 +143,17 @@ Task("Destroy") }; TerraformDestroy(settings); }); +``` + + +## TerraformValidate + +```csharp +#addin Cake.Terraform + +Task("Validate") + .Does(() => +{ + TerraformValidate(); +}); ``` \ No newline at end of file