diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index 9aaa70a..0000000 --- a/.rubocop.yml +++ /dev/null @@ -1,19 +0,0 @@ -AllCops: - Exclude: - - 'spec/**/*' - - 'vendor/**/*' - TargetRubyVersion: 2.4.1 -Metrics/AbcSize: - Max: 30 -Metrics/LineLength: - Max: 150 -Metrics/MethodLength: - Max: 30 -Style/Documentation: - Enabled: false -Style/SignalException: - Enabled: false -Layout/SpaceAroundEqualsInParameterDefault: - EnforcedStyle: no_space -Layout/SpaceInsideBrackets: - Enabled: false diff --git a/Gemfile b/Gemfile index 5f10ba8..b4e2a20 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,3 @@ -# frozen_string_literal: true +source "https://rubygems.org" -source 'https://rubygems.org' gemspec diff --git a/Rakefile b/Rakefile index eb60b1f..997c19c 100644 --- a/Rakefile +++ b/Rakefile @@ -1,21 +1,27 @@ -# frozen_string_literal: true +require "bundler/gem_tasks" -require 'bundler/gem_tasks' -require 'rspec/core/rake_task' -require 'rubocop/rake_task' +begin + require "rspec/core/rake_task" -RSpec::Core::RakeTask.new(:spec) -RuboCop::RakeTask.new(:style) + RSpec::Core::RakeTask.new do |t| + t.pattern = "spec/**/*_spec.rb" + end +rescue LoadError + desc "rspec is not installed, this task is disabled" + task :spec do + abort "rspec is not installed. bundle install first to make sure all dependencies are installed." + end +end begin - require 'github_changelog_generator/task' - - GitHubChangelogGenerator::RakeTask.new :changelog do |config| - config.future_release = KnifeVrealize::VERSION - config.issues = true + require "chefstyle" + require "rubocop/rake_task" + desc "Run Chefstyle tests" + RuboCop::RakeTask.new(:style) do |task| + task.options += ["--display-cop-names", "--no-color"] end rescue LoadError - puts 'github_changelog_generator is not available. gem install github_changelog_generator to generate changelogs' + puts "chefstyle gem is not installed. bundle install first to make sure all dependencies are installed." end task default: %i[spec style] diff --git a/knife-vrealize.gemspec b/knife-vrealize.gemspec index cc94ba7..fd1d2af 100644 --- a/knife-vrealize.gemspec +++ b/knife-vrealize.gemspec @@ -1,33 +1,32 @@ # coding: utf-8 # frozen_string_literal: true -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'knife-vrealize/version' +require "knife-vrealize/version" Gem::Specification.new do |spec| - spec.name = 'knife-vrealize' + spec.name = "knife-vrealize" spec.version = KnifeVrealize::VERSION - spec.authors = ['Chef Partner Engineering'] - spec.email = ['partnereng@chef.io'] - spec.summary = 'Knife plugin to interact with VMware vRealize.' + spec.authors = ["Chef Software"] + spec.email = ["oss@chef.io"] + spec.summary = "Chef Infra Knife plugin to interact with VMware vRealize." spec.description = spec.summary - spec.homepage = 'https://github.com/chef-partners/knife-vrealize' - spec.license = 'Apache 2.0' + spec.homepage = "https://github.com/chef/knife-vrealize" + spec.license = "Apache-2.0" spec.files = `git ls-files -z`.split("\x0") spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ['lib'] + spec.require_paths = ["lib"] - spec.add_dependency 'cookstyle' - spec.add_dependency 'knife-cloud', '~> 1.2.0' - spec.add_dependency 'vmware-vra', '~> 2' - spec.add_dependency 'vcoworkflows', '~> 0.2' - spec.add_dependency 'rb-readline', '~> 0.5' + spec.add_dependency "cookstyle" + spec.add_dependency "knife-cloud", "~> 1.2.0" + spec.add_dependency "vmware-vra", "~> 2" + spec.add_dependency "vcoworkflows", "~> 0.2" + spec.add_dependency "rb-readline", "~> 0.5" - spec.add_development_dependency 'bundler', '~> 1.7' - spec.add_development_dependency 'rake', '~> 10.0' - spec.add_development_dependency 'rubocop', '~> 0.35' + spec.add_development_dependency "rake", ">= 10.0" + spec.add_development_dependency "chefstyle", "~> 0.13.3" end diff --git a/lib/chef/knife/cloud/vra_service.rb b/lib/chef/knife/cloud/vra_service.rb index 9c9e350..8a30dc6 100644 --- a/lib/chef/knife/cloud/vra_service.rb +++ b/lib/chef/knife/cloud/vra_service.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,18 +18,18 @@ # limitations under the License. # -require 'chef/knife/cloud/exceptions' -require 'chef/knife/cloud/service' -require 'chef/knife/cloud/helpers' -require 'chef/knife/cloud/vra_service_helpers' -require 'vra' +require "chef/knife/cloud/exceptions" +require "chef/knife/cloud/service" +require "chef/knife/cloud/helpers" +require "chef/knife/cloud/vra_service_helpers" +require "vra" class Chef class Knife class Cloud class VraService < Service include VraServiceHelpers - def initialize(options={}) + def initialize(options = {}) super(options) @username = options[:username] @@ -51,7 +51,7 @@ def connection ) end - def create_server(options={}) + def create_server(options = {}) submitted_request = catalog_request(options).submit ui.msg("Catalog request #{submitted_request.id} submitted.") wait_for_request(submitted_request, options[:wait_time].to_i, options[:refresh_rate]) @@ -61,8 +61,8 @@ def create_server(options={}) raise CloudExceptions::ServerCreateError, submitted_request.completion_details if submitted_request.failed? servers = submitted_request.resources.select(&:vm?) - raise CloudExceptions::ServerCreateError, 'The vRA request created more than one server, but we were only expecting 1' if servers.length > 1 - raise CloudExceptions::ServerCreateError, 'The vRA request did not create any servers' if servers.length.zero? + raise CloudExceptions::ServerCreateError, "The vRA request created more than one server, but we were only expecting 1" if servers.length > 1 + raise CloudExceptions::ServerCreateError, "The vRA request did not create any servers" if servers.length == 0 servers.first end @@ -70,19 +70,19 @@ def create_server(options={}) def delete_server(instance_id) server = get_server(instance_id) server_summary(server) - ui.msg('') + ui.msg("") - if server.status == 'DELETED' + if server.status == "DELETED" ui.warn("Server is already deleted.\n") return end - ui.confirm('Do you really want to delete this server') + ui.confirm("Do you really want to delete this server") destroy_request = server.destroy ui.msg("Destroy request #{destroy_request.id} submitted.") wait_for_request(destroy_request) - ui.msg('Destroy request complete.') + ui.msg("Destroy request complete.") request_summary(destroy_request) end @@ -102,22 +102,22 @@ def get_server(instance_id) connection.resources.by_id(instance_id) end - def server_summary(server, _columns_with_info=nil) - msg_pair('Server ID', server.id) - msg_pair('Server Name', server.name) - msg_pair('IP Addresses', server.ip_addresses.nil? ? 'none' : server.ip_addresses.join(', ')) - msg_pair('Status', server.status) - msg_pair('Catalog Name', server.catalog_name) - msg_pair('Owner IDs', server.owner_ids.empty? ? 'none' : server.owner_ids.join(', ')) - msg_pair('Owner Names', server.owner_names.empty? ? 'none' : server.owner_names.join(', ')) + def server_summary(server, _columns_with_info = nil) + msg_pair("Server ID", server.id) + msg_pair("Server Name", server.name) + msg_pair("IP Addresses", server.ip_addresses.nil? ? "none" : server.ip_addresses.join(", ")) + msg_pair("Status", server.status) + msg_pair("Catalog Name", server.catalog_name) + msg_pair("Owner IDs", server.owner_ids.empty? ? "none" : server.owner_ids.join(", ")) + msg_pair("Owner Names", server.owner_names.empty? ? "none" : server.owner_names.join(", ")) end def request_summary(request) - ui.msg('') - msg_pair('Request Status', request.status) - msg_pair('Completion State', request.completion_state) - msg_pair('Completion Details', request.completion_details) - ui.msg('') + ui.msg("") + msg_pair("Request Status", request.status) + msg_pair("Completion State", request.completion_state) + msg_pair("Completion Details", request.completion_details) + ui.msg("") end def catalog_request(options) diff --git a/lib/chef/knife/cloud/vra_service_helpers.rb b/lib/chef/knife/cloud/vra_service_helpers.rb index 001f8a3..3c0b5f4 100644 --- a/lib/chef/knife/cloud/vra_service_helpers.rb +++ b/lib/chef/knife/cloud/vra_service_helpers.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +18,7 @@ # limitations under the License. # -require 'chef/knife/cloud/helpers' +require "chef/knife/cloud/helpers" class Chef class Knife @@ -39,10 +39,10 @@ def verify_ssl? !locate_config_value(:vra_disable_ssl_verify) end - def wait_for_request(request, wait_time=600, refresh_rate=2) - print 'Waiting for request to complete.' + def wait_for_request(request, wait_time = 600, refresh_rate = 2) + print "Waiting for request to complete." - last_status = '' + last_status = "" begin Timeout.timeout(wait_time) do @@ -55,7 +55,7 @@ def wait_for_request(request, wait_time=600, refresh_rate=2) end if last_status == request.status - print '.' + print "." else last_status = request.status print "\n" @@ -66,7 +66,7 @@ def wait_for_request(request, wait_time=600, refresh_rate=2) end end rescue Timeout::Error - ui.msg('') + ui.msg("") ui.error("Request did not complete in #{wait_time} seconds. Check the Requests tab in the vRA UI for more information.") exit 1 end @@ -81,7 +81,7 @@ def check_for_missing_config_values!(*keys) missing = keys.select { |x| locate_config_value(x).nil? } unless missing.empty? - ui.error("The following required parameters are missing: #{missing.join(', ')}") + ui.error("The following required parameters are missing: #{missing.join(", ")}") exit(1) end end diff --git a/lib/chef/knife/cloud/vra_service_options.rb b/lib/chef/knife/cloud/vra_service_options.rb index f211cc6..3e3f8ba 100644 --- a/lib/chef/knife/cloud/vra_service_options.rb +++ b/lib/chef/knife/cloud/vra_service_options.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,37 +27,37 @@ module VraServiceOptions def self.included(includer) includer.class_eval do option :vra_base_url, - long: '--vra-base-url API_URL', - description: 'URL for the vRA server', - proc: proc { |url| url.sub(%r{/(\/)+$/}, '') } + long: "--vra-base-url API_URL", + description: "URL for the vRA server", + proc: proc { |url| url.sub(%r{/(\/)+$/}, "") } option :vra_username, - long: '--vra-username USERNAME', - description: 'Username to use with the vRA API' + long: "--vra-username USERNAME", + description: "Username to use with the vRA API" option :vra_password, - long: '--vra-password PASSWORD', - description: 'Password to use with the vRA API' + long: "--vra-password PASSWORD", + description: "Password to use with the vRA API" option :vra_tenant, - long: '--vra-tenant TENANT', - description: 'Tenant name (organization) to use with the vRA API' + long: "--vra-tenant TENANT", + description: "Tenant name (organization) to use with the vRA API" option :vra_disable_ssl_verify, - long: '--vra-disable-ssl-verify', - description: 'Skip any SSL verification for the vRA API', + long: "--vra-disable-ssl-verify", + description: "Skip any SSL verification for the vRA API", boolean: true, default: false option :vra_page_size, - long: '--page-size NUM_OF_ITEMS', - description: 'Maximum number of items to fetch from the vRA API when pagination is forced', + long: "--page-size NUM_OF_ITEMS", + description: "Maximum number of items to fetch from the vRA API when pagination is forced", default: 200, proc: proc { |page_size| page_size.to_i } option :request_refresh_rate, - long: '--request-refresh-rate SECS', - description: 'Number of seconds to sleep between each check of the request status, defaults to 2', + long: "--request-refresh-rate SECS", + description: "Number of seconds to sleep between each check of the request status, defaults to 2", default: 2, proc: proc { |secs| secs.to_i } end diff --git a/lib/chef/knife/vra_catalog_list.rb b/lib/chef/knife/vra_catalog_list.rb index cd5ca66..fe4f36a 100644 --- a/lib/chef/knife/vra_catalog_list.rb +++ b/lib/chef/knife/vra_catalog_list.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,11 +18,11 @@ # limitations under the License. # -require 'chef/knife' -require 'chef/knife/cloud/list_resource_command' -require 'chef/knife/cloud/vra_service' -require 'chef/knife/cloud/vra_service_helpers' -require 'chef/knife/cloud/vra_service_options' +require "chef/knife" +require "chef/knife/cloud/list_resource_command" +require "chef/knife/cloud/vra_service" +require "chef/knife/cloud/vra_service_helpers" +require "chef/knife/cloud/vra_service_options" class Chef class Knife @@ -31,24 +31,24 @@ class VraCatalogList < ResourceListCommand include VraServiceHelpers include VraServiceOptions - banner 'knife vra catalog list' + banner "knife vra catalog list" option :entitled, - long: '--entitled-only', - description: 'only list entitled vRA catalog entries', - boolean: true, - default: false + long: "--entitled-only", + description: "only list entitled vRA catalog entries", + boolean: true, + default: false def before_exec_command @columns_with_info = [ - { label: 'Catalog ID', key: 'id' }, - { label: 'Name', key: 'name' }, - { label: 'Description', key: 'description' }, - { label: 'Status', key: 'status', value_callback: method(:format_status_value) }, - { label: 'Subtenant', key: 'subtenant_name' } + { label: "Catalog ID", key: "id" }, + { label: "Name", key: "name" }, + { label: "Description", key: "description" }, + { label: "Status", key: "status", value_callback: method(:format_status_value) }, + { label: "Subtenant", key: "subtenant_name" }, ] - @sort_by_field = 'name' + @sort_by_field = "name" end def query_resource @@ -57,7 +57,7 @@ def query_resource def format_status_value(status) status = status.downcase - color = if status == 'published' + color = if status == "published" :green else :red diff --git a/lib/chef/knife/vra_server_create.rb b/lib/chef/knife/vra_server_create.rb index 71ab542..3bd5fc0 100644 --- a/lib/chef/knife/vra_server_create.rb +++ b/lib/chef/knife/vra_server_create.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,12 +18,12 @@ # limitations under the License. # -require 'chef/knife' -require 'chef/knife/cloud/server/create_command' -require 'chef/knife/cloud/server/create_options' -require 'chef/knife/cloud/vra_service' -require 'chef/knife/cloud/vra_service_helpers' -require 'chef/knife/cloud/vra_service_options' +require "chef/knife" +require "chef/knife/cloud/server/create_command" +require "chef/knife/cloud/server/create_options" +require "chef/knife/cloud/vra_service" +require "chef/knife/cloud/vra_service_helpers" +require "chef/knife/cloud/vra_service_options" class Chef class Knife @@ -33,57 +33,57 @@ class VraServerCreate < ServerCreateCommand include VraServiceOptions include ServerCreateOptions - banner 'knife vra server create CATALOG_ID (options)' + banner "knife vra server create CATALOG_ID (options)" option :cpus, - long: '--cpus NUM_CPUS', - description: 'Number of CPUs the server should have' + long: "--cpus NUM_CPUS", + description: "Number of CPUs the server should have" option :node_ssl_verify_mode, - long: '--node-ssl-verify-mode [peer|none]', - description: 'Whether or not to verify the SSL cert for all HTTPS requests when bootstrapping' + long: "--node-ssl-verify-mode [peer|none]", + description: "Whether or not to verify the SSL cert for all HTTPS requests when bootstrapping" option :memory, - long: '--memory RAM_IN_MB', - description: 'Amount of RAM, in MB, the server should have' + long: "--memory RAM_IN_MB", + description: "Amount of RAM, in MB, the server should have" option :requested_for, - long: '--requested-for LOGIN', - description: 'The login to list as the owner of this resource. Will default to the vra_username parameter' + long: "--requested-for LOGIN", + description: "The login to list as the owner of this resource. Will default to the vra_username parameter" option :server_create_timeout, - long: '--server-create-timeout SECONDS', - description: 'number of seconds to wait for the server to complete', - default: 600 + long: "--server-create-timeout SECONDS", + description: "number of seconds to wait for the server to complete", + default: 600 option :subtenant_id, - long: '--subtenant-id ID', - description: 'The subtenant ID (a.k.a "business group") to list as the owner of this resource. ' \ - 'Will default to the blueprint subtenant if it exists.' + long: "--subtenant-id ID", + description: 'The subtenant ID (a.k.a "business group") to list as the owner of this resource. ' \ + "Will default to the blueprint subtenant if it exists." option :lease_days, - long: '--lease-days NUM_DAYS', - description: 'Number of days requested for the server lease, provided the blueprint allows this to be specified' + long: "--lease-days NUM_DAYS", + description: "Number of days requested for the server lease, provided the blueprint allows this to be specified" option :notes, - long: '--notes NOTES', - description: 'String of text to be included in the request notes.' + long: "--notes NOTES", + description: "String of text to be included in the request notes." option :extra_params, - long: '--extra-param KEY=TYPE:VALUE', - description: 'Additional parameters to pass to vRA for this catalog request. TYPE must be "string" or "integer". ' \ - 'Can be used multiple times.', - default: {}, - proc: proc { |param| - Chef::Config[:knife][:vra_extra_params] ||= {} - key, value_str = param.split('=') - Chef::Config[:knife][:vra_extra_params].merge!(key => value_str) - } + long: "--extra-param KEY=TYPE:VALUE", + description: 'Additional parameters to pass to vRA for this catalog request. TYPE must be "string" or "integer". ' \ + "Can be used multiple times.", + default: {}, + proc: proc { |param| + Chef::Config[:knife][:vra_extra_params] ||= {} + key, value_str = param.split("=") + Chef::Config[:knife][:vra_extra_params].merge!(key => value_str) + } def validate_params! super if @name_args.empty? - ui.error('You must supply a Catalog ID to use for your new server.') + ui.error("You must supply a Catalog ID to use for your new server.") exit 1 end @@ -96,16 +96,16 @@ def before_exec_command super @create_options = { - catalog_id: @name_args.first, - cpus: locate_config_value(:cpus), - memory: locate_config_value(:memory), - requested_for: locate_config_value(:requested_for), - subtenant_id: locate_config_value(:subtenant_id), - lease_days: locate_config_value(:lease_days), - notes: locate_config_value(:notes), - extra_params: extra_params, - wait_time: locate_config_value(:server_create_timeout), - refresh_rate: locate_config_value(:request_refresh_rate) + catalog_id: @name_args.first, + cpus: locate_config_value(:cpus), + memory: locate_config_value(:memory), + requested_for: locate_config_value(:requested_for), + subtenant_id: locate_config_value(:subtenant_id), + lease_days: locate_config_value(:lease_days), + notes: locate_config_value(:notes), + extra_params: extra_params, + wait_time: locate_config_value(:server_create_timeout), + refresh_rate: locate_config_value(:request_refresh_rate), } end @@ -120,7 +120,7 @@ def extra_params return if Chef::Config[:knife][:vra_extra_params].nil? || Chef::Config[:knife][:vra_extra_params].empty? Chef::Config[:knife][:vra_extra_params].each_with_object([]) do |(key, value_str), memo| - type, value = value_str.split(':') + type, value = value_str.split(":") memo << { key: key, type: type, value: value } end end @@ -131,7 +131,7 @@ def validate_extra_params! extra_params.each do |param| raise ArgumentError, "No type and value set for extra parameter #{param[:key]}" if param[:type].nil? || param[:value].nil? raise ArgumentError, "Invalid parameter type for #{param[:key]} - must be string or integer" unless - param[:type] == 'string' || param[:type] == 'integer' + param[:type] == "string" || param[:type] == "integer" end end diff --git a/lib/chef/knife/vra_server_delete.rb b/lib/chef/knife/vra_server_delete.rb index e290dc5..19168ed 100644 --- a/lib/chef/knife/vra_server_delete.rb +++ b/lib/chef/knife/vra_server_delete.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,12 +18,12 @@ # limitations under the License. # -require 'chef/knife' -require 'chef/knife/cloud/server/delete_options' -require 'chef/knife/cloud/server/delete_command' -require 'chef/knife/cloud/vra_service' -require 'chef/knife/cloud/vra_service_helpers' -require 'chef/knife/cloud/vra_service_options' +require "chef/knife" +require "chef/knife/cloud/server/delete_options" +require "chef/knife/cloud/server/delete_command" +require "chef/knife/cloud/vra_service" +require "chef/knife/cloud/vra_service_helpers" +require "chef/knife/cloud/vra_service_options" class Chef class Knife @@ -33,12 +33,12 @@ class VraServerDelete < ServerDeleteCommand include VraServiceHelpers include VraServiceOptions - banner 'knife vra server delete RESOURCE_ID [RESOURCE_ID] (options)' + banner "knife vra server delete RESOURCE_ID [RESOURCE_ID] (options)" # rubocop:disable Style/GuardClause def validate_params! if @name_args.empty? - ui.error('You must supply a resource ID of a server to delete.') + ui.error("You must supply a resource ID of a server to delete.") exit(1) if @name_args.empty? end end diff --git a/lib/chef/knife/vra_server_list.rb b/lib/chef/knife/vra_server_list.rb index 641b126..c219d27 100644 --- a/lib/chef/knife/vra_server_list.rb +++ b/lib/chef/knife/vra_server_list.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,12 +18,12 @@ # limitations under the License. # -require 'chef/knife' -require 'chef/knife/cloud/server/list_command' -require 'chef/knife/cloud/server/list_options' -require 'chef/knife/cloud/vra_service' -require 'chef/knife/cloud/vra_service_helpers' -require 'chef/knife/cloud/vra_service_options' +require "chef/knife" +require "chef/knife/cloud/server/list_command" +require "chef/knife/cloud/server/list_options" +require "chef/knife/cloud/vra_service" +require "chef/knife/cloud/vra_service_helpers" +require "chef/knife/cloud/vra_service_options" class Chef class Knife @@ -32,25 +32,25 @@ class VraServerList < ServerListCommand include VraServiceHelpers include VraServiceOptions - banner 'knife vra server list' + banner "knife vra server list" def before_exec_command @columns_with_info = [ - { label: 'Resource ID', key: 'id' }, - { label: 'Name', key: 'name' }, - { label: 'Status', key: 'status', value_callback: method(:format_status_value) }, - { label: 'Catalog Name', key: 'catalog_name' } + { label: "Resource ID", key: "id" }, + { label: "Name", key: "name" }, + { label: "Status", key: "status", value_callback: method(:format_status_value) }, + { label: "Catalog Name", key: "catalog_name" }, ] - @sort_by_field = 'name' + @sort_by_field = "name" end def format_status_value(status) status = status.downcase status_color = case status - when 'active' + when "active" :green - when 'deleted' + when "deleted" :red else :yellow diff --git a/lib/chef/knife/vra_server_show.rb b/lib/chef/knife/vra_server_show.rb index fc7ce21..78649ee 100644 --- a/lib/chef/knife/vra_server_show.rb +++ b/lib/chef/knife/vra_server_show.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,12 +18,12 @@ # limitations under the License. # -require 'chef/knife' -require 'chef/knife/cloud/server/show_options' -require 'chef/knife/cloud/server/show_command' -require 'chef/knife/cloud/vra_service' -require 'chef/knife/cloud/vra_service_helpers' -require 'chef/knife/cloud/vra_service_options' +require "chef/knife" +require "chef/knife/cloud/server/show_options" +require "chef/knife/cloud/server/show_command" +require "chef/knife/cloud/vra_service" +require "chef/knife/cloud/vra_service_helpers" +require "chef/knife/cloud/vra_service_options" class Chef class Knife @@ -33,16 +33,16 @@ class VraServerShow < ServerShowCommand include VraServiceHelpers include VraServiceOptions - banner 'knife vra server show RESOURCE_ID (options)' + banner "knife vra server show RESOURCE_ID (options)" def validate_params! if @name_args.empty? - ui.error('You must supply a Resource ID for a server to display.') + ui.error("You must supply a Resource ID for a server to display.") exit 1 end if @name_args.size > 1 - ui.error('You may only supply one Resource ID.') + ui.error("You may only supply one Resource ID.") exit 1 end diff --git a/lib/chef/knife/vro_workflow_execute.rb b/lib/chef/knife/vro_workflow_execute.rb index 31d7061..e4a9638 100644 --- a/lib/chef/knife/vro_workflow_execute.rb +++ b/lib/chef/knife/vro_workflow_execute.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,8 +18,8 @@ # limitations under the License. # -require 'chef/knife' -require 'vcoworkflows' +require "chef/knife" +require "vcoworkflows" class Chef class Knife @@ -30,33 +30,33 @@ class VroWorkflowExecute < Chef::Knife include Chef::Knife::Cloud::Helpers - banner 'knife vro workflow execute WORKFLOW_NAME [KEY=VALUE] [KEY=VALUE] (options)' + banner "knife vro workflow execute WORKFLOW_NAME [KEY=VALUE] [KEY=VALUE] (options)" option :vro_base_url, - long: '--vro-base-url API_URL', - description: 'URL for the vro server' + long: "--vro-base-url API_URL", + description: "URL for the vro server" option :vro_username, - long: '--vro-username USERNAME', - description: 'Username to use with the vro API' + long: "--vro-username USERNAME", + description: "Username to use with the vro API" option :vro_password, - long: '--vro-password PASSWORD', - description: 'Password to use with the vro API' + long: "--vro-password PASSWORD", + description: "Password to use with the vro API" option :vro_disable_ssl_verify, - long: '--vro-disable-ssl-verify', - description: 'Skip any SSL verification for the vro API', + long: "--vro-disable-ssl-verify", + description: "Skip any SSL verification for the vro API", boolean: true, default: false option :vro_workflow_id, - long: '--vro-workflow-id WORKFLOW_ID', - description: 'ID of the workflow to execute' + long: "--vro-workflow-id WORKFLOW_ID", + description: "ID of the workflow to execute" option :request_timeout, - long: '--request-timeout SECONDS', - description: 'number of seconds to wait for the workflow to complete', + long: "--request-timeout SECONDS", + description: "number of seconds to wait for the workflow to complete", default: 300 def verify_ssl? @@ -82,7 +82,7 @@ def vro_client def parse_and_validate_params!(args) args.each_with_object({}) do |arg, memo| - key, value = arg.split('=') + key, value = arg.split("=") raise "Invalid parameter, must be in KEY=VALUE format: #{arg}" if key.nil? || value.nil? memo[key] = value @@ -119,18 +119,18 @@ def wait_for_workflow end def missing_config_parameters - %i[vro_username vro_password vro_base_url].each_with_object([]) do |param, memo| + %i{vro_username vro_password vro_base_url}.each_with_object([]) do |param, memo| memo << param if locate_config_value(param).nil? end end def validate! unless missing_config_parameters.empty? - print_error_and_exit('The following parameters are missing but required:' \ - "#{missing_config_parameters.join(', ')}") + print_error_and_exit("The following parameters are missing but required:" \ + "#{missing_config_parameters.join(", ")}") end - print_error_and_exit('You must supply a workflow name.') if @name_args.empty? + print_error_and_exit("You must supply a workflow name.") if @name_args.empty? end def print_error_and_exit(msg) @@ -139,7 +139,7 @@ def print_error_and_exit(msg) end def print_results - ui.msg('') + ui.msg("") print_output_parameters print_execution_log end @@ -148,18 +148,18 @@ def print_output_parameters token = vro_client.token return if token.output_parameters.empty? - ui.msg(ui.color('Output Parameters:', :bold)) + ui.msg(ui.color("Output Parameters:", :bold)) token.output_parameters.each do |k, v| msg_pair(k, "#{v.value} (#{v.type})") unless v.value.nil? || (v.value.respond_to?(:empty?) && v.value.empty?) end - ui.msg('') + ui.msg("") end def print_execution_log log = vro_client.log.to_s return if log.nil? || log.empty? - ui.msg(ui.color('Workflow Execution Log:', :bold)) + ui.msg(ui.color("Workflow Execution Log:", :bold)) ui.msg(log) end @@ -174,13 +174,13 @@ def run set_parameters - ui.msg('Starting workflow execution...') + ui.msg("Starting workflow execution...") execution_id = execute_workflow ui.msg("Workflow execution #{execution_id} started. Waiting for it to complete...") wait_for_workflow - ui.msg('Workflow execution complete.') + ui.msg("Workflow execution complete.") print_results end diff --git a/lib/knife-vrealize/version.rb b/lib/knife-vrealize/version.rb index a5f1970..b068a27 100644 --- a/lib/knife-vrealize/version.rb +++ b/lib/knife-vrealize/version.rb @@ -2,7 +2,7 @@ # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,5 +19,5 @@ # module KnifeVrealize - VERSION = '3.0.0' + VERSION = "3.0.0" end diff --git a/spec/unit/cloud/vra_service_helpers_spec.rb b/spec/unit/cloud/vra_service_helpers_spec.rb index 7f21e35..01a1531 100644 --- a/spec/unit/cloud/vra_service_helpers_spec.rb +++ b/spec/unit/cloud/vra_service_helpers_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,37 +17,37 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife' -require 'chef/knife/cloud/vra_service' -require 'chef/knife/cloud/vra_service_helpers' +require "spec_helper" +require "chef/knife" +require "chef/knife/cloud/vra_service" +require "chef/knife/cloud/vra_service_helpers" class HelpersTester include Chef::Knife::Cloud::VraServiceHelpers attr_accessor :ui end -describe 'Chef::Knife::Cloud::VraServiceHelpers' do +describe "Chef::Knife::Cloud::VraServiceHelpers" do subject { HelpersTester.new } before(:each) do subject.ui = Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {}) end - describe '#create_service_instance' do - it 'creates a service instance' do - allow(subject).to receive(:locate_config_value).with(:vra_username).and_return('myuser') - allow(subject).to receive(:locate_config_value).with(:vra_password).and_return('mypassword') - allow(subject).to receive(:locate_config_value).with(:vra_base_url).and_return('https://vra.corp.local') - allow(subject).to receive(:locate_config_value).with(:vra_tenant).and_return('mytenant') + describe "#create_service_instance" do + it "creates a service instance" do + allow(subject).to receive(:locate_config_value).with(:vra_username).and_return("myuser") + allow(subject).to receive(:locate_config_value).with(:vra_password).and_return("mypassword") + allow(subject).to receive(:locate_config_value).with(:vra_base_url).and_return("https://vra.corp.local") + allow(subject).to receive(:locate_config_value).with(:vra_tenant).and_return("mytenant") allow(subject).to receive(:locate_config_value).with(:vra_page_size).and_return(50) allow(subject).to receive(:locate_config_value).with(:vra_disable_ssl_verify).and_return(false) expect(Chef::Knife::Cloud::VraService).to receive(:new) - .with(username: 'myuser', - password: 'mypassword', - base_url: 'https://vra.corp.local', - tenant: 'mytenant', + .with(username: "myuser", + password: "mypassword", + base_url: "https://vra.corp.local", + tenant: "mytenant", page_size: 50, verify_ssl: true) @@ -55,23 +55,23 @@ class HelpersTester end end - describe '#verify_ssl?' do - context 'when vra_disable_ssl_verify is true' do - it 'returns false' do + describe "#verify_ssl?" do + context "when vra_disable_ssl_verify is true" do + it "returns false" do allow(subject).to receive(:locate_config_value).with(:vra_disable_ssl_verify).and_return(true) expect(subject.verify_ssl?).to be false end end - context 'when vra_disable_ssl_verify is false' do - it 'returns true' do + context "when vra_disable_ssl_verify is false" do + it "returns true" do allow(subject).to receive(:locate_config_value).with(:vra_disable_ssl_verify).and_return(false) expect(subject.verify_ssl?).to be true end end end - describe '#wait_for_request' do + describe "#wait_for_request" do before(:each) do # muffle any stdout output from this method allow(subject).to receive(:print) @@ -80,9 +80,9 @@ class HelpersTester allow(subject).to receive(:sleep) end - context 'when the requests completes normally, 3 loops' do - it 'only refreshes the request 3 times' do - request = double('request') + context "when the requests completes normally, 3 loops" do + it "only refreshes the request 3 times" do + request = double("request") allow(request).to receive(:status) allow(request).to receive(:completed?).exactly(3).times.and_return(false, false, true) expect(request).to receive(:refresh).exactly(3).times @@ -91,9 +91,9 @@ class HelpersTester end end - context 'when the request is completed on the first loop' do - it 'only refreshes the request 1 time' do - request = double('request') + context "when the request is completed on the first loop" do + it "only refreshes the request 1 time" do + request = double("request") allow(request).to receive(:status) allow(request).to receive(:completed?).once.and_return(true) expect(request).to receive(:refresh).once @@ -102,28 +102,28 @@ class HelpersTester end end - context 'when the timeout is exceeded' do - it 'prints a warning and exits' do - request = double('request') + context "when the timeout is exceeded" do + it "prints a warning and exits" do + request = double("request") allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) - expect(subject.ui).to receive(:msg).with('') + expect(subject.ui).to receive(:msg).with("") expect(subject.ui).to receive(:error) - .with('Request did not complete in 600 seconds. Check the Requests tab in the vRA UI for more information.') + .with("Request did not complete in 600 seconds. Check the Requests tab in the vRA UI for more information.") expect { subject.wait_for_request(request) }.to raise_error(SystemExit) end end - context 'when a non-timeout exception is raised' do - it 'raises the original exception' do - request = double('request') + context "when a non-timeout exception is raised" do + it "raises the original exception" do + request = double("request") allow(request).to receive(:refresh).and_raise(RuntimeError) expect { subject.wait_for_request(request) }.to raise_error(RuntimeError) end end end - describe '#validate!' do - it 'calls checks for empty VRA connection configuration values' do + describe "#validate!" do + it "calls checks for empty VRA connection configuration values" do expect(subject).to receive(:check_for_missing_config_values!) .with(:vra_username, :vra_password, :vra_base_url, :vra_tenant) @@ -131,19 +131,19 @@ class HelpersTester end end - describe '#check_for_missing_config_values!' do - context 'when all values exist' do - it 'does not raise an error' do - allow(subject).to receive(:locate_config_value).with(:key1).and_return('value') - allow(subject).to receive(:locate_config_value).with(:key2).and_return('value') + describe "#check_for_missing_config_values!" do + context "when all values exist" do + it "does not raise an error" do + allow(subject).to receive(:locate_config_value).with(:key1).and_return("value") + allow(subject).to receive(:locate_config_value).with(:key2).and_return("value") expect(subject.ui).not_to receive(:error) expect { subject.check_for_missing_config_values!(:key1, :key2) }.not_to raise_error end end - context 'when a value does not exist' do - it 'prints an error and exits' do - allow(subject).to receive(:locate_config_value).with(:key1).and_return('value') + context "when a value does not exist" do + it "prints an error and exits" do + allow(subject).to receive(:locate_config_value).with(:key1).and_return("value") allow(subject).to receive(:locate_config_value).with(:key2).and_return(nil) expect(subject.ui).to receive(:error) expect { subject.check_for_missing_config_values!(:key1, :key2) }.to raise_error(SystemExit) diff --git a/spec/unit/cloud/vra_service_spec.rb b/spec/unit/cloud/vra_service_spec.rb index 912af95..5ec8c9e 100644 --- a/spec/unit/cloud/vra_service_spec.rb +++ b/spec/unit/cloud/vra_service_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,41 +17,41 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/cloud/exceptions' -require 'chef/knife/cloud/vra_service' -require 'support/shared_examples_for_service' +require "spec_helper" +require "chef/knife/cloud/exceptions" +require "chef/knife/cloud/vra_service" +require "support/shared_examples_for_service" describe Chef::Knife::Cloud::VraService do subject do - Chef::Knife::Cloud::VraService.new(username: 'myuser', - password: 'mypassword', - base_url: 'https://vra.corp.local', - tenant: 'mytenant', + Chef::Knife::Cloud::VraService.new(username: "myuser", + password: "mypassword", + base_url: "https://vra.corp.local", + tenant: "mytenant", verify_ssl: true) end before(:each) do subject.ui = Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {}) - allow(subject.ui).to receive(:msg).with('') + allow(subject.ui).to receive(:msg).with("") end - describe '#connection' do - it 'creates a Vra::Client object' do + describe "#connection" do + it "creates a Vra::Client object" do expect(subject.connection).to be_an_instance_of(Vra::Client) end end - describe '#create_server' do + describe "#create_server" do before(:each) do - allow(subject.ui).to receive(:msg).with('Catalog request 1 submitted.') + allow(subject.ui).to receive(:msg).with("Catalog request 1 submitted.") allow(subject.ui).to receive(:msg).with("Catalog request complete.\n") end - context 'when the request is successful' do - it 'calls all the appropriate methods' do - submitted_request = double('submitted_request', id: 1, failed?: false) - resource = double('resource', vm?: true) + context "when the request is successful" do + it "calls all the appropriate methods" do + submitted_request = double("submitted_request", id: 1, failed?: false) + resource = double("resource", vm?: true) allow(submitted_request).to receive(:resources).and_return([resource]) expect(subject).to receive_message_chain(:catalog_request, :submit).and_return(submitted_request) expect(subject).to receive(:wait_for_request) @@ -62,9 +62,9 @@ end end - context 'when the request failed' do - it 'raises an exception' do - submitted_request = double('submitted_request', id: 1, failed?: true, completion_details: 'it failed') + context "when the request failed" do + it "raises an exception" do + submitted_request = double("submitted_request", id: 1, failed?: true, completion_details: "it failed") expect(subject).to receive_message_chain(:catalog_request, :submit).and_return(submitted_request) expect(subject).to receive(:wait_for_request) expect(subject).to receive(:request_summary) @@ -73,9 +73,9 @@ end end - context 'when the request returns no resources' do - it 'raises an exception' do - submitted_request = double('submitted_request', id: 1, failed?: false) + context "when the request returns no resources" do + it "raises an exception" do + submitted_request = double("submitted_request", id: 1, failed?: false) allow(submitted_request).to receive(:resources).and_return([]) expect(subject).to receive_message_chain(:catalog_request, :submit).and_return(submitted_request) expect(subject).to receive(:wait_for_request) @@ -85,11 +85,11 @@ end end - context 'when the request returns more than one VM resource' do - it 'raises an exception' do - submitted_request = double('submitted_request', id: 1, failed?: false) - resource1 = double('resource1', vm?: true) - resource2 = double('resource2', vm?: true) + context "when the request returns more than one VM resource" do + it "raises an exception" do + submitted_request = double("submitted_request", id: 1, failed?: false) + resource1 = double("resource1", vm?: true) + resource2 = double("resource2", vm?: true) allow(submitted_request).to receive(:resources).and_return([resource1, resource2]) expect(subject).to receive_message_chain(:catalog_request, :submit).and_return(submitted_request) expect(subject).to receive(:wait_for_request) @@ -99,11 +99,11 @@ end end - context 'when the request returns multiple resources, but only 1 VM' do - it 'happily returns the server resource' do - submitted_request = double('submitted_request', id: 1, failed?: false) - resource1 = double('resource1', vm?: true) - resource2 = double('resource2', vm?: false) + context "when the request returns multiple resources, but only 1 VM" do + it "happily returns the server resource" do + submitted_request = double("submitted_request", id: 1, failed?: false) + resource1 = double("resource1", vm?: true) + resource2 = double("resource2", vm?: false) allow(submitted_request).to receive(:resources).and_return([resource1, resource2]) expect(subject).to receive_message_chain(:catalog_request, :submit).and_return(submitted_request) expect(subject).to receive(:wait_for_request) @@ -115,39 +115,39 @@ end end - describe '#delete_server' do - context 'when the server exists' do - it 'calls the appropriate methods' do - server = double('server', status: 'ACTIVE') - destroy_request = double('destroy_request', id: 1) - expect(subject).to receive(:get_server).with('12345').and_return(server) + describe "#delete_server" do + context "when the server exists" do + it "calls the appropriate methods" do + server = double("server", status: "ACTIVE") + destroy_request = double("destroy_request", id: 1) + expect(subject).to receive(:get_server).with("12345").and_return(server) expect(subject).to receive(:server_summary).with(server) expect(subject.ui).to receive(:confirm) expect(server).to receive(:destroy).and_return(destroy_request) - expect(subject.ui).to receive(:msg).with('Destroy request 1 submitted.') + expect(subject.ui).to receive(:msg).with("Destroy request 1 submitted.") expect(subject).to receive(:wait_for_request) - expect(subject.ui).to receive(:msg).with('Destroy request complete.') + expect(subject.ui).to receive(:msg).with("Destroy request complete.") expect(subject).to receive(:request_summary) - subject.delete_server('12345') + subject.delete_server("12345") end end - context 'when the server is already deleted' do - it 'does not call #destroy on the server object' do - server = double('server', status: 'DELETED') - expect(subject).to receive(:get_server).with('12345').and_return(server) + context "when the server is already deleted" do + it "does not call #destroy on the server object" do + server = double("server", status: "DELETED") + expect(subject).to receive(:get_server).with("12345").and_return(server) expect(subject).to receive(:server_summary).with(server) expect(subject.ui).to receive(:warn).with("Server is already deleted.\n") expect(server).not_to receive(:destroy) - subject.delete_server('12345') + subject.delete_server("12345") end end end - describe '#list_servers' do - it 'calls all_resources' do + describe "#list_servers" do + it "calls all_resources" do expect(subject).to receive_message_chain(:connection, :resources, :all_resources) .and_return([]) @@ -155,17 +155,17 @@ end end - describe '#list_catalog_items' do - context 'when requesting entitled items only' do - it 'calls entitled_items' do + describe "#list_catalog_items" do + context "when requesting entitled items only" do + it "calls entitled_items" do expect(subject).to receive_message_chain(:connection, :catalog, :entitled_items) subject.list_catalog_items(true) end end - context 'when requesting all items' do - it 'calls all_items' do + context "when requesting all items" do + it "calls all_items" do expect(subject).to receive_message_chain(:connection, :catalog, :all_items) subject.list_catalog_items(false) @@ -173,77 +173,77 @@ end end - describe '#get_server' do - it 'calls resources.by_id' do - expect(subject).to receive_message_chain(:connection, :resources, :by_id).with('12345') + describe "#get_server" do + it "calls resources.by_id" do + expect(subject).to receive_message_chain(:connection, :resources, :by_id).with("12345") - subject.get_server('12345') + subject.get_server("12345") end end - describe '#catalog_request' do - context 'when handling a proper request' do - it 'calls the appropriate methods' do - catalog_request = double('catalog_request') + describe "#catalog_request" do + context "when handling a proper request" do + it "calls the appropriate methods" do + catalog_request = double("catalog_request") expect(catalog_request).to receive(:cpus=).with(1) expect(catalog_request).to receive(:memory=).with(512) - expect(catalog_request).to receive(:requested_for=).with('myuser@corp.local') + expect(catalog_request).to receive(:requested_for=).with("myuser@corp.local") expect(catalog_request).to receive(:lease_days=).with(5) - expect(catalog_request).to receive(:notes=).with('my notes') - expect(catalog_request).to receive(:subtenant_id=).with('tenant1') + expect(catalog_request).to receive(:notes=).with("my notes") + expect(catalog_request).to receive(:subtenant_id=).with("tenant1") expect(subject).to receive_message_chain(:connection, :catalog, :request) - .with('12345') + .with("12345") .and_return(catalog_request) - subject.catalog_request(catalog_id: '12345', + subject.catalog_request(catalog_id: "12345", cpus: 1, memory: 512, - requested_for: 'myuser@corp.local', + requested_for: "myuser@corp.local", lease_days: 5, - notes: 'my notes', - subtenant_id: 'tenant1') + notes: "my notes", + subtenant_id: "tenant1") end end - context 'when optional arguments are missing' do - it 'does not call the attr setters for the missing attributes' do - catalog_request = double('catalog_request') + context "when optional arguments are missing" do + it "does not call the attr setters for the missing attributes" do + catalog_request = double("catalog_request") expect(catalog_request).to receive(:cpus=).with(1) expect(catalog_request).to receive(:memory=).with(512) - expect(catalog_request).to receive(:requested_for=).with('myuser@corp.local') + expect(catalog_request).to receive(:requested_for=).with("myuser@corp.local") expect(catalog_request).to_not receive(:lease_days=) expect(catalog_request).to_not receive(:notes=) expect(catalog_request).to_not receive(:subtenant_id=) expect(subject).to receive_message_chain(:connection, :catalog, :request) - .with('12345') + .with("12345") .and_return(catalog_request) - subject.catalog_request(catalog_id: '12345', + subject.catalog_request(catalog_id: "12345", cpus: 1, memory: 512, - requested_for: 'myuser@corp.local') + requested_for: "myuser@corp.local") end end - context 'when extra parameters are supplied' do - it 'calls set_parameter on the catalog_request' do - catalog_request = double('catalog_request') + context "when extra parameters are supplied" do + it "calls set_parameter on the catalog_request" do + catalog_request = double("catalog_request") expect(catalog_request).to receive(:cpus=).with(1) expect(catalog_request).to receive(:memory=).with(512) - expect(catalog_request).to receive(:requested_for=).with('myuser@corp.local') - expect(catalog_request).to receive(:set_parameter).with('key1', 'string', 'value1') - expect(catalog_request).to receive(:set_parameter).with('key2', 'integer', '2') + expect(catalog_request).to receive(:requested_for=).with("myuser@corp.local") + expect(catalog_request).to receive(:set_parameter).with("key1", "string", "value1") + expect(catalog_request).to receive(:set_parameter).with("key2", "integer", "2") expect(subject).to receive_message_chain(:connection, :catalog, :request) - .with('12345') + .with("12345") .and_return(catalog_request) - subject.catalog_request(catalog_id: '12345', + subject.catalog_request(catalog_id: "12345", cpus: 1, memory: 512, - requested_for: 'myuser@corp.local', + requested_for: "myuser@corp.local", extra_params: [ - { key: 'key1', type: 'string', value: 'value1' }, - { key: 'key2', type: 'integer', value: '2' } + { key: "key1", type: "string", value: "value1" }, + { key: "key2", type: "integer", value: "2" }, ]) end end diff --git a/spec/unit/vra_catalog_list_spec.rb b/spec/unit/vra_catalog_list_spec.rb index 86a2314..295f82a 100644 --- a/spec/unit/vra_catalog_list_spec.rb +++ b/spec/unit/vra_catalog_list_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,27 +17,27 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/vra_catalog_list' -require 'support/shared_examples_for_command' +require "spec_helper" +require "chef/knife/vra_catalog_list" +require "support/shared_examples_for_command" describe Chef::Knife::Cloud::VraCatalogList do it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::VraCatalogList.new subject { described_class.new } - describe '#format_status_value' do + describe "#format_status_value" do context 'when the status is "published"' do - it 'displays with green' do - expect(subject.ui).to receive(:color).with('published', :green) - subject.format_status_value('published') + it "displays with green" do + expect(subject.ui).to receive(:color).with("published", :green) + subject.format_status_value("published") end end context 'when the status it not "published"' do - it 'displays with red' do - expect(subject.ui).to receive(:color).with('unpublished', :red) - subject.format_status_value('unpublished') + it "displays with red" do + expect(subject.ui).to receive(:color).with("unpublished", :red) + subject.format_status_value("unpublished") end end end diff --git a/spec/unit/vra_server_create_spec.rb b/spec/unit/vra_server_create_spec.rb index d00257d..d44c973 100644 --- a/spec/unit/vra_server_create_spec.rb +++ b/spec/unit/vra_server_create_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,10 +17,10 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/vra_server_create' -require 'support/shared_examples_for_command' -require 'support/shared_examples_for_servercreatecommand' +require "spec_helper" +require "chef/knife/vra_server_create" +require "support/shared_examples_for_command" +require "support/shared_examples_for_servercreatecommand" describe Chef::Knife::Cloud::VraServerCreate do before(:each) do @@ -28,29 +28,29 @@ end argv = [] - argv += %w(--cpus 1) - argv += %w(--memory 512) - argv += %w(--requested-for myuser@corp.local) - argv += %w(--bootstrap-protocol ssh) - argv += %w(--ssh-password password) - argv += %w(--extra-param key1=string:value1) - argv += %w(--extra-param key2=integer:2) - argv += %w(d5ba201a-449f-47a4-9d02-39196224bf01) + argv += %w{--cpus 1} + argv += %w{--memory 512} + argv += %w{--requested-for myuser@corp.local} + argv += %w{--bootstrap-protocol ssh} + argv += %w{--ssh-password password} + argv += %w{--extra-param key1=string:value1} + argv += %w{--extra-param key2=integer:2} + argv += %w{d5ba201a-449f-47a4-9d02-39196224bf01} subject { Chef::Knife::Cloud::VraServerCreate.new(argv) } it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::VraServerCreate.new(argv) it_behaves_like Chef::Knife::Cloud::ServerCreateCommand, Chef::Knife::Cloud::VraServerCreate.new(argv) - describe '#validate_params!' do - context 'when no catalog ID is supplied' do - it 'raises an error' do + describe "#validate_params!" do + context "when no catalog ID is supplied" do + it "raises an error" do argv = [] - argv += %w(--cpus 1) - argv += %w(--memory 512) - argv += %w(--requested-for myuser@corp.local) - argv += %w(--bootstrap-protocol ssh) - argv += %w(--ssh-password password) + argv += %w{--cpus 1} + argv += %w{--memory 512} + argv += %w{--requested-for myuser@corp.local} + argv += %w{--bootstrap-protocol ssh} + argv += %w{--ssh-password password} command = Chef::Knife::Cloud::VraServerCreate.new(argv) expect(command.ui).to receive(:error) @@ -58,90 +58,90 @@ end end - it 'validates extra parameters' do + it "validates extra parameters" do expect(subject).to receive(:validate_extra_params!) subject.validate_params! end end - describe '#extra_params' do - context 'when there are no extra params' do + describe "#extra_params" do + context "when there are no extra params" do before do Chef::Config[:knife][:vra_extra_params] = {} end - it 'returns nil' do + it "returns nil" do expect(subject.extra_params).to eq(nil) end end - context 'when extra params are provided' do + context "when extra params are provided" do before do Chef::Config[:knife][:vra_extra_params] = { - 'key1' => 'string:value1', - 'key2' => 'integer:2' + "key1" => "string:value1", + "key2" => "integer:2", } end - it 'parses extra parameters properly' do + it "parses extra parameters properly" do params = subject.extra_params - expect(params[0][:key]).to eq 'key1' - expect(params[0][:type]).to eq 'string' - expect(params[0][:value]).to eq 'value1' - expect(params[1][:key]).to eq 'key2' - expect(params[1][:type]).to eq 'integer' - expect(params[1][:value]).to eq '2' + expect(params[0][:key]).to eq "key1" + expect(params[0][:type]).to eq "string" + expect(params[0][:value]).to eq "value1" + expect(params[1][:key]).to eq "key2" + expect(params[1][:type]).to eq "integer" + expect(params[1][:value]).to eq "2" end end end - describe '#validate_extra_params!' do - context 'when no extra parameters are supplied' do - it 'does not raise an exception' do + describe "#validate_extra_params!" do + context "when no extra parameters are supplied" do + it "does not raise an exception" do argv = [] - argv += %w(--cpus 1) - argv += %w(--memory 512) - argv += %w(--requested-for myuser@corp.local) - argv += %w(--bootstrap-protocol ssh) - argv += %w(--ssh-password password) + argv += %w{--cpus 1} + argv += %w{--memory 512} + argv += %w{--requested-for myuser@corp.local} + argv += %w{--bootstrap-protocol ssh} + argv += %w{--ssh-password password} command = Chef::Knife::Cloud::VraServerCreate.new(argv) expect { command.validate_extra_params! }.not_to raise_error end end - context 'when correct parameters are supplied' do - it 'does not raise an exception' do + context "when correct parameters are supplied" do + it "does not raise an exception" do expect { subject.validate_extra_params! }.not_to raise_error end end - context 'when a type or value is missing' do - it 'raises an exception' do + context "when a type or value is missing" do + it "raises an exception" do argv = [] - argv += %w(--cpus 1) - argv += %w(--memory 512) - argv += %w(--requested-for myuser@corp.local) - argv += %w(--bootstrap-protocol ssh) - argv += %w(--ssh-password password) - argv += %w(d5ba201a-449f-47a4-9d02-39196224bf01) - argv += %w(--extra-param key1=value1) + argv += %w{--cpus 1} + argv += %w{--memory 512} + argv += %w{--requested-for myuser@corp.local} + argv += %w{--bootstrap-protocol ssh} + argv += %w{--ssh-password password} + argv += %w{d5ba201a-449f-47a4-9d02-39196224bf01} + argv += %w{--extra-param key1=value1} command = Chef::Knife::Cloud::VraServerCreate.new(argv) expect { command.validate_extra_params! }.to raise_error(ArgumentError) end end - context 'when an invalid parameter type is provided' do - it 'raises an exception' do + context "when an invalid parameter type is provided" do + it "raises an exception" do argv = [] - argv += %w(--cpus 1) - argv += %w(--memory 512) - argv += %w(--requested-for myuser@corp.local) - argv += %w(--bootstrap-protocol ssh) - argv += %w(--ssh-password password) - argv += %w(d5ba201a-449f-47a4-9d02-39196224bf01) - argv += %w(--extra-param key1=faketype:value1) + argv += %w{--cpus 1} + argv += %w{--memory 512} + argv += %w{--requested-for myuser@corp.local} + argv += %w{--bootstrap-protocol ssh} + argv += %w{--ssh-password password} + argv += %w{d5ba201a-449f-47a4-9d02-39196224bf01} + argv += %w{--extra-param key1=faketype:value1} command = Chef::Knife::Cloud::VraServerCreate.new(argv) expect { command.validate_extra_params! }.to raise_error(ArgumentError) @@ -149,23 +149,23 @@ end end - describe '#hostname_for_server' do - let(:server) { double('server') } - let(:ip_addresses) { [ '1.2.3.4' ] } + describe "#hostname_for_server" do + let(:server) { double("server") } + let(:ip_addresses) { [ "1.2.3.4" ] } - it 'returns the IP address if it exists' do + it "returns the IP address if it exists" do allow(subject).to receive(:server).and_return(server) allow(server).to receive(:ip_addresses).and_return(ip_addresses) - expect(subject.hostname_for_server).to eq('1.2.3.4') + expect(subject.hostname_for_server).to eq("1.2.3.4") end - it 'returns the hostname if the IP address is missing' do + it "returns the hostname if the IP address is missing" do allow(subject).to receive(:server).and_return(server) allow(server).to receive(:ip_addresses).and_return([]) - allow(server).to receive(:name).and_return('test_name') + allow(server).to receive(:name).and_return("test_name") - expect(subject.hostname_for_server).to eq('test_name') + expect(subject.hostname_for_server).to eq("test_name") end end end diff --git a/spec/unit/vra_server_delete_spec.rb b/spec/unit/vra_server_delete_spec.rb index ff700fa..5db3772 100644 --- a/spec/unit/vra_server_delete_spec.rb +++ b/spec/unit/vra_server_delete_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,47 +17,47 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/vra_server_delete' -require 'support/shared_examples_for_command' -require 'support/shared_examples_for_serverdeletecommand' +require "spec_helper" +require "chef/knife/vra_server_delete" +require "support/shared_examples_for_command" +require "support/shared_examples_for_serverdeletecommand" describe Chef::Knife::Cloud::VraServerDelete do - subject { Chef::Knife::Cloud::VraServerDelete.new(%w(12345 54321)) } + subject { Chef::Knife::Cloud::VraServerDelete.new(%w{12345 54321}) } - it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::VraServerDelete.new(%w(12345 54321)) + it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::VraServerDelete.new(%w{12345 54321}) - describe '#validate_params!' do - context 'when no resource IDs are supplied' do + describe "#validate_params!" do + context "when no resource IDs are supplied" do let(:command) { Chef::Knife::Cloud::VraServerDelete.new } - it 'prints an error and exits' do + it "prints an error and exits" do expect(command.ui).to receive(:error) expect { command.validate_params! }.to raise_error(SystemExit) end end end - describe '#execute_command' do + describe "#execute_command" do before(:each) do - server1 = double('server1', name: 'server1') - server2 = double('server2', name: 'server2') - allow(subject).to receive_message_chain(:service, :get_server).with('12345').and_return(server1) - allow(subject).to receive_message_chain(:service, :get_server).with('54321').and_return(server2) + server1 = double("server1", name: "server1") + server2 = double("server2", name: "server2") + allow(subject).to receive_message_chain(:service, :get_server).with("12345").and_return(server1) + allow(subject).to receive_message_chain(:service, :get_server).with("54321").and_return(server2) end - it 'calls delete_server for each server' do - allow(subject).to receive(:delete_from_chef).with('server1') - allow(subject).to receive(:delete_from_chef).with('server2') - expect(subject).to receive_message_chain(:service, :delete_server).with('12345') - expect(subject).to receive_message_chain(:service, :delete_server).with('54321') + it "calls delete_server for each server" do + allow(subject).to receive(:delete_from_chef).with("server1") + allow(subject).to receive(:delete_from_chef).with("server2") + expect(subject).to receive_message_chain(:service, :delete_server).with("12345") + expect(subject).to receive_message_chain(:service, :delete_server).with("54321") subject.execute_command end - it 'calls delete_from_chef using the server names for each server' do - allow(subject).to receive_message_chain(:service, :delete_server).with('12345') - allow(subject).to receive_message_chain(:service, :delete_server).with('54321') - expect(subject).to receive(:delete_from_chef).with('server1') - expect(subject).to receive(:delete_from_chef).with('server2') + it "calls delete_from_chef using the server names for each server" do + allow(subject).to receive_message_chain(:service, :delete_server).with("12345") + allow(subject).to receive_message_chain(:service, :delete_server).with("54321") + expect(subject).to receive(:delete_from_chef).with("server1") + expect(subject).to receive(:delete_from_chef).with("server2") subject.execute_command end end diff --git a/spec/unit/vra_server_list_spec.rb b/spec/unit/vra_server_list_spec.rb index f40c49a..c4d0e3c 100644 --- a/spec/unit/vra_server_list_spec.rb +++ b/spec/unit/vra_server_list_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,34 +17,34 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/vra_server_list' -require 'support/shared_examples_for_command' +require "spec_helper" +require "chef/knife/vra_server_list" +require "support/shared_examples_for_command" describe Chef::Knife::Cloud::VraServerList do it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::VraServerList.new subject { described_class.new } - describe '#format_status_value' do + describe "#format_status_value" do context 'when the status is "active"' do - it 'displays with green' do - expect(subject.ui).to receive(:color).with('active', :green) - subject.format_status_value('active') + it "displays with green" do + expect(subject.ui).to receive(:color).with("active", :green) + subject.format_status_value("active") end end context 'when the status is "deleted"' do - it 'displays with red' do - expect(subject.ui).to receive(:color).with('deleted', :red) - subject.format_status_value('deleted') + it "displays with red" do + expect(subject.ui).to receive(:color).with("deleted", :red) + subject.format_status_value("deleted") end end - context 'when the status is anything else' do - it 'displays with yellow' do - expect(subject.ui).to receive(:color).with('unknown', :yellow) - subject.format_status_value('unknown') + context "when the status is anything else" do + it "displays with yellow" do + expect(subject.ui).to receive(:color).with("unknown", :yellow) + subject.format_status_value("unknown") end end end diff --git a/spec/unit/vra_server_show_spec.rb b/spec/unit/vra_server_show_spec.rb index 4dd0bbd..3408ab1 100644 --- a/spec/unit/vra_server_show_spec.rb +++ b/spec/unit/vra_server_show_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,25 +17,25 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/vra_server_show' -require 'support/shared_examples_for_command' +require "spec_helper" +require "chef/knife/vra_server_show" +require "support/shared_examples_for_command" describe Chef::Knife::Cloud::VraServerShow do it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::VraServerShow.new - describe '#validate_params!' do - context 'when no resources are supplied' do + describe "#validate_params!" do + context "when no resources are supplied" do let(:command) { Chef::Knife::Cloud::VraServerShow.new } - it 'prints an error and exits' do + it "prints an error and exits" do expect(command.ui).to receive(:error) expect { command.validate_params! }.to raise_error(SystemExit) end end - context 'when more than one resource is supplied' do - let(:command) { Chef::Knife::Cloud::VraServerShow.new(%w(12345 54321)) } - it 'prints an error and exits' do + context "when more than one resource is supplied" do + let(:command) { Chef::Knife::Cloud::VraServerShow.new(%w{12345 54321}) } + it "prints an error and exits" do expect(command.ui).to receive(:error) expect { command.validate_params! }.to raise_error(SystemExit) end diff --git a/spec/unit/vro_workflow_execute_spec.rb b/spec/unit/vro_workflow_execute_spec.rb index cba7579..931a131 100644 --- a/spec/unit/vro_workflow_execute_spec.rb +++ b/spec/unit/vro_workflow_execute_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # # Author:: Chef Partner Engineering () -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: 2015-2019, Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,24 +17,24 @@ # limitations under the License. # -require 'spec_helper' -require 'chef/knife/vro_workflow_execute' +require "spec_helper" +require "chef/knife/vro_workflow_execute" describe Chef::Knife::VroWorkflowExecute do - let(:workflow_name) { 'test workflow' } - let(:workflow_id) { '1d335f85-5328-42fc-a842-a956d1ccdf08' } - let(:vro_username) { 'myuser' } - let(:vro_password) { 'mypassword' } - let(:vro_base_url) { 'https://vro.corp.local' } + let(:workflow_name) { "test workflow" } + let(:workflow_id) { "1d335f85-5328-42fc-a842-a956d1ccdf08" } + let(:vro_username) { "myuser" } + let(:vro_password) { "mypassword" } + let(:vro_base_url) { "https://vro.corp.local" } let(:vro_disable_ssl_verify) { true } - let(:key1) { 'value1' } - let(:key2) { '2' } + let(:key1) { "value1" } + let(:key2) { "2" } let(:argv) do [ workflow_name, "key1=#{key1}", "key2=#{key2}", - '--vro-workflow-id', workflow_id] + "--vro-workflow-id", workflow_id] end subject { described_class.new(argv) } @@ -46,24 +46,24 @@ Chef::Config[:knife][:vro_disable_ssl_verify] = vro_disable_ssl_verify end - describe '#verify_ssl?' do - context 'when vro_disable_ssl_verify is set to true' do + describe "#verify_ssl?" do + context "when vro_disable_ssl_verify is set to true" do let(:vro_disable_ssl_verify) { true } - it 'returns false' do + it "returns false" do expect(subject.verify_ssl?).to eq(false) end end - context 'when vro_disable_ssl_verify is set to false' do + context "when vro_disable_ssl_verify is set to false" do let(:vro_disable_ssl_verify) { false } - it 'returns true' do + it "returns true" do expect(subject.verify_ssl?).to eq(true) end end end - describe '#vro_config' do - it 'creates a config object' do + describe "#vro_config" do + it "creates a config object" do expect(VcoWorkflows::Config).to receive(:new).with(url: vro_base_url, username: vro_username, password: vro_password, @@ -72,40 +72,40 @@ end end - describe '#vro_client' do - it 'creates a client object' do - config = double('config') + describe "#vro_client" do + it "creates a client object" do + config = double("config") allow(subject).to receive(:vro_config).and_return(config) expect(VcoWorkflows::Workflow).to receive(:new).with(workflow_name, - id: workflow_id, - config: config) + id: workflow_id, + config: config) subject.set_parameters subject.vro_client end end - describe '#parse_and_validate_params' do - context 'when proper parameters are supplied' do - let(:args) { %w(key1=value1 key2=value2) } - it 'returns a hash of parameters' do - expect(subject.parse_and_validate_params!(args)).to eq('key1' => 'value1', - 'key2' => 'value2') + describe "#parse_and_validate_params" do + context "when proper parameters are supplied" do + let(:args) { %w{key1=value1 key2=value2} } + it "returns a hash of parameters" do + expect(subject.parse_and_validate_params!(args)).to eq("key1" => "value1", + "key2" => "value2") end end - context 'when a parameter is malformed' do - let(:args) { %w(key1=value1 key2) } - it 'raises an exception' do + context "when a parameter is malformed" do + let(:args) { %w{key1=value1 key2} } + it "raises an exception" do expect { subject.parse_and_validate_params!(args) }.to raise_error(RuntimeError) end end end - describe 'execute_workflow' do + describe "execute_workflow" do before do - config = double('config') - client = double('client') + config = double("config") + client = double("client") allow(subject).to receive(:vro_config).and_return(config) allow(subject).to receive(:vro_client).and_return(client) allow(client).to receive(:parameter) @@ -113,49 +113,49 @@ subject.set_parameters end - it 'sets the workflow parameters' do - expect(subject.vro_client).to receive(:parameter).with('key1', key1) - expect(subject.vro_client).to receive(:parameter).with('key2', key2) + it "sets the workflow parameters" do + expect(subject.vro_client).to receive(:parameter).with("key1", key1) + expect(subject.vro_client).to receive(:parameter).with("key2", key2) subject.execute_workflow end - it 'executes the workflow' do + it "executes the workflow" do expect(subject.vro_client).to receive(:execute) subject.execute_workflow end - context 'when execute fails with a RestClient::BadRequest' do - it 'prints an error with the HTTP response' do + context "when execute fails with a RestClient::BadRequest" do + it "prints an error with the HTTP response" do HTTPResponse = Struct.new(:code, :to_s) - response = HTTPResponse.new(400, 'an HTTP error occurred') + response = HTTPResponse.new(400, "an HTTP error occurred") exception = RestClient::BadRequest.new exception.response = response allow(subject.vro_client).to receive(:execute).and_raise(exception) - expect(subject.ui).to receive(:error).with('The workflow execution request failed: an HTTP error occurred') + expect(subject.ui).to receive(:error).with("The workflow execution request failed: an HTTP error occurred") expect { subject.execute_workflow }.to raise_error(RestClient::BadRequest) end end - context 'when execute fails with any other exception' do - it 'prints an error with the exception message' do - allow(subject.vro_client).to receive(:execute).and_raise(RuntimeError, 'a non-HTTP error occurred') - expect(subject.ui).to receive(:error).with('The workflow execution request failed: a non-HTTP error occurred') + context "when execute fails with any other exception" do + it "prints an error with the exception message" do + allow(subject.vro_client).to receive(:execute).and_raise(RuntimeError, "a non-HTTP error occurred") + expect(subject.ui).to receive(:error).with("The workflow execution request failed: a non-HTTP error occurred") expect { subject.execute_workflow }.to raise_error(RuntimeError) end end end - describe '#wait_for_workflow' do + describe "#wait_for_workflow" do before(:each) do # don't actually sleep allow(subject).to receive(:sleep) end - context 'when the requests completes normally, 3 loops' do - it 'only fetches the token 3 times' do - client = double('client') - token = double('token') + context "when the requests completes normally, 3 loops" do + it "only fetches the token 3 times" do + client = double("client") + token = double("token") allow(subject).to receive(:vro_client).and_return(client) allow(client).to receive(:token).and_return(token) allow(token).to receive(:alive?).exactly(3).times.and_return(true, true, false) @@ -165,10 +165,10 @@ end end - context 'when the request is completed on the first loop' do - it 'only refreshes the request 1 time' do - client = double('client') - token = double('token') + context "when the request is completed on the first loop" do + it "only refreshes the request 1 time" do + client = double("client") + token = double("token") allow(subject).to receive(:vro_client).and_return(client) allow(client).to receive(:token).and_return(token) expect(token).to receive(:alive?).once.and_return(false) @@ -177,49 +177,49 @@ end end - context 'when the timeout is exceeded' do + context "when the timeout is exceeded" do before do Chef::Config[:knife][:request_timeout] = 600 end - it 'raises a Timeout exception' do + it "raises a Timeout exception" do allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) expect { subject.wait_for_workflow }.to raise_error( - Timeout::Error, 'Workflow did not complete in 600 seconds. ' \ - 'Please check the vRO UI for more information.' \ + Timeout::Error, "Workflow did not complete in 600 seconds. " \ + "Please check the vRO UI for more information." \ ) end end - context 'when a non-timeout exception is raised' do - it 'raises the original exception' do - client = double('client') + context "when a non-timeout exception is raised" do + it "raises the original exception" do + client = double("client") allow(subject).to receive(:vro_client).and_return(client) - allow(client).to receive(:token).and_raise(RuntimeError, 'an error occurred') - expect { subject.wait_for_workflow }.to raise_error(RuntimeError, 'an error occurred') + allow(client).to receive(:token).and_raise(RuntimeError, "an error occurred") + expect { subject.wait_for_workflow }.to raise_error(RuntimeError, "an error occurred") end end end - describe '#missing_config_parameters' do - context 'when all parameters are supplied' do - it 'returns an empty array' do + describe "#missing_config_parameters" do + context "when all parameters are supplied" do + it "returns an empty array" do expect(subject.missing_config_parameters).to eq([]) end end - context 'when a parameter is missing' do + context "when a parameter is missing" do before do Chef::Config[:knife][:vro_username] = nil end - it 'returns an array with that parameter' do + it "returns an array with that parameter" do expect(subject.missing_config_parameters).to eq([ :vro_username ]) end end end - describe '#validate!' do - context 'when a config parameter is missing' do - it 'calls #print_error_and_exit' do + describe "#validate!" do + context "when a config parameter is missing" do + it "calls #print_error_and_exit" do allow(subject).to receive(:missing_config_parameters).and_return([ :parameter ]) expect(subject).to receive(:print_error_and_exit) @@ -227,9 +227,9 @@ end end - context 'when no workflow name is provided' do + context "when no workflow name is provided" do let(:knife) { Chef::Knife::VroWorkflowExecute.new } - it 'calls #print_error_and_exit' do + it "calls #print_error_and_exit" do expect(knife).to receive(:print_error_and_exit) knife.validate! @@ -237,16 +237,16 @@ end end - describe '#print_error_and_exit' do - it 'prints an error and exits' do - expect(subject.ui).to receive(:error).with('an error occurred') - expect { subject.print_error_and_exit('an error occurred') }.to raise_error(SystemExit) + describe "#print_error_and_exit" do + it "prints an error and exits" do + expect(subject.ui).to receive(:error).with("an error occurred") + expect { subject.print_error_and_exit("an error occurred") }.to raise_error(SystemExit) end end - describe '#print_results' do - it 'prints a blank line and calls the other print methods' do - expect(subject.ui).to receive(:msg).with('') + describe "#print_results" do + it "prints a blank line and calls the other print methods" do + expect(subject.ui).to receive(:msg).with("") expect(subject).to receive(:print_output_parameters) expect(subject).to receive(:print_execution_log) @@ -254,34 +254,34 @@ end end - describe '#print_output_parameters' do - context 'when there are no output parameters' do + describe "#print_output_parameters" do + context "when there are no output parameters" do before do - client = double('client') - token = double('token') + client = double("client") + token = double("token") allow(subject).to receive(:vro_client).and_return(client) allow(client).to receive(:token).and_return(token) allow(token).to receive(:output_parameters).and_return({}) end - it 'does not print any output' do + it "does not print any output" do expect(subject.ui).not_to receive(:msg) subject.print_output_parameters end end - context 'when output parameters exist' do + context "when output parameters exist" do before do - client = double('client') - token = double('token') - param1 = double('param1', value: 'value1', type: 'string') - param2 = double('param2', value: 2.0, type: 'number') + client = double("client") + token = double("token") + param1 = double("param1", value: "value1", type: "string") + param2 = double("param2", value: 2.0, type: "number") allow(subject).to receive(:vro_client).and_return(client) allow(client).to receive(:token).and_return(token) allow(token).to receive(:output_parameters).and_return( - 'key1' => param1, - 'key2' => param2 + "key1" => param1, + "key2" => param2 ) # squelch output during testing @@ -289,75 +289,75 @@ allow(subject).to receive(:msg_pair) end - it 'prints a header and a blank line' do - header = subject.ui.color('Output Parameters:', :bold) + it "prints a header and a blank line" do + header = subject.ui.color("Output Parameters:", :bold) expect(subject.ui).to receive(:msg).with(header) - expect(subject.ui).to receive(:msg).with('') + expect(subject.ui).to receive(:msg).with("") subject.print_output_parameters end - it 'prints out the output parameters' do - expect(subject).to receive(:msg_pair).with('key1', 'value1 (string)') - expect(subject).to receive(:msg_pair).with('key2', '2.0 (number)') + it "prints out the output parameters" do + expect(subject).to receive(:msg_pair).with("key1", "value1 (string)") + expect(subject).to receive(:msg_pair).with("key2", "2.0 (number)") subject.print_output_parameters end end end - describe '#print_execution_log' do - context 'when the log is empty' do + describe "#print_execution_log" do + context "when the log is empty" do before do - client = double('client', log: nil) + client = double("client", log: nil) allow(subject).to receive(:vro_client).and_return(client) end - it 'does not print any information' do + it "does not print any information" do expect(subject.ui).not_to receive(:msg) subject.print_execution_log end end - context 'when the log exists' do + context "when the log exists" do before do - client = double('client', log: 'log entries go here') + client = double("client", log: "log entries go here") allow(subject).to receive(:vro_client).and_return(client) end - it 'prints a header and the log entry' do - header = subject.ui.color('Workflow Execution Log:', :bold) + it "prints a header and the log entry" do + header = subject.ui.color("Workflow Execution Log:", :bold) expect(subject.ui).to receive(:msg).with(header) - expect(subject.ui).to receive(:msg).with('log entries go here') + expect(subject.ui).to receive(:msg).with("log entries go here") subject.print_execution_log end end end - describe '#set_parameters' do - it 'sets the instance variables correctly' do + describe "#set_parameters" do + it "sets the instance variables correctly" do subject.set_parameters expect(subject.workflow_name).to eq(workflow_name) expect(subject.workflow_id).to eq(workflow_id) expect(subject.parameters).to eq( - 'key1' => 'value1', - 'key2' => '2' + "key1" => "value1", + "key2" => "2" ) end end - describe '#run' do - it 'calls the correct methods' do + describe "#run" do + it "calls the correct methods" do expect(subject).to receive(:validate!) expect(subject).to receive(:set_parameters) - expect(subject.ui).to receive(:msg).with('Starting workflow execution...') - expect(subject).to receive(:execute_workflow).and_return('12345') - expect(subject.ui).to receive(:msg).with('Workflow execution 12345 started. Waiting for it to complete...') + expect(subject.ui).to receive(:msg).with("Starting workflow execution...") + expect(subject).to receive(:execute_workflow).and_return("12345") + expect(subject.ui).to receive(:msg).with("Workflow execution 12345 started. Waiting for it to complete...") expect(subject).to receive(:wait_for_workflow) - expect(subject.ui).to receive(:msg).with('Workflow execution complete.') + expect(subject.ui).to receive(:msg).with("Workflow execution complete.") expect(subject).to receive(:print_results) subject.run