From f2646c65ac5d095b784d44163d63e5b9f9a3ccba Mon Sep 17 00:00:00 2001 From: Chris Cheetham Date: Tue, 6 Nov 2018 11:16:53 -0500 Subject: [PATCH] [#34] Adds support for deploying apps --- .gitignore | 2 +- src/Steeltoe.Cli/AddCommand.cs | 18 +- src/Steeltoe.Cli/ArgsCommand.cs | 24 +- src/Steeltoe.Cli/Command.cs | 37 ++- src/Steeltoe.Cli/DeployCommand.cs | 7 +- src/Steeltoe.Cli/DisableCommand.cs | 41 ---- src/Steeltoe.Cli/DoctorCommand.cs | 34 +-- src/Steeltoe.Cli/EnableCommand.cs | 41 ---- src/Steeltoe.Cli/InitCommand.cs | 35 +-- src/Steeltoe.Cli/ListCommand.cs | 42 +--- src/Steeltoe.Cli/Program.cs | 4 +- src/Steeltoe.Cli/RemoveCommand.cs | 12 +- src/Steeltoe.Cli/StatusCommand.cs | 6 +- src/Steeltoe.Cli/TargetCommand.cs | 16 +- src/Steeltoe.Cli/UndeployCommand.cs | 6 +- .../{Service.cs => AppInfo.cs} | 12 +- src/Steeltoe.Tooling/Cli.cs | 2 +- .../CloudFoundry/CloudFoundryBackend.cs | 130 +++++++++++ .../CloudFoundry/CloudFoundryManifest.cs | 49 ++++ .../CloudFoundry/CloudFoundryManifestFile.cs | 54 +++++ .../CloudFoundryServiceBackend.cs | 68 ------ ...ryEnvironment.cs => CloudFoundryTarget.cs} | 8 +- src/Steeltoe.Tooling/Configuration.cs | 184 +++++++++++++++ ...figurationFile.cs => ConfigurationFile.cs} | 24 +- src/Steeltoe.Tooling/Context.cs | 18 +- ...ckerServiceBackend.cs => DockerBackend.cs} | 60 +++-- .../{DockerEnvironment.cs => DockerTarget.cs} | 8 +- ...DummyServiceBackend.cs => DummyBackend.cs} | 42 +++- .../Dummy/DummyServiceDatabase.cs | 4 +- .../{DummyEnvironment.cs => DummyTarget.cs} | 8 +- ...ployServicesExecutor.cs => AddExecutor.cs} | 25 +- .../Executor/DeployExecutor.cs | 50 ++++ .../Executor/DisableServiceExecutor.cs | 30 --- src/Steeltoe.Tooling/Executor/Executor.cs | 57 +++++ ...eServiceExecutor.cs => GetArgsExecutor.cs} | 18 +- .../GetServiceDeploymentArgsExecutor.cs | 37 --- .../Executor/GetTargetExecutor.cs | 14 +- .../Executor/GroupExecutor.cs | 88 +++++++ .../Executor/InitializationExecutor.cs | 47 ++++ src/Steeltoe.Tooling/Executor/ListExecutor.cs | 31 ++- .../Executor/ListServiceTypesExecutor.cs | 15 +- .../Executor/ListServicesExectutor.cs | 46 ---- ...entsExecutor.cs => ListTargetsExecutor.cs} | 20 +- .../Executor/RemoveExecutor.cs | 49 ++++ .../RequiresTargetAttribute.cs} | 14 +- .../Executor/ServiceExecutor.cs | 17 +- .../Executor/ServicesExecutor.cs | 56 ----- ...mentArgsExecutor.cs => SetArgsExecutor.cs} | 24 +- .../Executor/SetTargetExecutor.cs | 26 +-- .../Executor/StatusApplicationsExecutor.cs | 24 ++ ...ddServiceExecutor.cs => StatusExecutor.cs} | 24 +- .../Executor/UndeployApplicationsExecutor.cs | 25 ++ ...ServiceExecutor.cs => UndeployExecutor.cs} | 17 +- .../Executor/UndeployServicesExecutor.cs | 33 --- .../StatusServicesExecutor.cs => IBackend.cs} | 20 +- ...IExecutor.cs => IConfigurationListener.cs} | 8 +- .../IToolingConfigurationListener.cs | 7 - src/Steeltoe.Tooling/Lifecycle.cs | 122 ++++++++++ ...ycleException.cs => LifecycleException.cs} | 10 +- src/Steeltoe.Tooling/NotFoundException.cs | 30 +++ src/Steeltoe.Tooling/Registry.cs | 106 ++++++--- src/Steeltoe.Tooling/RegistryConfiguration.cs | 62 ----- ...iceNotFoundException.cs => ServiceInfo.cs} | 11 +- src/Steeltoe.Tooling/ServiceLifecycle.cs | 194 ---------------- src/Steeltoe.Tooling/ServiceManager.cs | 132 ----------- .../{ServiceType.cs => ServiceTypeInfo.cs} | 4 +- src/Steeltoe.Tooling/Settings.cs | 2 + .../{Environment.cs => Target.cs} | 12 +- ...onfiguration.cs => TargetConfiguration.cs} | 2 +- src/Steeltoe.Tooling/ToolingConfiguration.cs | 61 ----- src/Steeltoe.Tooling/steeltoe.rc/registry.yml | 19 +- test/Steeltoe.Cli.Test/AddFeature.cs | 61 +++-- test/Steeltoe.Cli.Test/ArgsFeature.cs | 194 +++++++++------- test/Steeltoe.Cli.Test/DeployFeature.cs | 39 ++-- test/Steeltoe.Cli.Test/DisableFeature.cs | 96 -------- test/Steeltoe.Cli.Test/DoctorFeature.cs | 67 +++--- test/Steeltoe.Cli.Test/EnableFeature.cs | 97 -------- test/Steeltoe.Cli.Test/FeatureSpecs.cs | 43 ++-- test/Steeltoe.Cli.Test/InitFeature.cs | 19 +- test/Steeltoe.Cli.Test/ListFeature.cs | 65 +++--- test/Steeltoe.Cli.Test/ProgramFeature.cs | 36 ++- test/Steeltoe.Cli.Test/RemoveFeature.cs | 38 +-- test/Steeltoe.Cli.Test/StatusFeature.cs | 39 +--- test/Steeltoe.Cli.Test/SystemShellFeature.cs | 2 +- test/Steeltoe.Cli.Test/TargetFeature.cs | 38 ++- test/Steeltoe.Cli.Test/UndeployFeature.cs | 44 ++-- .../CloudFoundry/CloudFoundryBackendTest.cs | 164 +++++++++++++ .../CloudFoundryManifestFileTest.cs | 70 ++++++ .../CloudFoundryServiceManagerTest.cs | 99 -------- ...nmentTest.cs => CloudFoundryTargetTest.cs} | 21 +- .../ConfigurationFileTest.cs | 78 +++++++ .../ConfigurationTest.cs | 218 ++++++++++++++++++ test/Steeltoe.Tooling.Test/ContextTest.cs | 25 +- ...iceManagerTest.cs => DockerBackendTest.cs} | 89 +++---- ...EnvironmentTest.cs => DockerTargetTest.cs} | 13 +- ...viceBackendTest.cs => DummyBackendTest.cs} | 22 +- ...yEnvironmentTest.cs => DummyTargetTest.cs} | 13 +- .../Executor/AddExecutorTest.cs | 77 +++++++ .../Executor/AddServiceExecutorTest.cs | 57 ----- .../DeployApplicationsExecutorTest.cs | 38 +++ .../Executor/DeployExecutorTest.cs | 57 +++++ .../Executor/DeployServicesExecutorTest.cs | 67 ------ .../Executor/DisableServiceExecutorTest.cs | 41 ---- .../Executor/EnableServiceExecutorTest.cs | 41 ---- .../Executor/GetArgsExecutorTest.cs | 63 +++++ .../GetServiceDeploymentArgsExecutorTest.cs | 50 ---- .../Executor/GetTargetExecutorTest.cs | 15 +- .../Executor/InitializationExecutorTest.cs | 45 ++++ .../Executor/ListApplicationsExecutorTest.cs | 58 +++++ .../Executor/ListExecutorTest.cs | 65 ++++++ .../Executor/ListServiceTypesExecutorTest.cs | 5 +- .../Executor/ListServicesExecutorTest.cs | 62 ----- ...utorTest.cs => ListTargetsExecutorTest.cs} | 19 +- ...eExecutorTest.cs => RemoveExecutorTest.cs} | 23 +- .../Executor/SetArgsExecutorTest.cs | 66 ++++++ .../SetServiceDeploymentArgsExecutorTest.cs | 56 ----- .../Executor/SetTargetExecutorTest.cs | 17 +- .../Executor/StatusExecutorTest.cs | 74 ++++++ .../Executor/StatusServiceExecutorTest.cs | 77 ------- .../UndeployApplicationsExecutorTest.cs | 49 ++++ .../Executor/UndeployExecutorTest.cs | 61 +++++ .../Executor/UndeployServiceExecutorTest.cs | 66 ------ ...ptionTest.cs => LifecycleExceptionTest.cs} | 10 +- test/Steeltoe.Tooling.Test/LifecyleTest.cs | 54 +++++ .../RegistryConfigurationTest.cs | 65 +++--- test/Steeltoe.Tooling.Test/RegistryTest.cs | 20 +- .../ServiceLifecyleTest.cs | 80 ------- .../ServiceManagerTest.cs | 98 -------- .../ServiceNotFoundExceptionTest.cs | 14 +- .../ToolingConfigurationFileTest.cs | 102 -------- .../ToolingConfigurationTest.cs | 42 ---- test/Steeltoe.Tooling.Test/ToolingTest.cs | 6 +- 132 files changed, 3128 insertions(+), 2887 deletions(-) delete mode 100644 src/Steeltoe.Cli/DisableCommand.cs delete mode 100644 src/Steeltoe.Cli/EnableCommand.cs rename src/Steeltoe.Tooling/{Service.cs => AppInfo.cs} (77%) create mode 100644 src/Steeltoe.Tooling/CloudFoundry/CloudFoundryBackend.cs create mode 100644 src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifest.cs create mode 100644 src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifestFile.cs delete mode 100644 src/Steeltoe.Tooling/CloudFoundry/CloudFoundryServiceBackend.cs rename src/Steeltoe.Tooling/CloudFoundry/{CloudFoundryEnvironment.cs => CloudFoundryTarget.cs} (85%) create mode 100644 src/Steeltoe.Tooling/Configuration.cs rename src/Steeltoe.Tooling/{ToolingConfigurationFile.cs => ConfigurationFile.cs} (69%) rename src/Steeltoe.Tooling/Docker/{DockerServiceBackend.cs => DockerBackend.cs} (62%) rename src/Steeltoe.Tooling/Docker/{DockerEnvironment.cs => DockerTarget.cs} (88%) rename src/Steeltoe.Tooling/Dummy/{DummyServiceBackend.cs => DummyBackend.cs} (57%) rename src/Steeltoe.Tooling/Dummy/{DummyEnvironment.cs => DummyTarget.cs} (73%) rename src/Steeltoe.Tooling/Executor/{DeployServicesExecutor.cs => AddExecutor.cs} (52%) create mode 100644 src/Steeltoe.Tooling/Executor/DeployExecutor.cs delete mode 100644 src/Steeltoe.Tooling/Executor/DisableServiceExecutor.cs create mode 100644 src/Steeltoe.Tooling/Executor/Executor.cs rename src/Steeltoe.Tooling/Executor/{EnableServiceExecutor.cs => GetArgsExecutor.cs} (60%) delete mode 100644 src/Steeltoe.Tooling/Executor/GetServiceDeploymentArgsExecutor.cs create mode 100644 src/Steeltoe.Tooling/Executor/GroupExecutor.cs create mode 100644 src/Steeltoe.Tooling/Executor/InitializationExecutor.cs delete mode 100644 src/Steeltoe.Tooling/Executor/ListServicesExectutor.cs rename src/Steeltoe.Tooling/Executor/{ListEnvironmentsExecutor.cs => ListTargetsExecutor.cs} (58%) create mode 100644 src/Steeltoe.Tooling/Executor/RemoveExecutor.cs rename src/Steeltoe.Tooling/{IServiceBackend.cs => Executor/RequiresTargetAttribute.cs} (66%) delete mode 100644 src/Steeltoe.Tooling/Executor/ServicesExecutor.cs rename src/Steeltoe.Tooling/Executor/{SetServiceDeploymentArgsExecutor.cs => SetArgsExecutor.cs} (50%) create mode 100644 src/Steeltoe.Tooling/Executor/StatusApplicationsExecutor.cs rename src/Steeltoe.Tooling/Executor/{AddServiceExecutor.cs => StatusExecutor.cs} (57%) create mode 100644 src/Steeltoe.Tooling/Executor/UndeployApplicationsExecutor.cs rename src/Steeltoe.Tooling/Executor/{RemoveServiceExecutor.cs => UndeployExecutor.cs} (58%) delete mode 100644 src/Steeltoe.Tooling/Executor/UndeployServicesExecutor.cs rename src/Steeltoe.Tooling/{Executor/StatusServicesExecutor.cs => IBackend.cs} (63%) rename src/Steeltoe.Tooling/{Executor/IExecutor.cs => IConfigurationListener.cs} (77%) delete mode 100644 src/Steeltoe.Tooling/IToolingConfigurationListener.cs create mode 100644 src/Steeltoe.Tooling/Lifecycle.cs rename src/Steeltoe.Tooling/{ServiceLifecycleException.cs => LifecycleException.cs} (69%) create mode 100644 src/Steeltoe.Tooling/NotFoundException.cs delete mode 100644 src/Steeltoe.Tooling/RegistryConfiguration.cs rename src/Steeltoe.Tooling/{ServiceNotFoundException.cs => ServiceInfo.cs} (73%) delete mode 100644 src/Steeltoe.Tooling/ServiceLifecycle.cs delete mode 100644 src/Steeltoe.Tooling/ServiceManager.cs rename src/Steeltoe.Tooling/{ServiceType.cs => ServiceTypeInfo.cs} (93%) rename src/Steeltoe.Tooling/{Environment.cs => Target.cs} (72%) rename src/Steeltoe.Tooling/{EnvironmentConfiguration.cs => TargetConfiguration.cs} (96%) delete mode 100644 src/Steeltoe.Tooling/ToolingConfiguration.cs delete mode 100644 test/Steeltoe.Cli.Test/DisableFeature.cs delete mode 100644 test/Steeltoe.Cli.Test/EnableFeature.cs create mode 100644 test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryBackendTest.cs create mode 100644 test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryManifestFileTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryServiceManagerTest.cs rename test/Steeltoe.Tooling.Test/CloudFoundry/{CloudFoundryEnvironmentTest.cs => CloudFoundryTargetTest.cs} (75%) create mode 100644 test/Steeltoe.Tooling.Test/ConfigurationFileTest.cs create mode 100644 test/Steeltoe.Tooling.Test/ConfigurationTest.cs rename test/Steeltoe.Tooling.Test/Docker/{DockerServiceManagerTest.cs => DockerBackendTest.cs} (62%) rename test/Steeltoe.Tooling.Test/Docker/{DockerEnvironmentTest.cs => DockerTargetTest.cs} (85%) rename test/Steeltoe.Tooling.Test/Dummy/{DummyServiceBackendTest.cs => DummyBackendTest.cs} (60%) rename test/Steeltoe.Tooling.Test/Dummy/{DummyEnvironmentTest.cs => DummyTargetTest.cs} (71%) create mode 100644 test/Steeltoe.Tooling.Test/Executor/AddExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/AddServiceExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/DeployApplicationsExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/DeployExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/DeployServicesExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/DisableServiceExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/EnableServiceExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/GetArgsExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/GetServiceDeploymentArgsExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/InitializationExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/ListApplicationsExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/ListExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/ListServicesExecutorTest.cs rename test/Steeltoe.Tooling.Test/Executor/{ListEnvironmentsExecutorTest.cs => ListTargetsExecutorTest.cs} (73%) rename test/Steeltoe.Tooling.Test/Executor/{RemoveServiceExecutorTest.cs => RemoveExecutorTest.cs} (53%) create mode 100644 test/Steeltoe.Tooling.Test/Executor/SetArgsExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/SetServiceDeploymentArgsExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/StatusExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/StatusServiceExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/UndeployApplicationsExecutorTest.cs create mode 100644 test/Steeltoe.Tooling.Test/Executor/UndeployExecutorTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/Executor/UndeployServiceExecutorTest.cs rename test/Steeltoe.Tooling.Test/{ServiceLifecycleExceptionTest.cs => LifecycleExceptionTest.cs} (70%) create mode 100644 test/Steeltoe.Tooling.Test/LifecyleTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/ServiceLifecyleTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/ServiceManagerTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/ToolingConfigurationFileTest.cs delete mode 100644 test/Steeltoe.Tooling.Test/ToolingConfigurationTest.cs diff --git a/.gitignore b/.gitignore index 3dbd534..af53a3d 100644 --- a/.gitignore +++ b/.gitignore @@ -267,7 +267,7 @@ __pycache__/ \.*\.sw[A-z] # test files -\.steeltoe.tooling.yml +steeltoe.yml \.steeltoe.dummies dummy-service-backend.db diff --git a/src/Steeltoe.Cli/AddCommand.cs b/src/Steeltoe.Cli/AddCommand.cs index b85ccd2..66bc7c5 100644 --- a/src/Steeltoe.Cli/AddCommand.cs +++ b/src/Steeltoe.Cli/AddCommand.cs @@ -18,26 +18,26 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "Add a service.")] + [Command(Description = "Add an app or service")] public class AddCommand : Command { public const string Name = "add"; - [Required(ErrorMessage = "Service type not specified")] - [Argument(0, Name = "type", Description = "Service type")] - private string ServiceType { get; } = null; + [Required(ErrorMessage = "'app' or service type not specified")] + [Argument(0, Name = "type", Description = "'app' or service type")] + private string ProjectOrServiceType { get; } = null; - [Required(ErrorMessage = "Service name not specified")] - [Argument(1, Name = "service", Description = "Service name")] - private string ServiceName { get; } = null; + [Required(ErrorMessage = "App or service name not specified")] + [Argument(1, Name = "name", Description = "App or service name")] + private string ProjectOrServiceName { get; } = null; public AddCommand(IConsole console) : base(console) { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - return new AddServiceExecutor(ServiceName, ServiceType); + return new AddExecutor(ProjectOrServiceName, ProjectOrServiceType); } } } diff --git a/src/Steeltoe.Cli/ArgsCommand.cs b/src/Steeltoe.Cli/ArgsCommand.cs index 84b01b9..4dd2015 100644 --- a/src/Steeltoe.Cli/ArgsCommand.cs +++ b/src/Steeltoe.Cli/ArgsCommand.cs @@ -22,24 +22,24 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { [Command( - Description = "Set or get the deployment environment arguments for a service.", + Description = "Set or get the deployment arguments for an app or service", ExtendedHelpText = - "If run with no deployment environment arguments, show the service's current deployment environment arguments.", + "If run with no arguments, show the current deployment arguments for the app or service.", AllowArgumentSeparator = true )] public class ArgsCommand : Command { public const string Name = "args"; - [Required(ErrorMessage = "Deployment environment not specified")] - [Argument(0, Name = "environment", Description = "Deployment environment")] - private string EnvironmentName { get; } + [Required(ErrorMessage = "App or service name not specified")] + [Argument(0, Name = "name", Description = "App or service name")] + private string ApplicationOrService { get; } - [Required(ErrorMessage = "Service name not specified")] - [Argument(1, Name = "service", Description = "Service name")] - private string ServiceName { get; } + [Required(ErrorMessage = "Deployment target name not specified")] + [Argument(1, Name = "target", Description = "Deployment target name")] + private string Target { get; } - [Argument(2, Name = "args", Description = "Deployment environment arguments")] + [Argument(2, Name = "args", Description = "Deployment arguments")] private List Arguments { get; } [Option("-F|--force", Description = "Overwrite existing deployment environment arguments")] @@ -51,7 +51,7 @@ namespace Steeltoe.Cli { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { var args = new List(); if (Arguments != null) @@ -66,10 +66,10 @@ namespace Steeltoe.Cli if (args.Count == 0) { - return new GetServiceDeploymentArgsExecutor(EnvironmentName, ServiceName); + return new GetArgsExecutor(ApplicationOrService, Target); } - return new SetServiceDeploymentArgsExecutor(EnvironmentName, ServiceName, string.Join(" ", args), Force); + return new SetArgsExecutor(ApplicationOrService, Target, string.Join(" ", args), Force); } } } diff --git a/src/Steeltoe.Cli/Command.cs b/src/Steeltoe.Cli/Command.cs index a3c1748..12e6b5d 100644 --- a/src/Steeltoe.Cli/Command.cs +++ b/src/Steeltoe.Cli/Command.cs @@ -35,38 +35,29 @@ namespace Steeltoe.Cli { try { - var executor = GetExecutor(); - var minfo = executor.GetType(); - Logger.LogDebug($"executor is {minfo}"); - var requiresInitializedProject = false; - foreach (var attr in minfo.GetCustomAttributes(true)) - { - var initAttr = attr as RequiresInitializationAttribute; - if (initAttr != null) - { - Logger.LogDebug($"{minfo} requires initialized project"); - requiresInitializedProject = true; - } - } - - Logger.LogDebug($"tooling working directory is {app.WorkingDirectory}"); + Logger.LogDebug($"tooling working directory: {app.WorkingDirectory}"); var cfgFilePath = Program.ProjectConfigurationPath; if (cfgFilePath == null) { cfgFilePath = app.WorkingDirectory; } - var cfgFile = new ToolingConfigurationFile(cfgFilePath); - Logger.LogDebug($"tooling configuration file is {cfgFile.File}"); - if (requiresInitializedProject && !cfgFile.Exists()) + Configuration cfg; + var cfgFile = new ConfigurationFile(cfgFilePath); + if (cfgFile.Exists()) { - throw new ToolingException("Project has not been initialized for Steeltoe Developer Tools"); + cfg = cfgFile.Configuration; + Logger.LogDebug($"tooling configuration file: {cfgFile.File}"); + } + else + { + cfg = null; + Logger.LogDebug($"tooling configuration file not found: {cfgFile.File}"); } - var context = new Context( app.WorkingDirectory, - cfgFile.ToolingConfiguration, + cfg, _console.Out, new CommandShell() ); @@ -93,12 +84,12 @@ namespace Steeltoe.Cli } catch (Exception e) { - Logger.LogDebug($"unhandled exception: {e}{System.Environment.NewLine}{e.StackTrace}"); + Logger.LogDebug($"unhandled exception: {e}{Environment.NewLine}{e.StackTrace}"); app.Error.WriteLine(e.Message); return -1; } } - protected abstract IExecutor GetExecutor(); + protected abstract Executor GetExecutor(); } } diff --git a/src/Steeltoe.Cli/DeployCommand.cs b/src/Steeltoe.Cli/DeployCommand.cs index 0cd75ad..bda5ff0 100644 --- a/src/Steeltoe.Cli/DeployCommand.cs +++ b/src/Steeltoe.Cli/DeployCommand.cs @@ -17,7 +17,7 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "Deploy enabled services to the targeted deployment environment.")] + [Command(Description = "Deploy apps and services to the target")] public class DeployCommand : Command { public const string Name = "deploy"; @@ -26,10 +26,9 @@ namespace Steeltoe.Cli { } - protected override IExecutor -GetExecutor() + protected override Executor GetExecutor() { - return new DeployServicesExecutor(); + return new DeployExecutor(); } } } diff --git a/src/Steeltoe.Cli/DisableCommand.cs b/src/Steeltoe.Cli/DisableCommand.cs deleted file mode 100644 index d41650b..0000000 --- a/src/Steeltoe.Cli/DisableCommand.cs +++ /dev/null @@ -1,41 +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 System.ComponentModel.DataAnnotations; -using McMaster.Extensions.CommandLineUtils; -using Steeltoe.Tooling.Executor; - -// ReSharper disable UnassignedGetOnlyAutoProperty - -namespace Steeltoe.Cli -{ - [Command(Description = "Disable a service.")] - public class DisableCommand : Command - { - public const string Name = "disable"; - - [Required(ErrorMessage = "Service name not specified")] - [Argument(0, Name = "service", Description = "Service name")] - private string ServiceName { get; } - - public DisableCommand(IConsole console) : base(console) - { - } - - protected override IExecutor GetExecutor() - { - return new DisableServiceExecutor(ServiceName); - } - } -} diff --git a/src/Steeltoe.Cli/DoctorCommand.cs b/src/Steeltoe.Cli/DoctorCommand.cs index a01f79a..e9bcc92 100644 --- a/src/Steeltoe.Cli/DoctorCommand.cs +++ b/src/Steeltoe.Cli/DoctorCommand.cs @@ -18,7 +18,7 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "Check for potential problems.")] + [Command(Description = "Check for potential problems")] public class DoctorCommand : Command { public const string Name = "doctor"; @@ -27,45 +27,45 @@ namespace Steeltoe.Cli { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { return new DoctorExecutor(); } } - internal class DoctorExecutor : IExecutor + internal class DoctorExecutor : Executor { - public void Execute(Context context) + protected override void Execute() { - context.Console.WriteLine($"Steeltoe Developer Tools version ... {Program.GetVersion()}"); + Context.Console.WriteLine($"Steeltoe Developer Tools version ... {Program.GetVersion()}"); // dotnet - context.Console.Write("DotNet ... "); - var dotnetVersion = new Tooling.Cli("dotnet", context.Shell).Run("--version").Trim(); - context.Console.WriteLine($"dotnet version {dotnetVersion}"); + Context.Console.Write("DotNet ... "); + var dotnetVersion = new Tooling.Cli("dotnet", Context.Shell).Run("--version").Trim(); + Context.Console.WriteLine($"dotnet version {dotnetVersion}"); // is intialized? - context.Console.Write("initialized ... "); - var cfgFile = new ToolingConfigurationFile(context.ProjectDirectory); + Context.Console.Write("initialized ... "); + var cfgFile = new ConfigurationFile(Context.ProjectDirectory); if (!cfgFile.Exists()) { - context.Console.WriteLine($"!!! no (run '{Program.Name} {InitCommand.Name}' to initialize)"); + Context.Console.WriteLine($"!!! no (run '{Program.Name} {InitCommand.Name}' to initialize)"); return; } - context.Console.WriteLine("yes"); + Context.Console.WriteLine("yes"); // target deployment environment - context.Console.Write("target deployment environment ... "); - var target = cfgFile.ToolingConfiguration.EnvironmentName; + Context.Console.Write("target ... "); + string target = cfgFile.Configuration.Target; if (target == null) { - context.Console.WriteLine($"!!! not set (run '{Program.Name} {TargetCommand.Name} ' to set)"); + Context.Console.WriteLine($"!!! not set (run '{Program.Name} {TargetCommand.Name} ' to set)"); return; } - context.Console.WriteLine(target); - Registry.GetEnvironment(target).IsHealthy(context); + Context.Console.WriteLine(target); + Registry.GetTarget(target).IsHealthy(Context); } } } diff --git a/src/Steeltoe.Cli/EnableCommand.cs b/src/Steeltoe.Cli/EnableCommand.cs deleted file mode 100644 index bfb612f..0000000 --- a/src/Steeltoe.Cli/EnableCommand.cs +++ /dev/null @@ -1,41 +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 System.ComponentModel.DataAnnotations; -using McMaster.Extensions.CommandLineUtils; -using Steeltoe.Tooling.Executor; - -// ReSharper disable UnassignedGetOnlyAutoProperty - -namespace Steeltoe.Cli -{ - [Command(Description = "Enable a service.")] - public class EnableCommand : Command - { - public const string Name = "enable"; - - [Required(ErrorMessage = "Service name not specified")] - [Argument(0, Name = "service", Description = "Service name")] - private string ServiceName { get; } - - public EnableCommand(IConsole console) : base(console) - { - } - - protected override IExecutor GetExecutor() - { - return new EnableServiceExecutor(ServiceName); - } - } -} diff --git a/src/Steeltoe.Cli/InitCommand.cs b/src/Steeltoe.Cli/InitCommand.cs index 7523a92..c4d9afd 100644 --- a/src/Steeltoe.Cli/InitCommand.cs +++ b/src/Steeltoe.Cli/InitCommand.cs @@ -12,17 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; -using System.IO; using McMaster.Extensions.CommandLineUtils; -using Steeltoe.Tooling; using Steeltoe.Tooling.Executor; // ReSharper disable UnassignedGetOnlyAutoProperty namespace Steeltoe.Cli { - [Command(Description = "Initialize a project for Steeltoe Developer Tools.")] + [Command(Description = "Initialize Steeltoe Developer Tools")] public class InitCommand : Command { public const string Name = "init"; @@ -34,35 +31,9 @@ namespace Steeltoe.Cli { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - return new InitExecutor(Force); - } - } - - internal class InitExecutor : IExecutor - { - private readonly bool _force; - - internal InitExecutor(Boolean force = false) - { - _force = force; - } - - public void Execute(Context context) - { - var cfgFilePath = Program.ProjectConfigurationPath == null - ? context.ProjectDirectory - : Path.Combine(context.ProjectDirectory, Program.ProjectConfigurationPath); - - var cfgFile = new ToolingConfigurationFile(cfgFilePath); - if (cfgFile.Exists() && !_force) - { - throw new ToolingException("Project already initialized"); - } - - cfgFile.Store(); - context.Console.WriteLine("Project initialized for Steeltoe Developer Tools"); + return new InitializationExecutor(Program.ProjectConfigurationPath, Force); } } } diff --git a/src/Steeltoe.Cli/ListCommand.cs b/src/Steeltoe.Cli/ListCommand.cs index b759358..c484730 100644 --- a/src/Steeltoe.Cli/ListCommand.cs +++ b/src/Steeltoe.Cli/ListCommand.cs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; using McMaster.Extensions.CommandLineUtils; using Steeltoe.Tooling.Executor; @@ -20,21 +19,11 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "List services, service types, or deployment environments.", - ExtendedHelpText = "If run with no options, list services.")] + [Command(Description = "List apps and services")] public class ListCommand : Command { public const string Name = "list"; - [Option("-e|--environments", Description = "List deployment environments")] - private bool ListEnvironments { get; } - - [Option("-s|--services", Description = "List services")] - private bool ListServices { get; } - - [Option("-t|--service-types", Description = "List service types")] - private bool ListServiceTypes { get; } - [Option("-v|--verbose", Description = "Verbose")] private bool Verbose { get; } @@ -42,34 +31,9 @@ namespace Steeltoe.Cli { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - var optionCount = 0; - IExecutor executor = new ListServicesExecutor(Verbose); - if (ListServices) - { - ++optionCount; - } - - if (ListServiceTypes) - { - executor = new ListServiceTypesExecutor(Verbose); - ++optionCount; - } - - if (ListEnvironments) - { - executor = new ListEnvironmentsExecutor(Verbose); - ++optionCount; - } - - if (optionCount > 1) - { - throw new ArgumentException( - "Specify at most one of: -e|--environments, -s|--services, -t|--service-types"); - } - - return executor; + return new ListExecutor(Verbose); } } } diff --git a/src/Steeltoe.Cli/Program.cs b/src/Steeltoe.Cli/Program.cs index dfa122e..4afcd49 100644 --- a/src/Steeltoe.Cli/Program.cs +++ b/src/Steeltoe.Cli/Program.cs @@ -24,8 +24,6 @@ namespace Steeltoe.Cli [Subcommand(TargetCommand.Name, typeof(TargetCommand))] [Subcommand(AddCommand.Name, typeof(AddCommand))] [Subcommand(RemoveCommand.Name, typeof(RemoveCommand))] - [Subcommand(DisableCommand.Name, typeof(DisableCommand))] - [Subcommand(EnableCommand.Name, typeof(EnableCommand))] [Subcommand(DeployCommand.Name, typeof(DeployCommand))] [Subcommand(UndeployCommand.Name, typeof(UndeployCommand))] [Subcommand(StatusCommand.Name, typeof(StatusCommand))] @@ -43,7 +41,7 @@ namespace Steeltoe.Cli .InformationalVersion; [Option("-C|--config-file", Description = - "Configure tooling using the specified file instead of .steeltoe.tooling.yml")] + "Configure tooling using the specified file instead of steeltoe.yml")] // ReSharper disable once UnassignedGetOnlyAutoProperty public static string ProjectConfigurationPath { get; } diff --git a/src/Steeltoe.Cli/RemoveCommand.cs b/src/Steeltoe.Cli/RemoveCommand.cs index bf68cfd..c23d2fa 100644 --- a/src/Steeltoe.Cli/RemoveCommand.cs +++ b/src/Steeltoe.Cli/RemoveCommand.cs @@ -20,22 +20,22 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "Remove a service.")] + [Command(Description = "Remove an app or service")] public class RemoveCommand : Command { public const string Name = "remove"; - [Required(ErrorMessage = "Service name not specified")] - [Argument(0, Name = "service", Description = "Service name")] - private string ServiceName { get; } + [Required(ErrorMessage = "App or service name not specified")] + [Argument(0, Name = "name", Description = "App or service name")] + private string AppOrServiceName { get; } public RemoveCommand(IConsole console) : base(console) { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - return new RemoveServiceExecutor(ServiceName); + return new RemoveExecutor(AppOrServiceName); } } } diff --git a/src/Steeltoe.Cli/StatusCommand.cs b/src/Steeltoe.Cli/StatusCommand.cs index b73c163..c3b9bf9 100644 --- a/src/Steeltoe.Cli/StatusCommand.cs +++ b/src/Steeltoe.Cli/StatusCommand.cs @@ -17,7 +17,7 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "Show service statuses.")] + [Command(Description = "Show app and service statuses")] public class StatusCommand : Command { public const string Name = "status"; @@ -26,9 +26,9 @@ namespace Steeltoe.Cli { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - return new StatusServicesExecutor(); + return new StatusExecutor(); } } } diff --git a/src/Steeltoe.Cli/TargetCommand.cs b/src/Steeltoe.Cli/TargetCommand.cs index f55280f..505577f 100644 --- a/src/Steeltoe.Cli/TargetCommand.cs +++ b/src/Steeltoe.Cli/TargetCommand.cs @@ -20,30 +20,30 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { [Command(Description = - "Set or get the targeted deployment environment.", - ExtendedHelpText = "If run with no args, show the currently targeted deployment environment.")] + "Set or get the deployment target", + ExtendedHelpText = "If run with no args, show the current deployment target.")] public class TargetCommand : Command { public const string Name = "target"; - [Argument(0, Name = "environment", Description = "Deployment environment")] - private string EnvironmentName { get; } + [Argument(0, Name = "target", Description = "Deployment target name")] + private string Target { get; } - [Option("-F|--force", Description = "Target the deployment environment even if checks fail")] + [Option("-F|--force", Description = "Set the deployment target even if checks fail")] private bool Force { get; } public TargetCommand(IConsole console) : base(console) { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - if (EnvironmentName == null) + if (Target == null) { return new GetTargetExecutor(); } - return new SetTargetExecutor(EnvironmentName, Force); + return new SetTargetExecutor(Target, Force); } } } diff --git a/src/Steeltoe.Cli/UndeployCommand.cs b/src/Steeltoe.Cli/UndeployCommand.cs index 8c5d625..5ec253f 100644 --- a/src/Steeltoe.Cli/UndeployCommand.cs +++ b/src/Steeltoe.Cli/UndeployCommand.cs @@ -17,7 +17,7 @@ using Steeltoe.Tooling.Executor; namespace Steeltoe.Cli { - [Command(Description = "Undeploy enabled services from the targeted deployment environment.")] + [Command(Description = "Undeploy apps and services from the target")] public class UndeployCommand : Command { public const string Name = "undeploy"; @@ -26,9 +26,9 @@ namespace Steeltoe.Cli { } - protected override IExecutor GetExecutor() + protected override Executor GetExecutor() { - return new UndeployServicesExecutor(); + return new UndeployExecutor(); } } } diff --git a/src/Steeltoe.Tooling/Service.cs b/src/Steeltoe.Tooling/AppInfo.cs similarity index 77% rename from src/Steeltoe.Tooling/Service.cs rename to src/Steeltoe.Tooling/AppInfo.cs index 444de5c..46aa90b 100644 --- a/src/Steeltoe.Tooling/Service.cs +++ b/src/Steeltoe.Tooling/AppInfo.cs @@ -14,17 +14,13 @@ namespace Steeltoe.Tooling { - public class Service + public class AppInfo { - public string Name { get; } + public string App { get; } - public string Type { get; } - - public Service(string name, string type) + public AppInfo(string app) { - - Name = name; - Type = type; + App = app; } } } diff --git a/src/Steeltoe.Tooling/Cli.cs b/src/Steeltoe.Tooling/Cli.cs index 3c5aa61..f283a0a 100644 --- a/src/Steeltoe.Tooling/Cli.cs +++ b/src/Steeltoe.Tooling/Cli.cs @@ -18,7 +18,7 @@ namespace Steeltoe.Tooling { public string Command { get; } - public Shell Shell { get; } + private Shell Shell { get; } public Cli(string command, Shell shell) { diff --git a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryBackend.cs b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryBackend.cs new file mode 100644 index 0000000..eca3e97 --- /dev/null +++ b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryBackend.cs @@ -0,0 +1,130 @@ +// 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 System.Collections.Generic; +using System.IO; +using System.Text.RegularExpressions; + +namespace Steeltoe.Tooling.CloudFoundry +{ + public class CloudFoundryBackend : IBackend + { + private readonly Context _context; + + private readonly Cli _cfCli; + + private readonly Cli _dotnetCli; + + public CloudFoundryBackend(Context context) + { + _context = context; + _cfCli = new CloudFoundryCli(context.Shell); + _dotnetCli = new Cli("dotnet", _context.Shell); + } + + public void DeployApp(string application) + { + var manifestPath = Path.Combine(_context.ProjectDirectory, "manifest-steeltoe.yml"); + if (File.Exists(manifestPath)) + { + File.Delete(manifestPath); + } + + var manifestFile = new CloudFoundryManifestFile(manifestPath); + manifestFile.CloudFoundryManifest.Applications.Add(new CloudFoundryManifest.Application + { + Name = application, + Command = $"cmd /c .\\{Path.GetFileName(_context.ProjectDirectory)}", + BuildPacks = new List {"binary_buildpack"}, + Stack = "windows2016", + Memory = "512M", + Environment = new Dictionary {{"ASPNETCORE_ENVIRONMENT", "development"}}, + ServiceNames = _context.Configuration.GetServices() + } + ); + manifestFile.Store(); + _dotnetCli.Run("publish -f netcoreapp2.1 -r win10-x64"); + _cfCli.Run("push -f manifest-steeltoe.yml -p bin/Debug/netcoreapp2.1/win10-x64/publish"); + } + + public void UndeployApp(string application) + { + _cfCli.Run($"delete {application} -f"); + } + + public Lifecycle.Status GetAppStatus(string application) + { + try + { + var appInfo = _cfCli.Run($"app {application}"); + var state = new Regex(@"^#0\s+(\S+)", RegexOptions.Multiline).Match(appInfo).Groups[1].ToString() + .Trim(); + switch (state) + { + case "down": + return Lifecycle.Status.Starting; + case "running": + return Lifecycle.Status.Online; + } + } + catch (CliException e) + { + if (e.Error.Contains($"App {application} not found")) + { + return Lifecycle.Status.Offline; + } + } + + return Lifecycle.Status.Unknown; + } + + public void DeployService(string service) + { + var svcInfo = _context.Configuration.GetServiceInfo(service); + var cfServiceDef = _context.Target.Configuration.ServiceTypeProperties[svcInfo.ServiceType]; + _cfCli.Run($"create-service {cfServiceDef["service"]} {cfServiceDef["plan"]} {service}"); + } + + public void UndeployService(string service) + { + _cfCli.Run($"delete-service {service} -f"); + } + + public Lifecycle.Status GetServiceStatus(string service) + { + try + { + var serviceInfo = _cfCli.Run($"service {service}"); + var state = new Regex(@"^status:\s+(.*)$", RegexOptions.Multiline).Match(serviceInfo).Groups[1] + .ToString().Trim(); + switch (state) + { + case "create in progress": + return Lifecycle.Status.Starting; + case "create succeeded": + return Lifecycle.Status.Online; + } + } + catch (CliException e) + { + if (e.Error.Contains($"Service instance {service} not found")) + { + return Lifecycle.Status.Offline; + } + } + + return Lifecycle.Status.Unknown; + } + } +} diff --git a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifest.cs b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifest.cs new file mode 100644 index 0000000..a06f702 --- /dev/null +++ b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifest.cs @@ -0,0 +1,49 @@ +// 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 System.Collections.Generic; +using YamlDotNet.Serialization; + +namespace Steeltoe.Tooling.CloudFoundry +{ + public class CloudFoundryManifest + { + [YamlMember(Alias = "applications")] + public List Applications { get; set; }= new List(); + + public class Application + { + [YamlMember(Alias = "name")] + public string Name { get; set; } + + [YamlMember(Alias = "command")] + public string Command { get; set; } + + [YamlMember(Alias = "buildpacks")] + public List BuildPacks { get; set; } + + [YamlMember(Alias = "stack")] + public string Stack { get; set; } + + [YamlMember(Alias = "memory")] + public string Memory { get; set; } + + [YamlMember(Alias = "env")] + public Dictionary Environment { get; set; } + + [YamlMember(Alias = "services")] + public List ServiceNames { get; set; } + } + } +} diff --git a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifestFile.cs b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifestFile.cs new file mode 100644 index 0000000..50cb0bf --- /dev/null +++ b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryManifestFile.cs @@ -0,0 +1,54 @@ +using System.IO; +using Microsoft.Extensions.Logging; +using YamlDotNet.Serialization; + +namespace Steeltoe.Tooling.CloudFoundry +{ + public class CloudFoundryManifestFile + { + private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); + + public CloudFoundryManifest CloudFoundryManifest { get; private set; } + + public string File { get; } + + public CloudFoundryManifestFile(string file) + { + File = file; + if (Exists()) + { + Load(); + } + else + { + CloudFoundryManifest = new CloudFoundryManifest(); + } + } + + public void Load() + { + Logger.LogDebug($"loading cloud foundry manifest from {File}"); + var deserializer = new DeserializerBuilder().Build(); + using (var reader = new StreamReader(File)) + { + CloudFoundryManifest = deserializer.Deserialize(reader); + } + } + + public void Store() + { + Logger.LogDebug($"storing cloud foundry manifest to {File}"); + var serializer = new SerializerBuilder().Build(); + var yaml = serializer.Serialize(CloudFoundryManifest); + using (var writer = new StreamWriter(File)) + { + writer.Write(yaml); + } + } + + public bool Exists() + { + return System.IO.File.Exists(File); + } + } +} diff --git a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryServiceBackend.cs b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryServiceBackend.cs deleted file mode 100644 index 8f3b454..0000000 --- a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryServiceBackend.cs +++ /dev/null @@ -1,68 +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 System.Text.RegularExpressions; - -namespace Steeltoe.Tooling.CloudFoundry -{ - public class CloudFoundryServiceBackend : IServiceBackend - { - private readonly Context _context; - - private readonly CloudFoundryCli _cli; - - public CloudFoundryServiceBackend(Context context) - { - _context = context; - _cli = new CloudFoundryCli(_context.Shell); - } - - public void DeployService(string name, string type) - { - var cfServiceDef = _context.Environment.Configuration.ServiceTypeProperties[type]; - _cli.Run($"create-service {cfServiceDef["service"]} {cfServiceDef["plan"]} {name}"); - } - - public void UndeployService(string name) - { - _cli.Run($"delete-service {name} -f"); - } - - public ServiceLifecycle.State GetServiceLifecleState(string name) - { - try - { - var serviceInfo = _cli.Run($"service {name}"); - var state = new Regex(@"^status:\s+(.*)$", RegexOptions.Multiline).Match(serviceInfo).Groups[1] - .ToString().TrimEnd(); - switch (state) - { - case "create in progress": - return ServiceLifecycle.State.Starting; - case "create succeeded": - return ServiceLifecycle.State.Online; - } - } - catch (CliException e) - { - if (e.Error.Contains($"Service instance {name} not found")) - { - return ServiceLifecycle.State.Offline; - } - } - - return ServiceLifecycle.State.Unknown; - } - } -} diff --git a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryEnvironment.cs b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryTarget.cs similarity index 85% rename from src/Steeltoe.Tooling/CloudFoundry/CloudFoundryEnvironment.cs rename to src/Steeltoe.Tooling/CloudFoundry/CloudFoundryTarget.cs index 9af0781..5b20744 100644 --- a/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryEnvironment.cs +++ b/src/Steeltoe.Tooling/CloudFoundry/CloudFoundryTarget.cs @@ -14,15 +14,15 @@ namespace Steeltoe.Tooling.CloudFoundry { - public class CloudFoundryEnvironment : Environment + public class CloudFoundryTarget : Target { - public CloudFoundryEnvironment(EnvironmentConfiguration config) : base(config) + public CloudFoundryTarget(TargetConfiguration configuration) : base(configuration) { } - public override IServiceBackend GetServiceBackend(Context context) + public override IBackend GetBackend(Context context) { - return new CloudFoundryServiceBackend(context); + return new CloudFoundryBackend(context); } public override bool IsHealthy(Context context) diff --git a/src/Steeltoe.Tooling/Configuration.cs b/src/Steeltoe.Tooling/Configuration.cs new file mode 100644 index 0000000..58fe4fa --- /dev/null +++ b/src/Steeltoe.Tooling/Configuration.cs @@ -0,0 +1,184 @@ +// 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 System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Logging; +using YamlDotNet.Serialization; + +namespace Steeltoe.Tooling +{ + public class Configuration + { + private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); + + [YamlMember(Alias = "target")] public string Target { get; set; } + + [YamlMember(Alias = "apps")] + public SortedDictionary Apps { get; set; } = new SortedDictionary(); + + [YamlMember(Alias = "services")] + public SortedDictionary Services { get; set; } = new SortedDictionary(); + + private readonly List _listeners = new List(); + + public void AddApp(string app) + { + Logger.LogDebug($"adding app {app}"); + if (Apps.ContainsKey(app)) + { + throw new ToolingException($"App '{app}' already exists"); + } + + Apps[app] = new App(); + NotifyChanged(); + } + + public void RemoveApp(string app) + { + Logger.LogDebug($"removing app {app}"); + if (!Apps.Remove(app)) + { + throw new NotFoundException(app, "app"); + } + + NotifyChanged(); + } + + public List GetApps() + { + return Apps.Keys.ToList(); + } + + public AppInfo GetAppInfo(string app) + { + try + { + return new AppInfo(app); + } + catch (KeyNotFoundException) + { + throw new NotFoundException(app, "app"); + } + } + + public void AddService(string service, string serviceType) + { + Logger.LogDebug($"adding service {serviceType} '{service}'"); + if (!Registry.GetServiceTypes().Contains(serviceType)) + { + throw new NotFoundException(serviceType, "type"); + } + + if (Services.ContainsKey(service)) + { + throw new ToolingException($"Service '{service}' already exists"); + } + + Services[service] = new Service {ServiceTypeName = serviceType}; + NotifyChanged(); + } + + public void RemoveService(string service) + { + Logger.LogDebug($"removing service {service}"); + if (!Services.Remove(service)) + { + throw new NotFoundException(service, "service"); + } + + NotifyChanged(); + } + + public List GetServices() + { + return Services.Keys.ToList(); + } + + public ServiceInfo GetServiceInfo(string service) + { + try + { + return new ServiceInfo(service, Services[service].ServiceTypeName); + } + catch (KeyNotFoundException) + { + throw new NotFoundException(service, "service"); + } + } + + public void SetServiceDeploymentArgs(string service, string target, string args) + { + Logger.LogDebug($"setting service '{service}' args for target '{target} to '{args}'"); + if (!Registry.Targets.Contains(target)) + { + throw new NotFoundException(target, "target"); + } + + if (!Services.ContainsKey(service)) + { + throw new NotFoundException(service, "service"); + } + + Services[service].Args[target] = args; + NotifyChanged(); + } + + public string GetServiceDeploymentArgs(string service, string target) + { + if (!Registry.Targets.Contains(target)) + { + throw new NotFoundException(target, "target"); + } + + try + { + var svc = Services[service]; + string args; + svc.Args.TryGetValue(target, out args); + return args; + } + catch (KeyNotFoundException) + { + throw new NotFoundException(service, "service"); + } + } + + public void AddListener(IConfigurationListener listener) + { + _listeners.Add(listener); + } + + public void NotifyChanged() + { + Logger.LogDebug("configuration changed"); + foreach (var listener in _listeners) + { + listener.ConfigurationChangeEvent(); + } + } + + public class App + { + } + + public class Service + { + [YamlMember(Alias = "type")] public string ServiceTypeName { get; set; } + + [YamlMember(Alias = "args")] + public SortedDictionary Args { get; set; } = new SortedDictionary(); + } + } +} diff --git a/src/Steeltoe.Tooling/ToolingConfigurationFile.cs b/src/Steeltoe.Tooling/ConfigurationFile.cs similarity index 69% rename from src/Steeltoe.Tooling/ToolingConfigurationFile.cs rename to src/Steeltoe.Tooling/ConfigurationFile.cs index 8ffc63c..3a4b3a3 100644 --- a/src/Steeltoe.Tooling/ToolingConfigurationFile.cs +++ b/src/Steeltoe.Tooling/ConfigurationFile.cs @@ -18,42 +18,46 @@ using YamlDotNet.Serialization; namespace Steeltoe.Tooling { - public class ToolingConfigurationFile : IToolingConfigurationListener + public class ConfigurationFile : IConfigurationListener { - private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); + private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); - public const string DefaultFileName = ".steeltoe.tooling.yml"; + public const string DefaultFileName = "steeltoe.yml"; - public ToolingConfiguration ToolingConfiguration { get; private set; } = new ToolingConfiguration(); + public Configuration Configuration { get; private set; } public string File { get; } - public ToolingConfigurationFile(string path) + public ConfigurationFile(string path) { File = Directory.Exists(path) ? Path.Combine(path, DefaultFileName) : path; if (Exists()) { Load(); } + else + { + Configuration = new Configuration(); + } } public void Load() { - Logger.LogDebug($"loading tooling configuration from {File}"); + Logger.LogDebug($"loading configuration from {File}"); var deserializer = new DeserializerBuilder().Build(); using (var reader = new StreamReader(File)) { - ToolingConfiguration = deserializer.Deserialize(reader); + Configuration = deserializer.Deserialize(reader); } - ToolingConfiguration.AddListener(this); + Configuration.AddListener(this); } public void Store() { - Logger.LogDebug($"storing tooling configuration to {File}"); + Logger.LogDebug($"storing configuration to {File}"); var serializer = new SerializerBuilder().Build(); - var yaml = serializer.Serialize(ToolingConfiguration); + var yaml = serializer.Serialize(Configuration); using (var writer = new StreamWriter(File)) { writer.Write(yaml); diff --git a/src/Steeltoe.Tooling/Context.cs b/src/Steeltoe.Tooling/Context.cs index 8f75851..58abd8f 100644 --- a/src/Steeltoe.Tooling/Context.cs +++ b/src/Steeltoe.Tooling/Context.cs @@ -20,35 +20,33 @@ namespace Steeltoe.Tooling { public string ProjectDirectory { get; } - public ToolingConfiguration ToolingConfiguration { get; } + public Configuration Configuration { get; } public TextWriter Console { get; } public Shell Shell { get; } - public Environment Environment + public Target Target { get { - if (ToolingConfiguration?.EnvironmentName == null) + if (Configuration?.Target == null) { - throw new ToolingException("Target deployment environment not set"); + throw new ToolingException("Target not set"); } - return Registry.GetEnvironment(ToolingConfiguration.EnvironmentName); + return Registry.GetTarget(Configuration.Target); } } - public ServiceManager ServiceManager { get; } + public IBackend Backend => Target.GetBackend(this); - - public Context(string dir, ToolingConfiguration config, TextWriter console, Shell shell) + public Context(string dir, Configuration config, TextWriter console, Shell shell) { ProjectDirectory = dir; - ToolingConfiguration = config; + Configuration = config; Console = console; Shell = shell; - ServiceManager = new ServiceManager(this); } } } diff --git a/src/Steeltoe.Tooling/Docker/DockerServiceBackend.cs b/src/Steeltoe.Tooling/Docker/DockerBackend.cs similarity index 62% rename from src/Steeltoe.Tooling/Docker/DockerServiceBackend.cs rename to src/Steeltoe.Tooling/Docker/DockerBackend.cs index 17e8ed8..6f8bb19 100644 --- a/src/Steeltoe.Tooling/Docker/DockerServiceBackend.cs +++ b/src/Steeltoe.Tooling/Docker/DockerBackend.cs @@ -19,7 +19,7 @@ using System.Text.RegularExpressions; namespace Steeltoe.Tooling.Docker { - public class DockerServiceBackend : IServiceBackend + public class DockerBackend : IBackend { private const string Localhost = "127.0.0.1"; @@ -27,24 +27,40 @@ namespace Steeltoe.Tooling.Docker private readonly DockerCli _cli; - internal DockerServiceBackend(Context context) + internal DockerBackend(Context context) { _context = context; _cli = new DockerCli(_context.Shell); } - public void DeployService(string name, string type) + public void DeployApp(string application) + { + throw new NotImplementedException(); + } + + public void UndeployApp(string application) + { + throw new NotImplementedException(); + } + + public Lifecycle.Status GetAppStatus(string application) + { + throw new NotImplementedException(); + } + + public void DeployService(string service) { var dockerInfo = new DockerCli(_context.Shell).Run("info"); var dockerOs = new Regex(@"OSType:\s*(.+)", RegexOptions.Multiline).Match(dockerInfo).Groups[1].ToString(); - DeployService(name, type, dockerOs); + DeployService(service, dockerOs); } - public void DeployService(string name, string type, string os) + public void DeployService(string service, string os) { - var image = LookupImage(type, os); - var port = GetServicePort(type); - var args = _context.ServiceManager.GetServiceDeploymentArgs("docker", name); + var svcInfo = _context.Configuration.GetServiceInfo(service); + var port = GetPort(svcInfo.ServiceType); + var image = LookupImage(svcInfo.ServiceType, os); + var args = _context.Configuration.GetServiceDeploymentArgs(service, "docker"); if (args == null) { args = ""; @@ -54,52 +70,52 @@ namespace Steeltoe.Tooling.Docker args += " "; } - _cli.Run($"run --name {name} --publish {port}:{port} --detach --rm {args}{image}"); + _cli.Run($"run --name {service} --publish {port}:{port} --detach --rm {args}{image}"); } - public void UndeployService(string name) + public void UndeployService(string service) { - _cli.Run($"stop {name}"); + _cli.Run($"stop {service}"); } - public ServiceLifecycle.State GetServiceLifecleState(string name) + public Lifecycle.Status GetServiceStatus(string service) { - var containerInfo = _cli.Run($"ps --no-trunc --filter name=^/{name}$").Split('\n'); + var containerInfo = _cli.Run($"ps --no-trunc --filter name=^/{service}$").Split('\n'); if (containerInfo.Length <= 2) { - return ServiceLifecycle.State.Offline; + return Lifecycle.Status.Offline; } var statusStart = containerInfo[0].IndexOf("STATUS", StringComparison.Ordinal); if (!containerInfo[1].Substring(statusStart).StartsWith("Up ")) { - return ServiceLifecycle.State.Unknown; + return Lifecycle.Status.Unknown; } var imageStart = containerInfo[0].IndexOf("IMAGE", StringComparison.Ordinal); var image = new Regex(@"\S+").Match(containerInfo[1].Substring(imageStart)).ToString(); var svc = LookupServiceType(image); - var port = GetServicePort(svc); + var port = GetPort(svc); try { new TcpClient(Localhost, port).Dispose(); - return ServiceLifecycle.State.Online; + return Lifecycle.Status.Online; } catch (SocketException) { - return ServiceLifecycle.State.Starting; + return Lifecycle.Status.Starting; } } private string LookupImage(string type, string os) { - var images = _context.Environment.Configuration.ServiceTypeProperties[type]; + var images = _context.Target.Configuration.ServiceTypeProperties[type]; return images.TryGetValue($"image-{os}", out var image) ? image : images["image"]; } private string LookupServiceType(string image) { - var svcTypes = _context.Environment.Configuration.ServiceTypeProperties; + var svcTypes = _context.Target.Configuration.ServiceTypeProperties; foreach (var svcType in svcTypes.Keys) { var images = svcTypes[svcType]; @@ -112,9 +128,9 @@ namespace Steeltoe.Tooling.Docker throw new ToolingException($"Failed to lookup service type for image '{image}'"); } - private int GetServicePort(string name) + private int GetPort(string serviceType) { - return Registry.GetServiceType(name).Port; + return Registry.GetServiceTypeInfo(serviceType).Port; } } } diff --git a/src/Steeltoe.Tooling/Docker/DockerEnvironment.cs b/src/Steeltoe.Tooling/Docker/DockerTarget.cs similarity index 88% rename from src/Steeltoe.Tooling/Docker/DockerEnvironment.cs rename to src/Steeltoe.Tooling/Docker/DockerTarget.cs index 6f2cd7d..e2e87fd 100644 --- a/src/Steeltoe.Tooling/Docker/DockerEnvironment.cs +++ b/src/Steeltoe.Tooling/Docker/DockerTarget.cs @@ -16,15 +16,15 @@ using System.Text.RegularExpressions; namespace Steeltoe.Tooling.Docker { - public class DockerEnvironment : Environment + public class DockerTarget : Target { - public DockerEnvironment(EnvironmentConfiguration config) : base(config) + public DockerTarget(TargetConfiguration configuration) : base(configuration) { } - public override IServiceBackend GetServiceBackend(Context context) + public override IBackend GetBackend(Context context) { - return new DockerServiceBackend(context); + return new DockerBackend(context); } public override bool IsHealthy(Context context) diff --git a/src/Steeltoe.Tooling/Dummy/DummyServiceBackend.cs b/src/Steeltoe.Tooling/Dummy/DummyBackend.cs similarity index 57% rename from src/Steeltoe.Tooling/Dummy/DummyServiceBackend.cs rename to src/Steeltoe.Tooling/Dummy/DummyBackend.cs index 9bd6d58..0d91c6a 100644 --- a/src/Steeltoe.Tooling/Dummy/DummyServiceBackend.cs +++ b/src/Steeltoe.Tooling/Dummy/DummyBackend.cs @@ -14,45 +14,65 @@ namespace Steeltoe.Tooling.Dummy { - public class DummyServiceBackend : IServiceBackend + public class DummyBackend : IBackend { private readonly string _path; private DummyServiceDatabase _database; - public DummyServiceBackend(string path) + public DummyBackend(string path) { _path = path; _database = DummyServiceDatabase.Load(_path); } - public void DeployService(string serviceName, string serviceTypeName) + public void DeployApp(string app) { - _database.Services[serviceName] = ServiceLifecycle.State.Starting; + throw new System.NotImplementedException(); + } + + public void UndeployApp(string app) + { + throw new System.NotImplementedException(); + } + + public Lifecycle.Status GetAppStatus(string app) + { + return GetStatus(app); + } + + public void DeployService(string service) + { + _database.Services[service] = Lifecycle.Status.Starting; Store(); } - public void UndeployService(string name) + public void UndeployService(string service) { - _database.Services[name] = ServiceLifecycle.State.Stopping; + _database.Services[service] = Lifecycle.Status.Stopping; Store(); } - public ServiceLifecycle.State GetServiceLifecleState(string name) + public Lifecycle.Status GetServiceStatus(string service) + { + return GetStatus(service); + } + + private Lifecycle.Status GetStatus(string name) { if (!_database.Services.ContainsKey(name)) { - return ServiceLifecycle.State.Offline; + return Lifecycle.Status.Offline; } var state = _database.Services[name]; switch (state) { - case ServiceLifecycle.State.Starting: - _database.Services[name] = ServiceLifecycle.State.Online; + case Lifecycle.Status.Starting: + _database.Services[name] = Lifecycle.Status.Online; Store(); break; - case ServiceLifecycle.State.Stopping: + case Lifecycle.Status.Stopping: _database.Services.Remove(name); Store(); break; diff --git a/src/Steeltoe.Tooling/Dummy/DummyServiceDatabase.cs b/src/Steeltoe.Tooling/Dummy/DummyServiceDatabase.cs index 7601627..5c38be2 100644 --- a/src/Steeltoe.Tooling/Dummy/DummyServiceDatabase.cs +++ b/src/Steeltoe.Tooling/Dummy/DummyServiceDatabase.cs @@ -21,8 +21,8 @@ namespace Steeltoe.Tooling.Dummy { public class DummyServiceDatabase { - public SortedDictionary Services { get; set; } = - new SortedDictionary(); + public SortedDictionary Services { get; set; } = + new SortedDictionary(); internal static void Store(string path, DummyServiceDatabase database) { diff --git a/src/Steeltoe.Tooling/Dummy/DummyEnvironment.cs b/src/Steeltoe.Tooling/Dummy/DummyTarget.cs similarity index 73% rename from src/Steeltoe.Tooling/Dummy/DummyEnvironment.cs rename to src/Steeltoe.Tooling/Dummy/DummyTarget.cs index 37adae7..2d0b66f 100644 --- a/src/Steeltoe.Tooling/Dummy/DummyEnvironment.cs +++ b/src/Steeltoe.Tooling/Dummy/DummyTarget.cs @@ -16,15 +16,15 @@ using System.IO; namespace Steeltoe.Tooling.Dummy { - public class DummyEnvironment : Environment + public class DummyTarget : Target { - public DummyEnvironment(EnvironmentConfiguration config) : base(config) + public DummyTarget(TargetConfiguration configuration) : base(configuration) { } - public override IServiceBackend GetServiceBackend(Context context) + public override IBackend GetBackend(Context context) { - return new DummyServiceBackend(Path.Combine(context.ProjectDirectory, "dummy-service-backend.db")); + return new DummyBackend(Path.Combine(context.ProjectDirectory, "dummy-service-backend.db")); } public override bool IsHealthy(Context context) diff --git a/src/Steeltoe.Tooling/Executor/DeployServicesExecutor.cs b/src/Steeltoe.Tooling/Executor/AddExecutor.cs similarity index 52% rename from src/Steeltoe.Tooling/Executor/DeployServicesExecutor.cs rename to src/Steeltoe.Tooling/Executor/AddExecutor.cs index ffef2ee..fe5257f 100644 --- a/src/Steeltoe.Tooling/Executor/DeployServicesExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/AddExecutor.cs @@ -14,19 +14,30 @@ namespace Steeltoe.Tooling.Executor { - public class DeployServicesExecutor : ServicesExecutor + [RequiresInitialization] + public class AddExecutor : Executor { - protected override void Execute(Context context, string serviceName) + private readonly string _itemName; + + private readonly string _itemType; + + public AddExecutor(string itemName, string itemType) { - var state = context.ServiceManager.GetServiceState(serviceName); - if (state == ServiceLifecycle.State.Disabled) + _itemName = itemName; + _itemType = itemType; + } + + protected override void Execute() + { + if (_itemType == "app") { - context.Console.WriteLine($"Ignoring disabled service '{serviceName}'"); + Context.Configuration.AddApp(_itemName); + Context.Console.WriteLine($"Added app '{_itemName}'"); } else { - context.Console.WriteLine($"Deploying service '{serviceName}'"); - context.ServiceManager.DeployService(serviceName); + Context.Configuration.AddService(_itemName, _itemType); + Context.Console.WriteLine($"Added {_itemType} service '{_itemName}'"); } } } diff --git a/src/Steeltoe.Tooling/Executor/DeployExecutor.cs b/src/Steeltoe.Tooling/Executor/DeployExecutor.cs new file mode 100644 index 0000000..72edc95 --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/DeployExecutor.cs @@ -0,0 +1,50 @@ +// 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 System.Threading; + +namespace Steeltoe.Tooling.Executor +{ + public class DeployExecutor : GroupExecutor + { + public DeployExecutor() : base(false) + { + } + + protected override void ExecuteForApp(string app) + { + foreach (var service in Context.Configuration.GetServices()) + { + int count = 0; + while (Context.Backend.GetServiceStatus(service) != Lifecycle.Status.Online) + { + if (++count == 10) + { + throw new ToolingException("max check exceeded"); + } + Context.Console.WriteLine($"Waiting for service '{service}' to come online"); + Thread.Sleep(Settings.WaitDuration); + } + } + Context.Console.WriteLine($"Deploying app '{app}'"); + new Lifecycle(Context, app).Deploy(); + } + + protected override void ExecuteForService(string service) + { + Context.Console.WriteLine($"Deploying service '{service}'"); + new Lifecycle(Context, service).Deploy(); + } + } +} diff --git a/src/Steeltoe.Tooling/Executor/DisableServiceExecutor.cs b/src/Steeltoe.Tooling/Executor/DisableServiceExecutor.cs deleted file mode 100644 index 5c72da0..0000000 --- a/src/Steeltoe.Tooling/Executor/DisableServiceExecutor.cs +++ /dev/null @@ -1,30 +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. - -namespace Steeltoe.Tooling.Executor -{ - public class DisableServiceExecutor : ServiceExecutor - { - public DisableServiceExecutor(string serviceName) : base(serviceName) - { - } - - public override void Execute(Context context) - { - base.Execute(context); - context.ServiceManager.DisableService(ServiceName); - context.Console.WriteLine($"Disabled service '{ServiceName}'"); - } - } -} diff --git a/src/Steeltoe.Tooling/Executor/Executor.cs b/src/Steeltoe.Tooling/Executor/Executor.cs new file mode 100644 index 0000000..327873f --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/Executor.cs @@ -0,0 +1,57 @@ +// 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 Microsoft.Extensions.Logging; + +namespace Steeltoe.Tooling.Executor +{ + public abstract class Executor + { + private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); + + protected Context Context { get; private set; } + + public void Execute(Context context) + { + var type = GetType(); + Logger.LogDebug($"executor is {type}"); + foreach (var attr in type.GetCustomAttributes(true)) + { + if (attr is RequiresInitializationAttribute) + { + Logger.LogDebug($"{type} requires configuration"); + if (context.Configuration == null) + { + throw new ToolingException("Steeltoe Developer Tools has not been initialized"); + } + + } + + if (attr is RequiresTargetAttribute) + { + Logger.LogDebug($"{type} requires target"); + if (context.Target == null) + { + throw new ToolingException("Target has not been set"); + } + } + } + + Context = context; + Execute(); + } + + protected abstract void Execute(); + } +} diff --git a/src/Steeltoe.Tooling/Executor/EnableServiceExecutor.cs b/src/Steeltoe.Tooling/Executor/GetArgsExecutor.cs similarity index 60% rename from src/Steeltoe.Tooling/Executor/EnableServiceExecutor.cs rename to src/Steeltoe.Tooling/Executor/GetArgsExecutor.cs index ee1433b..d4ce435 100644 --- a/src/Steeltoe.Tooling/Executor/EnableServiceExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/GetArgsExecutor.cs @@ -14,17 +14,23 @@ namespace Steeltoe.Tooling.Executor { - public class EnableServiceExecutor : ServiceExecutor + public class GetArgsExecutor : ServiceExecutor { - public EnableServiceExecutor(string serviceName) : base(serviceName) + private string _target; + + public GetArgsExecutor(string service, string target) : base(service) { + _target = target; } - public override void Execute(Context context) + protected override void Execute() { - base.Execute(context); - context.ServiceManager.EnableService(ServiceName); - context.Console.WriteLine($"Enabled service '{ServiceName}'"); + base.Execute(); + var args = Context.Configuration.GetServiceDeploymentArgs(Service, _target); + if (args != null) + { + Context.Console.WriteLine(args); + } } } } diff --git a/src/Steeltoe.Tooling/Executor/GetServiceDeploymentArgsExecutor.cs b/src/Steeltoe.Tooling/Executor/GetServiceDeploymentArgsExecutor.cs deleted file mode 100644 index a00b81d..0000000 --- a/src/Steeltoe.Tooling/Executor/GetServiceDeploymentArgsExecutor.cs +++ /dev/null @@ -1,37 +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. - -namespace Steeltoe.Tooling.Executor -{ - public class GetServiceDeploymentArgsExecutor : ServiceExecutor - { - private string _environmentName; - - public GetServiceDeploymentArgsExecutor(string environmentName, string serviceName) : base(serviceName) - { - _environmentName = environmentName; - } - - public override void Execute(Context context) - { - Registry.GetEnvironment(_environmentName); - base.Execute(context); - var args = context.ServiceManager.GetServiceDeploymentArgs(_environmentName, ServiceName); - if (args != null) - { - context.Console.WriteLine(args); - } - } - } -} diff --git a/src/Steeltoe.Tooling/Executor/GetTargetExecutor.cs b/src/Steeltoe.Tooling/Executor/GetTargetExecutor.cs index 4819f0e..bf20ed7 100644 --- a/src/Steeltoe.Tooling/Executor/GetTargetExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/GetTargetExecutor.cs @@ -14,18 +14,12 @@ namespace Steeltoe.Tooling.Executor { - [RequiresInitialization] - public class GetTargetExecutor : IExecutor + [RequiresTarget] + public class GetTargetExecutor : Executor { - public void Execute(Context context) + protected override void Execute() { - if (context.Environment == null) - { - throw new ToolingException("Target deployment environment not set"); - } - - context.Console.WriteLine( - $"Target deployment environment set to '{context.Environment.Name}'."); + Context.Console.WriteLine(Context.Configuration.Target); } } } diff --git a/src/Steeltoe.Tooling/Executor/GroupExecutor.cs b/src/Steeltoe.Tooling/Executor/GroupExecutor.cs new file mode 100644 index 0000000..e788b3c --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/GroupExecutor.cs @@ -0,0 +1,88 @@ +// 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 System; +using System.Threading.Tasks; + +namespace Steeltoe.Tooling.Executor +{ + [RequiresTarget] + public abstract class GroupExecutor : Executor + { + private readonly bool _appsFirst; + + protected GroupExecutor(Boolean appsFirst) + { + _appsFirst = appsFirst; + } + + protected override void Execute() + { + var services = Context.Configuration.GetServices(); + var apps = Context.Configuration.GetApps(); + + if (Settings.ParallelExecutionEnabled) + { + try + { + if (_appsFirst) + { + Parallel.ForEach(apps, app => { ExecuteForApp(app); }); + Parallel.ForEach(services, service => { ExecuteForService(service); }); + } + else + { + Parallel.ForEach(services, service => { ExecuteForService(service); }); + Parallel.ForEach(apps, app => { ExecuteForApp(app); }); + } + } + catch (AggregateException e) + { + throw e.InnerException; + } + } + else + { + if (_appsFirst) + { + foreach (var app in apps) + { + ExecuteForApp(app); + } + + foreach (var service in services) + { + ExecuteForService(service); + } + } + else + { + foreach (var service in services) + { + ExecuteForService(service); + } + + foreach (var app in apps) + { + ExecuteForApp(app); + } + } + } + } + + protected abstract void ExecuteForApp(string app); + + protected abstract void ExecuteForService(string service); + } +} diff --git a/src/Steeltoe.Tooling/Executor/InitializationExecutor.cs b/src/Steeltoe.Tooling/Executor/InitializationExecutor.cs new file mode 100644 index 0000000..788526d --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/InitializationExecutor.cs @@ -0,0 +1,47 @@ +// 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 System.IO; + +namespace Steeltoe.Tooling.Executor +{ + public class InitializationExecutor : Executor + { + private readonly string _path; + + private readonly bool _force; + + public InitializationExecutor(string path = null, bool force = false) + { + _path = path; + _force = force; + } + + protected override void Execute() + { + var path = _path == null + ? Context.ProjectDirectory + : Path.Combine(Context.ProjectDirectory, _path); + + var cfgFile = new ConfigurationFile(path); + if (cfgFile.Exists() && !_force) + { + throw new ToolingException("Steeltoe Developer Tools already initialized"); + } + + cfgFile.Store(); + Context.Console.WriteLine("Initialized Steeltoe Developer Tools"); + } + } +} diff --git a/src/Steeltoe.Tooling/Executor/ListExecutor.cs b/src/Steeltoe.Tooling/Executor/ListExecutor.cs index 436995c..7035f17 100644 --- a/src/Steeltoe.Tooling/Executor/ListExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/ListExecutor.cs @@ -12,32 +12,47 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Linq; + namespace Steeltoe.Tooling.Executor { [RequiresInitialization] - public abstract class ListExecutor : IExecutor + public class ListExecutor : GroupExecutor { - private bool _verbose; + private readonly bool _verbose; - protected ListExecutor(bool verbose) + private string _format; + + public ListExecutor(bool verbose = false) : base(false) { _verbose = verbose; } - public void Execute(Context context) + protected override void Execute() { + var services = Context.Configuration.GetServices(); if (_verbose) { - ExecuteListVerbose(context); + var max = services.Max(n => n.Length); + _format = "{0,-" + max + "} {1,5} {2}"; } else { - ExecuteList(context); + _format = "{0}"; } + base.Execute(); } - protected abstract void ExecuteList(Context context); + protected override void ExecuteForApp(string app) + { + Context.Console.WriteLine(_format, app, "", "app"); + } - protected abstract void ExecuteListVerbose(Context context); + protected override void ExecuteForService(string service) + { + var svcInfo = Context.Configuration.GetServiceInfo(service); + var svcTypeInfo = Registry.GetServiceTypeInfo(svcInfo.ServiceType); + Context.Console.WriteLine(_format, service, svcTypeInfo.Port, svcTypeInfo.Name); + } } } diff --git a/src/Steeltoe.Tooling/Executor/ListServiceTypesExecutor.cs b/src/Steeltoe.Tooling/Executor/ListServiceTypesExecutor.cs index e09a181..15d259e 100644 --- a/src/Steeltoe.Tooling/Executor/ListServiceTypesExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/ListServiceTypesExecutor.cs @@ -22,24 +22,23 @@ namespace Steeltoe.Tooling.Executor { } - protected override void ExecuteList(Context context) + protected void ExecuteList(Context context) { - foreach (var svcTypeName in Registry.ServiceTypeNames) + foreach (var svcType in Registry.GetServiceTypes()) { - context.Console.WriteLine(svcTypeName); - + context.Console.WriteLine(svcType); } } - protected override void ExecuteListVerbose(Context context) + protected void ExecuteListVerbose(Context context) { - var svcTypeNames = Registry.ServiceTypeNames; + var svcTypeNames = Registry.GetServiceTypes(); var max = svcTypeNames.Max(n => n.Length); var format = "{0,-" + max + "} {1,5} {2}"; foreach (var svcTypeName in svcTypeNames) { - var svcType = Registry.GetServiceType(svcTypeName); - context.Console.WriteLine(format, svcType.Name, svcType.Port, svcType.Description); + var svcTypeInfo = Registry.GetServiceTypeInfo(svcTypeName); + context.Console.WriteLine(format, svcTypeInfo.Name, svcTypeInfo.Port, svcTypeInfo.Description); } } } diff --git a/src/Steeltoe.Tooling/Executor/ListServicesExectutor.cs b/src/Steeltoe.Tooling/Executor/ListServicesExectutor.cs deleted file mode 100644 index 46ff6d2..0000000 --- a/src/Steeltoe.Tooling/Executor/ListServicesExectutor.cs +++ /dev/null @@ -1,46 +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 System.Linq; - -namespace Steeltoe.Tooling.Executor -{ - public class ListServicesExecutor : ListExecutor - { - public ListServicesExecutor(bool verbose = false) : base(verbose) - { - } - - protected override void ExecuteList(Context context) - { - foreach (var svcName in context.ServiceManager.GetServiceNames()) - { - context.Console.WriteLine(svcName); - } - } - - protected override void ExecuteListVerbose(Context context) - { - var svcNames = context.ServiceManager.GetServiceNames(); - var max = svcNames.Max(n => n.Length); - var format = "{0,-" + max + "} {1,5} {2}"; - foreach (var svcName in svcNames) - { - var svc = context.ServiceManager.GetService(svcName); - var svcType = Registry.GetServiceType(svc.Type); - context.Console.WriteLine(format, svcName, svcType.Port, svcType.Name); - } - } - } -} diff --git a/src/Steeltoe.Tooling/Executor/ListEnvironmentsExecutor.cs b/src/Steeltoe.Tooling/Executor/ListTargetsExecutor.cs similarity index 58% rename from src/Steeltoe.Tooling/Executor/ListEnvironmentsExecutor.cs rename to src/Steeltoe.Tooling/Executor/ListTargetsExecutor.cs index 285907d..67f74c3 100644 --- a/src/Steeltoe.Tooling/Executor/ListEnvironmentsExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/ListTargetsExecutor.cs @@ -16,29 +16,29 @@ using System.Linq; namespace Steeltoe.Tooling.Executor { - public class ListEnvironmentsExecutor : ListExecutor + public class ListTargetsExecutor : ListExecutor { - public ListEnvironmentsExecutor(bool verbose = false) : base(verbose) + public ListTargetsExecutor(bool verbose = false) : base(verbose) { } - protected override void ExecuteList(Context context) + protected void ExecuteList(Context context) { - foreach (var envName in Registry.EnvironmentNames) + foreach (var envName in Registry.Targets) { context.Console.WriteLine(envName); } } - protected override void ExecuteListVerbose(Context context) + protected void ExecuteListVerbose(Context context) { - var envNames = Registry.EnvironmentNames; - var max = envNames.Max(n => n.Length); + var targets = Registry.Targets; + var max = targets.Max(n => n.Length); var format = "{0,-" + max + "} {1}"; - foreach (var envName in Registry.EnvironmentNames) + foreach (var target in targets) { - var env = Registry.GetEnvironment(envName); - context.Console.WriteLine(format, env.Name, env.Description); + var tgtInfo = Registry.GetTarget(target); + context.Console.WriteLine(format, tgtInfo.Name, tgtInfo.Description); } } } diff --git a/src/Steeltoe.Tooling/Executor/RemoveExecutor.cs b/src/Steeltoe.Tooling/Executor/RemoveExecutor.cs new file mode 100644 index 0000000..b7fc777 --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/RemoveExecutor.cs @@ -0,0 +1,49 @@ +// 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. + +namespace Steeltoe.Tooling.Executor +{ + // TODO : write unit test for init req + [RequiresInitialization] + public class RemoveExecutor : Executor + { + private readonly string _name; + + public RemoveExecutor(string name) + { + _name = name; + } + + protected override void Execute() + { + var cfg = Context.Configuration; + if (cfg.GetApps().Contains(_name)) + { + Context.Configuration.RemoveApp(_name); + Context.Console.WriteLine($"Removed app '{_name}'"); + } + else if (cfg.GetServices().Contains(_name)) + { + var svcInfo = cfg.GetServiceInfo(_name); + Context.Configuration.RemoveService(_name); + Context.Console.WriteLine($"Removed {svcInfo.ServiceType} service '{_name}'"); + + } + else + { + throw new NotFoundException(_name, "app or service"); + } + } + } +} diff --git a/src/Steeltoe.Tooling/IServiceBackend.cs b/src/Steeltoe.Tooling/Executor/RequiresTargetAttribute.cs similarity index 66% rename from src/Steeltoe.Tooling/IServiceBackend.cs rename to src/Steeltoe.Tooling/Executor/RequiresTargetAttribute.cs index a6c6a20..fc4d4e9 100644 --- a/src/Steeltoe.Tooling/IServiceBackend.cs +++ b/src/Steeltoe.Tooling/Executor/RequiresTargetAttribute.cs @@ -1,4 +1,4 @@ -// Copyright 2018 the original author or authors. +// 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. @@ -12,14 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -namespace Steeltoe.Tooling +using System; + +namespace Steeltoe.Tooling.Executor { - public interface IServiceBackend + [AttributeUsage(AttributeTargets.Class)] + public class RequiresTargetAttribute: RequiresInitializationAttribute { - void DeployService(string name, string type); - - void UndeployService(string name); - - ServiceLifecycle.State GetServiceLifecleState(string name); } } diff --git a/src/Steeltoe.Tooling/Executor/ServiceExecutor.cs b/src/Steeltoe.Tooling/Executor/ServiceExecutor.cs index c2628d3..9e4c777 100644 --- a/src/Steeltoe.Tooling/Executor/ServiceExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/ServiceExecutor.cs @@ -15,21 +15,20 @@ namespace Steeltoe.Tooling.Executor { [RequiresInitialization] - public abstract class ServiceExecutor : IExecutor + public abstract class ServiceExecutor : Executor { - protected string ServiceName { get; } + protected string Service { get; } - public ServiceExecutor(string serviceName) + protected ServiceInfo ServiceInfo { get; private set; } + + public ServiceExecutor(string service) { - ServiceName = serviceName; + Service = service; } - public virtual void Execute(Context context) + protected override void Execute() { - if (!context.ServiceManager.HasService(ServiceName)) - { - throw new ServiceNotFoundException(ServiceName); - } + ServiceInfo = Context.Configuration.GetServiceInfo(Service); } } } diff --git a/src/Steeltoe.Tooling/Executor/ServicesExecutor.cs b/src/Steeltoe.Tooling/Executor/ServicesExecutor.cs deleted file mode 100644 index aa551b5..0000000 --- a/src/Steeltoe.Tooling/Executor/ServicesExecutor.cs +++ /dev/null @@ -1,56 +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 System; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace Steeltoe.Tooling.Executor -{ - [RequiresInitialization] - public abstract class ServicesExecutor : IExecutor - { - protected List ServiceNames { get; private set; } - - public virtual void Execute(Context context) - { - ServiceNames = context.ServiceManager.GetServiceNames(); - if (ServiceNames.Count == 0) - { - return; - } - - if (Settings.ParallelExecutionEnabled) - { - try - { - Parallel.ForEach(ServiceNames, svcName => { Execute(context, svcName); }); - } - catch (AggregateException e) - { - throw e.InnerException; - } - } - else - { - foreach (var serviceName in ServiceNames) - { - Execute(context, serviceName); - } - } - } - - protected abstract void Execute(Context context, string serviceName); - } -} diff --git a/src/Steeltoe.Tooling/Executor/SetServiceDeploymentArgsExecutor.cs b/src/Steeltoe.Tooling/Executor/SetArgsExecutor.cs similarity index 50% rename from src/Steeltoe.Tooling/Executor/SetServiceDeploymentArgsExecutor.cs rename to src/Steeltoe.Tooling/Executor/SetArgsExecutor.cs index 5780def..a869886 100644 --- a/src/Steeltoe.Tooling/Executor/SetServiceDeploymentArgsExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/SetArgsExecutor.cs @@ -14,36 +14,34 @@ namespace Steeltoe.Tooling.Executor { - public class SetServiceDeploymentArgsExecutor : ServiceExecutor + public class SetArgsExecutor : ServiceExecutor { - private readonly string _environmentName; + private readonly string _target; private readonly string _arguments; private readonly bool _force; - public SetServiceDeploymentArgsExecutor(string environmentName, string serviceName, string arguments, - bool force = false) : - base(serviceName) + public SetArgsExecutor(string service, string target, string arguments, bool force = false) : + base(service) { - _environmentName = environmentName; + _target = target; _arguments = arguments; _force = force; } - public override void Execute(Context context) + protected override void Execute() { - base.Execute(context); - var args = context.ServiceManager.GetServiceDeploymentArgs(_environmentName, ServiceName); + base.Execute(); + var args = Context.Configuration.GetServiceDeploymentArgs(Service, _target); if (args != null && !_force) { throw new ToolingException( - $"'{_environmentName}' deployment environment arguments for service '{ServiceName}' already set."); + $"Service '{Service}' args for target '{_target}' already set to '{args}'"); } - context.ServiceManager.SetServiceDeploymentArgs(_environmentName, ServiceName, _arguments); - context.Console.WriteLine( - $"Set the '{_environmentName}' deployment environment arguments for service '{ServiceName}' to '{_arguments}'"); + Context.Configuration.SetServiceDeploymentArgs(Service, _target, _arguments); + Context.Console.WriteLine($"Service '{Service}' args for target '{_target}' set to '{_arguments}'"); } } } diff --git a/src/Steeltoe.Tooling/Executor/SetTargetExecutor.cs b/src/Steeltoe.Tooling/Executor/SetTargetExecutor.cs index 52064bc..f41ef9c 100644 --- a/src/Steeltoe.Tooling/Executor/SetTargetExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/SetTargetExecutor.cs @@ -15,36 +15,36 @@ namespace Steeltoe.Tooling.Executor { [RequiresInitialization] - public class SetTargetExecutor : IExecutor + public class SetTargetExecutor : Executor { - private readonly string _environmentName; + private readonly string _target; private readonly bool _force; - public SetTargetExecutor(string environmentName, bool force = false) + public SetTargetExecutor(string target, bool force = false) { - _environmentName = environmentName; + _target = target; _force = force; } - public void Execute(Context context) + protected override void Execute() { - Environment env = Registry.GetEnvironment(_environmentName); + Target tgt = Registry.GetTarget(_target); - if (!env.IsHealthy(context)) + if (!tgt.IsHealthy(Context)) { if (!_force) { - context.Console.WriteLine("Fix errors above or re-run with '-F|--force'"); - throw new ToolingException($"Environment '{_environmentName }' does not appear healthy"); + Context.Console.WriteLine("Fix errors above or re-run with '-F|--force'"); + throw new ToolingException($"Target '{_target}' does not appear healthy"); } - context.Console.WriteLine("Ignoring poor health report above :-("); + Context.Console.WriteLine("Ignoring poor health report above :-("); } - context.ToolingConfiguration.EnvironmentName = _environmentName; - context.ToolingConfiguration.NotifyChanged(); - context.Console.WriteLine($"Target deployment environment set to '{_environmentName}'."); + Context.Configuration.Target = _target; + Context.Configuration.NotifyChanged(); + Context.Console.WriteLine($"Target set to '{_target}'"); } } } diff --git a/src/Steeltoe.Tooling/Executor/StatusApplicationsExecutor.cs b/src/Steeltoe.Tooling/Executor/StatusApplicationsExecutor.cs new file mode 100644 index 0000000..e06b520 --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/StatusApplicationsExecutor.cs @@ -0,0 +1,24 @@ +// 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. + +namespace Steeltoe.Tooling.Executor +{ + public class StatusApplicationsExecutor : Executor + { + protected override void Execute() + { + throw new System.NotImplementedException(); + } + } +} diff --git a/src/Steeltoe.Tooling/Executor/AddServiceExecutor.cs b/src/Steeltoe.Tooling/Executor/StatusExecutor.cs similarity index 57% rename from src/Steeltoe.Tooling/Executor/AddServiceExecutor.cs rename to src/Steeltoe.Tooling/Executor/StatusExecutor.cs index a5e816f..5ef9140 100644 --- a/src/Steeltoe.Tooling/Executor/AddServiceExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/StatusExecutor.cs @@ -14,24 +14,22 @@ namespace Steeltoe.Tooling.Executor { - [RequiresInitialization] - public class AddServiceExecutor : IExecutor + public class StatusExecutor : GroupExecutor { - private string _name; - - private string _type; - - public AddServiceExecutor(string serviceName, string serviceType) + public StatusExecutor() : base(false) { - _name = serviceName; - _type = serviceType; } - public void Execute(Context context) + protected override void ExecuteForApp(string app) { - context.ServiceManager.AddService(_name, _type); - context.ServiceManager.EnableService(_name); - context.Console.WriteLine($"Added {_type} service '{_name}'"); + var status = Context.Backend.GetAppStatus(app); + Context.Console.WriteLine($"{app} {status.ToString().ToLower()}"); + } + + protected override void ExecuteForService(string service) + { + var status = Context.Backend.GetServiceStatus(service); + Context.Console.WriteLine($"{service} {status.ToString().ToLower()}"); } } } diff --git a/src/Steeltoe.Tooling/Executor/UndeployApplicationsExecutor.cs b/src/Steeltoe.Tooling/Executor/UndeployApplicationsExecutor.cs new file mode 100644 index 0000000..8d38a53 --- /dev/null +++ b/src/Steeltoe.Tooling/Executor/UndeployApplicationsExecutor.cs @@ -0,0 +1,25 @@ +// 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. + +namespace Steeltoe.Tooling.Executor +{ + public class UndeployApplicationsExecutor : Executor + { + protected override void Execute() + { +// context.Console.WriteLine($"Undeploying project '{serviceName}'"); + throw new System.NotImplementedException(); + } + } +} diff --git a/src/Steeltoe.Tooling/Executor/RemoveServiceExecutor.cs b/src/Steeltoe.Tooling/Executor/UndeployExecutor.cs similarity index 58% rename from src/Steeltoe.Tooling/Executor/RemoveServiceExecutor.cs rename to src/Steeltoe.Tooling/Executor/UndeployExecutor.cs index 8dda5fb..ff063fd 100644 --- a/src/Steeltoe.Tooling/Executor/RemoveServiceExecutor.cs +++ b/src/Steeltoe.Tooling/Executor/UndeployExecutor.cs @@ -14,17 +14,22 @@ namespace Steeltoe.Tooling.Executor { - public class RemoveServiceExecutor : ServiceExecutor + public class UndeployExecutor : GroupExecutor { - public RemoveServiceExecutor(string serviceName) : base(serviceName) + public UndeployExecutor() : base(true) { } - public override void Execute(Context context) + protected override void ExecuteForApp(string app) { - base.Execute(context); - context.ServiceManager.RemoveService(ServiceName); - context.Console.WriteLine($"Removed service '{ServiceName}'"); + Context.Console.WriteLine($"Undeploying app '{app}'"); + new Lifecycle(Context, app).Undeploy(); + } + + protected override void ExecuteForService(string service) + { + Context.Console.WriteLine($"Undeploying service '{service}'"); + new Lifecycle(Context, service).Undeploy(); } } } diff --git a/src/Steeltoe.Tooling/Executor/UndeployServicesExecutor.cs b/src/Steeltoe.Tooling/Executor/UndeployServicesExecutor.cs deleted file mode 100644 index 6c86d37..0000000 --- a/src/Steeltoe.Tooling/Executor/UndeployServicesExecutor.cs +++ /dev/null @@ -1,33 +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. - -namespace Steeltoe.Tooling.Executor -{ - public class UndeployServicesExecutor : ServicesExecutor - { - protected override void Execute(Context context, string serviceName) - { - var state = context.ServiceManager.GetServiceState(serviceName); - if (state == ServiceLifecycle.State.Disabled) - { - context.Console.WriteLine($"Ignoring disabled service '{serviceName}'"); - } - else - { - context.Console.WriteLine($"Undeploying service '{serviceName}'"); - context.ServiceManager.UndeployService(serviceName); - } - } - } -} diff --git a/src/Steeltoe.Tooling/Executor/StatusServicesExecutor.cs b/src/Steeltoe.Tooling/IBackend.cs similarity index 63% rename from src/Steeltoe.Tooling/Executor/StatusServicesExecutor.cs rename to src/Steeltoe.Tooling/IBackend.cs index 4ece357..2af3be5 100644 --- a/src/Steeltoe.Tooling/Executor/StatusServicesExecutor.cs +++ b/src/Steeltoe.Tooling/IBackend.cs @@ -12,14 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -namespace Steeltoe.Tooling.Executor +namespace Steeltoe.Tooling { - public class StatusServicesExecutor : ServicesExecutor + public interface IBackend { - protected override void Execute(Context context, string serviceName) - { - var status = context.ServiceManager.GetServiceState(serviceName).ToString().ToLower(); - context.Console.WriteLine($"{serviceName} {status}"); - } + void DeployApp(string application); + + void UndeployApp(string application); + + Lifecycle.Status GetAppStatus(string application); + + void DeployService(string service); + + void UndeployService(string service); + + Lifecycle.Status GetServiceStatus(string service); } } diff --git a/src/Steeltoe.Tooling/Executor/IExecutor.cs b/src/Steeltoe.Tooling/IConfigurationListener.cs similarity index 77% rename from src/Steeltoe.Tooling/Executor/IExecutor.cs rename to src/Steeltoe.Tooling/IConfigurationListener.cs index 2cfc076..a5383fd 100644 --- a/src/Steeltoe.Tooling/Executor/IExecutor.cs +++ b/src/Steeltoe.Tooling/IConfigurationListener.cs @@ -1,4 +1,4 @@ -// Copyright 2018 the original author or authors. +// 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. @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -namespace Steeltoe.Tooling.Executor +namespace Steeltoe.Tooling { - public interface IExecutor + public interface IConfigurationListener { - void Execute(Context context); + void ConfigurationChangeEvent(); } } diff --git a/src/Steeltoe.Tooling/IToolingConfigurationListener.cs b/src/Steeltoe.Tooling/IToolingConfigurationListener.cs deleted file mode 100644 index 9f14175..0000000 --- a/src/Steeltoe.Tooling/IToolingConfigurationListener.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Steeltoe.Tooling -{ - public interface IToolingConfigurationListener - { - void ConfigurationChangeEvent(); - } -} diff --git a/src/Steeltoe.Tooling/Lifecycle.cs b/src/Steeltoe.Tooling/Lifecycle.cs new file mode 100644 index 0000000..88e820c --- /dev/null +++ b/src/Steeltoe.Tooling/Lifecycle.cs @@ -0,0 +1,122 @@ +// 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. + +namespace Steeltoe.Tooling +{ + public class Lifecycle + { + public enum Status + { + Offline, + Starting, + Online, + Stopping, + Unknown, + } + + private readonly IBackend _backend; + + private readonly string _name; + + private readonly BackendBridge _bridge; + + public Lifecycle(Context context, string name) + { + _backend = context.Backend; + _name = name; + _bridge = new ServiceBackendBridge(); + } + + public Status GetStatus() + { + return _bridge.GetStatus(_name, _backend); + } + + public void Undeploy() + { + GetState().Undeploy(_name, _backend); + } + + public void Deploy() + { + GetState().Deploy(_name, _backend); + } + + private State GetState() + { + var status = GetStatus(); + switch (status) + { + case Status.Offline: + return new OfflineState(); + case Status.Starting: + return new StartingState(); + case Status.Online: + return new OnlineState(); + case Status.Stopping: + return new StoppingState(); + default: + throw new ToolingException($"Unhandled lifecycle status: {status}"); + } + } + + abstract class State + { + internal virtual void Deploy(string name, IBackend backend) + { + } + + internal virtual void Undeploy(string name, IBackend backend) + { + } + } + + class OfflineState : State + { + internal override void Deploy(string name, IBackend backend) + { + backend.DeployService(name); + } + } + + class StartingState : State + { + } + + class OnlineState : State + { + internal override void Undeploy(string name, IBackend backend) + { + backend.UndeployService(name); + } + } + + class StoppingState : State + { + } + + abstract class BackendBridge + { + internal abstract Status GetStatus(string name, IBackend backend); + } + + class ServiceBackendBridge : BackendBridge + { + internal override Status GetStatus(string name, IBackend backend) + { + return backend.GetServiceStatus(name); + } + } + } +} diff --git a/src/Steeltoe.Tooling/ServiceLifecycleException.cs b/src/Steeltoe.Tooling/LifecycleException.cs similarity index 69% rename from src/Steeltoe.Tooling/ServiceLifecycleException.cs rename to src/Steeltoe.Tooling/LifecycleException.cs index c9d3786..36625a2 100644 --- a/src/Steeltoe.Tooling/ServiceLifecycleException.cs +++ b/src/Steeltoe.Tooling/LifecycleException.cs @@ -14,14 +14,14 @@ namespace Steeltoe.Tooling { - public class ServiceLifecycleException : ToolingException + public class LifecycleException : ToolingException { - public ServiceLifecycle.State State { get; } + public Lifecycle.Status Status { get; } - public ServiceLifecycleException(ServiceLifecycle.State state) : base( - $"Invalid service status '{state.ToString().ToLower()}'") + public LifecycleException(Lifecycle.Status status) : base( + $"Invalid status '{status.ToString().ToLower()}'") { - State = state; + Status = status; } } } diff --git a/src/Steeltoe.Tooling/NotFoundException.cs b/src/Steeltoe.Tooling/NotFoundException.cs new file mode 100644 index 0000000..ced9f55 --- /dev/null +++ b/src/Steeltoe.Tooling/NotFoundException.cs @@ -0,0 +1,30 @@ +// 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. + +namespace Steeltoe.Tooling +{ + public class NotFoundException : ToolingException + { + public string Name { get; } + + public string Description { get; } + + + public NotFoundException(string itemName, string description) : base($"Unknown {description} '{itemName}'") + { + Name = itemName; + Description = description; + } + } +} diff --git a/src/Steeltoe.Tooling/Registry.cs b/src/Steeltoe.Tooling/Registry.cs index 48c723c..6d2b7a6 100644 --- a/src/Steeltoe.Tooling/Registry.cs +++ b/src/Steeltoe.Tooling/Registry.cs @@ -21,6 +21,7 @@ using Steeltoe.Tooling.CloudFoundry; using Steeltoe.Tooling.Docker; using Steeltoe.Tooling.Dummy; using YamlDotNet.Serialization; +// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global namespace Steeltoe.Tooling { @@ -28,7 +29,51 @@ namespace Steeltoe.Tooling { private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); - private static RegistryConfiguration _configuration = new RegistryConfiguration(); + public class Configuration + { + [YamlMember(Alias = "serviceTypes")] + public SortedDictionary ServiceTypes { get; set; } = + new SortedDictionary(); + + [YamlMember(Alias = "targets")] + public SortedDictionary TargetConfigurations { get; set; } = + new SortedDictionary(); + + public void DefineServiceType(string name, int port, string description) + { + ServiceTypes[name] = new ServiceTypeInfo {Port = port, Description = description}; + } + + public void DefineTarget(string target, string description) + { + TargetConfigurations[target] = new TargetConfiguration {Description = description}; + } + + public void DefineTargetServiceTypeProperty( + string target, + string serviceType, + string propertyName, + string propertyValue) + { + if (!TargetConfigurations.ContainsKey(target)) + { + TargetConfigurations[target] = new TargetConfiguration(); + } + + var targetCfg = TargetConfigurations[target]; + if (!targetCfg.ServiceTypeProperties.ContainsKey(serviceType)) + { + targetCfg.ServiceTypeProperties[serviceType] = new Dictionary(); + } + + targetCfg.ServiceTypeProperties[serviceType][propertyName] = propertyValue; + TargetConfigurations[target] = targetCfg; + } + } + + private static Configuration _configuration = new Configuration(); + + public static List Targets => _configuration.TargetConfigurations.Keys.ToList(); static Registry() { @@ -36,12 +81,12 @@ namespace Steeltoe.Tooling if (Settings.DummiesEnabled) { Logger.LogDebug("loading dummy registry"); - var dummyCfg = new RegistryConfiguration(); - var dummySvcType = new ServiceType {Port = 0, Description = "A Dummy Service"}; + var dummyCfg = new Configuration(); + var dummySvcType = new ServiceTypeInfo {Port = 0, Description = "A Dummy Service"}; dummyCfg.ServiceTypes.Add("dummy-svc", dummySvcType); - var dummyEnvCfg = new EnvironmentConfiguration(); - dummyEnvCfg.Description = "A Dummy Environment"; - dummyCfg.EnvironmentConfigurations.Add("dummy-env", dummyEnvCfg); + var dummyTarget = new TargetConfiguration(); + dummyTarget.Description = "A Dummy Target"; + dummyCfg.TargetConfigurations.Add("dummy-target", dummyTarget); AddRegistryConfiguration(dummyCfg); } @@ -52,48 +97,49 @@ namespace Steeltoe.Tooling var deserializer = new DeserializerBuilder().Build(); using (var reader = new StreamReader(path)) { - var defaultCfg = deserializer.Deserialize(reader); + var defaultCfg = deserializer.Deserialize(reader); AddRegistryConfiguration(defaultCfg); } } - public static List ServiceTypeNames => _configuration.ServiceTypes.Keys.ToList(); - - public static ServiceType GetServiceType(string serviceTypeName) + public static List GetServiceTypes() { - ServiceType svcType; - if (_configuration.ServiceTypes.TryGetValue(serviceTypeName, out svcType)) + return _configuration.ServiceTypes.Keys.ToList(); + } + + public static ServiceTypeInfo GetServiceTypeInfo(string serviceType) + { + ServiceTypeInfo svcTypeInfo; + if (_configuration.ServiceTypes.TryGetValue(serviceType, out svcTypeInfo)) { - svcType.Name = serviceTypeName; - return svcType; + svcTypeInfo.Name = serviceType; + return svcTypeInfo; } return null; } - public static List EnvironmentNames => _configuration.EnvironmentConfigurations.Keys.ToList(); - - public static Environment GetEnvironment(string environmentName) + public static Target GetTarget(string target) { - EnvironmentConfiguration envCfg; - if (_configuration.EnvironmentConfigurations.TryGetValue(environmentName, out envCfg)) + TargetConfiguration targetCfg; + if (_configuration.TargetConfigurations.TryGetValue(target, out targetCfg)) { - envCfg.Name = environmentName; - switch (envCfg.Name) + targetCfg.Name = target; + switch (targetCfg.Name) { case "cloud-foundry": - return new CloudFoundryEnvironment(envCfg); + return new CloudFoundryTarget(targetCfg); case "docker": - return new DockerEnvironment(envCfg); - case "dummy-env": - return new DummyEnvironment(envCfg); + return new DockerTarget(targetCfg); + case "dummy-target": + return new DummyTarget(targetCfg); } } - throw new ToolingException($"Unknown deployment environment '{environmentName}'"); + throw new ToolingException($"Unknown target '{target}'"); } - private static void AddRegistryConfiguration(RegistryConfiguration configuration) + private static void AddRegistryConfiguration(Configuration configuration) { foreach (var svcTypeEntry in configuration.ServiceTypes) { @@ -101,10 +147,10 @@ namespace Steeltoe.Tooling _configuration.ServiceTypes[svcTypeEntry.Key] = svcTypeEntry.Value; } - foreach (var envCfgEntry in configuration.EnvironmentConfigurations) + foreach (var targetCfgEntry in configuration.TargetConfigurations) { - envCfgEntry.Value.Name = null; - _configuration.EnvironmentConfigurations[envCfgEntry.Key] = envCfgEntry.Value; + targetCfgEntry.Value.Name = null; + _configuration.TargetConfigurations[targetCfgEntry.Key] = targetCfgEntry.Value; } } } diff --git a/src/Steeltoe.Tooling/RegistryConfiguration.cs b/src/Steeltoe.Tooling/RegistryConfiguration.cs deleted file mode 100644 index 83bde9e..0000000 --- a/src/Steeltoe.Tooling/RegistryConfiguration.cs +++ /dev/null @@ -1,62 +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 System.Collections.Generic; -using YamlDotNet.Serialization; - -namespace Steeltoe.Tooling -{ - public class RegistryConfiguration - { - [YamlMember(Alias = "serviceTypes")] - public SortedDictionary ServiceTypes { get; set; } = - new SortedDictionary(); - - [YamlMember(Alias = "environments")] - public SortedDictionary EnvironmentConfigurations { get; set; } = - new SortedDictionary(); - - public void DefineServiceType(string name, int port, string description) - { - ServiceTypes[name] = new ServiceType {Port = port, Description = description}; - } - - public void DefineEnvironment(string environmentName, string description) - { - var envCfg = new EnvironmentConfiguration {Description = description}; - EnvironmentConfigurations[environmentName] = envCfg; - } - - public void DefineEnvironmentServiceTypeProperty( - string environmentName, - string serviceTypeName, - string propertyName, - string propertyValue) - { - if (!EnvironmentConfigurations.ContainsKey(environmentName)) - { - EnvironmentConfigurations[environmentName] = new EnvironmentConfiguration(); - } - - var envCfg = EnvironmentConfigurations[environmentName]; - if (!envCfg.ServiceTypeProperties.ContainsKey(serviceTypeName)) - { - envCfg.ServiceTypeProperties[serviceTypeName] = new Dictionary(); - } - - envCfg.ServiceTypeProperties[serviceTypeName][propertyName] = propertyValue; - EnvironmentConfigurations[environmentName] = envCfg; - } - } -} diff --git a/src/Steeltoe.Tooling/ServiceNotFoundException.cs b/src/Steeltoe.Tooling/ServiceInfo.cs similarity index 73% rename from src/Steeltoe.Tooling/ServiceNotFoundException.cs rename to src/Steeltoe.Tooling/ServiceInfo.cs index d11f6c4..70b9263 100644 --- a/src/Steeltoe.Tooling/ServiceNotFoundException.cs +++ b/src/Steeltoe.Tooling/ServiceInfo.cs @@ -14,13 +14,16 @@ namespace Steeltoe.Tooling { - public class ServiceNotFoundException : ToolingException + public class ServiceInfo { - public string Name { get; } + public string Service { get; } - public ServiceNotFoundException(string name) : base($"Service '{name}' not found") + public string ServiceType { get; } + + public ServiceInfo(string service, string serviceType) { - Name = name; + Service = service; + ServiceType = serviceType; } } } diff --git a/src/Steeltoe.Tooling/ServiceLifecycle.cs b/src/Steeltoe.Tooling/ServiceLifecycle.cs deleted file mode 100644 index 943d56a..0000000 --- a/src/Steeltoe.Tooling/ServiceLifecycle.cs +++ /dev/null @@ -1,194 +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. - -namespace Steeltoe.Tooling -{ - public class ServiceLifecycle - { - public enum State - { - Disabled, - Offline, - Starting, - Online, - Stopping, - Unknown, - } - - public Context Context { get; } - - public string Name { get; } - - public ServiceLifecycle(Context context, string name) - { - Context = context; - Name = name; - } - - public void Enable() - { - GetStateManager().Enable(); - } - - public void Disable() - { - GetStateManager().Disable(); - } - - public void Deploy() - { - GetStateManager().Deploy(); - } - - public void Undeploy() - { - GetStateManager().Undeploy(); - } - - private StateManager GetStateManager() - { - var state = Context.ServiceManager.GetServiceState(Name); - switch (state) - { - case State.Disabled: - return new ServiceDisabled(Context, Name, state); - case State.Offline: - return new ServiceOffline(Context, Name, state); - case State.Starting: - return new ServiceStarting(Context, Name, state); - case State.Online: - return new ServiceOnline(Context, Name, state); - case State.Stopping: - return new ServiceStopping(Context, Name, state); - } - - throw new ToolingException($"Unhandled service state '{state.ToString().ToLower()}'"); - } - - private class StateManager - { - internal Context Context { get; } - - internal string Name { get; } - - internal State State { get; } - - internal StateManager(Context context, string name, State state) - { - Context = context; - Name = name; - State = state; - } - - internal virtual void Enable() - { - throw new ServiceLifecycleException(State); - } - - internal virtual void Disable() - { - throw new ServiceLifecycleException(State); - } - - internal virtual void Deploy() - { - throw new ServiceLifecycleException(State); - } - - internal virtual void Undeploy() - { - throw new ServiceLifecycleException(State); - } - } - - private class ServiceDisabled : StateManager - { - internal ServiceDisabled(Context context, string name, State state) : base(context, name, state) - { - } - - internal override void Enable() - { - Context.ToolingConfiguration.Services[Name].Enabled = true; - Context.ToolingConfiguration.NotifyChanged(); - } - - internal override void Disable() - { - } - } - - private class ServiceOffline : StateManager - { - internal ServiceOffline(Context context, string name, State state) : base(context, name, state) - { - } - - internal override void Enable() - { - } - - internal override void Disable() - { - Context.ToolingConfiguration.Services[Name].Enabled = false; - Context.ToolingConfiguration.NotifyChanged(); - } - - internal override void Deploy() - { - Context.ServiceManager.GetServiceBackend() - .DeployService(Name, Context.ToolingConfiguration.Services[Name].ServiceTypeName); - } - - internal override void Undeploy() - { - } - } - - private class ServiceOnline : StateManager - { - internal ServiceOnline(Context context, string name, State state) : base(context, name, state) - { - } - - internal override void Deploy() - { - } - - internal override void Undeploy() - { - Context.ServiceManager.GetServiceBackend().UndeployService(Name); - } - } - - private class ServiceStarting : StateManager - { - internal ServiceStarting(Context context, string name, State state) : base(context, name, state) - { - } - - internal override void Undeploy() - { - Context.ServiceManager.GetServiceBackend().UndeployService(Name); - } - } - - private class ServiceStopping : StateManager - { - internal ServiceStopping(Context context, string name, State state) : base(context, name, state) - { - } - } - } -} diff --git a/src/Steeltoe.Tooling/ServiceManager.cs b/src/Steeltoe.Tooling/ServiceManager.cs deleted file mode 100644 index 6ea118d..0000000 --- a/src/Steeltoe.Tooling/ServiceManager.cs +++ /dev/null @@ -1,132 +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 System.Collections.Generic; -using System.Linq; - -namespace Steeltoe.Tooling -{ - public class ServiceManager - { - protected internal readonly Context Context; - - public ServiceManager(Context context) - { - Context = context; - } - - public IServiceBackend GetServiceBackend() - { - return Context.Environment.GetServiceBackend(Context); - } - - public Service AddService(string name, string type) - { - if (!Registry.ServiceTypeNames.Contains(type)) - { - throw new ToolingException($"Unknown service type '{type}'"); - } - - if (Context.ToolingConfiguration.Services.ContainsKey(name)) - { - throw new ToolingException($"Service '{name}' already exists"); - } - - Context.ToolingConfiguration.Services[name] = new ToolingConfiguration.Service {ServiceTypeName = type}; - Context.ToolingConfiguration.NotifyChanged(); - return GetService(name); - } - - public void RemoveService(string name) - { - if (!Context.ToolingConfiguration.Services.Remove(name)) - { - throw new ServiceNotFoundException(name); - } - - Context.ToolingConfiguration.NotifyChanged(); - } - - public Service GetService(string name) - { - try - { - var svcCfg = Context.ToolingConfiguration.Services[name]; - return new Service(name, svcCfg.ServiceTypeName); - } - catch (KeyNotFoundException) - { - throw new ServiceNotFoundException(name); - } - } - - public List GetServiceNames() - { - return Context.ToolingConfiguration.Services.Keys.ToList(); - } - - public bool HasService(string name) - { - return Context.ToolingConfiguration.Services.ContainsKey(name); - } - - public void SetServiceDeploymentArgs(string environmentName, string serviceName, string arguments) - { - Context.ToolingConfiguration.Services[serviceName].Args[environmentName] = arguments; - Context.ToolingConfiguration.NotifyChanged(); - } - - public string GetServiceDeploymentArgs(string environmentName, string serviceName) - { - try - { - return Context.ToolingConfiguration.Services[serviceName].Args[environmentName]; - } - catch (KeyNotFoundException) - { - return null; - } - } - - public ServiceLifecycle.State GetServiceState(string name) - { - if (!Context.ToolingConfiguration.Services[name].Enabled) - { - return ServiceLifecycle.State.Disabled; - } - - return GetServiceBackend().GetServiceLifecleState(name); - } - - public void EnableService(string name) - { - new ServiceLifecycle(Context, name).Enable(); - } - - public void DisableService(string name) - { - new ServiceLifecycle(Context, name).Disable(); - } - - public void DeployService(string name) - { - new ServiceLifecycle(Context, name).Deploy(); - } - - public void UndeployService(string name) - { - new ServiceLifecycle(Context, name).Undeploy(); - } - } -} diff --git a/src/Steeltoe.Tooling/ServiceType.cs b/src/Steeltoe.Tooling/ServiceTypeInfo.cs similarity index 93% rename from src/Steeltoe.Tooling/ServiceType.cs rename to src/Steeltoe.Tooling/ServiceTypeInfo.cs index 149e818..9627ce2 100644 --- a/src/Steeltoe.Tooling/ServiceType.cs +++ b/src/Steeltoe.Tooling/ServiceTypeInfo.cs @@ -16,10 +16,10 @@ using YamlDotNet.Serialization; namespace Steeltoe.Tooling { - public class ServiceType + public class ServiceTypeInfo { [YamlMember(Alias = "name")] - public string Name { get; set; } + public string Name { get; set; } [YamlMember(Alias = "port")] public int Port { get; set; } diff --git a/src/Steeltoe.Tooling/Settings.cs b/src/Steeltoe.Tooling/Settings.cs index 8687996..05af68a 100644 --- a/src/Steeltoe.Tooling/Settings.cs +++ b/src/Steeltoe.Tooling/Settings.cs @@ -24,6 +24,8 @@ namespace Steeltoe.Tooling public static bool ParallelExecutionEnabled { get; set; } = true; + public static int WaitDuration = 1000; + static Settings() { DummiesEnabled = File.Exists(".steeltoe.dummies"); diff --git a/src/Steeltoe.Tooling/Environment.cs b/src/Steeltoe.Tooling/Target.cs similarity index 72% rename from src/Steeltoe.Tooling/Environment.cs rename to src/Steeltoe.Tooling/Target.cs index 36666f2..6a20630 100644 --- a/src/Steeltoe.Tooling/Environment.cs +++ b/src/Steeltoe.Tooling/Target.cs @@ -14,26 +14,26 @@ namespace Steeltoe.Tooling { - public abstract class Environment + public abstract class Target { - public EnvironmentConfiguration Configuration { get; } + public TargetConfiguration Configuration { get; } public string Name => Configuration.Name; public string Description => Configuration.Description; - protected Environment(EnvironmentConfiguration config) + protected Target(TargetConfiguration configuration) { - Configuration = config; + Configuration = configuration; } - public abstract IServiceBackend GetServiceBackend(Context context); + public abstract IBackend GetBackend(Context context); public abstract bool IsHealthy(Context context); public override string ToString() { - return $"Environment[name={Name},desc=\"{Description}\"]"; + return $"Target[name={Name},desc=\"{Description}\"]"; } } } diff --git a/src/Steeltoe.Tooling/EnvironmentConfiguration.cs b/src/Steeltoe.Tooling/TargetConfiguration.cs similarity index 96% rename from src/Steeltoe.Tooling/EnvironmentConfiguration.cs rename to src/Steeltoe.Tooling/TargetConfiguration.cs index 67a55bb..c693694 100644 --- a/src/Steeltoe.Tooling/EnvironmentConfiguration.cs +++ b/src/Steeltoe.Tooling/TargetConfiguration.cs @@ -17,7 +17,7 @@ using YamlDotNet.Serialization; namespace Steeltoe.Tooling { - public class EnvironmentConfiguration + public class TargetConfiguration { [YamlMember(Alias = "name")] public string Name { get; set; } diff --git a/src/Steeltoe.Tooling/ToolingConfiguration.cs b/src/Steeltoe.Tooling/ToolingConfiguration.cs deleted file mode 100644 index 5267184..0000000 --- a/src/Steeltoe.Tooling/ToolingConfiguration.cs +++ /dev/null @@ -1,61 +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 System.Collections.Generic; -using Microsoft.Extensions.Logging; -using YamlDotNet.Serialization; - -namespace Steeltoe.Tooling -{ - public class ToolingConfiguration - { - private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); - - [YamlMember(Alias = "environment")] - public string EnvironmentName { get; set; } - - [YamlMember(Alias = "services")] - public SortedDictionary Services { get; protected set; } = - new SortedDictionary(); - - public class Service - { - [YamlMember(Alias = "type")] - public string ServiceTypeName { get; set; } - - [YamlMember(Alias = "enabled")] - public bool Enabled { get; set; } - - [YamlMember(Alias = "args")] - public SortedDictionary Args { get; protected set; } = - new SortedDictionary(); - } - - private readonly List _listeners = new List(); - - public void AddListener(IToolingConfigurationListener listener) - { - _listeners.Add(listener); - } - - public void NotifyChanged() - { - Logger.LogDebug("configuration changed"); - foreach (var listener in _listeners) - { - listener.ConfigurationChangeEvent(); - } - } - } -} diff --git a/src/Steeltoe.Tooling/steeltoe.rc/registry.yml b/src/Steeltoe.Tooling/steeltoe.rc/registry.yml index 4ac834e..8d376c0 100644 --- a/src/Steeltoe.Tooling/steeltoe.rc/registry.yml +++ b/src/Steeltoe.Tooling/steeltoe.rc/registry.yml @@ -32,22 +32,25 @@ serviceTypes: description: Zipkin Tracing Collector and UI port: 9411 -environments: +targets: cloud-foundry: description: Cloud Foundry serviceTypeProperties: - circuit-breaker-dashboard: - service: p-circuit-breaker-dashboard - plan: standard + dummy-svc: + service: dummy-server + plan: dummy-plan config-server: service: p-config-server plan: standard - redis-server: - service: p-redis - plan: shared-vm - service-registry: + eureka-server: service: p-service-registry plan: standard + hystrix-dashboard: + service: p-circuit-breaker-dashboard + plan: standard + redis: + service: p-redis + plan: shared-vm docker: description: Docker serviceTypeProperties: diff --git a/test/Steeltoe.Cli.Test/AddFeature.cs b/test/Steeltoe.Cli.Test/AddFeature.cs index 2e45982..aa846ae 100644 --- a/test/Steeltoe.Cli.Test/AddFeature.cs +++ b/test/Steeltoe.Cli.Test/AddFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("add")] public class AddFeature : FeatureSpecs { [Scenario] - [Label("help")] public void AddHelp() { Runner.RunScenario( @@ -30,11 +27,11 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("add --help"), then => the_cli_should_output(new[] { - "Add a service.", + "Add an app or service", $"Usage: {Program.Name} add [arguments] [options]", "Arguments:", - "type Service type", - "service Service name", + "type 'app' or service type", + "name App or service name", "Options:", "-?|-h|--help Show help information", }) @@ -45,15 +42,15 @@ namespace Steeltoe.Cli.Test public void AddNotEnoughArgs() { Runner.RunScenario( - given => a_dotnet_project("add_not_enough_args0"), + given => a_dotnet_project("add_not_enough"), when => the_developer_runs_cli_command("add"), - then => the_cli_should_error(ErrorCode.Argument, "Service type not specified") + then => the_cli_should_error(ErrorCode.Argument, "'app' or service type not specified") ); Console.Clear(); Runner.RunScenario( given => a_dotnet_project("add_not_enough_args1"), when => the_developer_runs_cli_command("add arg1"), - then => the_cli_should_error(ErrorCode.Argument, "Service name not specified") + then => the_cli_should_error(ErrorCode.Argument, "App or service name not specified") ); } @@ -68,23 +65,44 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void AddUnknownServiceType() + public void AddUninitialized() { Runner.RunScenario( - given => a_steeltoe_project("add_unknown_service_type"), - when => the_developer_runs_cli_command("add no-such-type foo"), - then => the_cli_should_error(ErrorCode.Tooling, "Unknown service type 'no-such-type'") + given => a_dotnet_project("add_uninitialized"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") ); } [Scenario] - public void AddUninitializedProject() + public void AddUnknownServiceType() { Runner.RunScenario( - given => a_dotnet_project("add_uninitialized_project"), - when => the_developer_runs_cli_command("add dummy-svc my-service"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + given => a_steeltoe_project("add_unknown_type"), + when => the_developer_runs_cli_command("add no-such-service-type foo"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown type 'no-such-service-type'") + ); + } + + [Scenario] + public void AddApp() + { + Runner.RunScenario( + given => a_steeltoe_project("add_app"), + when => the_developer_runs_cli_command("add app my-app"), + then => the_cli_should_output("Added app 'my-app'"), + and => the_configuration_should_contain_app("my-app") + ); + } + + [Scenario] + public void AddExistingApp() + { + Runner.RunScenario( + given => a_steeltoe_project("add_existing_app"), + when => the_developer_runs_cli_command("add app existing-app"), + and => the_developer_runs_cli_command("add app existing-app"), + then => the_cli_should_error(ErrorCode.Tooling, "App 'existing-app' already exists") ); } @@ -95,16 +113,15 @@ namespace Steeltoe.Cli.Test given => a_steeltoe_project("add_service"), when => the_developer_runs_cli_command("add dummy-svc my-service"), then => the_cli_should_output("Added dummy-svc service 'my-service'"), - and => the_configuration_should_contain_service("my-service"), - and => the_configuration_service_should_be_enabled("my-service") + and => the_configuration_should_contain_service("my-service", "dummy-svc") ); } [Scenario] - public void AddPreExistingService() + public void AddExistingService() { Runner.RunScenario( - given => a_steeltoe_project("add_pre_existing_service"), + given => a_steeltoe_project("add_existing_service"), when => the_developer_runs_cli_command("add dummy-svc existing-service"), and => the_developer_runs_cli_command("add dummy-svc existing-service"), then => the_cli_should_error(ErrorCode.Tooling, "Service 'existing-service' already exists") diff --git a/test/Steeltoe.Cli.Test/ArgsFeature.cs b/test/Steeltoe.Cli.Test/ArgsFeature.cs index 9a29b8f..33c78ca 100644 --- a/test/Steeltoe.Cli.Test/ArgsFeature.cs +++ b/test/Steeltoe.Cli.Test/ArgsFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("args")] public class ArgsFeature : FeatureSpecs { [Scenario] - [Label("help")] public void ArgsHelp() { Runner.RunScenario( @@ -30,16 +27,16 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("args --help"), then => the_cli_should_output(new[] { - "Set or get the deployment environment arguments for a service.", + "Set or get the deployment arguments for an app or service", $"Usage: {Program.Name} args [arguments] [options] [[--] ...]", "Arguments:", - "environment Deployment environment", - "service Service name", - "args Deployment environment arguments", + "name App or service name", + "target Deployment target name", + "args Deployment arguments", "Options:", "-F|--force Overwrite existing deployment environment arguments", "-?|-h|--help Show help information", - "If run with no deployment environment arguments, show the service's current deployment environment arguments.", + "If run with no arguments, show the current deployment arguments for the app or service.", }) ); } @@ -48,109 +45,136 @@ namespace Steeltoe.Cli.Test public void ArgsNotEnoughArgs() { Runner.RunScenario( - given => a_dotnet_project("args_not_enough_args0"), + given => a_dotnet_project("args_not_enough"), when => the_developer_runs_cli_command("args"), - then => the_cli_should_error(ErrorCode.Argument, "Deployment environment not specified") + then => the_cli_should_error(ErrorCode.Argument, "App or service name not specified") ); Console.Clear(); Runner.RunScenario( given => a_dotnet_project("args_not_enough_args1"), when => the_developer_runs_cli_command("args arg1"), - then => the_cli_should_error(ErrorCode.Argument, "Service name not specified") + then => the_cli_should_error(ErrorCode.Argument, "Deployment target name not specified") ); } [Scenario] - public void ArgsUninitializedProject() + public void ArgsGetUninitialized() { Runner.RunScenario( - given => a_dotnet_project("args_uninitialized_project"), - when => the_developer_runs_cli_command("args dummy-env a-service"), + given => a_dotnet_project("args_get_uninitialized"), + when => the_developer_runs_cli_command("args dummy-env my-service"), + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") + ); + } + + [Scenario] + public void ArgsSetUninitialized() + { + Runner.RunScenario( + given => a_dotnet_project("args_set_uninitialized"), + when => the_developer_runs_cli_command("args dummy-env my-service arg1"), + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") + ); + } + + [Scenario] + public void ArgsGetUnknownTarget() + { + Runner.RunScenario( + given => a_steeltoe_project("args_get_unknown_target"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + and => the_developer_runs_cli_command("args my-service no-such-target"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown target 'no-such-target'") + ); + } + + [Scenario] + public void ArgsSetUnknownTarget() + { + Runner.RunScenario( + given => a_steeltoe_project("args_set_unknown_target"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + and => the_developer_runs_cli_command("args my-service no-such-target arg1"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown target 'no-such-target'") + ); + } + + [Scenario] + public void ArgsGetUnknownService() + { + Runner.RunScenario( + given => a_steeltoe_project("args_get_unknown_service"), + when => the_developer_runs_cli_command("args no-such-svc dummy-target"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown service 'no-such-svc'") + ); + } + + [Scenario] + public void ArgsSetUnknownService() + { + Runner.RunScenario( + given => a_steeltoe_project("args_set_unknown_service"), + when => the_developer_runs_cli_command("args no-such-svc dummy-target arg1"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown service 'no-such-svc'") + ); + } + + [Scenario] + public void ArgsSet() + { + Runner.RunScenario( + given => a_steeltoe_project("args_set"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + and => the_developer_runs_cli_command("args my-service dummy-target arg1 arg2"), + then => the_cli_should_output("Service 'my-service' args for target 'dummy-target' set to 'arg1 arg2'") + ); + } + + [Scenario] + public void ArgsSetWithOpt() + { + Runner.RunScenario( + given => a_steeltoe_project("args_set_with_opt"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + and => the_developer_runs_cli_command("args my-service dummy-target -- arg1 -opt2"), + then => the_cli_should_output("Service 'my-service' args for target 'dummy-target' set to 'arg1 -opt2'") + ); + } + + [Scenario] + public void ArgsSetForce() + { + Runner.RunScenario( + given => a_steeltoe_project("args_set_force"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + and => the_developer_runs_cli_command("args my-service dummy-target arg1 arg2"), + and => the_developer_runs_cli_command("args my-service dummy-target arg1 arg2"), then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + "Service 'my-service' args for target 'dummy-target' already set to 'arg1 arg2'"), + when => the_developer_runs_cli_command("args my-service dummy-target arg3 --force"), + then => the_cli_should_output("Service 'my-service' args for target 'dummy-target' set to 'arg3'") ); } [Scenario] - public void ArgsUnknownEnvironment() + public void ArgsGet() { Runner.RunScenario( - given => a_steeltoe_project("args_unknown_environment"), - when => the_developer_runs_cli_command("args no-such-env a-service"), - then => the_cli_should_error(ErrorCode.Tooling, "Unknown deployment environment 'no-such-env'") - ); - } - - [Scenario] - public void ArgsNoSuchService() - { - Runner.RunScenario( - given => a_steeltoe_project("args_nonexistent_service"), - when => the_developer_runs_cli_command("args dummy-env no-such-svc"), - then => the_cli_should_error(ErrorCode.Tooling, "Service 'no-such-svc' not found") - ); - Console.Clear(); - Runner.RunScenario( - given => a_steeltoe_project("args_nonexistent_service"), - when => the_developer_runs_cli_command("args dummy-env no-such-svc arg1"), - then => the_cli_should_error(ErrorCode.Tooling, "Service 'no-such-svc' not found") - ); - } - - [Scenario] - public void ArgsSetDeploymentEnvironmentArgs() - { - Runner.RunScenario( - given => a_steeltoe_project("args_set_deployment_environment_args"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("args dummy-env a-service arg1 arg2"), - then => the_cli_should_output( - "Set the 'dummy-env' deployment environment arguments for service 'a-service' to 'arg1 arg2'") - ); - Runner.RunScenario( - given => a_steeltoe_project("args_set_deployment_environment_args_with_opt"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("args dummy-env a-service -- arg1 -opt2"), - then => the_cli_should_output( - "Set the 'dummy-env' deployment environment arguments for service 'a-service' to 'arg1 -opt2'") - ); - } - - [Scenario] - public void ArgsSetDeploymentEnvironmentArgsForce() - { - Runner.RunScenario( - given => a_steeltoe_project("args_set_deployment_environment_args_force"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("args dummy-env a-service arg1 arg2"), - and => the_developer_runs_cli_command("args dummy-env a-service arg1 arg2"), - then => the_cli_should_error(ErrorCode.Tooling, - "'dummy-env' deployment environment arguments for service 'a-service' already set."), - when => the_developer_runs_cli_command("args dummy-env a-service arg1 arg2 --force"), - then => the_cli_should_output( - "Set the 'dummy-env' deployment environment arguments for service 'a-service' to 'arg1 arg2'") - ); - } - - [Scenario] - public void ArgsGetDeploymentEnvironmentArgs() - { - Runner.RunScenario( - given => a_steeltoe_project("args_get_deployment_environment_args"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("args dummy-env a-service arg1 arg2"), - and => the_developer_runs_cli_command("args dummy-env a-service"), + given => a_steeltoe_project("args_get"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + and => the_developer_runs_cli_command("args my-service dummy-target arg1 arg2"), + and => the_developer_runs_cli_command("args my-service dummy-target"), then => the_cli_should_output("arg1 arg2") ); } [Scenario] - public void ArgsGetDeploymentEnvironmentNoArgs() + public void ArgsGetNoArgs() { Runner.RunScenario( - given => a_steeltoe_project("args_get_deployment_environment_no_args"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - when => the_developer_runs_cli_command("args dummy-env a-service"), + given => a_steeltoe_project("args_get_no_args"), + when => the_developer_runs_cli_command("add dummy-svc my-service"), + when => the_developer_runs_cli_command("args my-service dummy-target"), then => the_cli_should_output_nothing() ); } diff --git a/test/Steeltoe.Cli.Test/DeployFeature.cs b/test/Steeltoe.Cli.Test/DeployFeature.cs index 35e369f..1b9c570 100644 --- a/test/Steeltoe.Cli.Test/DeployFeature.cs +++ b/test/Steeltoe.Cli.Test/DeployFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("deploy")] public class DeployFeature : FeatureSpecs { [Scenario] - [Label("help")] public void DeployHelp() { Runner.RunScenario( @@ -30,7 +27,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("deploy --help"), then => the_cli_should_output(new[] { - "Deploy enabled services to the targeted deployment environment.", + "Deploy apps and services to the target", $"Usage: {Program.Name} deploy [options]", "Options:", "-?|-h|--help Show help information", @@ -49,44 +46,40 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void DeployUninitializedProject() + public void DeployUninitialized() { Runner.RunScenario( - given => a_dotnet_project("deploy_uninitialized_project"), + given => a_dotnet_project("deploy_uninitialized"), when => the_developer_runs_cli_command("deploy"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") ); } [Scenario] - public void DeployServices() + public void Deploy() { Runner.RunScenario( given => a_steeltoe_project("deploy_services"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("add dummy-svc defunct-service"), - and => the_developer_runs_cli_command("disable defunct-service"), + when => the_developer_runs_cli_command("add app my-app"), + and => the_developer_runs_cli_command("add dummy-svc my-service-a"), + and => the_developer_runs_cli_command("add dummy-svc my-service-b"), and => the_developer_runs_cli_command("-S deploy"), then => the_cli_should_output(new[] { - "Deploying service 'a-service'", - "Ignoring disabled service 'defunct-service'", - }), - when => the_developer_runs_cli_command("-S status"), - then => the_cli_should_output(new[] - { - "a-service starting", - "defunct-service disabled", + "Deploying service 'my-service-a'", + "Deploying service 'my-service-b'", + "Waiting for service 'my-service-a' to come online", + "Waiting for service 'my-service-b' to come online", + "Deploying app 'my-app'", }) ); } [Scenario] - public void DeployNoServices() + public void DeployNothing() { Runner.RunScenario( - given => a_steeltoe_project("deploy_no_services"), + given => a_steeltoe_project("deploy_nothing"), when => the_developer_runs_cli_command("deploy"), then => the_cli_should_output_nothing() ); @@ -100,7 +93,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("init"), and => the_developer_runs_cli_command("add dummy-svc a-server"), and => the_developer_runs_cli_command("deploy"), - then => the_cli_should_error(ErrorCode.Tooling, "Target deployment environment not set") + then => the_cli_should_error(ErrorCode.Tooling, "Target not set") ); } } diff --git a/test/Steeltoe.Cli.Test/DisableFeature.cs b/test/Steeltoe.Cli.Test/DisableFeature.cs deleted file mode 100644 index 072be53..0000000 --- a/test/Steeltoe.Cli.Test/DisableFeature.cs +++ /dev/null @@ -1,96 +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; - -namespace Steeltoe.Cli.Test -{ - [Label("disable")] - public class DisableFeature : FeatureSpecs - { - [Scenario] - [Label("help")] - public void DisableHelp() - { - Runner.RunScenario( - given => a_dotnet_project("disable_help"), - when => the_developer_runs_cli_command("disable --help"), - then => the_cli_should_output(new[] - { - "Disable a service.", - $"Usage: {Program.Name} disable [arguments] [options]", - "Arguments:", - "service Service name", - "Options:", - "-?|-h|--help Show help information", - }) - ); - } - - [Scenario] - public void DisableNotEnoughArgs() - { - Runner.RunScenario( - given => a_dotnet_project("disable_not_enough_args"), - when => the_developer_runs_cli_command("disable"), - then => the_cli_should_error(ErrorCode.Argument, "Service name not specified") - ); - } - - [Scenario] - public void DisableTooManyArgs() - { - Runner.RunScenario( - given => a_dotnet_project("disable_too_many_args"), - when => the_developer_runs_cli_command("disable arg1 arg2"), - then => the_cli_should_fail_parse("Unrecognized command or argument 'arg2'") - ); - } - - [Scenario] - public void DisableUninitializedProject() - { - Runner.RunScenario( - given => a_dotnet_project("disable_uninitialized_project"), - when => the_developer_runs_cli_command("disable my-service"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") - ); - } - - [Scenario] - public void DisableService() - { - Runner.RunScenario( - given => a_steeltoe_project("disable_service"), - when => the_developer_runs_cli_command("add dummy-svc my-service"), - and => the_developer_runs_cli_command("disable my-service"), - then => the_cli_should_output("Disabled service 'my-service'"), - and => the_configuration_service_should_not_be_enabled("my-service") - ); - } - - [Scenario] - public void DisableNonExistingService() - { - Runner.RunScenario( - given => a_steeltoe_project("disable_non_existing_service"), - when => the_developer_runs_cli_command("disable unknown-service"), - then => the_cli_should_error(ErrorCode.Tooling, "Service 'unknown-service' not found") - ); - } - } -} diff --git a/test/Steeltoe.Cli.Test/DoctorFeature.cs b/test/Steeltoe.Cli.Test/DoctorFeature.cs index 48f895c..d099c0e 100644 --- a/test/Steeltoe.Cli.Test/DoctorFeature.cs +++ b/test/Steeltoe.Cli.Test/DoctorFeature.cs @@ -12,25 +12,22 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("doctor")] public class DoctorFeature : FeatureSpecs { [Scenario] - [Label("help")] public void DoctorHelp() { Runner.RunScenario( - given => a_dotnet_project("dotnet_help"), + given => a_dotnet_project("doctor_help"), when => the_developer_runs_cli_command("doctor --help"), then => the_cli_should_output(new[] { - "Check for potential problems.", + "Check for potential problems", $"Usage: {Program.Name} doctor [options]", "Options:", "-?|-h|--help Show help information", @@ -48,6 +45,27 @@ namespace Steeltoe.Cli.Test ); } + [Scenario] + public void DoctorUninitialized() + { + Runner.RunScenario( + given => a_dotnet_project("doctor_uninitialized"), + when => the_developer_runs_cli_command("doctor"), + then => the_cli_output_should_include( + $"initialized ... !!! no (run '{Program.Name} init' to initialize)") + ); + } + + [Scenario] + public void DoctorInitialized() + { + Runner.RunScenario( + given => a_steeltoe_project("doctor_initialized"), + when => the_developer_runs_cli_command("doctor"), + then => the_cli_output_should_include("initialized ... yes") + ); + } + [Scenario] public void DoctorVersion() { @@ -68,40 +86,25 @@ namespace Steeltoe.Cli.Test ); } - [Scenario] - public void DoctorUninitializedProject() - { - Runner.RunScenario( - given => a_dotnet_project("doctor_uninitialized_project"), - when => the_developer_runs_cli_command("doctor"), - then => the_cli_output_should_include( - $"initialized ... !!! no (run '{Program.Name} init' to initialize)") - ); - } - - [Scenario] - public void DoctorInitializedProject() - { - Runner.RunScenario( - given => a_steeltoe_project("doctor_initialized_project"), - when => the_developer_runs_cli_command("doctor"), - then => the_cli_output_should_include("initialized ... yes") - ); - } - [Scenario] public void DoctorTarget() { Runner.RunScenario( - given => a_dotnet_project("doctor_target"), + given => a_steeltoe_project("doctor_target"), + when => the_developer_runs_cli_command("doctor"), + then => the_cli_output_should_include("target ... dummy-target") + ); + } + + [Scenario] + public void DoctorNoTarget() + { + Runner.RunScenario( + given => a_dotnet_project("doctor_no_target"), when => the_developer_runs_cli_command("init"), and => the_developer_runs_cli_command("doctor"), then => the_cli_output_should_include( - $"target deployment environment ... !!! not set (run '{Program.Name} target ' to set)"), - when => the_developer_runs_cli_command("target dummy-env"), - and => the_developer_runs_cli_command("doctor"), - then => the_cli_output_should_include("target deployment environment ... dummy-env"), - and => the_cli_output_should_include("dummy tool version ... 0.0.0") + $"target ... !!! not set (run '{Program.Name} target ' to set)") ); } } diff --git a/test/Steeltoe.Cli.Test/EnableFeature.cs b/test/Steeltoe.Cli.Test/EnableFeature.cs deleted file mode 100644 index 70e24de..0000000 --- a/test/Steeltoe.Cli.Test/EnableFeature.cs +++ /dev/null @@ -1,97 +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; - -namespace Steeltoe.Cli.Test -{ - [Label("enable")] - public class EnableFeature : FeatureSpecs - { - [Scenario] - [Label("help")] - public void EnableHelp() - { - Runner.RunScenario( - given => a_dotnet_project("enable_help"), - when => the_developer_runs_cli_command("enable --help"), - then => the_cli_should_output(new[] - { - "Enable a service.", - $"Usage: {Program.Name} enable [arguments] [options]", - "Arguments:", - "service Service name", - "Options:", - "-?|-h|--help Show help information", - }) - ); - } - - [Scenario] - public void EnableNotEnoughArgs() - { - Runner.RunScenario( - given => a_dotnet_project("enable_not_enough_args"), - when => the_developer_runs_cli_command("enable"), - then => the_cli_should_error(ErrorCode.Argument, "Service name not specified") - ); - } - - [Scenario] - public void EnableTooManyArgs() - { - Runner.RunScenario( - given => a_dotnet_project("enable_too_many_args"), - when => the_developer_runs_cli_command("enable arg1 arg2"), - then => the_cli_should_fail_parse("Unrecognized command or argument 'arg2'") - ); - } - - [Scenario] - public void EnableUninitializedProject() - { - Runner.RunScenario( - given => a_dotnet_project("enable_uninitialized_project"), - when => the_developer_runs_cli_command("enable my-service"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") - ); - } - - [Scenario] - public void EnableService() - { - Runner.RunScenario( - given => a_steeltoe_project("enable_service"), - when => the_developer_runs_cli_command("add dummy-svc my-service"), - and => the_developer_runs_cli_command("disable my-service"), - and => the_developer_runs_cli_command("enable my-service"), - then => the_cli_should_output("Enabled service 'my-service'"), - and => the_configuration_service_should_be_enabled("my-service") - ); - } - - [Scenario] - public void EnableNonExistingService() - { - Runner.RunScenario( - given => a_steeltoe_project("enable_non_existing_service"), - when => the_developer_runs_cli_command("enable unknown-service"), - then => the_cli_should_error(ErrorCode.Tooling, "Service 'unknown-service' not found") - ); - } - } -} diff --git a/test/Steeltoe.Cli.Test/FeatureSpecs.cs b/test/Steeltoe.Cli.Test/FeatureSpecs.cs index 69c609f..5a6ae8c 100644 --- a/test/Steeltoe.Cli.Test/FeatureSpecs.cs +++ b/test/Steeltoe.Cli.Test/FeatureSpecs.cs @@ -90,8 +90,8 @@ namespace scratch { a_dotnet_project(name); Logger.LogInformation($"enabling steeltoe developer tools"); - var cfgFile = new ToolingConfigurationFile(ProjectDirectory); - cfgFile.ToolingConfiguration.EnvironmentName = "dummy-env"; + var cfgFile = new ConfigurationFile(ProjectDirectory); + cfgFile.Configuration.Target = "dummy-target"; cfgFile.Store(); } @@ -212,33 +212,34 @@ namespace scratch protected void the_configuration_should_target(string env) { Logger.LogInformation($"checking the target config '{env}' exists"); - new ToolingConfigurationFile(ProjectDirectory).ToolingConfiguration.EnvironmentName.ShouldBe(env); + new ConfigurationFile(ProjectDirectory).Configuration.Target.ShouldBe(env); } - protected void the_configuration_should_contain_service(string service) + protected void the_configuration_should_contain_app(string app) { - Logger.LogInformation($"checking the service '{service}' exists"); - new ToolingConfigurationFile(ProjectDirectory).ToolingConfiguration.Services.Keys.ShouldContain(service); + Logger.LogInformation($"checking the app '{app}' exists"); + var cfg = new ConfigurationFile(ProjectDirectory); + cfg.Configuration.Apps.Keys.ShouldContain(app); + } + + protected void the_configuration_should_not_contain_app(string app) + { + Logger.LogInformation($"checking the app '{app}' does not exist"); + new ConfigurationFile(ProjectDirectory).Configuration.Apps.Keys.ShouldNotContain(app); + } + + protected void the_configuration_should_contain_service(string service, string serviceType) + { + Logger.LogInformation($"checking the service {serviceType} '{service}' exists"); + var cfg = new ConfigurationFile(ProjectDirectory); + cfg.Configuration.Services.Keys.ShouldContain(service); + cfg.Configuration.Services[service].ServiceTypeName.ShouldBe(serviceType); } protected void the_configuration_should_not_contain_service(string service) { Logger.LogInformation($"checking the service '{service}' does not exist"); - new ToolingConfigurationFile(ProjectDirectory).ToolingConfiguration.Services.Keys.ShouldNotContain(service); - } - - protected void the_configuration_service_should_be_enabled(string service) - { - Logger.LogInformation($"checking the service '{service}' is enabled"); - new ToolingConfigurationFile(ProjectDirectory).ToolingConfiguration.Services[service].Enabled - .ShouldBeTrue(); - } - - protected void the_configuration_service_should_not_be_enabled(string service) - { - Logger.LogInformation($"checking the service '{service}' is not enabled"); - new ToolingConfigurationFile(ProjectDirectory).ToolingConfiguration.Services[service].Enabled - .ShouldBeFalse(); + new ConfigurationFile(ProjectDirectory).Configuration.Services.Keys.ShouldNotContain(service); } private static string NormalizeString(string s) diff --git a/test/Steeltoe.Cli.Test/InitFeature.cs b/test/Steeltoe.Cli.Test/InitFeature.cs index b9c2c2e..491e941 100644 --- a/test/Steeltoe.Cli.Test/InitFeature.cs +++ b/test/Steeltoe.Cli.Test/InitFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("init")] public class InitFeature : FeatureSpecs { [Scenario] - [Label("help")] public void InitHelp() { Runner.RunScenario( @@ -30,7 +27,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("init --help"), then => the_cli_should_output(new[] { - "Initialize a project for Steeltoe Developer Tools.", + "Initialize Steeltoe Developer Tools", $"Usage: {Program.Name} init [options]", "Options:", "-F|--force Initialize the project even if already initialized", @@ -50,24 +47,24 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void InitProject() + public void Init() { Runner.RunScenario( - given => a_dotnet_project("init_project"), + given => a_dotnet_project("init"), when => the_developer_runs_cli_command("init"), - then => the_cli_should_output("Project initialized for Steeltoe Developer Tools") + then => the_cli_should_output("Initialized Steeltoe Developer Tools") ); } [Scenario] - public void InitProjectForce() + public void InitForce() { Runner.RunScenario( - given => a_steeltoe_project("init_project_already_initialized"), + given => a_steeltoe_project("init_force"), when => the_developer_runs_cli_command("init"), - then => the_cli_should_error(ErrorCode.Tooling, "Project already initialized"), + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools already initialized"), when => the_developer_runs_cli_command("init --force"), - then => the_cli_should_output("Project initialized for Steeltoe Developer Tools") + then => the_cli_should_output("Initialized Steeltoe Developer Tools") ); } } diff --git a/test/Steeltoe.Cli.Test/ListFeature.cs b/test/Steeltoe.Cli.Test/ListFeature.cs index 395b810..da885ed 100644 --- a/test/Steeltoe.Cli.Test/ListFeature.cs +++ b/test/Steeltoe.Cli.Test/ListFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("list")] public class ListFeature : FeatureSpecs { [Scenario] - [Label("help")] public void ListHelp() { Runner.RunScenario( @@ -30,15 +27,11 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("list --help"), then => the_cli_should_output(new[] { - "List services, service types, or deployment environments.", + "List apps and services", $"Usage: {Program.Name} list [options]", "Options:", - "-e|--environments List deployment environments", - "-s|--services List services", - "-t|--service-types List service types", "-v|--verbose Verbose", "-?|-h|--help Show help information", - "If run with no options, list services.", }) ); } @@ -54,27 +47,16 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void ListMutallyExclusiveOptions() + public void ListUninitialized() { Runner.RunScenario( - given => a_steeltoe_project("list_mutually_exclusive_options"), - when => the_developer_runs_cli_command("list -e -t"), - then => the_cli_should_error(ErrorCode.Argument, - "Specify at most one of: -e|--environments, -s|--services, -t|--service-types") - ); - } - - [Scenario] - public void ListUninitializedProject() - { - Runner.RunScenario( - given => a_dotnet_project("list_uninitialized_project"), + given => a_dotnet_project("list_uninitialized"), when => the_developer_runs_cli_command("list"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") ); } + /* [Scenario] public void ListEnvironments() { @@ -85,11 +67,13 @@ namespace Steeltoe.Cli.Test { "cloud-foundry", "docker", - "dummy-env", + "dummy-target", }) ); } + */ + /* [Scenario] public void ListEnvironmentsVerbose() { @@ -100,11 +84,13 @@ namespace Steeltoe.Cli.Test { "cloud-foundry Cloud Foundry", "docker Docker", - "dummy-env A Dummy Environment", + "dummy-target A Dummy Target", }) ); } + */ + /* [Scenario] public void ListServiceTypes() { @@ -123,7 +109,9 @@ namespace Steeltoe.Cli.Test }) ); } + */ + /* [Scenario] public void ListServiceTypesVerbose() { @@ -142,21 +130,22 @@ namespace Steeltoe.Cli.Test }) ); } + */ [Scenario] public void ListServices() { Runner.RunScenario( given => a_steeltoe_project("list_services"), - when => the_developer_runs_cli_command("add dummy-svc c-service"), - and => the_developer_runs_cli_command("add dummy-svc b-service"), - and => the_developer_runs_cli_command("add dummy-svc a-service"), + when => the_developer_runs_cli_command("add dummy-svc my-service-c"), + and => the_developer_runs_cli_command("add dummy-svc my-service-b"), + and => the_developer_runs_cli_command("add dummy-svc my-service-a"), and => the_developer_runs_cli_command("list"), then => the_cli_should_output(new[] { - "a-service", - "b-service", - "c-service", + "my-service-a", + "my-service-b", + "my-service-c", }) ); } @@ -166,15 +155,15 @@ namespace Steeltoe.Cli.Test { Runner.RunScenario( given => a_steeltoe_project("list_services_verbose"), - and => the_developer_runs_cli_command("add dummy-svc b-service"), - and => the_developer_runs_cli_command("add dummy-svc c-service"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("list -s -v"), + when => the_developer_runs_cli_command("add dummy-svc my-service-c"), + and => the_developer_runs_cli_command("add dummy-svc my-service-b"), + and => the_developer_runs_cli_command("add dummy-svc my-service-a"), + and => the_developer_runs_cli_command("-S list -v"), then => the_cli_should_output(new[] { - "a-service 0 dummy-svc", - "b-service 0 dummy-svc", - "c-service 0 dummy-svc", + "my-service-a 0 dummy-svc", + "my-service-b 0 dummy-svc", + "my-service-c 0 dummy-svc", }) ); } diff --git a/test/Steeltoe.Cli.Test/ProgramFeature.cs b/test/Steeltoe.Cli.Test/ProgramFeature.cs index 94f83c2..b33170e 100644 --- a/test/Steeltoe.Cli.Test/ProgramFeature.cs +++ b/test/Steeltoe.Cli.Test/ProgramFeature.cs @@ -12,14 +12,12 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("program")] public class ProgramFeature : FeatureSpecs { // [Scenario] @@ -33,7 +31,6 @@ namespace Steeltoe.Cli.Test // } [Scenario] - [Label("help")] public void ProgramHelp() { Runner.RunScenario( @@ -43,33 +40,30 @@ namespace Steeltoe.Cli.Test { "1.0.0", "Steeltoe Developer Tools", - $"Usage: {Program.Name} [options] [command]", + $"Usage: st [options] [command]", "Options:", "-V|--version Show version information", - $"-C|--config-file Configure tooling using the specified file instead of {ToolingConfigurationFile.DefaultFileName}", + $"-C|--config-file Configure tooling using the specified file instead of steeltoe.yml", "-D|--debug Enable debug output", "-S|--no-parallel Disable parallel execution", "-?|-h|--help Show help information", "Commands:", - "add Add a service.", - "args Set or get the deployment environment arguments for a service.", - "deploy Deploy enabled services to the targeted deployment environment.", - "disable Disable a service.", - "doctor Check for potential problems.", - "enable Enable a service.", - "init Initialize a project for Steeltoe Developer Tools.", - "list List services, service types, or deployment environments.", - "remove Remove a service.", - "status Show service statuses.", - "target Set or get the targeted deployment environment.", - "undeploy Undeploy enabled services from the targeted deployment environment.", - $"Run '{Program.Name} [command] --help' for more information about a command.", + "add Add an app or service", + "args Set or get the deployment arguments for an app or service", + "deploy Deploy apps and services to the target", + "doctor Check for potential problems", + "init Initialize Steeltoe Developer Tools", + "list List apps and services", + "remove Remove an app or service", + "status Show app and service statuses", + "target Set or get the deployment target", + "undeploy Undeploy apps and services from the target", + $"Run 'st [command] --help' for more information about a command.", }) ); } [Scenario] - [Label("version")] public void ProgramVersion() { Runner.RunScenario( @@ -80,7 +74,6 @@ namespace Steeltoe.Cli.Test } [Scenario] - [Label("version")] public void ProgramDebug() { Runner.RunScenario( @@ -91,11 +84,10 @@ namespace Steeltoe.Cli.Test } [Scenario] - [Label("version")] public void ProgramNoParallel() { Runner.RunScenario( - given => a_dotnet_project("program_parallel"), + given => a_dotnet_project("program_noparallel"), when => the_developer_runs_cli_command("--no-parallel --version"), then => setting_should_be(Settings.ParallelExecutionEnabled, false) ); diff --git a/test/Steeltoe.Cli.Test/RemoveFeature.cs b/test/Steeltoe.Cli.Test/RemoveFeature.cs index 1245408..90de0de 100644 --- a/test/Steeltoe.Cli.Test/RemoveFeature.cs +++ b/test/Steeltoe.Cli.Test/RemoveFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("remove")] public class RemoveFeature : FeatureSpecs { [Scenario] - [Label("help")] public void RemoveHelp() { Runner.RunScenario( @@ -30,10 +27,10 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("remove --help"), then => the_cli_should_output(new[] { - "Remove a service.", + "Remove an app or service", $"Usage: {Program.Name} remove [arguments] [options]", "Arguments:", - "service Service name", + "name App or service name", "Options:", "-?|-h|--help Show help information", }) @@ -46,7 +43,7 @@ namespace Steeltoe.Cli.Test Runner.RunScenario( given => a_dotnet_project("remove_not_enough_args"), when => the_developer_runs_cli_command("remove"), - then => the_cli_should_error(ErrorCode.Argument, "Service name not specified") + then => the_cli_should_error(ErrorCode.Argument, "App or service name not specified") ); } @@ -61,13 +58,24 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void RemoveUninitializedProject() + public void RemoveUninitialized() { Runner.RunScenario( - given => a_dotnet_project("remove_uninitialized_project"), + given => a_dotnet_project("remove_uninitialized"), when => the_developer_runs_cli_command("remove my-service"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") + ); + } + + [Scenario] + public void RemoveApp() + { + Runner.RunScenario( + given => a_steeltoe_project("remove_app"), + when => the_developer_runs_cli_command("add app my-app"), + and => the_developer_runs_cli_command("remove my-app"), + then => the_cli_should_output("Removed app 'my-app'"), + and => the_configuration_should_not_contain_app("my-app") ); } @@ -78,18 +86,18 @@ namespace Steeltoe.Cli.Test given => a_steeltoe_project("remove_service"), when => the_developer_runs_cli_command("add dummy-svc my-service"), and => the_developer_runs_cli_command("remove my-service"), - then => the_cli_should_output("Removed service 'my-service'"), + then => the_cli_should_output("Removed dummy-svc service 'my-service'"), and => the_configuration_should_not_contain_service("my-service") ); } [Scenario] - public void RemoveNonExistingService() + public void RemoveUnknownItem() { Runner.RunScenario( - given => a_steeltoe_project("remove_non_existing_service"), - when => the_developer_runs_cli_command("remove unknown-service"), - then => the_cli_should_error(ErrorCode.Tooling, "Service 'unknown-service' not found") + given => a_steeltoe_project("remove_unknown_item"), + when => the_developer_runs_cli_command("remove no-such-item"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown app or service 'no-such-item'") ); } } diff --git a/test/Steeltoe.Cli.Test/StatusFeature.cs b/test/Steeltoe.Cli.Test/StatusFeature.cs index f92639c..f0cf20c 100644 --- a/test/Steeltoe.Cli.Test/StatusFeature.cs +++ b/test/Steeltoe.Cli.Test/StatusFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("status")] public class StatusFeature : FeatureSpecs { [Scenario] - [Label("help")] public void StatusHelp() { Runner.RunScenario( @@ -30,7 +27,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("status --help"), then => the_cli_should_output(new[] { - "Show service statuses.", + "Show app and service statuses", $"Usage: {Program.Name} status [options]", "Options:", "-?|-h|--help Show help information", @@ -49,13 +46,12 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void StatusUninitializedProject() + public void StatusUninitialized() { Runner.RunScenario( - given => a_dotnet_project("status_uninitialized_project"), + given => a_dotnet_project("status_uninitialized"), when => the_developer_runs_cli_command("status"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") ); } @@ -64,28 +60,13 @@ namespace Steeltoe.Cli.Test { Runner.RunScenario( given => a_steeltoe_project("status_services"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - when => the_developer_runs_cli_command("add dummy-svc defunct-service"), - and => the_developer_runs_cli_command("disable defunct-service"), - and => the_developer_runs_cli_command("-S status"), + when => the_developer_runs_cli_command("add dummy-svc my-service-a"), + and => the_developer_runs_cli_command("add dummy-svc my-service-b"), + and => the_developer_runs_cli_command("status"), then => the_cli_should_output(new[] { - "a-service offline", - "defunct-service disabled", - }), - when => the_developer_runs_cli_command("deploy"), - and => the_developer_runs_cli_command("-S status"), - then => the_cli_should_output(new[] - { - "a-service starting", - "defunct-service disabled", - }), - when => the_developer_runs_cli_command("undeploy"), - and => the_developer_runs_cli_command("-S status"), - then => the_cli_should_output(new[] - { - "a-service stopping", - "defunct-service disabled", + "my-service-a offline", + "my-service-b offline", }) ); } @@ -108,7 +89,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("init"), and => the_developer_runs_cli_command("add dummy-svc a-server"), and => the_developer_runs_cli_command("status"), - then => the_cli_should_error(ErrorCode.Tooling, "Target deployment environment not set") + then => the_cli_should_error(ErrorCode.Tooling, "Target not set") ); } } diff --git a/test/Steeltoe.Cli.Test/SystemShellFeature.cs b/test/Steeltoe.Cli.Test/SystemShellFeature.cs index 7e906ab..31a724a 100644 --- a/test/Steeltoe.Cli.Test/SystemShellFeature.cs +++ b/test/Steeltoe.Cli.Test/SystemShellFeature.cs @@ -70,7 +70,7 @@ namespace Steeltoe.Cli.Test public void RunNoSuchCommand() { Runner.RunScenario( - when => the_command_is_run("NoSuchCommand"), + when => the_command_is_run("no-such-command"), then => the_command_should_raise_exception() ); } diff --git a/test/Steeltoe.Cli.Test/TargetFeature.cs b/test/Steeltoe.Cli.Test/TargetFeature.cs index ba82dba..4d7fc31 100644 --- a/test/Steeltoe.Cli.Test/TargetFeature.cs +++ b/test/Steeltoe.Cli.Test/TargetFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("target")] public class TargetFeature : FeatureSpecs { [Scenario] - [Label("help")] public void TargetHelp() { Runner.RunScenario( @@ -30,14 +27,14 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("target --help"), then => the_cli_should_output(new[] { - "Set or get the targeted deployment environment.", + "Set or get the deployment target", $"Usage: {Program.Name} target [arguments] [options]", "Arguments:", - "environment Deployment environment", + "target Deployment target name", "Options:", - "-F|--force Target the deployment environment even if checks fail", + "-F|--force Set the deployment target even if checks fail", "-?|-h|--help Show help information", - "If run with no args, show the currently targeted deployment environment.", + "If run with no args, show the current deployment target.", }) ); } @@ -53,39 +50,38 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void TargetUninitializedProject() + public void TargetUninitialized() { Runner.RunScenario( - given => a_dotnet_project("target_uninitialized_project"), + given => a_dotnet_project("target_uninitialized"), when => the_developer_runs_cli_command("target"), - then => the_cli_should_error(ErrorCode.Tooling, - "Project has not been initialized for Steeltoe Developer Tools") + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") ); } [Scenario] - public void TargetEnvironment() + public void TargetSet() { Runner.RunScenario( - given => a_dotnet_project("target_environment"), + given => a_dotnet_project("target_set"), when => the_developer_runs_cli_command("init"), - and => the_developer_runs_cli_command("target dummy-env"), + and => the_developer_runs_cli_command("target dummy-target"), then => the_cli_should_output(new[] { "dummy tool version ... 0.0.0", - "Target deployment environment set to 'dummy-env'.", + "Target set to 'dummy-target'", }), - and => the_configuration_should_target("dummy-env") + and => the_configuration_should_target("dummy-target") ); } [Scenario] - public void TargetUnknownEnvironment() + public void TargetSetUnknown() { Runner.RunScenario( - given => a_steeltoe_project("target_unknown_environment"), - when => the_developer_runs_cli_command("target no-such-environment"), - then => the_cli_should_error(ErrorCode.Tooling, "Unknown deployment environment 'no-such-environment'") + given => a_steeltoe_project("target_set_unknown"), + when => the_developer_runs_cli_command("target no-such-target"), + then => the_cli_should_error(ErrorCode.Tooling, "Unknown target 'no-such-target'") ); } @@ -95,7 +91,7 @@ namespace Steeltoe.Cli.Test Runner.RunScenario( given => a_steeltoe_project("show_target_environment"), when => the_developer_runs_cli_command("target"), - then => the_cli_should_output("Target deployment environment set to 'dummy-env'.") + then => the_cli_should_output("dummy-target") ); } } diff --git a/test/Steeltoe.Cli.Test/UndeployFeature.cs b/test/Steeltoe.Cli.Test/UndeployFeature.cs index 58abe64..f3d0274 100644 --- a/test/Steeltoe.Cli.Test/UndeployFeature.cs +++ b/test/Steeltoe.Cli.Test/UndeployFeature.cs @@ -12,17 +12,14 @@ // 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; namespace Steeltoe.Cli.Test { - [Label("undeploy")] public class UndeployFeature : FeatureSpecs { [Scenario] - [Label("help")] public void UndeployHelp() { Runner.RunScenario( @@ -30,7 +27,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("undeploy --help"), then => the_cli_should_output(new[] { - "Undeploy enabled services from the targeted deployment environment.", + "Undeploy apps and services from the target", $"Usage: {Program.Name} undeploy [options]", "Options:", "-?|-h|--help Show help information", @@ -49,35 +46,40 @@ namespace Steeltoe.Cli.Test } [Scenario] - public void UndeployService() + public void UndeployUninitialized() + { + Runner.RunScenario( + given => a_dotnet_project("undeploy_uninitialized"), + when => the_developer_runs_cli_command("undeploy"), + then => the_cli_should_error(ErrorCode.Tooling, "Steeltoe Developer Tools has not been initialized") + ); + } + + [Scenario] + public void Undeploy() { Runner.RunScenario( given => a_steeltoe_project("undeploy_services"), - when => the_developer_runs_cli_command("add dummy-svc a-service"), - and => the_developer_runs_cli_command("add dummy-svc defunct-service"), - and => the_developer_runs_cli_command("disable defunct-service"), + when => the_developer_runs_cli_command("add app my-app"), + and => the_developer_runs_cli_command("add dummy-svc my-service-a"), + and => the_developer_runs_cli_command("add dummy-svc my-service-b"), and => the_developer_runs_cli_command("deploy"), - when => the_developer_runs_cli_command("status"), - and => the_developer_runs_cli_command("-S undeploy"), + and => the_developer_runs_cli_command("status"), + and => the_developer_runs_cli_command("undeploy"), then => the_cli_should_output(new[] { - "Undeploying service 'a-service'", - "Ignoring disabled service 'defunct-service'", - }), - when => the_developer_runs_cli_command("-S status"), - then => the_cli_should_output(new[] - { - "a-service stopping", - "defunct-service disabled", + "Undeploying app 'my-app'", + "Undeploying service 'my-service-a'", + "Undeploying service 'my-service-b'", }) ); } [Scenario] - public void DeployNoServices() + public void DeployNothing() { Runner.RunScenario( - given => a_steeltoe_project("undeploy_no_services"), + given => a_steeltoe_project("undeploy_nothing"), when => the_developer_runs_cli_command("deploy"), then => the_cli_should_output_nothing() ); @@ -91,7 +93,7 @@ namespace Steeltoe.Cli.Test when => the_developer_runs_cli_command("init"), and => the_developer_runs_cli_command("add dummy-svc a-server"), and => the_developer_runs_cli_command("undeploy"), - then => the_cli_should_error(ErrorCode.Tooling, "Target deployment environment not set") + then => the_cli_should_error(ErrorCode.Tooling, "Target not set") ); } } diff --git a/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryBackendTest.cs b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryBackendTest.cs new file mode 100644 index 0000000..7ee41d2 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryBackendTest.cs @@ -0,0 +1,164 @@ +// 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 System.IO; +using Shouldly; +using Steeltoe.Tooling.CloudFoundry; +using Xunit; + +namespace Steeltoe.Tooling.Test.CloudFoundry +{ + public class CloudFoundryBackendTest : ToolingTest + { + private readonly CloudFoundryBackend _backend; + + public CloudFoundryBackendTest() + { + Context.Configuration.Target = "cloud-foundry"; + _backend = Context.Target.GetBackend(Context) as CloudFoundryBackend; + } + + [Fact] + public void TestDeployApp() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + Context.Configuration.AddApp("my-app"); + _backend.DeployApp("my-app"); + Shell.Commands.Count.ShouldBe(2); + Shell.Commands[0].ShouldBe("dotnet publish -f netcoreapp2.1 -r win10-x64"); + Shell.Commands[1].ShouldBe("cf push -f manifest-steeltoe.yml -p bin/Debug/netcoreapp2.1/win10-x64/publish"); + var manifestFile = new CloudFoundryManifestFile(Path.Combine(Context.ProjectDirectory, "manifest-steeltoe.yml")); + manifestFile.Exists().ShouldBeTrue(); + manifestFile.CloudFoundryManifest.Applications.Count.ShouldBe(1); + var app = manifestFile.CloudFoundryManifest.Applications[0]; + app.Name.ShouldBe("my-app"); + app.Command.ShouldBe($"cmd /c .\\{Path.GetFileName(Context.ProjectDirectory)}"); + app.BuildPacks.Count.ShouldBe(1); + app.BuildPacks[0].ShouldBe("binary_buildpack"); + app.Stack.ShouldBe("windows2016"); + app.Memory.ShouldBe("512M"); + app.Environment["ASPNETCORE_ENVIRONMENT"].ShouldBe("development"); + app.ServiceNames.Count.ShouldBe(1); + app.ServiceNames[0].ShouldBe("my-service"); + } + + [Fact] + public void TestDeployService() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + _backend.DeployService("my-service"); + Shell.LastCommand.ShouldBe("cf create-service dummy-server dummy-plan my-service"); + } + + [Fact] + public void TestDeployConfigServer() + { + Context.Configuration.AddService("my-service", "config-server"); + _backend.DeployService("my-service"); + Shell.LastCommand.ShouldBe("cf create-service p-config-server standard my-service"); + } + + [Fact] + public void TestDeployEurekaServer() + { + Context.Configuration.AddService("my-service", "eureka-server"); + _backend.DeployService("my-service"); + Shell.LastCommand.ShouldBe("cf create-service p-service-registry standard my-service"); + } + + [Fact] + public void TestDeployHystrixDashboard() + { + Context.Configuration.AddService("my-service", "hystrix-dashboard"); + _backend.DeployService("my-service"); + Shell.LastCommand.ShouldBe("cf create-service p-circuit-breaker-dashboard standard my-service"); + } + + [Fact] + public void TestDeployRedis() + { + Context.Configuration.AddService("my-service", "redis"); + _backend.DeployService("my-service"); + Shell.LastCommand.ShouldBe("cf create-service p-redis shared-vm my-service"); + } + + [Fact] + public void TestUndeployApp() + { + Context.Configuration.AddApp("my-app"); + _backend.UndeployApp("my-app"); + Shell.LastCommand.ShouldBe("cf delete my-app -f"); + } + + [Fact] + public void TestUndeployService() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + _backend.UndeployService("my-service"); + Shell.LastCommand.ShouldBe("cf delete-service my-service -f"); + } + + [Fact] + public void TestCheckApp() + { + Context.Configuration.AddApp("my-app"); + _backend.GetAppStatus("my-app"); + Shell.LastCommand.ShouldBe("cf app my-app"); + } + + [Fact] + public void TestCheckService() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + _backend.GetServiceStatus("my-service"); + Shell.LastCommand.ShouldBe("cf service my-service"); + } + + [Fact] + public void TestServiceStarting() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + Shell.AddResponse("status: create in progress"); + var status = _backend.GetServiceStatus("my-service"); + status.ShouldBe(Lifecycle.Status.Starting); + } + + [Fact] + public void TestAppOnline() + { + Context.Configuration.AddApp("my-app"); + Shell.AddResponse("#0 running 2018-11-02T16:32:37Z 16.9% 129.2M of 512M 124.8M of 1G"); + var status = _backend.GetAppStatus("my-app"); + status.ShouldBe(Lifecycle.Status.Online); + } + + [Fact] + public void TestServiceOnline() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + Shell.AddResponse("status: create succeeded"); + var status = _backend.GetServiceStatus("my-service"); + status.ShouldBe(Lifecycle.Status.Online); + } + + [Fact] + public void TestServiceOffline() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + Shell.AddResponse("Service instance my-service not found", 1); + var status = _backend.GetServiceStatus("my-service"); + status.ShouldBe(Lifecycle.Status.Offline); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryManifestFileTest.cs b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryManifestFileTest.cs new file mode 100644 index 0000000..f8c96d4 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryManifestFileTest.cs @@ -0,0 +1,70 @@ +using System.Collections.Generic; +using Shouldly; +using System.IO; +using Steeltoe.Tooling.CloudFoundry; +using Xunit; + +namespace Steeltoe.Tooling.Test.CloudFoundry +{ + public class CloudFoundryManifestFileTest : ToolingTest + { + private readonly string _configFile; + + public CloudFoundryManifestFileTest() + { + _configFile = Path.Join(Context.ProjectDirectory, "config-file"); + } + + [Fact] + public void TestLoadFromFile() + { + File.WriteAllText(_configFile, SampleConfig); + var cfgFile = new CloudFoundryManifestFile(_configFile); + cfgFile.CloudFoundryManifest.Applications.Count.ShouldBe(1); + var app = cfgFile.CloudFoundryManifest.Applications[0]; + app.Name.ShouldBe("myapp"); + app.Command.ShouldBe("my command"); + app.BuildPacks.Count.ShouldBe(1); + app.BuildPacks[0].ShouldBe("my_build_pack"); + app.Stack.ShouldBe("my stack"); + app.Memory.ShouldBe("my mem"); + app.Environment.Count.ShouldBe(1); + app.Environment["myenv"].ShouldBe("my var"); + app.ServiceNames.Count.ShouldBe(2); + app.ServiceNames.ShouldContain("my-service"); + app.ServiceNames.ShouldContain("my-other-service"); + } + + [Fact] + public void TestStoreToFile() + { + var cfgFile = new CloudFoundryManifestFile(_configFile); + cfgFile.CloudFoundryManifest.Applications.Add(new CloudFoundryManifest.Application + { + Name = "myapp", + Command = "my command", + BuildPacks = new List {"my_build_pack"}, + Stack = "my stack", + Memory = "my mem", + Environment = new Dictionary {{"myenv", "my var"}}, + ServiceNames = new List {"my-service", "my-other-service"}, + }); + cfgFile.Store(); + File.ReadAllText(_configFile).ShouldBe(SampleConfig); + } + + private const string SampleConfig = @"applications: +- name: myapp + command: my command + buildpacks: + - my_build_pack + stack: my stack + memory: my mem + env: + myenv: my var + services: + - my-service + - my-other-service +"; + } +} diff --git a/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryServiceManagerTest.cs b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryServiceManagerTest.cs deleted file mode 100644 index 3649d7e..0000000 --- a/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryServiceManagerTest.cs +++ /dev/null @@ -1,99 +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 Shouldly; -using Steeltoe.Tooling.CloudFoundry; -using Xunit; - -namespace Steeltoe.Tooling.Test.CloudFoundry -{ - public class CloudFoundryServiceManagerTest : ToolingTest - { - private readonly IServiceBackend _backend; - - public CloudFoundryServiceManagerTest() - { - Context.ToolingConfiguration.EnvironmentName = "cloud-foundry"; - _backend = Context.Environment.GetServiceBackend(Context); - _backend.ShouldBeOfType(typeof(CloudFoundryServiceBackend)); - - } - - [Fact] - public void TestDeployCircuitBreakerDashboard() - { - _backend.DeployService("my-service", "circuit-breaker-dashboard"); - Shell.LastCommand.ShouldBe("cf create-service p-circuit-breaker-dashboard standard my-service"); - } - - [Fact] - public void TestDeployConfigServer() - { - _backend.DeployService("my-service", "config-server"); - Shell.LastCommand.ShouldBe("cf create-service p-config-server standard my-service"); - } - - [Fact] - public void TestDeployRedisServer() - { - _backend.DeployService("my-service", "redis-server"); - Shell.LastCommand.ShouldBe("cf create-service p-redis shared-vm my-service"); - } - - [Fact] - public void TestDeployServiceRegistry() - { - _backend.DeployService("my-service", "service-registry"); - Shell.LastCommand.ShouldBe("cf create-service p-service-registry standard my-service"); - } - - [Fact] - public void TestStopService() - { - _backend.UndeployService("my-service"); - Shell.LastCommand.ShouldBe("cf delete-service my-service -f"); - } - - [Fact] - public void TestCheckService() - { - _backend.GetServiceLifecleState("my-service"); - Shell.LastCommand.ShouldBe("cf service my-service"); - } - - [Fact] - public void TestServiceStarting() - { - Shell.AddResponse("status: create in progress"); - var state = _backend.GetServiceLifecleState("my-service"); - state.ShouldBe(ServiceLifecycle.State.Starting); - } - - [Fact] - public void TestServiceOnline() - { - Shell.AddResponse("status: create succeeded"); - var state = _backend.GetServiceLifecleState("my-service"); - state.ShouldBe(ServiceLifecycle.State.Online); - } - - [Fact] - public void TestServiceOffline() - { - Shell.AddResponse("Service instance my-service not found", 1); - var state = _backend.GetServiceLifecleState("my-service"); - state.ShouldBe(ServiceLifecycle.State.Offline); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryEnvironmentTest.cs b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryTargetTest.cs similarity index 75% rename from test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryEnvironmentTest.cs rename to test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryTargetTest.cs index 8ae9179..c71a71d 100644 --- a/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryEnvironmentTest.cs +++ b/test/Steeltoe.Tooling.Test/CloudFoundry/CloudFoundryTargetTest.cs @@ -18,39 +18,38 @@ using Xunit; namespace Steeltoe.Tooling.Test.CloudFoundry { - public class CloudFoundryEnvironmentTest : ToolingTest + public class CloudFoundryTargetTest : ToolingTest { - private readonly Environment _env; + private readonly CloudFoundryTarget _target; - public CloudFoundryEnvironmentTest() + public CloudFoundryTargetTest() { - _env = Registry.GetEnvironment("cloud-foundry"); - _env.ShouldBeOfType(); + _target = Registry.GetTarget("cloud-foundry") as CloudFoundryTarget; } [Fact] public void TestGetName() { - _env.Name.ShouldBe("cloud-foundry"); + _target.Name.ShouldBe("cloud-foundry"); } [Fact] public void TestGetDescription() { - _env.Description.ShouldBe("Cloud Foundry"); + _target.Description.ShouldBe("Cloud Foundry"); } [Fact] - public void TestGetServiceManager() + public void TestGetBackend() { - _env.GetServiceBackend(Context).ShouldBeOfType(); + _target.GetBackend(Context).ShouldBeOfType(); } [Fact] public void TestIsHealthy() { Shell.AddResponse("cf version SOME VERSION"); - var healthy = _env.IsHealthy(Context); + var healthy = _target.IsHealthy(Context); healthy.ShouldBeTrue(); var expected = new[] { @@ -71,7 +70,7 @@ namespace Steeltoe.Tooling.Test.CloudFoundry { Shell.AddResponse(""); Shell.AddResponse("", 1); - var healthy = _env.IsHealthy(Context); + var healthy = _target.IsHealthy(Context); healthy.ShouldBeFalse(); Console.ToString().ShouldContain("logged into Cloud Foundry ... !!! no"); } diff --git a/test/Steeltoe.Tooling.Test/ConfigurationFileTest.cs b/test/Steeltoe.Tooling.Test/ConfigurationFileTest.cs new file mode 100644 index 0000000..e732c72 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/ConfigurationFileTest.cs @@ -0,0 +1,78 @@ +// 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 System.IO; +using Shouldly; +using Xunit; + +namespace Steeltoe.Tooling.Test +{ + public class ConfigurationFileTest : ToolingTest + { + [Fact] + public void TestLoadFromFile() + { + var file = Path.Combine(Context.ProjectDirectory, "config-file"); + File.WriteAllText(file, SampleConfig); + var cfgFile = new ConfigurationFile(file); + cfgFile.Configuration.Target.ShouldBe("dummy-target"); + cfgFile.Configuration.GetServices().ShouldContain("my-service"); + cfgFile.Configuration.GetServiceInfo("my-service").ServiceType.ShouldBe("dummy-svc"); + } + + [Fact] + public void TestLoadFromDirectory() + { + var defaultFile = Path.Combine(Context.ProjectDirectory, ConfigurationFile.DefaultFileName); + File.WriteAllText(defaultFile, SampleConfig); + var cfgFile = new ConfigurationFile(Context.ProjectDirectory); + cfgFile.Configuration.Target.ShouldBe("dummy-target"); + cfgFile.Configuration.GetServices().ShouldContain("my-service"); + cfgFile.Configuration.GetServiceInfo("my-service").ServiceType.ShouldBe("dummy-svc"); + } + + [Fact] + public void TestStoreToFile() + { + var file = Path.Combine(Context.ProjectDirectory, "config-file"); + var cfgFile = new ConfigurationFile(file); + cfgFile.Configuration.Target = "dummy-target"; + cfgFile.Configuration.AddApp("my-app"); + cfgFile.Configuration.AddService("my-service", "dummy-svc"); + cfgFile.Store(); + File.ReadAllText(file).ShouldBe(SampleConfig); + } + + [Fact] + public void TestStoreToDirectory() + { + var cfgFile = new ConfigurationFile(Context.ProjectDirectory); + cfgFile.Configuration.Target = "dummy-target"; + cfgFile.Configuration.AddApp("my-app"); + cfgFile.Configuration.AddService("my-service", "dummy-svc"); + cfgFile.Store(); + var defaultFile = Path.Combine(Context.ProjectDirectory, ConfigurationFile.DefaultFileName); + File.ReadAllText(defaultFile).ShouldBe(SampleConfig); + } + + private const string SampleConfig = @"target: dummy-target +apps: + my-app: {} +services: + my-service: + type: dummy-svc + args: {} +"; + } +} diff --git a/test/Steeltoe.Tooling.Test/ConfigurationTest.cs b/test/Steeltoe.Tooling.Test/ConfigurationTest.cs new file mode 100644 index 0000000..7a3eda3 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/ConfigurationTest.cs @@ -0,0 +1,218 @@ +// 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 Shouldly; +using Xunit; + +namespace Steeltoe.Tooling.Test +{ + public class ConfigurationTest : ToolingTest + { + private readonly Configuration _cfg; + + private readonly MyListener _listener; + + public ConfigurationTest() + { + _cfg = new Configuration(); + _listener = new MyListener(); + _cfg.AddListener(_listener); + } + + [Fact] + public void TestAddService() + { + _cfg.AddService("my-service", "dummy-svc"); + _cfg.Services.Count.ShouldBe(1); + _cfg.Services.Keys.ShouldContain("my-service"); + _cfg.Services["my-service"].ServiceTypeName.ShouldBe("dummy-svc"); + } + + [Fact] + public void TestListenAddService() + { + _cfg.AddService("my-service", "dummy-svc"); + _listener.ReceivedEvent.ShouldBeTrue(); + } + + [Fact] + public void TestAddAlreadyExistingService() + { + _cfg.AddService("preexisting-service", "dummy-svc"); + var e = Assert.Throws( + () => _cfg.AddService("preexisting-service", "dummy-svc") + ); + e.Message.ShouldBe("Service 'preexisting-service' already exists"); + } + + [Fact] + public void TestAddServiceUnknownServiceType() + { + var e = Assert.Throws( + () => _cfg.AddService("my-service", "no-such-service-type") + ); + e.Name.ShouldBe("no-such-service-type"); + e.Description.ShouldBe("type"); + } + + [Fact] + public void TestRemoveService() + { + _cfg.AddService("my-service", "dummy-svc"); + _cfg.RemoveService("my-service"); + _cfg.Services.Count.ShouldBe(0); + } + + [Fact] + public void TestListenRemoveService() + { + _cfg.AddService("my-service", "dummy-svc"); + _listener.ReceivedEvent = false; + _cfg.RemoveService("my-service"); + _listener.ReceivedEvent.ShouldBeTrue(); + } + + [Fact] + public void TestRemoveUnknownService() + { + var e = Assert.Throws( + () => _cfg.RemoveService("no-such-service") + ); + e.Name.ShouldBe("no-such-service"); + } + + [Fact] + public void TestGetServiceInfo() + { + _cfg.AddService("my-service", "dummy-svc"); + var info = _cfg.GetServiceInfo("my-service"); + info.Service.ShouldBe("my-service"); + info.ServiceType.ShouldBe("dummy-svc"); + } + + [Fact] + public void TestGetServiceInfoUnknownService() + { + var e = Assert.Throws( + () => _cfg.GetServiceInfo("no-such-service") + ); + e.Name.ShouldBe("no-such-service"); + } + + [Fact] + public void TestGetServiceNames() + { + _cfg.GetServices().Count.ShouldBe(0); + _cfg.AddService("my-service", "dummy-svc"); + _cfg.AddService("another-service", "dummy-svc"); + var names = _cfg.GetServices(); + names.Remove("my-service").ShouldBeTrue(); + names.Remove("another-service").ShouldBeTrue(); + names.Count.ShouldBe(0); + } + + [Fact] + public void TestSetServiceDeploymentArgs() + { + _cfg.AddService("my-service", "dummy-svc"); + _cfg.SetServiceDeploymentArgs("my-service", "dummy-target", "arg1 arg2"); + _cfg.Services["my-service"].Args["dummy-target"].ShouldBe("arg1 arg2"); + } + + [Fact] + public void TestListenSetServiceDeploymentArgs() + { + _cfg.AddService("my-service", "dummy-svc"); + _listener.ReceivedEvent = false; + _cfg.SetServiceDeploymentArgs("my-service", "dummy-target", "arg1 arg2"); + _listener.ReceivedEvent.ShouldBeTrue(); + } + + [Fact] + public void TestSetServiceDeploymentArgsUnknownService() + { + var e = Assert.Throws( + () => _cfg.SetServiceDeploymentArgs("no-such-service", "dummy-target", "arg1 arg2") + ); + e.Name.ShouldBe("no-such-service"); + e.Description.ShouldBe("service"); + } + + [Fact] + public void TestSetServiceDeploymentArgsUnknownEnvironment() + { + var e = Assert.Throws( + () => _cfg.SetServiceDeploymentArgs("my-service", "no-such-target", "arg1 arg2") + ); + e.Name.ShouldBe("no-such-target"); + e.Description.ShouldBe("target"); + } + + [Fact] + public void TestGetServiceDeploymentArgs() + { + _cfg.AddService("my-service", "dummy-svc"); + _cfg.SetServiceDeploymentArgs("my-service", "dummy-target", "arg1 arg2"); + _cfg.GetServiceDeploymentArgs("my-service", "dummy-target").ShouldBe("arg1 arg2"); + } + + [Fact] + public void TestGetServiceDeploymentNoArgs() + { + _cfg.AddService("my-service", "dummy-svc"); + _cfg.GetServiceDeploymentArgs("my-service", "dummy-target").ShouldBe(null); + } + + [Fact] + public void TestGetServiceDeploymentArgsUnknownService() + { + var e = Assert.Throws( + () => _cfg.GetServiceDeploymentArgs("no-such-service", "dummy-target") + ); + e.Name.ShouldBe("no-such-service"); + e.Description.ShouldBe("service"); + } + + [Fact] + public void TestGetServiceDeploymentArgsUnknownTarget() + { + _cfg.AddService("my-service", "dummy-svc"); + var e = Assert.Throws( + () => _cfg.GetServiceDeploymentArgs("my-service", "no-such-target") + ); + e.Name.ShouldBe("no-such-target"); + e.Description.ShouldBe("target"); + } + + [Fact] + public void TestChangeEvent() + { + var cfg = new Configuration(); + var listener = new MyListener(); + cfg.AddListener(listener); + cfg.NotifyChanged(); + listener.ReceivedEvent.ShouldBeTrue(); + } + + internal class MyListener : IConfigurationListener + { + internal bool ReceivedEvent { get; set; } + + public void ConfigurationChangeEvent() + { + ReceivedEvent = true; + } + } + } +} diff --git a/test/Steeltoe.Tooling.Test/ContextTest.cs b/test/Steeltoe.Tooling.Test/ContextTest.cs index d7fcff4..a11164e 100644 --- a/test/Steeltoe.Tooling.Test/ContextTest.cs +++ b/test/Steeltoe.Tooling.Test/ContextTest.cs @@ -22,29 +22,12 @@ namespace Steeltoe.Tooling.Test [Fact] public void TestContext() { - var cfg = new ToolingConfiguration(); - cfg.EnvironmentName = "dummy-env"; + var cfg = new Configuration(); + cfg.Target = "dummy-target"; var ctx = new Context(null, cfg, Console, Shell); - ctx.ToolingConfiguration.ShouldBe(cfg); + ctx.Configuration.ShouldBe(cfg); ctx.Shell.ShouldBe(Shell); - ctx.Environment.Name.ShouldBe("dummy-env"); - ctx.ServiceManager.ShouldNotBeNull(); - } - - [Fact] - public void TestContextEnvironmentNotSet() - { - var cfg = new ToolingConfiguration(); - var ctx = new Context(null, cfg, Console, Shell); - try - { - var unused = ctx.Environment; - Assert.True(false, "expected ToolingException"); - } - catch (ToolingException e) - { - e.Message.ShouldBe("Target deployment environment not set"); - } + ctx.Target.Name.ShouldBe("dummy-target"); } } } diff --git a/test/Steeltoe.Tooling.Test/Docker/DockerServiceManagerTest.cs b/test/Steeltoe.Tooling.Test/Docker/DockerBackendTest.cs similarity index 62% rename from test/Steeltoe.Tooling.Test/Docker/DockerServiceManagerTest.cs rename to test/Steeltoe.Tooling.Test/Docker/DockerBackendTest.cs index 4967066..21b4756 100644 --- a/test/Steeltoe.Tooling.Test/Docker/DockerServiceManagerTest.cs +++ b/test/Steeltoe.Tooling.Test/Docker/DockerBackendTest.cs @@ -18,38 +18,50 @@ using Xunit; namespace Steeltoe.Tooling.Test.Docker { - public class DockerServiceManagerTest : ToolingTest + public class DockerBackendTest : ToolingTest { - private readonly DockerServiceBackend _backend; + private readonly DockerBackend _backend; - public DockerServiceManagerTest() + public DockerBackendTest() { - Context.ToolingConfiguration.EnvironmentName = "docker"; - _backend = Context.Environment.GetServiceBackend(Context) as DockerServiceBackend; - _backend.ShouldBeOfType(typeof(DockerServiceBackend)); + Context.Configuration.Target = "docker"; + _backend = Context.Target.GetBackend(Context) as DockerBackend; } [Fact] public void TestDeployService() { - _backend.DeployService("a-service", "dummy-svc"); + Context.Configuration.AddService("my-service", "dummy-svc"); + _backend.DeployService("my-service"); Shell.LastCommand.ShouldBe( - "docker run --name a-service --publish 0:0 --detach --rm dummy-server:0.1"); + "docker run --name my-service --publish 0:0 --detach --rm dummy-server:0.1"); + } + + [Fact] + public void TestDeployServiceWithArgs() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + Context.Configuration.SetServiceDeploymentArgs("my-service", "docker", "arg1 arg2"); + _backend.DeployService("my-service", "dummy-svc"); + Shell.LastCommand.ShouldBe( + "docker run --name my-service --publish 0:0 --detach --rm arg1 arg2 dummy-server:0.1"); } [Fact] public void TestDeployServiceForOs() { + Context.Configuration.AddService("my-service", "dummy-svc"); Shell.AddResponse("OSType: dummyos"); - _backend.DeployService("a-service", "dummy-svc"); + _backend.DeployService("my-service"); Shell.LastCommand.ShouldBe( - "docker run --name a-service --publish 0:0 --detach --rm dummy-server:for_dummyos"); + "docker run --name my-service --publish 0:0 --detach --rm dummy-server:for_dummyos"); } [Fact] public void TestDeployConfigServer() { - _backend.DeployService("my-service", "config-server"); + Context.Configuration.AddService("my-service", "config-server"); + _backend.DeployService("my-service"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 8888:8888 --detach --rm steeltoeoss/config-server:2.0.1"); } @@ -57,7 +69,8 @@ namespace Steeltoe.Tooling.Test.Docker [Fact] public void TestDeployEurekaServer() { - _backend.DeployService("my-service", "eureka-server"); + Context.Configuration.AddService("my-service", "eureka-server"); + _backend.DeployService("my-service"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 8761:8761 --detach --rm steeltoeoss/eureka-server:2.0.1"); } @@ -65,18 +78,20 @@ namespace Steeltoe.Tooling.Test.Docker [Fact] public void TestDeployHystrixDashboard() { - _backend.DeployService("my-service", "hystrix-dashboard"); + Context.Configuration.AddService("my-service", "hystrix-dashboard"); + _backend.DeployService("my-service"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 7979:7979 --detach --rm steeltoeoss/hystrix-dashboard:1.4.5"); } [Fact] - public void TestDeployMssql() + public void TestDeployMicrosoftSqlServer() { - _backend.DeployService("my-service", "mssql", "linux"); + Context.Configuration.AddService("my-service", "mssql"); + _backend.DeployService("my-service", "linux"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 1433:1433 --detach --rm steeltoeoss/mssql:2017-CU11-linux"); - _backend.DeployService("my-service", "mssql", "windows"); + _backend.DeployService("my-service", "windows"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 1433:1433 --detach --rm steeltoeoss/mssql:2017-CU1-windows"); } @@ -84,10 +99,11 @@ namespace Steeltoe.Tooling.Test.Docker [Fact] public void TestDeployRedis() { - _backend.DeployService("my-service", "redis", "linux"); + Context.Configuration.AddService("my-service", "redis"); + _backend.DeployService("my-service", "linux"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 6379:6379 --detach --rm steeltoeoss/redis:4.0.11-linux"); - _backend.DeployService("my-service", "redis", "windows"); + _backend.DeployService("my-service", "windows"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 6379:6379 --detach --rm steeltoeoss/redis:3.0.504-windows"); } @@ -95,40 +111,31 @@ namespace Steeltoe.Tooling.Test.Docker [Fact] public void TestDeployZipkin() { - _backend.DeployService("my-service", "zipkin"); + Context.Configuration.AddService("my-service", "zipkin"); + _backend.DeployService("my-service"); Shell.LastCommand.ShouldBe( "docker run --name my-service --publish 9411:9411 --detach --rm steeltoeoss/zipkin:2.11.6"); } - [Fact] - public void TestDeployServiceWithArgs() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.SetServiceDeploymentArgs("docker", "a-service", "arg1 arg2"); - _backend.DeployService("a-service", "dummy-svc"); - Shell.LastCommand.ShouldBe( - "docker run --name a-service --publish 0:0 --detach --rm arg1 arg2 dummy-server:0.1"); - } - [Fact] public void TestUndeployService() { - _backend.UndeployService("a-service"); - Shell.LastCommand.ShouldBe("docker stop a-service"); + _backend.UndeployService("my-service"); + Shell.LastCommand.ShouldBe("docker stop my-service"); } [Fact] public void TestGetServiceLifecycleStateCommand() { - _backend.GetServiceLifecleState("a-service"); - Shell.LastCommand.ShouldBe("docker ps --no-trunc --filter name=^/a-service$"); + _backend.GetServiceStatus("my-service"); + Shell.LastCommand.ShouldBe("docker ps --no-trunc --filter name=^/my-service$"); } [Fact] public void TestGetServiceLifecycleStateOffline() { - var state = _backend.GetServiceLifecleState("a-service"); - state.ShouldBe(ServiceLifecycle.State.Offline); + var state = _backend.GetServiceStatus("my-service"); + state.ShouldBe(Lifecycle.Status.Offline); } [Fact] @@ -136,10 +143,10 @@ namespace Steeltoe.Tooling.Test.Docker { Shell.AddResponse( @"CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -0000000000000000000000000000000000000000000000000000000000000000 dummy-server:0.1 java -Djava.security.egd=file:/dev/./urandom -jar config-server.jar 37 seconds ago Up 36 seconds 0.0.0.0:0000->0000/tcp a-service +0000000000000000000000000000000000000000000000000000000000000000 dummy-server:0.1 java -Djava.security.egd=file:/dev/./urandom -jar config-server.jar 37 seconds ago Up 36 seconds 0.0.0.0:0000->0000/tcp my-service "); - var state = _backend.GetServiceLifecleState("a-service"); - state.ShouldBe(ServiceLifecycle.State.Starting); + var state = _backend.GetServiceStatus("my-service"); + state.ShouldBe(Lifecycle.Status.Starting); } [Fact] @@ -147,11 +154,11 @@ namespace Steeltoe.Tooling.Test.Docker { Shell.AddResponse( @"CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -0000000000000000000000000000000000000000000000000000000000000000 dummy-server:for_dummyos java -Djava.security.egd=file:/dev/./urandom -jar config-server.jar 37 seconds ago Up 36 seconds 0.0.0.0:0000->0000/tcp a-service +0000000000000000000000000000000000000000000000000000000000000000 dummy-server:for_dummyos java -Djava.security.egd=file:/dev/./urandom -jar config-server.jar 37 seconds ago Up 36 seconds 0.0.0.0:0000->0000/tcp my-service "); Shell.AddResponse("OSType: dummyos"); - var state = _backend.GetServiceLifecleState("a-service"); - state.ShouldBe(ServiceLifecycle.State.Starting); + var state = _backend.GetServiceStatus("my-service"); + state.ShouldBe(Lifecycle.Status.Starting); } } } diff --git a/test/Steeltoe.Tooling.Test/Docker/DockerEnvironmentTest.cs b/test/Steeltoe.Tooling.Test/Docker/DockerTargetTest.cs similarity index 85% rename from test/Steeltoe.Tooling.Test/Docker/DockerEnvironmentTest.cs rename to test/Steeltoe.Tooling.Test/Docker/DockerTargetTest.cs index 05f14c5..6012961 100644 --- a/test/Steeltoe.Tooling.Test/Docker/DockerEnvironmentTest.cs +++ b/test/Steeltoe.Tooling.Test/Docker/DockerTargetTest.cs @@ -18,14 +18,13 @@ using Xunit; namespace Steeltoe.Tooling.Test.Docker { - public class DockerEnvironmentTest : ToolingTest + public class DockerTargetTest : ToolingTest { - private readonly Environment _env; + private readonly DockerTarget _env; - public DockerEnvironmentTest() + public DockerTargetTest() { - _env = Registry.GetEnvironment("docker"); - _env.ShouldBeOfType(); + _env = Registry.GetTarget("docker") as DockerTarget; } [Fact] @@ -41,9 +40,9 @@ namespace Steeltoe.Tooling.Test.Docker } [Fact] - public void TestGetServiceManager() + public void TestGetBackend() { - _env.GetServiceBackend(Context).ShouldBeOfType(); + _env.GetBackend(Context).ShouldBeOfType(); } [Fact] diff --git a/test/Steeltoe.Tooling.Test/Dummy/DummyServiceBackendTest.cs b/test/Steeltoe.Tooling.Test/Dummy/DummyBackendTest.cs similarity index 60% rename from test/Steeltoe.Tooling.Test/Dummy/DummyServiceBackendTest.cs rename to test/Steeltoe.Tooling.Test/Dummy/DummyBackendTest.cs index dd584b7..2579cdb 100644 --- a/test/Steeltoe.Tooling.Test/Dummy/DummyServiceBackendTest.cs +++ b/test/Steeltoe.Tooling.Test/Dummy/DummyBackendTest.cs @@ -21,17 +21,17 @@ using Xunit; namespace Steeltoe.Tooling.Test.Dummy { - public class DummyServiceBackendTest : ToolingTest + public class DummyBackendTest : ToolingTest { private readonly string _dbFile; - private readonly DummyServiceBackend _backend; + private readonly DummyBackend _backend; - public DummyServiceBackendTest() + public DummyBackendTest() { Directory.CreateDirectory(Context.ProjectDirectory); _dbFile = Path.Combine(Context.ProjectDirectory, "dummy.db"); - _backend = new DummyServiceBackend(_dbFile); + _backend = new DummyBackend(_dbFile); } [Fact] @@ -40,17 +40,17 @@ namespace Steeltoe.Tooling.Test.Dummy File.Exists(_dbFile).ShouldBeTrue(); // start state -> offline - _backend.GetServiceLifecleState("a-service").ShouldBe(ServiceLifecycle.State.Offline); + _backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Offline); // offline -> deploy -> starting -> online - _backend.DeployService("a-service", "dummy-svc"); - _backend.GetServiceLifecleState("a-service").ShouldBe(ServiceLifecycle.State.Starting); - _backend.GetServiceLifecleState("a-service").ShouldBe(ServiceLifecycle.State.Online); + _backend.DeployService("my-service"); + _backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Starting); + _backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Online); // online -> undeploy -> stopping -> offline - _backend.UndeployService("a-service"); - _backend.GetServiceLifecleState("a-service").ShouldBe(ServiceLifecycle.State.Stopping); - _backend.GetServiceLifecleState("a-service").ShouldBe(ServiceLifecycle.State.Offline); + _backend.UndeployService("my-service"); + _backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Stopping); + _backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Offline); } } } diff --git a/test/Steeltoe.Tooling.Test/Dummy/DummyEnvironmentTest.cs b/test/Steeltoe.Tooling.Test/Dummy/DummyTargetTest.cs similarity index 71% rename from test/Steeltoe.Tooling.Test/Dummy/DummyEnvironmentTest.cs rename to test/Steeltoe.Tooling.Test/Dummy/DummyTargetTest.cs index 1f43223..e0e33d1 100644 --- a/test/Steeltoe.Tooling.Test/Dummy/DummyEnvironmentTest.cs +++ b/test/Steeltoe.Tooling.Test/Dummy/DummyTargetTest.cs @@ -18,26 +18,25 @@ using Xunit; namespace Steeltoe.Tooling.Test.Dummy { - public class DummyEnvironmentTest : ToolingTest + public class DummyTargetTest : ToolingTest { - private readonly Environment _env; + private readonly DummyTarget _target; - public DummyEnvironmentTest() + public DummyTargetTest() { - _env = Registry.GetEnvironment("dummy-env"); - _env.ShouldBeOfType(); + _target = Registry.GetTarget("dummy-target") as DummyTarget; } [Fact] public void TestGetName() { - _env.Name.ShouldBe("dummy-env"); + _target.Name.ShouldBe("dummy-target"); } [Fact] public void TestGetDescription() { - _env.Description.ShouldBe("A Dummy Environment"); + _target.Description.ShouldBe("A Dummy Target"); } } } diff --git a/test/Steeltoe.Tooling.Test/Executor/AddExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/AddExecutorTest.cs new file mode 100644 index 0000000..4530907 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/AddExecutorTest.cs @@ -0,0 +1,77 @@ +// 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 Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class AddExecutorTest : ToolingTest + { + [Fact] + public void TestAddApplication() + { + new AddExecutor("my-app", "app").Execute(Context); + Console.ToString().Trim().ShouldBe("Added app 'my-app'"); + Context.Configuration.GetApps().Count.ShouldBe(1); + Context.Configuration.GetApps()[0].ShouldBe("my-app"); + var appInfo = Context.Configuration.GetAppInfo("my-app"); + appInfo.App.ShouldBe("my-app"); + } + + [Fact] + public void TestAddExistingApplication() + { + Context.Configuration.AddApp("existing-app"); + var e = Assert.Throws( + () => new AddExecutor("existing-app", "app").Execute(Context) + ); + e.Message.ShouldBe("App 'existing-app' already exists"); + } + + [Fact] + public void TestAddService() + { + new AddExecutor("my-service", "dummy-svc").Execute(Context); + Console.ToString().Trim().ShouldBe("Added dummy-svc service 'my-service'"); + Context.Configuration.GetServices().Count.ShouldBe(1); + var svcName = Context.Configuration.GetServices()[0]; + svcName.ShouldBe("my-service"); + var svcInfo = Context.Configuration.GetServiceInfo(svcName); + svcInfo.Service.ShouldBe("my-service"); + svcInfo.ServiceType.ShouldBe("dummy-svc"); + } + + [Fact] + public void TestAddExistingService() + { + Context.Configuration.AddService("existing-service", "dummy-svc"); + var e = Assert.Throws( + () => new AddExecutor("existing-service", "dummy-svc").Execute(Context) + ); + e.Message.ShouldBe("Service 'existing-service' already exists"); + } + + [Fact] + public void TestAddUnknownServiceType() + { + var e = Assert.Throws( + () => new AddExecutor("unknown-service", "unknown-service-type").Execute(Context) + ); + e.Name.ShouldBe("unknown-service-type"); + e.Description.ShouldBe("type"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/AddServiceExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/AddServiceExecutorTest.cs deleted file mode 100644 index 1ab073c..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/AddServiceExecutorTest.cs +++ /dev/null @@ -1,57 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class AddServiceExecutorTest : ToolingTest - { - [Fact] - public void TestAddService() - { - new AddServiceExecutor("a-service", "dummy-svc").Execute(Context); - Console.ToString().Trim().ShouldBe("Added dummy-svc service 'a-service'"); - Context.ServiceManager.GetServiceNames().Count.ShouldBe(1); - var svcName = Context.ServiceManager.GetServiceNames()[0]; - svcName.ShouldBe("a-service"); - var svc = Context.ServiceManager.GetService(svcName); - svc.Type.ShouldBe("dummy-svc"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - } - - [Fact] - public void TestAddPreExistingService() - { - Context.ServiceManager.AddService("pre-existing-service", "dummy-svc"); - var executor = new AddServiceExecutor("pre-existing-service", "dummy-svc"); - var e = Assert.Throws( - () => executor.Execute(Context) - ); - e.Message.ShouldBe("Service 'pre-existing-service' already exists"); - } - - [Fact] - public void TestAddUnknownServiceType() - { - var executor = new AddServiceExecutor("unknown-service", "unknown-service-type"); - var e = Assert.Throws( - () => executor.Execute(Context) - ); - e.Message.ShouldBe("Unknown service type 'unknown-service-type'"); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/DeployApplicationsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/DeployApplicationsExecutorTest.cs new file mode 100644 index 0000000..026fbe5 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/DeployApplicationsExecutorTest.cs @@ -0,0 +1,38 @@ +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class DeployApplicationsExecutorTest : ToolingTest + { + [Fact] + public void TestDeployApplications() + { +// Context.ApplicationManager.AddApplication("an-app"); +// Context.ApplicationManager.AddApplication("another-app"); +// ClearConsole(); +// new DeployApplicationsExecutor().Execute(Context); +// Console.ToString().ShouldContain("Deploying application 'an-app'"); +// Console.ToString().ShouldContain("Deploying application 'another-app'"); +// Context.ApplicationManager.GetApplicationState("an-app").ShouldBe(Lifecycle.State.Starting); +// Context.ApplicationManager.GetApplicationState("another-app").ShouldBe(Lifecycle.State.Starting); + } + + [Fact] + public void TestDeployNoApplications() + { +// new DeployApplicationsExecutor().Execute(Context); +// Console.ToString().Trim().ShouldBeEmpty(); + } + + [Fact] + public void TestDeployNoTarget() + { +// Context.Configuration.EnvironmentName = null; +// Context.ApplicationManager.AddApplication("an-app"); +// var e = Assert.Throws( +// () => new DeployApplicationsExecutor().Execute(Context) +// ); +// e.Message.ShouldBe("Target deployment environment not set"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/DeployExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/DeployExecutorTest.cs new file mode 100644 index 0000000..9bc6a21 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/DeployExecutorTest.cs @@ -0,0 +1,57 @@ +// 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 Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class DeployExecutorTest : ToolingTest + { + [Fact] + public void TestDeploy() + { + Context.Configuration.AddApp("my-app"); + Context.Configuration.AddService("my-service", "dummy-svc"); + Context.Configuration.AddService("another-service", "dummy-svc"); + ClearConsole(); + new DeployExecutor().Execute(Context); + Console.ToString().ShouldContain("Deploying service 'my-service'"); + Console.ToString().ShouldContain("Deploying service 'another-service'"); + Console.ToString().ShouldContain("Deploying app 'my-app'"); + Context.Backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Online); + Context.Backend.GetServiceStatus("another-service").ShouldBe(Lifecycle.Status.Online); + Context.Backend.GetAppStatus("my-app").ShouldBe(Lifecycle.Status.Starting); + } + + [Fact] + public void TestDeployNothing() + { + new DeployExecutor().Execute(Context); + Console.ToString().Trim().ShouldBeEmpty(); + } + + [Fact] + public void TestDeployNoTarget() + { + Context.Configuration.Target = null; + Context.Configuration.AddService("my-service", "dummy-svc"); + var e = Assert.Throws( + () => new DeployExecutor().Execute(Context) + ); + e.Message.ShouldBe("Target not set"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/DeployServicesExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/DeployServicesExecutorTest.cs deleted file mode 100644 index c58683e..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/DeployServicesExecutorTest.cs +++ /dev/null @@ -1,67 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class DeployServicesExecutorTest : ToolingTest - { - [Fact] - public void TestDeployServices() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-service"); - Context.ServiceManager.AddService("defunct-service", "dummy-svc"); - Context.ServiceManager.DisableService("defunct-service"); - ClearConsole(); - new DeployServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("Deploying service 'a-service'"); - Console.ToString().ShouldContain("Ignoring disabled service 'defunct-service'"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Starting); - Context.ServiceManager.GetServiceState("defunct-service").ShouldBe(ServiceLifecycle.State.Disabled); - - new UndeployServicesExecutor().Execute(Context); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Stopping); - Context.ServiceManager.GetServiceState("defunct-service").ShouldBe(ServiceLifecycle.State.Disabled); - ClearConsole(); - new DeployServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("Deploying service 'a-service'"); - Console.ToString().ShouldContain("Ignoring disabled service 'defunct-service'"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Starting); - Context.ServiceManager.GetServiceState("defunct-service").ShouldBe(ServiceLifecycle.State.Disabled); - } - - [Fact] - public void TestDeployNoServices() - { - new DeployServicesExecutor().Execute(Context); - Console.ToString().Trim().ShouldBeEmpty(); - } - - [Fact] - public void TestDeployNoTarget() - { - Context.ToolingConfiguration.EnvironmentName = null; - Context.ServiceManager.AddService("a-existing-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-existing-service"); - var e = Assert.Throws( - () => new DeployServicesExecutor().Execute(Context) - ); - e.Message.ShouldBe("Target deployment environment not set"); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/DisableServiceExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/DisableServiceExecutorTest.cs deleted file mode 100644 index c0a7f1a..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/DisableServiceExecutorTest.cs +++ /dev/null @@ -1,41 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class DisableServiceExecutorTest : ToolingTest - { - [Fact] - public void TestDisableService() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.DisableService("a-service"); - new DisableServiceExecutor("a-service").Execute(Context); - Console.ToString().Trim().ShouldBe("Disabled service 'a-service'"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Disabled); - } - - [Fact] - public void TestDisableNonExistentService() - { - Assert.Throws( - () => new DisableServiceExecutor("non-existent-service").Execute(Context) - ); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/EnableServiceExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/EnableServiceExecutorTest.cs deleted file mode 100644 index ab74940..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/EnableServiceExecutorTest.cs +++ /dev/null @@ -1,41 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class EnableServiceExecutorTest : ToolingTest - { - [Fact] - public void TestEnableService() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-service"); - new EnableServiceExecutor("a-service").Execute(Context); - Console.ToString().Trim().ShouldBe("Enabled service 'a-service'"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - } - - [Fact] - public void TestEnableNonExistentService() - { - Assert.Throws( - () => new EnableServiceExecutor("non-existent-service").Execute(Context) - ); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/GetArgsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/GetArgsExecutorTest.cs new file mode 100644 index 0000000..b525040 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/GetArgsExecutorTest.cs @@ -0,0 +1,63 @@ +// 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 Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class GetArgsExecutorTest : ToolingTest + { + [Fact] + public void TestGetServiceArgs() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + new SetArgsExecutor("my-service", "dummy-target", "arg1 arg2").Execute(Context); + ClearConsole(); + new GetArgsExecutor("my-service", "dummy-target").Execute(Context); + Console.ToString().Trim().ShouldBe("arg1 arg2"); + } + + [Fact] + public void TestGetServiceNoArgs() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + ClearConsole(); + new GetArgsExecutor("my-service", "dummy-target").Execute(Context); + Console.ToString().Trim().ShouldBeEmpty(); + } + + [Fact] + public void TestGetServiceArgsUnknownService() + { + var e = Assert.Throws( + () => new GetArgsExecutor("no-such-service", "dummy-target").Execute(Context) + ); + e.Name.ShouldBe("no-such-service"); + e.Description.ShouldBe("service"); + } + + [Fact] + public void TestGetServiceArgsUnknownTarget() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + var e = Assert.Throws( + () => new GetArgsExecutor("my-service", "no-such-target").Execute(Context) + ); + e.Name.ShouldBe("no-such-target"); + e.Description.ShouldBe("target"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/GetServiceDeploymentArgsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/GetServiceDeploymentArgsExecutorTest.cs deleted file mode 100644 index 3d8ead3..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/GetServiceDeploymentArgsExecutorTest.cs +++ /dev/null @@ -1,50 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class GetServiceDeploymentArgsExecutorTest : ToolingTest - { - [Fact] - public void TestGetServiceDeploymentArgs() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - new SetServiceDeploymentArgsExecutor("dummy-env", "a-service", "arg1 arg2").Execute(Context); - ClearConsole(); - new GetServiceDeploymentArgsExecutor("dummy-env", "a-service").Execute(Context); - Console.ToString().Trim().ShouldBe("arg1 arg2"); - } - - [Fact] - public void TestGetServiceDeploymentNoArgs() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - ClearConsole(); - new GetServiceDeploymentArgsExecutor("dummy-env", "a-service").Execute(Context); - Console.ToString().Trim().ShouldBeEmpty(); - } - - [Fact] - public void TestGetNonExistentServiceDeploymentArgs() - { - Assert.Throws( - () => new GetServiceDeploymentArgsExecutor("dummy-env", "non-existent-service").Execute(Context) - ); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/GetTargetExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/GetTargetExecutorTest.cs index 6765510..ea6c2d9 100644 --- a/test/Steeltoe.Tooling.Test/Executor/GetTargetExecutorTest.cs +++ b/test/Steeltoe.Tooling.Test/Executor/GetTargetExecutorTest.cs @@ -21,22 +21,21 @@ namespace Steeltoe.Tooling.Test.Executor public class GetTargetExecutorTest : ToolingTest { [Fact] - public void TestShowTarget() + public void TestGetTarget() { - Context.ToolingConfiguration.EnvironmentName = "dummy-env"; + Context.Configuration.Target = "dummy-target"; new GetTargetExecutor().Execute(Context); - Console.ToString().Trim().ShouldBe("Target deployment environment set to 'dummy-env'."); + Console.ToString().Trim().ShouldBe("dummy-target"); } [Fact] - public void TestShowNoTarget() + public void TestGetTargetNotSet() { - Context.ToolingConfiguration.EnvironmentName = null; - var svc = new GetTargetExecutor(); + Context.Configuration.Target = null; var e = Assert.Throws( - () => svc.Execute(Context) + () => new GetTargetExecutor().Execute(Context) ); - e.Message.ShouldBe("Target deployment environment not set"); + e.Message.ShouldBe("Target not set"); } } } diff --git a/test/Steeltoe.Tooling.Test/Executor/InitializationExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/InitializationExecutorTest.cs new file mode 100644 index 0000000..bd00203 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/InitializationExecutorTest.cs @@ -0,0 +1,45 @@ +// 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 System.IO; +using Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class InitializationExecutorTest : ToolingTest + { + [Fact] + public void TestInitialization() + { + new InitializationExecutor().Execute(Context); + File.Exists(Path.Join(Context.ProjectDirectory, ConfigurationFile.DefaultFileName)).ShouldBeTrue(); + Console.ToString().Trim().ShouldBe("Initialized Steeltoe Developer Tools"); + } + + [Fact] + public void TestInitializationForce() + { + var file = "config-file"; + new ConfigurationFile(Path.Combine(Context.ProjectDirectory, file)).Store(); + var e = Assert.Throws( + () => new InitializationExecutor(file).Execute(Context) + ); + e.Message.ShouldBe("Steeltoe Developer Tools already initialized"); + new InitializationExecutor(file, true).Execute(Context); + Console.ToString().Trim().ShouldBe("Initialized Steeltoe Developer Tools"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/ListApplicationsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/ListApplicationsExecutorTest.cs new file mode 100644 index 0000000..9aff0a6 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/ListApplicationsExecutorTest.cs @@ -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 Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class ListApplicationsExecutorTest : ToolingTest + { + [Fact] + public void TestListNone() + { +// new ListApplicationsExecutor().Execute(Context); +// Console.ToString().ShouldBeEmpty(); + } + + [Fact] + public void TestListApplications() + { +// Context.ApplicationManager.AddApplication("a-app"); +// Context.ApplicationManager.AddApplication("c-app"); +// Context.ApplicationManager.AddApplication("b-app"); +// ClearConsole(); +// new ListApplicationsExecutor().Execute(Context); +// var reader = new StringReader(Console.ToString()); +// reader.ReadLine().ShouldBe("a-app"); +// reader.ReadLine().ShouldBe("b-app"); +// reader.ReadLine().ShouldBe("c-app"); +// reader.ReadLine().ShouldBeNull(); + } + + [Fact] + public void TestListApplicationsVerbose() + { +// Context.ApplicationManager.AddApplication("a-app"); +// Context.ApplicationManager.AddApplication("c-app"); +// Context.ApplicationManager.AddApplication("b-app"); +// ClearConsole(); +// new ListApplicationsExecutor(true).Execute(Context); +// var reader = new StringReader(Console.ToString()); +// reader.ReadLine().ShouldBe("a-app application"); +// reader.ReadLine().ShouldBe("b-app application"); +// reader.ReadLine().ShouldBe("c-app application"); +// reader.ReadLine().ShouldBeNull(); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/ListExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/ListExecutorTest.cs new file mode 100644 index 0000000..ffe9fbe --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/ListExecutorTest.cs @@ -0,0 +1,65 @@ +// 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 System.IO; +using Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class ListExecutorTest : ToolingTest + { + [Fact] + public void TestListNone() + { + new ListExecutor().Execute(Context); + Console.ToString().ShouldBeEmpty(); + } + + [Fact] + public void TestList() + { + Context.Configuration.AddApp("my-app"); + Context.Configuration.AddService("my-service-c", "dummy-svc"); + Context.Configuration.AddService("my-service-a", "dummy-svc"); + Context.Configuration.AddService("my-service-b", "dummy-svc"); + ClearConsole(); + new ListExecutor().Execute(Context); + var reader = new StringReader(Console.ToString()); + reader.ReadLine().ShouldBe("my-service-a"); + reader.ReadLine().ShouldBe("my-service-b"); + reader.ReadLine().ShouldBe("my-service-c"); + reader.ReadLine().ShouldBe("my-app"); + reader.ReadLine().ShouldBeNull(); + } + + [Fact] + public void TestListVerbose() + { + Context.Configuration.AddApp("my-app"); + Context.Configuration.AddService("my-service-c", "dummy-svc"); + Context.Configuration.AddService("my-service-a", "dummy-svc"); + Context.Configuration.AddService("my-service-b", "dummy-svc"); + ClearConsole(); + new ListExecutor(true).Execute(Context); + var reader = new StringReader(Console.ToString()); + reader.ReadLine().ShouldBe("my-service-a 0 dummy-svc"); + reader.ReadLine().ShouldBe("my-service-b 0 dummy-svc"); + reader.ReadLine().ShouldBe("my-service-c 0 dummy-svc"); + reader.ReadLine().ShouldBe("my-app app"); + reader.ReadLine().ShouldBeNull(); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/ListServiceTypesExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/ListServiceTypesExecutorTest.cs index 5a1b8af..18fef9c 100644 --- a/test/Steeltoe.Tooling.Test/Executor/ListServiceTypesExecutorTest.cs +++ b/test/Steeltoe.Tooling.Test/Executor/ListServiceTypesExecutorTest.cs @@ -15,13 +15,12 @@ using System.IO; using Shouldly; using Steeltoe.Tooling.Executor; -using Xunit; namespace Steeltoe.Tooling.Test.Executor { public class ListServiceTypesExecutorTest : ToolingTest { - [Fact] +// [Fact] public void TestListServiceTypes() { new ListServiceTypesExecutor().Execute(Context); @@ -36,7 +35,7 @@ namespace Steeltoe.Tooling.Test.Executor reader.ReadLine().ShouldBeNull(); } - [Fact] +// [Fact] public void TestListServiceTypesVerbose() { new ListServiceTypesExecutor(true).Execute(Context); diff --git a/test/Steeltoe.Tooling.Test/Executor/ListServicesExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/ListServicesExecutorTest.cs deleted file mode 100644 index 0a02595..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/ListServicesExecutorTest.cs +++ /dev/null @@ -1,62 +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 System.IO; -using Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class ListServicesExecutorTest : ToolingTest - { - [Fact] - public void TestListNone() - { - var svc = new ListServicesExecutor(); - svc.Execute(Context); - Console.ToString().ShouldBeEmpty(); - } - - [Fact] - public void TestListServices() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.AddService("c-service", "dummy-svc"); - Context.ServiceManager.AddService("b-service", "dummy-svc"); - ClearConsole(); - new ListServicesExecutor().Execute(Context); - var reader = new StringReader(Console.ToString()); - reader.ReadLine().ShouldBe("a-service"); - reader.ReadLine().ShouldBe("b-service"); - reader.ReadLine().ShouldBe("c-service"); - reader.ReadLine().ShouldBeNull(); - } - - [Fact] - public void TestListServicesVerbose() - { - Context.ServiceManager.AddService("b-service", "dummy-svc"); - Context.ServiceManager.AddService("c-service", "dummy-svc"); - Context.ServiceManager.AddService("a-service", "dummy-svc"); - ClearConsole(); - new ListServicesExecutor(true).Execute(Context); - var reader = new StringReader(Console.ToString()); - reader.ReadLine().ShouldBe("a-service 0 dummy-svc"); - reader.ReadLine().ShouldBe("b-service 0 dummy-svc"); - reader.ReadLine().ShouldBe("c-service 0 dummy-svc"); - reader.ReadLine().ShouldBeNull(); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/ListEnvironmentsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/ListTargetsExecutorTest.cs similarity index 73% rename from test/Steeltoe.Tooling.Test/Executor/ListEnvironmentsExecutorTest.cs rename to test/Steeltoe.Tooling.Test/Executor/ListTargetsExecutorTest.cs index 8a9bed0..15afe9f 100644 --- a/test/Steeltoe.Tooling.Test/Executor/ListEnvironmentsExecutorTest.cs +++ b/test/Steeltoe.Tooling.Test/Executor/ListTargetsExecutorTest.cs @@ -15,31 +15,30 @@ using System.IO; using Shouldly; using Steeltoe.Tooling.Executor; -using Xunit; namespace Steeltoe.Tooling.Test.Executor { - public class ListEnvironmentsExecutorTest : ToolingTest + public class ListTargetsExecutorTest : ToolingTest { - [Fact] - public void TestListEnvironments() +// [Fact] + public void TestListTargets() { - new ListEnvironmentsExecutor().Execute(Context); + new ListTargetsExecutor().Execute(Context); var reader = new StringReader(Console.ToString()); reader.ReadLine().ShouldBe("cloud-foundry"); reader.ReadLine().ShouldBe("docker"); - reader.ReadLine().ShouldBe("dummy-env"); + reader.ReadLine().ShouldBe("dummy-target"); reader.ReadLine().ShouldBeNull(); } - [Fact] - public void TestListEnvironmentsVerbose() +// [Fact] + public void TestListTargetsVerbose() { - new ListEnvironmentsExecutor(true).Execute(Context); + new ListTargetsExecutor(true).Execute(Context); var reader = new StringReader(Console.ToString()); reader.ReadLine().ShouldBe("cloud-foundry Cloud Foundry"); reader.ReadLine().ShouldBe("docker Docker"); - reader.ReadLine().ShouldBe("dummy-env A Dummy Environment"); + reader.ReadLine().ShouldBe("dummy-target A Dummy Target"); reader.ReadLine().ShouldBeNull(); } } diff --git a/test/Steeltoe.Tooling.Test/Executor/RemoveServiceExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/RemoveExecutorTest.cs similarity index 53% rename from test/Steeltoe.Tooling.Test/Executor/RemoveServiceExecutorTest.cs rename to test/Steeltoe.Tooling.Test/Executor/RemoveExecutorTest.cs index 7cc9ce5..d1d92f1 100644 --- a/test/Steeltoe.Tooling.Test/Executor/RemoveServiceExecutorTest.cs +++ b/test/Steeltoe.Tooling.Test/Executor/RemoveExecutorTest.cs @@ -18,26 +18,27 @@ using Xunit; namespace Steeltoe.Tooling.Test.Executor { - public class RemoveServiceExecutorTest : ToolingTest + public class RemoveExecutorTest : ToolingTest { [Fact] public void TestRemoveService() { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.AddService("another-service", "dummy-svc"); - new RemoveServiceExecutor("a-service").Execute(Context); - Console.ToString().Trim().ShouldBe("Removed service 'a-service'"); - Context.ServiceManager.GetServiceNames().ShouldNotContain("a-service"); - Context.ServiceManager.GetServiceNames().ShouldContain("another-service"); + Context.Configuration.AddService("my-service", "dummy-svc"); + Context.Configuration.AddService("my-other-service", "dummy-svc"); + new RemoveExecutor("my-service").Execute(Context); + Console.ToString().Trim().ShouldBe("Removed dummy-svc service 'my-service'"); + Context.Configuration.GetServices().ShouldNotContain("my-service"); + Context.Configuration.GetServices().ShouldContain("my-other-service"); } [Fact] - public void TestRemoveNonExistentService() + public void TestRemoveUnknownItem() { - var svc = new RemoveServiceExecutor("non-existent-service"); - Assert.Throws( - () => svc.Execute(Context) + var e = Assert.Throws( + () => new RemoveExecutor("no-such-item").Execute(Context) ); + e.Name.ShouldBe("no-such-item"); + e.Description.ShouldBe("app or service"); } } } diff --git a/test/Steeltoe.Tooling.Test/Executor/SetArgsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/SetArgsExecutorTest.cs new file mode 100644 index 0000000..e4d9827 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/SetArgsExecutorTest.cs @@ -0,0 +1,66 @@ +// 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 Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class SetArgsExecutorTest : ToolingTest + { + [Fact] + public void TestSetServiceArgs() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + ClearConsole(); + new SetArgsExecutor("my-service", "dummy-target", "arg1 arg2").Execute(Context); + Console.ToString().Trim().ShouldBe("Service 'my-service' args for target 'dummy-target' set to 'arg1 arg2'"); + } + [Fact] + + public void TestSetServiceArgsForce() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + ClearConsole(); + new SetArgsExecutor("my-service", "dummy-target", "arg1 arg2").Execute(Context); + var e = Assert.Throws( + () => new SetArgsExecutor("my-service", "dummy-target", "arg1 arg2").Execute(Context) + ); + e.Message.ShouldBe("Service 'my-service' args for target 'dummy-target' already set to 'arg1 arg2'"); + ClearConsole(); + new SetArgsExecutor("my-service", "dummy-target", "arg3", true).Execute(Context); + Console.ToString().Trim().ShouldBe("Service 'my-service' args for target 'dummy-target' set to 'arg3'"); + } + + [Fact] + public void TestSetServiceArgsUnknownService() + { + Assert.Throws( + () => new SetArgsExecutor("no-such-svc", "dummy-target", null).Execute(Context) + ); + } + + [Fact] + public void TestSetServiceArgsUnknownTarget() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + var e = Assert.Throws( + () => new SetArgsExecutor("my-service", "no-such-target", null).Execute(Context) + ); + e.Name.ShouldBe("no-such-target"); + e.Description.ShouldBe("target"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/SetServiceDeploymentArgsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/SetServiceDeploymentArgsExecutorTest.cs deleted file mode 100644 index f6eabf0..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/SetServiceDeploymentArgsExecutorTest.cs +++ /dev/null @@ -1,56 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class SetServiceDeploymentArgsExecutorTest : ToolingTest - { - [Fact] - public void TestSetServiceDeploymentArgs() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - ClearConsole(); - new SetServiceDeploymentArgsExecutor("dummy-env", "a-service", "arg1 arg2").Execute(Context); - Console.ToString().Trim().ShouldBe("Set the 'dummy-env' deployment environment arguments for service 'a-service' to 'arg1 arg2'"); - } - [Fact] - - public void TestSetServiceDeploymentArgsForce() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - ClearConsole(); - new SetServiceDeploymentArgsExecutor("dummy-env", "a-service", "arg1 arg2").Execute(Context); - var e = Assert.Throws( - () => new SetServiceDeploymentArgsExecutor("dummy-env", "a-service", "arg1 arg2").Execute(Context) - ); - e.Message.ShouldBe("'dummy-env' deployment environment arguments for service 'a-service' already set."); - ClearConsole(); - new SetServiceDeploymentArgsExecutor("dummy-env", "a-service", "arg1 arg2", true).Execute(Context); - Console.ToString().Trim().ShouldBe("Set the 'dummy-env' deployment environment arguments for service 'a-service' to 'arg1 arg2'"); - } - - - [Fact] - public void TestSetNonExistentServiceDeploymentArgs() - { - Assert.Throws( - () => new SetServiceDeploymentArgsExecutor("dummy-env", "non-existent-service", null).Execute(Context) - ); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/SetTargetExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/SetTargetExecutorTest.cs index dde58d0..f953637 100644 --- a/test/Steeltoe.Tooling.Test/Executor/SetTargetExecutorTest.cs +++ b/test/Steeltoe.Tooling.Test/Executor/SetTargetExecutorTest.cs @@ -21,22 +21,21 @@ namespace Steeltoe.Tooling.Test.Executor public class SetTargetExecutorTest : ToolingTest { [Fact] - public void TestSetTargetEnvironment() + public void TestSetTarget() { - Context.ToolingConfiguration.EnvironmentName = null; - new SetTargetExecutor("dummy-env").Execute(Context); - Console.ToString().Trim().ShouldContain("Target deployment environment set to 'dummy-env'."); - Context.ToolingConfiguration.EnvironmentName.ShouldBe("dummy-env"); + Context.Configuration.Target = null; + new SetTargetExecutor("dummy-target").Execute(Context); + Console.ToString().Trim().ShouldContain("Target set to 'dummy-target'"); + Context.Configuration.Target.ShouldBe("dummy-target"); } [Fact] - public void TestSetTargetUnknownEnvironment() + public void TestSetUnknownTarget() { - var svc = new SetTargetExecutor("unknown-environment"); var e = Assert.Throws( - () => svc.Execute(Context) + () => new SetTargetExecutor("no-such-target").Execute(Context) ); - e.Message.ShouldBe("Unknown deployment environment 'unknown-environment'"); + e.Message.ShouldBe("Unknown target 'no-such-target'"); } } } diff --git a/test/Steeltoe.Tooling.Test/Executor/StatusExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/StatusExecutorTest.cs new file mode 100644 index 0000000..6a874b7 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/StatusExecutorTest.cs @@ -0,0 +1,74 @@ +// 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 Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class StatusExecutorTest : ToolingTest + { + [Fact] + public void TestStatusServices() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + Context.Configuration.AddService("another-service", "dummy-svc"); + ClearConsole(); + new StatusExecutor().Execute(Context); + Console.ToString().ShouldContain("my-service offline"); + Console.ToString().ShouldContain("another-service offline"); + + new DeployExecutor().Execute(Context); + ClearConsole(); + new StatusExecutor().Execute(Context); + Console.ToString().ShouldContain("my-service starting"); + Console.ToString().ShouldContain("another-service starting"); + + ClearConsole(); + new StatusExecutor().Execute(Context); + Console.ToString().ShouldContain("my-service online"); + Console.ToString().ShouldContain("another-service online"); + + new UndeployExecutor().Execute(Context); + ClearConsole(); + new StatusExecutor().Execute(Context); + Console.ToString().ShouldContain("my-service stopping"); + Console.ToString().ShouldContain("another-service stopping"); + + ClearConsole(); + new StatusExecutor().Execute(Context); + Console.ToString().ShouldContain("my-service offline"); + Console.ToString().ShouldContain("another-service offline"); + } + + [Fact] + public void TestStatusNoServices() + { + new StatusExecutor().Execute(Context); + Console.ToString().Trim().ShouldBeEmpty(); + } + + [Fact] + public void TestStatusNoTarget() + { + Context.Configuration.Target = null; + Context.Configuration.AddService("my-service", "dummy-svc"); + var e = Assert.Throws( + () => new StatusExecutor().Execute(Context) + ); + e.Message.ShouldBe("Target not set"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/StatusServiceExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/StatusServiceExecutorTest.cs deleted file mode 100644 index a4289fb..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/StatusServiceExecutorTest.cs +++ /dev/null @@ -1,77 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class StatusServiceExecutorTest : ToolingTest - { - [Fact] - public void TestStatusServices() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-service"); - Context.ServiceManager.AddService("defunct-service", "dummy-svc"); - Context.ServiceManager.DisableService("defunct-service"); - ClearConsole(); - new StatusServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("a-service offline"); - Console.ToString().ShouldContain("defunct-service disabled"); - - new DeployServicesExecutor().Execute(Context); - ClearConsole(); - new StatusServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("a-service starting"); - Console.ToString().ShouldContain("defunct-service disabled"); - - ClearConsole(); - new StatusServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("a-service online"); - Console.ToString().ShouldContain("defunct-service disabled"); - - new UndeployServicesExecutor().Execute(Context); - ClearConsole(); - new StatusServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("a-service stopping"); - Console.ToString().ShouldContain("defunct-service disabled"); - - ClearConsole(); - new StatusServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("a-service offline"); - Console.ToString().ShouldContain("defunct-service disabled"); - } - - [Fact] - public void TestStatusNoServices() - { - new StatusServicesExecutor().Execute(Context); - Console.ToString().Trim().ShouldBeEmpty(); - } - - [Fact] - public void TestStatusNoTarget() - { - Context.ToolingConfiguration.EnvironmentName = null; - Context.ServiceManager.AddService("a-existing-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-existing-service"); - var e = Assert.Throws( - () => new StatusServicesExecutor().Execute(Context) - ); - e.Message.ShouldBe("Target deployment environment not set"); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/Executor/UndeployApplicationsExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/UndeployApplicationsExecutorTest.cs new file mode 100644 index 0000000..b124b3a --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/UndeployApplicationsExecutorTest.cs @@ -0,0 +1,49 @@ +// 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 Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class UndeployApplicationsExecutorTest : ToolingTest + { + [Fact] + public void TestUndeployApplications() + { +// Context.ApplicationManager.AddApplication("an-app"); +// ClearConsole(); +// new UndeployApplicationsExecutor().Execute(Context); +// Console.ToString().ShouldContain("Undeploying application 'an-app'"); +// Context.ApplicationManager.GetApplicationState("an-app").ShouldBe(Lifecycle.State.Offline); + } + + [Fact] + public void TestUndeployNoApplications() + { +// new UndeployApplicationsExecutor().Execute(Context); +// Console.ToString().Trim().ShouldBeEmpty(); + } + + [Fact] + public void TestUndeployNoTarget() + { +// Context.Configuration.EnvironmentName = null; +// Context.Configuration.AddService("a-existing-service", "dummy-svc"); +// var e = Assert.Throws( +// () => new UndeployApplicationsExecutor().Execute(Context) +// ); +// e.Message.ShouldBe("Target deployment environment not set"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/UndeployExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/UndeployExecutorTest.cs new file mode 100644 index 0000000..b46ab83 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/Executor/UndeployExecutorTest.cs @@ -0,0 +1,61 @@ +// 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 Shouldly; +using Steeltoe.Tooling.Executor; +using Xunit; + +namespace Steeltoe.Tooling.Test.Executor +{ + public class UndeployExecutorTest : ToolingTest + { + [Fact] + public void TestUndeploy() + { + Context.Configuration.AddApp("my-app"); + Context.Configuration.AddService("my-service", "dummy-svc"); + Context.Configuration.AddService("my-other-service", "dummy-svc"); + new DeployExecutor().Execute(Context); + Context.Backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Online); + Context.Backend.GetServiceStatus("my-other-service").ShouldBe(Lifecycle.Status.Online); + Context.Backend.GetAppStatus("my-app").ShouldBe(Lifecycle.Status.Starting); + ClearConsole(); + new UndeployExecutor().Execute(Context); + Console.ToString().ShouldContain("Undeploying app 'my-app'"); + Console.ToString().ShouldContain("Undeploying service 'my-service'"); + Console.ToString().ShouldContain("Undeploying service 'my-other-service'"); + Context.Backend.GetServiceStatus("my-service").ShouldBe(Lifecycle.Status.Stopping); + Context.Backend.GetServiceStatus("my-other-service").ShouldBe(Lifecycle.Status.Stopping); + Context.Backend.GetAppStatus("my-app").ShouldBe(Lifecycle.Status.Stopping); + } + + [Fact] + public void TestUndeployNothing() + { + new UndeployExecutor().Execute(Context); + Console.ToString().Trim().ShouldBeEmpty(); + } + + [Fact] + public void TestUndeployNoTarget() + { + Context.Configuration.Target = null; + Context.Configuration.AddService("a-existing-service", "dummy-svc"); + var e = Assert.Throws( + () => new UndeployExecutor().Execute(Context) + ); + e.Message.ShouldBe("Target not set"); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/Executor/UndeployServiceExecutorTest.cs b/test/Steeltoe.Tooling.Test/Executor/UndeployServiceExecutorTest.cs deleted file mode 100644 index 763e038..0000000 --- a/test/Steeltoe.Tooling.Test/Executor/UndeployServiceExecutorTest.cs +++ /dev/null @@ -1,66 +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 Shouldly; -using Steeltoe.Tooling.Executor; -using Xunit; - -namespace Steeltoe.Tooling.Test.Executor -{ - public class UndeployServiceExecutorTest : ToolingTest - { - [Fact] - public void TestUndeployService() - { - Context.ServiceManager.AddService("a-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-service"); - Context.ServiceManager.AddService("defunct-service", "dummy-svc"); - Context.ServiceManager.DisableService("defunct-service"); - ClearConsole(); - new UndeployServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("Undeploying service 'a-service'"); - Console.ToString().ShouldContain("Ignoring disabled service 'defunct-service'"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - Context.ServiceManager.GetServiceState("defunct-service").ShouldBe(ServiceLifecycle.State.Disabled); - - new DeployServicesExecutor().Execute(Context); - new StatusServicesExecutor().Execute(Context); - ClearConsole(); - new UndeployServicesExecutor().Execute(Context); - Console.ToString().ShouldContain("Undeploying service 'a-service"); - Console.ToString().ShouldContain("Ignoring disabled service 'defunct-service'"); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Stopping); - Context.ServiceManager.GetServiceState("defunct-service").ShouldBe(ServiceLifecycle.State.Disabled); - } - - [Fact] - public void TestUndeployNoServices() - { - new UndeployServicesExecutor().Execute(Context); - Console.ToString().Trim().ShouldBeEmpty(); - } - - [Fact] - public void TestUndeployNoTarget() - { - Context.ToolingConfiguration.EnvironmentName = null; - Context.ServiceManager.AddService("a-existing-service", "dummy-svc"); - Context.ServiceManager.EnableService("a-existing-service"); - var e = Assert.Throws( - () => new UndeployServicesExecutor().Execute(Context) - ); - e.Message.ShouldBe("Target deployment environment not set"); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/ServiceLifecycleExceptionTest.cs b/test/Steeltoe.Tooling.Test/LifecycleExceptionTest.cs similarity index 70% rename from test/Steeltoe.Tooling.Test/ServiceLifecycleExceptionTest.cs rename to test/Steeltoe.Tooling.Test/LifecycleExceptionTest.cs index d1a1e8e..b1e4d12 100644 --- a/test/Steeltoe.Tooling.Test/ServiceLifecycleExceptionTest.cs +++ b/test/Steeltoe.Tooling.Test/LifecycleExceptionTest.cs @@ -17,20 +17,20 @@ using Xunit; namespace Steeltoe.Tooling.Test { - public class ServiceLifecycleExceptionTest + public class LifecycleExceptionTest { [Fact] public void TestMessage() { - var e = new ServiceLifecycleException(ServiceLifecycle.State.Offline); - e.Message.ShouldBe($"Invalid service status 'offline'"); + var e = new LifecycleException(Lifecycle.Status.Offline); + e.Message.ShouldBe($"Invalid status 'offline'"); } [Fact] public void TestState() { - var e = new ServiceLifecycleException(ServiceLifecycle.State.Offline); - e.State.ShouldBe(ServiceLifecycle.State.Offline); + var e = new LifecycleException(Lifecycle.Status.Offline); + e.Status.ShouldBe(Lifecycle.Status.Offline); } } } diff --git a/test/Steeltoe.Tooling.Test/LifecyleTest.cs b/test/Steeltoe.Tooling.Test/LifecyleTest.cs new file mode 100644 index 0000000..14ee643 --- /dev/null +++ b/test/Steeltoe.Tooling.Test/LifecyleTest.cs @@ -0,0 +1,54 @@ +// 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 Shouldly; +using Xunit; + +namespace Steeltoe.Tooling.Test +{ + public class LifecyleTest : ToolingTest + { + [Fact] + public void TestServiceStateMachine() + { + Context.Configuration.AddService("my-service", "dummy-svc"); + var lifecycle = new Lifecycle(Context, "my-service"); + TestLifecycle(lifecycle); + } + + private void TestLifecycle(Lifecycle lifecycle) + { + // start state -> offline + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Offline); + + // offline -> undeploy -> offline + lifecycle.Undeploy(); + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Offline); + + // offline -> deploy -> starting -> online + lifecycle.Deploy(); + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Starting); + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Online); + + // online -> deploy -> online + lifecycle.Deploy(); + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Online); + + // online -> undeploy -> stopping -> offline + lifecycle.Undeploy(); + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Stopping); + lifecycle.GetStatus().ShouldBe(Lifecycle.Status.Offline); + } + } +} diff --git a/test/Steeltoe.Tooling.Test/RegistryConfigurationTest.cs b/test/Steeltoe.Tooling.Test/RegistryConfigurationTest.cs index 49c5d96..3ad0296 100644 --- a/test/Steeltoe.Tooling.Test/RegistryConfigurationTest.cs +++ b/test/Steeltoe.Tooling.Test/RegistryConfigurationTest.cs @@ -19,54 +19,53 @@ namespace Steeltoe.Tooling.Test { public class RegistryConfigurationTest { + private Registry.Configuration _registryCfg = new Registry.Configuration(); + [Fact] public void TestDefineServiceType() { - var cfg = new RegistryConfiguration(); - cfg.DefineServiceType("my-service", 1234, "my service description"); - cfg.DefineServiceType("my-other-service", 4321, "my other service description"); - cfg.ServiceTypes.Count.ShouldBe(2); - cfg.ServiceTypes["my-service"].ShouldNotBeNull(); - cfg.ServiceTypes["my-service"].Port.ShouldBe(1234); - cfg.ServiceTypes["my-service"].Description.ShouldBe("my service description"); - cfg.ServiceTypes["my-other-service"].ShouldNotBeNull(); - cfg.ServiceTypes["my-other-service"].Port.ShouldBe(4321); - cfg.ServiceTypes["my-other-service"].Description.ShouldBe("my other service description"); + _registryCfg.DefineServiceType("my-service", 1234, "my service description"); + _registryCfg.DefineServiceType("my-other-service", 4321, "my other service description"); + _registryCfg.ServiceTypes.Count.ShouldBe(2); + _registryCfg.ServiceTypes["my-service"].ShouldNotBeNull(); + _registryCfg.ServiceTypes["my-service"].Port.ShouldBe(1234); + _registryCfg.ServiceTypes["my-service"].Description.ShouldBe("my service description"); + _registryCfg.ServiceTypes["my-other-service"].ShouldNotBeNull(); + _registryCfg.ServiceTypes["my-other-service"].Port.ShouldBe(4321); + _registryCfg.ServiceTypes["my-other-service"].Description.ShouldBe("my other service description"); } [Fact] public void TestDefineEnvironment() { - var cfg = new RegistryConfiguration(); - cfg.DefineEnvironment("my-env", "my environment"); - cfg.DefineEnvironment("my-other-env", "my other environment"); - cfg.EnvironmentConfigurations.Count.ShouldBe(2); - cfg.EnvironmentConfigurations["my-env"].ShouldNotBeNull(); - cfg.EnvironmentConfigurations["my-env"].Description.ShouldBe("my environment"); - cfg.EnvironmentConfigurations["my-other-env"].ShouldNotBeNull(); - cfg.EnvironmentConfigurations["my-other-env"].Description.ShouldBe("my other environment"); + _registryCfg.DefineTarget("my-env", "my environment"); + _registryCfg.DefineTarget("my-other-env", "my other environment"); + _registryCfg.TargetConfigurations.Count.ShouldBe(2); + _registryCfg.TargetConfigurations["my-env"].ShouldNotBeNull(); + _registryCfg.TargetConfigurations["my-env"].Description.ShouldBe("my environment"); + _registryCfg.TargetConfigurations["my-other-env"].ShouldNotBeNull(); + _registryCfg.TargetConfigurations["my-other-env"].Description.ShouldBe("my other environment"); } [Fact] public void TestDefineEnvironmentServiceTypeProperty() { - var cfg = new RegistryConfiguration(); - cfg.DefineEnvironment("my-env", "my environment"); - cfg.DefineEnvironmentServiceTypeProperty("my-env", "my-service", "my-prop-a", "my value a"); - cfg.DefineEnvironmentServiceTypeProperty("my-env", "my-service", "my-prop-b", "my value b"); - cfg.DefineEnvironmentServiceTypeProperty("my-env", "my-other-service", "my-other-prop", "my other value"); - cfg.DefineEnvironmentServiceTypeProperty("my-other-env", "my-other-env-service", "my-other-env-prop", + _registryCfg.DefineTarget("my-env", "my environment"); + _registryCfg.DefineTargetServiceTypeProperty("my-env", "my-service", "my-prop-a", "my value a"); + _registryCfg.DefineTargetServiceTypeProperty("my-env", "my-service", "my-prop-b", "my value b"); + _registryCfg.DefineTargetServiceTypeProperty("my-env", "my-other-service", "my-other-prop", "my other value"); + _registryCfg.DefineTargetServiceTypeProperty("my-other-env", "my-other-env-service", "my-other-env-prop", "my other env value"); - cfg.EnvironmentConfigurations.Count.ShouldBe(2); - cfg.EnvironmentConfigurations["my-env"].ShouldNotBeNull(); - cfg.EnvironmentConfigurations["my-env"].ServiceTypeProperties["my-service"].ShouldNotBeNull(); - cfg.EnvironmentConfigurations["my-env"].ServiceTypeProperties["my-service"]["my-prop-a"].ShouldBe("my value a"); - cfg.EnvironmentConfigurations["my-env"].ServiceTypeProperties["my-service"]["my-prop-b"].ShouldBe("my value b"); - cfg.EnvironmentConfigurations["my-env"].ServiceTypeProperties["my-other-service"]["my-other-prop"] + _registryCfg.TargetConfigurations.Count.ShouldBe(2); + _registryCfg.TargetConfigurations["my-env"].ShouldNotBeNull(); + _registryCfg.TargetConfigurations["my-env"].ServiceTypeProperties["my-service"].ShouldNotBeNull(); + _registryCfg.TargetConfigurations["my-env"].ServiceTypeProperties["my-service"]["my-prop-a"].ShouldBe("my value a"); + _registryCfg.TargetConfigurations["my-env"].ServiceTypeProperties["my-service"]["my-prop-b"].ShouldBe("my value b"); + _registryCfg.TargetConfigurations["my-env"].ServiceTypeProperties["my-other-service"]["my-other-prop"] .ShouldBe("my other value"); - cfg.EnvironmentConfigurations["my-other-env"].ShouldNotBeNull(); - cfg.EnvironmentConfigurations["my-other-env"].Description.ShouldBeNull(); - cfg.EnvironmentConfigurations["my-other-env"].ServiceTypeProperties["my-other-env-service"]["my-other-env-prop"] + _registryCfg.TargetConfigurations["my-other-env"].ShouldNotBeNull(); + _registryCfg.TargetConfigurations["my-other-env"].Description.ShouldBeNull(); + _registryCfg.TargetConfigurations["my-other-env"].ServiceTypeProperties["my-other-env-service"]["my-other-env-prop"] .ShouldBe("my other env value"); } } diff --git a/test/Steeltoe.Tooling.Test/RegistryTest.cs b/test/Steeltoe.Tooling.Test/RegistryTest.cs index cba1776..9e89891 100644 --- a/test/Steeltoe.Tooling.Test/RegistryTest.cs +++ b/test/Steeltoe.Tooling.Test/RegistryTest.cs @@ -36,7 +36,7 @@ namespace Steeltoe.Tooling.Test "redis", "zipkin", }; - foreach (var name in Registry.ServiceTypeNames) + foreach (var name in Registry.GetServiceTypes()) { expected.ShouldContain(name); expected.Remove(name); @@ -46,15 +46,15 @@ namespace Steeltoe.Tooling.Test } [Fact] - public void TestEnvironmentNames() + public void TestTargetNames() { var expected = new List() { - "dummy-env", + "dummy-target", "cloud-foundry", "docker" }; - foreach (var name in Registry.EnvironmentNames) + foreach (var name in Registry.Targets) { expected.ShouldContain(name); expected.Remove(name); @@ -64,21 +64,21 @@ namespace Steeltoe.Tooling.Test } [Fact] - public void TestDummyEnvironment() + public void TestDummyTarget() { - Registry.GetEnvironment("dummy-env").ShouldBeOfType(); + Registry.GetTarget("dummy-target").ShouldBeOfType(); } [Fact] - public void TestDockerEnvironment() + public void TestDockerTarget() { - Registry.GetEnvironment("docker").ShouldBeOfType(); + Registry.GetTarget("docker").ShouldBeOfType(); } [Fact] - public void TestCloudFoundryEnvironment() + public void TestCloudFoundryTarget() { - Registry.GetEnvironment("cloud-foundry").ShouldBeOfType(); + Registry.GetTarget("cloud-foundry").ShouldBeOfType(); } } } diff --git a/test/Steeltoe.Tooling.Test/ServiceLifecyleTest.cs b/test/Steeltoe.Tooling.Test/ServiceLifecyleTest.cs deleted file mode 100644 index 558df13..0000000 --- a/test/Steeltoe.Tooling.Test/ServiceLifecyleTest.cs +++ /dev/null @@ -1,80 +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 Shouldly; -using Xunit; - -namespace Steeltoe.Tooling.Test -{ - public class ServiceLifecyleTest : ToolingTest - { - [Fact] - public void TestStateMachine() - { - var svcMgr = Context.ServiceManager; - svcMgr.AddService("a-service", "dummy-svc"); - ServiceLifecycle lifecycle = new ServiceLifecycle(Context, "a-service"); - - // start state -> disabled - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Disabled); - - // disabled -> deploy -> ERROR - lifecycle.Disable(); - Assert.Throws( - () => lifecycle.Deploy() - ); - // disabled -> undeploy -> ERROR - Assert.Throws( - () => lifecycle.Undeploy() - ); - // disabled -> disable -> disabled - lifecycle.Disable(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Disabled); - // disabled -> enable -> enabled - lifecycle.Enable(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - - // offline -> undeploy -> offline - lifecycle.Undeploy(); - Context.ServiceManager.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - // offline -> enable -> offline - lifecycle.Enable(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - // offline -> disable -> disabled - lifecycle.Disable(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Disabled); - // offline -> deploy -> starting -> online - lifecycle.Enable(); - lifecycle.Deploy(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Starting); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Online); - - // online -> enable -> ERROR - Assert.Throws( - () => lifecycle.Enable() - ); - // online -> disable -> ERROR - Assert.Throws( - () => lifecycle.Disable() - ); - // online -> deploy -> online - lifecycle.Deploy(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Online); - // online -> undeploy -> stopping -> offline - lifecycle.Undeploy(); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Stopping); - svcMgr.GetServiceState("a-service").ShouldBe(ServiceLifecycle.State.Offline); - } - } -} diff --git a/test/Steeltoe.Tooling.Test/ServiceManagerTest.cs b/test/Steeltoe.Tooling.Test/ServiceManagerTest.cs deleted file mode 100644 index a839f66..0000000 --- a/test/Steeltoe.Tooling.Test/ServiceManagerTest.cs +++ /dev/null @@ -1,98 +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 Shouldly; -using Xunit; - -namespace Steeltoe.Tooling.Test -{ - public class ServiceManagerTest : ToolingTest - { - private readonly ServiceManager _serviceMgr; - - public ServiceManagerTest() - { - _serviceMgr = new ServiceManager(Context); - } - - [Fact] - public void TestAddService() - { - var svc = _serviceMgr.AddService("a-service", "dummy-svc"); - svc.Name.ShouldBe("a-service"); - svc.Type.ShouldBe("dummy-svc"); - _serviceMgr .GetServiceNames().Count.ShouldBe(1); - _serviceMgr .GetServiceNames()[0].ShouldBe("a-service"); - } - - [Fact] - public void TestRemoveService() - { - _serviceMgr.AddService("a-service", "dummy-svc"); - _serviceMgr.RemoveService("a-service"); - _serviceMgr.HasService("a-service").ShouldBeFalse(); - } - - [Fact] - public void TestRemoveNonExistentService() - { - Assert.Throws( - () => _serviceMgr.RemoveService("non-existent-service") - ); - } - - [Fact] - public void TestGetService() - { - _serviceMgr.AddService("a-service", "dummy-svc"); - _serviceMgr.GetService("a-service").Name.ShouldBe("a-service"); - } - - [Fact] - public void TestGetNonExistentService() - { - Assert.Throws( - () => _serviceMgr.GetService("non-existent-service") - ); - } - - [Fact] - public void TestGetServiceNames() - { - _serviceMgr.AddService("a-service", "dummy-svc"); - _serviceMgr.AddService("another-service", "dummy-svc"); - var names = _serviceMgr.GetServiceNames(); - names.Remove("a-service").ShouldBeTrue(); - names.Remove("another-service").ShouldBeTrue(); - names.Count.ShouldBe(0); - } - - [Fact] - public void TestHasService() - { - _serviceMgr.HasService("a-service").ShouldBeFalse(); - _serviceMgr.AddService("a-service", "dummy-svc"); - _serviceMgr.HasService("a-service").ShouldBeTrue(); - } - - [Fact] - public void TestServiceDeploymentArgs() - { - _serviceMgr.AddService("a-service", "dummy-svc"); - _serviceMgr.SetServiceDeploymentArgs("dummy-env", "a-service", "arg1 arg2"); - _serviceMgr.GetServiceDeploymentArgs("dummy-env", "a-service").ShouldBe("arg1 arg2"); - - } - } -} diff --git a/test/Steeltoe.Tooling.Test/ServiceNotFoundExceptionTest.cs b/test/Steeltoe.Tooling.Test/ServiceNotFoundExceptionTest.cs index 0ee193d..5232dda 100644 --- a/test/Steeltoe.Tooling.Test/ServiceNotFoundExceptionTest.cs +++ b/test/Steeltoe.Tooling.Test/ServiceNotFoundExceptionTest.cs @@ -19,18 +19,24 @@ namespace Steeltoe.Tooling.Test { public class ServiceNotFoundExceptionTest { + private readonly NotFoundException _exception = new NotFoundException("no-such-thing", "thing"); + [Fact] public void TestMessage() { - var e = new ServiceNotFoundException("non-existent-service"); - e.Message.ShouldBe("Service 'non-existent-service' not found"); + _exception.Message.ShouldBe("Unknown thing 'no-such-thing'"); } [Fact] public void TestName() { - var e = new ServiceNotFoundException("non-existent-service"); - e.Name.ShouldBe("non-existent-service"); + _exception.Name.ShouldBe("no-such-thing"); + } + + [Fact] + public void TestDescription() + { + _exception.Description.ShouldBe("thing"); } } } diff --git a/test/Steeltoe.Tooling.Test/ToolingConfigurationFileTest.cs b/test/Steeltoe.Tooling.Test/ToolingConfigurationFileTest.cs deleted file mode 100644 index f05bd65..0000000 --- a/test/Steeltoe.Tooling.Test/ToolingConfigurationFileTest.cs +++ /dev/null @@ -1,102 +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 System; -using System.IO; -using System.Linq; -using Shouldly; -using Xunit; - -namespace Steeltoe.Tooling.Test -{ - public class ToolingConfigurationFileTest : IDisposable - { - private readonly string _testDir; - - private readonly string _testConfigPath; - - private readonly string _defaultConfigPath; - - public ToolingConfigurationFileTest() - { - _testDir = new[] {Path.GetTempPath(), "Steeltoe.Tooling.Test", Guid.NewGuid().ToString()}.Aggregate(Path.Combine); - Directory.CreateDirectory(_testDir); - _testConfigPath = Path.Combine(_testDir, "config-file"); - _defaultConfigPath = Path.Combine(_testDir, ToolingConfigurationFile.DefaultFileName); - } - - public void Dispose() - { - Directory.Delete(_testDir, true); - } - - [Fact] - public void TestStoreToFile() - { - var cfgFile = new ToolingConfigurationFile(_testConfigPath); - cfgFile.ToolingConfiguration.EnvironmentName = "myEnvironment"; - cfgFile.ToolingConfiguration.Services.Add("myService", new ToolingConfiguration.Service - { - ServiceTypeName = "myServiceType", - Enabled = true - }); - cfgFile.Store(); - File.ReadAllText(_testConfigPath).ShouldBe(SampleConfig); - } - - [Fact] - public void TestStoreToDirectory() - { - var cfgFile = new ToolingConfigurationFile(_testDir); - cfgFile.ToolingConfiguration.EnvironmentName = "myEnvironment"; - cfgFile.ToolingConfiguration.Services.Add("myService", new ToolingConfiguration.Service - { - ServiceTypeName = "myServiceType", - Enabled = true - }); - cfgFile.Store(); - File.ReadAllText(_defaultConfigPath).ShouldBe(SampleConfig); - } - - [Fact] - public void TestLoadFromFile() - { - File.WriteAllText(_testConfigPath, SampleConfig); - var cfgFile = new ToolingConfigurationFile(_testConfigPath); - cfgFile.ToolingConfiguration.EnvironmentName.ShouldBe("myEnvironment"); - cfgFile.ToolingConfiguration.Services.ShouldContainKey("myService"); - cfgFile.ToolingConfiguration.Services["myService"].ServiceTypeName.ShouldBe("myServiceType"); - cfgFile.ToolingConfiguration.Services["myService"].Enabled.ShouldBeTrue(); - } - - [Fact] - public void TestLoadFromDirectory() - { - File.WriteAllText(_defaultConfigPath, SampleConfig); - var cfgFile = new ToolingConfigurationFile(_testDir); - cfgFile.ToolingConfiguration.EnvironmentName.ShouldBe("myEnvironment"); - cfgFile.ToolingConfiguration.Services.ShouldContainKey("myService"); - cfgFile.ToolingConfiguration.Services["myService"].ServiceTypeName.ShouldBe("myServiceType"); - cfgFile.ToolingConfiguration.Services["myService"].Enabled.ShouldBeTrue(); - } - - private const string SampleConfig = @"environment: myEnvironment -services: - myService: - type: myServiceType - enabled: true - args: {} -"; - } -} diff --git a/test/Steeltoe.Tooling.Test/ToolingConfigurationTest.cs b/test/Steeltoe.Tooling.Test/ToolingConfigurationTest.cs deleted file mode 100644 index abca5c7..0000000 --- a/test/Steeltoe.Tooling.Test/ToolingConfigurationTest.cs +++ /dev/null @@ -1,42 +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 Shouldly; -using Xunit; - -namespace Steeltoe.Tooling.Test -{ - public class ToolingConfigurationTest - { - [Fact] - public void TestChangeEvent() - { - var cfg = new ToolingConfiguration(); - var listener = new MyListener(); - cfg.AddListener(listener); - cfg.NotifyChanged(); - listener.ReceivedEvent.ShouldBeTrue(); - } - - internal class MyListener : IToolingConfigurationListener - { - internal bool ReceivedEvent { get; set; } - - public void ConfigurationChangeEvent() - { - ReceivedEvent = true; - } - } - } -} diff --git a/test/Steeltoe.Tooling.Test/ToolingTest.cs b/test/Steeltoe.Tooling.Test/ToolingTest.cs index 0296795..52e6d33 100644 --- a/test/Steeltoe.Tooling.Test/ToolingTest.cs +++ b/test/Steeltoe.Tooling.Test/ToolingTest.cs @@ -29,14 +29,16 @@ namespace Steeltoe.Tooling.Test static ToolingTest() { Settings.DummiesEnabled = true; + Settings.ParallelExecutionEnabled = false; + Settings.WaitDuration = 1; } public ToolingTest() { - var cfg = new ToolingConfiguration(); + var cfg = new Configuration(); var path = new[] {"sandboxes", Guid.NewGuid().ToString()}.Aggregate(Path.Combine); Directory.CreateDirectory(path); - cfg.EnvironmentName = "dummy-env"; + cfg.Target = "dummy-target"; Console = new StringWriter(); Shell = new MockShell(); Context = new Context(path, cfg, Console, Shell);