Finished initial implementation of add-service

This commit is contained in:
Chris Cheetham 2018-08-14 10:42:59 -04:00
Родитель b5009b05aa
Коммит 8bb0ec1af6
10 изменённых файлов: 188 добавлений и 94 удалений

Просмотреть файл

@ -92,11 +92,19 @@ namespace Steeltoe.Tooling.DotnetCli.Base.Feature
LastCommandResult.Error.ShouldMatch($".*{error}.*");
}
protected void the_target_environment_is_(string environment)
protected void the_tooling_config_target_environment_is_(string environment)
{
Logger.LogInformation($"checking target environment is '{environment}'");
Logger.LogInformation($"checking tooling config target environment is '{environment}'");
var cfg = ToolingConfiguration.Load(ProjectDirectory);
cfg.target.ShouldBe(environment);
}
protected void the_tooling_config_defines_the_service_(string name, string type)
{
Logger.LogInformation($"checking tooling config defines the service '{name}' of type {type}");
var cfg = ToolingConfiguration.Load(ProjectDirectory);
cfg.services.ShouldContainKey(name);
cfg.services[name].type.ShouldBe(type);
}
}
}

Просмотреть файл

@ -0,0 +1,95 @@
// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using LightBDD.Framework;
using LightBDD.Framework.Scenarios.Extended;
using LightBDD.XUnit2;
using Steeltoe.Tooling.DotnetCli.Base.Feature;
namespace Steeltoe.Tooling.DotnetCli.Service.Feature
{
[Label("service")]
public class AddServiceFeature : DotnetCliFeatureSpecs
{
[Scenario]
[Label("help")]
public void AddServiceHelp()
{
Runner.RunScenario(
given => a_dotnet_project("add_service_help"),
when => the_developer_runs_steeltoe_("add-service --help"),
then => the_command_succeeds(),
and => the_developer_sees_("Add a service to the target environment."),
and => the_developer_sees_(@"name\s+The service name"),
and => the_developer_sees_(@"-s|--service-type\s+The service type")
);
}
[Scenario]
public void AddServiceNotEnoughArgs()
{
Runner.RunScenario(
given => a_dotnet_project("add_service_not_enough_args"),
when => the_developer_runs_steeltoe_("add-service"),
then => the_command_fails(),
and => the_developer_sees_the_error_("Service name not specified")
);
}
[Scenario]
public void AddServiceTooManyArgs()
{
Runner.RunScenario(
given => a_dotnet_project("add_service_too_many_args"),
when => the_developer_runs_steeltoe_("add-service arg1 arg2"),
then => the_command_fails(),
and => the_developer_sees_the_error_("Unrecognized command or argument 'arg2'")
);
}
[Scenario]
public void AddServiceMissingType()
{
Runner.RunScenario(
given => a_dotnet_project("add_service_missing_type"),
when => the_developer_runs_steeltoe_("add-service foo"),
then => the_command_fails(),
and => the_developer_sees_the_error_("Service type not specified")
);
}
[Scenario]
public void AddUnknownService()
{
Runner.RunScenario(
given => a_dotnet_project("add_unknown_service"),
when => the_developer_runs_steeltoe_("add-service foo -s no-such-type"),
then => the_command_fails(),
and => the_developer_sees_the_error_("Unknown service type 'no-such-type'")
);
}
[Scenario]
public void AddCloudFoundryConfigServerService()
{
Runner.RunScenario(
given => a_dotnet_project("add_cloud_foundry_config_server_service"),
when => the_developer_runs_steeltoe_("add-service MyConfigServer -s cloud-foundry-config-server"),
then => the_command_succeeds(),
and => the_developer_sees_("Added cloud-foundry-config-server service 'MyConfigServer'"),
and => the_tooling_config_defines_the_service_("MyConfigServer", "cloud-foundry-config-server")
);
}
}
}

Просмотреть файл

@ -1,38 +0,0 @@
// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using LightBDD.Framework;
using LightBDD.Framework.Scenarios.Extended;
using LightBDD.XUnit2;
using Steeltoe.Tooling.DotnetCli.Base.Feature;
//[assembly: LightBddScope]
namespace Steeltoe.Tooling.DotnetCli.Service.Feature
{
[Label("service")]
public class DefineServiceFeature : DotnetCliFeatureSpecs
{
[Scenario]
public void RunDefineNoArgs()
{
Runner.RunScenario(
given => a_dotnet_project("define_no_args"),
when => the_developer_runs_steeltoe_("define-service"),
then => the_command_fails(),
and => the_developer_sees_the_error_("name not specified")
);
}
}
}

Просмотреть файл

@ -58,10 +58,10 @@ namespace Steeltoe.Tooling.DotnetCli.Target.Feature
}
[Scenario]
public void SetTargetToUnknownEnvironment()
public void SetUnknownTarget()
{
Runner.RunScenario(
given => a_dotnet_project("set_target_to_unknown_environment"),
given => a_dotnet_project("set_unknown_environment"),
when => the_developer_runs_steeltoe_("set-target no-such-environment"),
then => the_command_fails(),
and => the_developer_sees_the_error_("Unknown environment 'no-such-environment'")
@ -69,14 +69,14 @@ namespace Steeltoe.Tooling.DotnetCli.Target.Feature
}
[Scenario]
public void SetTargetToCloudFoundry()
public void SetCloudFoundryTarget()
{
Runner.RunScenario(
given => a_dotnet_project("set_target_to_cloud_foundry"),
given => a_dotnet_project("set_cloud_foundry_target"),
when => the_developer_runs_steeltoe_("set-target cloud-foundry"),
then => the_command_succeeds(),
and => the_developer_sees_("Target set to 'cloud-foundry'."),
and => the_target_environment_is_("cloud-foundry")
and => the_tooling_config_target_environment_is_("cloud-foundry")
);
}
}

Просмотреть файл

@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;
namespace Steeltoe.Tooling.Base
@ -9,6 +10,8 @@ namespace Steeltoe.Tooling.Base
public string target { get; set; }
public Dictionary<string, Service> services { get; set; } = new Dictionary<string, Service>();
public static ToolingConfiguration Load(string path)
{
var realPath = Directory.Exists(path) ? Path.Combine(path, DefaultFileName) : path;
@ -40,5 +43,10 @@ namespace Steeltoe.Tooling.Base
writer.Write(yaml);
writer.Flush();
}
public class Service
{
public string type { get; set; }
}
}
}

Просмотреть файл

@ -0,0 +1,58 @@
// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using McMaster.Extensions.CommandLineUtils;
using Steeltoe.Tooling.Base;
using Steeltoe.Tooling.DotnetCli.Base;
namespace Steeltoe.Tooling.DotnetCli.Service
{
[Command(Description = "Add a service to the target environment.")]
public class AddServiceCommand : DotnetCliCommand
{
[Argument(0, Description = "The service name")]
private string name { get; }
[Option("-s|--service-type", Description = "The service type")]
private string type { get; }
protected override void OnCommandExecute(CommandLineApplication app)
{
if (string.IsNullOrEmpty(name))
{
throw new UsageException("Service name not specified");
}
if (string.IsNullOrEmpty(type))
{
throw new UsageException("Service type not specified");
}
switch (type.ToLower())
{
case "cloud-foundry-config-server":
break;
default:
throw new CommandException($"Unknown service type '{type}'");
}
var cfg = new ToolingConfiguration();
var svc = new ToolingConfiguration.Service();
svc.type = type;
cfg.services.Add(name, svc);
cfg.Store(".");
app.Out.WriteLine($"Added {type} service '{name}'");
}
}
}

Просмотреть файл

@ -1,39 +0,0 @@
// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using McMaster.Extensions.CommandLineUtils;
using Steeltoe.Tooling.DotnetCli.Base;
namespace Steeltoe.Tooling.DotnetCli.Service
{
[Command(Description = "Define a service.")]
public class DefineServiceCommand : DotnetCliCommand
{
[Argument(0, Description = "The name of the service being defined.")]
private string Name { get; }
[Argument(1, Description = "The type of the service being defined.")]
private string Type { get; }
protected override void OnCommandExecute(CommandLineApplication app)
{
if (string.IsNullOrEmpty(Name))
{
throw new UsageException("name not specified");
}
app.Out.WriteLine($"defining {Type} service '{Name}'");
}
}
}

Просмотреть файл

@ -1,17 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\versions.props" />
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="$(McMasterExtensionsCommandLineUtilsVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Steeltoe.Tooling.Base\Steeltoe.Tooling.Base.csproj" />
<ProjectReference Include="..\Steeltoe.Tooling.DotnetCli.Base\Steeltoe.Tooling.DotnetCli.Base.csproj" />
</ItemGroup>
</Project>
</Project>

Просмотреть файл

@ -17,7 +17,7 @@ using McMaster.Extensions.CommandLineUtils;
namespace Steeltoe.Tooling.DotnetCli
{
[Command(Name = "steeltoe", Description = "Steeltoe Developer Tools")]
[Subcommand("define-service", typeof(Service.DefineServiceCommand))]
[Subcommand("add-service", typeof(Service.AddServiceCommand))]
[Subcommand("undefine-service", typeof(Service.UndefineServiceCommand))]
[Subcommand("list-services", typeof(Service.ListServicesCommand))]
[Subcommand("start-service", typeof(Service.StartServiceCommand))]

Просмотреть файл

@ -39,8 +39,11 @@ namespace Steeltoe.Tooling.Base.Test
var ostr = new StringWriter();
var cfg = new ToolingConfiguration();
cfg.target = "myTarget";
var svc = new ToolingConfiguration.Service();
svc.type = "myServiceType";
cfg.services.Add("myService", svc);
cfg.Store(ostr);
ostr.ToString().Trim().ShouldBe("target: myTarget");
ostr.ToString().ShouldMatch(@"\s*target: myTarget\s+services:\s+myService:\s+type:\s+myServiceType\s*");
}
[Fact]
@ -70,7 +73,10 @@ namespace Steeltoe.Tooling.Base.Test
[Fact]
public void TestLoadFromReader()
{
var istr = new StringReader("target: myTarget");
var istr = new StringReader(@"target: myTarget
services:
myService:
type: myServiceType");
var cfg = ToolingConfiguration.Load(istr);
cfg.target.ShouldBe("myTarget");
}