diff --git a/.gitignore b/.gitignore index 68be75f4e7..5611f34fc6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,10 @@ tags /pkg /doc Gemfile.lock +nbproject/* +*.gem +.idea/* +.project + + .DS_Store \ No newline at end of file diff --git a/README.md b/README.md index c03234c65b..bbe172ca9f 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ This project provides a Ruby package that makes it easy to access and manage Win * Virtual Machines * create linux based VMs and ssh with cert and key option enabled for ssh and WINRM (both http & https)enabled for windows based VMs * list, shut down, delete, find virtual machine deployments. While shutting down your VMs the provisioning state would be deallocated and this VM will not be included in the billing cycle. + * Create VM for a specific virtual network * Cloud Services * create, list, delete cloud services * Storage Accounts @@ -400,23 +401,25 @@ params = { :vm_user => 'azureuser', :image => '5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-63APR20130415', :password => 'Password', - :location => 'West US' + :location => 'West US', + :affinity_group_name => 'affinity1' } options = { :storage_account_name => 'storage_suse', :winrm_transport => ['https','http'], #Currently http is supported. To enable https, set the transport protocol to https, simply rdp to the VM once VM is in ready state, export the certificate ( CN name would be the deployment name) from the certstore of the VM and install to your local machine and communicate WinRM via https. :cloud_service_name => 'cloud_service_name', - :deployment_name =>'mydeployment', + :deployment_name =>'vm_name', :tcp_endpoints => '80,3389:3390', :private_key_file => 'c:/private_key.key', #required for ssh or winrm(https) certificate. :certificate_file => 'c:/certificate.pem', #required for ssh or winrm(https) certificate. :ssh_port => 2222, - :vm_size => 'Small' #valid choices are (ExtraSmall, Small, Medium, Large, ExtraLarge, A6, A7) + :vm_size => 'Small', #valid choices are (ExtraSmall, Small, Medium, Large, ExtraLarge, A6, A7) + :virtual_network_name => 'xplattestvnet' } virtual_machine_service.create_virtual_machine(params, options) #Get a list of available virtual machine images -virtual_machine_image_service = Azure::VirtualMachineImageService.new +virtual_machine_image_service = Azure::VirtualMachineImageManagementService.new virtual_machine_image_service.list_virtual_machine_images #Get a list of available regional data center locations @@ -499,7 +502,7 @@ require 'azure' #Create a virtual network service object -vnet = Azure::VirtualNetworkManagement::VirtualNetworkManagementService.new +vnet = Azure::VirtualNetworkManagementService.new #API to get a list of virtual networks created for a subscription. @@ -515,14 +518,14 @@ dns_servers = [{:name => 'dns-1', :ip_address=>'1.2.3.4'}, {:name => 'dns-2', options = {:subnet => subnets, :dns => dns_servers} -vnet.set_network_configuration ('virtual-network-name', 'affinity-group-name', address_space, options) +vnet.set_network_configuration('virtual-network-name', 'affinity-group-name', address_space, options) #API to configure virtual network from xml file that can be exported from management portal and customized to add or delete vnet vnetxml = './customnetwork.xml' -vnet.set_network_configuration (vnetxml) - +vnet.set_network_configuration(vnetxml) +``` # Need Help? Be sure to check out the Windows Azure [Developer Forums on Stack Overflow and MSDN](http://go.microsoft.com/fwlink/?LinkId=234489) if you have trouble with the provided code. # Contribute Code or Provide Feedback diff --git a/lib/azure.rb b/lib/azure.rb index a9e1a2118c..58959396e9 100644 --- a/lib/azure.rb +++ b/lib/azure.rb @@ -14,31 +14,30 @@ #-------------------------------------------------------------------------- module Azure; end -require "azure/core" -require "azure/blob/blob_service" -require "azure/queue/queue_service" -require "azure/service_bus/service_bus_service" -require "azure/service_bus/empty_rule_action" -require "azure/service_bus/sql_rule_action" -require "azure/service_bus/sql_filter" -require "azure/service_bus/true_filter" -require "azure/service_bus/correlation_filter" -require "azure/table/table_service" -require "azure/table/batch" -require "azure/table/query" +require 'azure/core' +require 'azure/blob/blob_service' +require 'azure/queue/queue_service' +require 'azure/service_bus/service_bus_service' +require 'azure/service_bus/empty_rule_action' +require 'azure/service_bus/sql_rule_action' +require 'azure/service_bus/sql_filter' +require 'azure/service_bus/true_filter' +require 'azure/service_bus/correlation_filter' +require 'azure/table/table_service' +require 'azure/table/batch' +require 'azure/table/query' +require 'azure/core/utility' +require 'azure/base_management/management_http_request' +require 'azure/base_management/base_management_service' +require 'azure/virtual_machine_image_management/virtual_machine_image_management_service' +require 'azure/virtual_machine_management/virtual_machine_management_service' +require 'azure/storage_management/storage_management_service' +require 'azure/cloud_service_management/cloud_service_management_service' +require 'azure/base_management/location' +require 'azure/sql_database_management/sql_database_management_service' +require 'azure/virtual_network_management/virtual_network_management_service' -require "azure/core/utility" -require "azure/base_management/management_http_request" -require "azure/base_management/base_management_service" -require "azure/virtual_machine_image_management/virtual_machine_image_management_service" -require "azure/virtual_machine_management/virtual_machine_management_service" -require "azure/storage_management/storage_management_service" -require "azure/cloud_service_management/cloud_service_management_service" -require "azure/base_management/location" -require "azure/sql_database_management/sql_database_management_service" -require "azure/virtual_network_management/virtual_network_management_service" - -# add some aliases for convenience +# add some aliases for convenience Azure::BlobService = Azure::Blob::BlobService Azure::QueueService = Azure::Queue::QueueService Azure::TableService = Azure::Table::TableService @@ -50,4 +49,3 @@ module Azure; end Azure::VirtualMachineManagementService = Azure::VirtualMachineManagement::VirtualMachineManagementService Azure::SqlDatabaseManagementService = Azure::SqlDatabaseManagement::SqlDatabaseManagementService Azure::VirtualNetworkManagementService = Azure::VirtualNetworkManagement::VirtualNetworkManagementService - diff --git a/lib/azure/base_management/affinity_group.rb b/lib/azure/base_management/affinity_group.rb index 7fc83e9d31..0c770a67f4 100644 --- a/lib/azure/base_management/affinity_group.rb +++ b/lib/azure/base_management/affinity_group.rb @@ -14,8 +14,8 @@ #-------------------------------------------------------------------------- module Azure module BaseManagement + # Represents an AffinityGroup class AffinityGroup - def initialize yield self if block_given? end diff --git a/lib/azure/base_management/base_management_service.rb b/lib/azure/base_management/base_management_service.rb index e6058a4733..bb65fbb98d 100644 --- a/lib/azure/base_management/base_management_service.rb +++ b/lib/azure/base_management/base_management_service.rb @@ -16,11 +16,11 @@ require 'nokogiri' require 'base64' require 'openssl' -require "uri" -require "rexml/document" -require "azure/base_management/serialization" -require "azure/base_management/location" -require "azure/base_management/affinity_group" +require 'uri' +require 'rexml/document' +require 'azure/base_management/serialization' +require 'azure/base_management/location' +require 'azure/base_management/affinity_group' include Azure::BaseManagement include Azure::Core::Utility @@ -29,22 +29,23 @@ module Azure module BaseManagement class BaseManagementService - def initialize validate_configuration - cert_file = File.read (Azure.config.management_certificate) + cert_file = File.read(Azure.config.management_certificate) begin if Azure.config.management_certificate =~ /(pem)$/ certificate_key = OpenSSL::X509::Certificate.new(cert_file) private_key = OpenSSL::PKey::RSA.new(cert_file) else - #Parse pfx content + # Parse pfx content cert_content = OpenSSL::PKCS12.new(Base64.decode64(cert_file)) - certificate_key = OpenSSL::X509::Certificate.new(cert_content.certificate.to_pem) + certificate_key = OpenSSL::X509::Certificate.new( + cert_content.certificate.to_pem + ) private_key = OpenSSL::PKey::RSA.new(cert_content.key.to_pem) end rescue Exception => e - raise RuntimeError, "Management certificate not valid. Error: #{e.message}" + raise "Management certificate not valid. Error: #{e.message}" end Azure.configure do |config| @@ -54,17 +55,28 @@ def initialize end def validate_configuration - raise RuntimeError, "Subscription ID not valid." if Azure.config.subscription_id.nil? or Azure.config.subscription_id.empty? - raise RuntimeError, "Management endpoint not valid." if Azure.config.management_endpoint.nil? or Azure.config.management_endpoint.empty? - raise RuntimeError, "Could not read from file '#{Azure.config.management_certificate}'." unless (test 'r', Azure.config.management_certificate) - raise RuntimeError, "Management certificate expects a .pem or .pfx file." unless(Azure.config.management_certificate =~ /(pem|pfx)$/) + subs_id = Azure.config.subscription_id + error_message = 'Subscription ID not valid.' + raise error_message if subs_id.nil? || subs_id.empty? + + m_ep = Azure.config.management_endpoint + error_message = 'Management endpoint not valid.' + raise error_message if m_ep.nil? || m_ep.empty? + + m_cert = Azure.config.management_certificate + error_message = "Could not read from file '#{m_cert}'." + raise error_message unless test('r', m_cert) + + m_cert = Azure.config.management_certificate + error_message = 'Management certificate expects a .pem or .pfx file.' + raise error_message unless m_cert =~ /(pem|pfx)$/ end # Public: Gets a list of regional data center locations from the server # # Returns an array of Azure::BaseManagement::Location objects def list_locations - request = ManagementHttpRequest.new(:get, "/locations") + request = ManagementHttpRequest.new(:get, '/locations') response = request.call Serialization.locations_from_xml(response) end @@ -76,7 +88,7 @@ def list_locations # # Returns an array of Azure::BaseManagement::AffinityGroup objects def list_affinity_groups - request_path = "/affinitygroups" + request_path = '/affinitygroups' request = ManagementHttpRequest.new(:get, request_path, nil) response = request.call Serialization.affinity_groups_from_xml(response) @@ -87,26 +99,37 @@ def list_affinity_groups # ==== Attributes # # * +name+ - String. Affinity Group name. - # * +location+ - String. The location where the affinity group will be created. - # * +label+ - String. Name for the affinity specified as a base-64 encoded string. + # * +location+ - String. The location where the affinity group will + # be created. + # * +label+ - String. Name for the affinity specified as a + # base-64 encoded string. # # ==== Options # # Accepted key/value pairs are: - # * +:description+ - String. A description for the affinity group. (optional) - # + # * +:description+ - String. A description for the affinity group. + # (optional) + # # See http://msdn.microsoft.com/en-us/library/windowsazure/gg715317.aspx # # Returns: None - def create_affinity_group(name, location, label, options={}) - if name.nil? or name.strip.empty? - raise "Affinity Group name cannot be empty" - elsif list_affinity_groups.collect(&:name).include?(name) - raise Azure::Error::Error.new("ConflictError", 409, "An affinity group #{name} already exists in the current subscription.") + def create_affinity_group(name, location, label, options = {}) + if name.nil? || name.strip.empty? + raise 'Affinity Group name cannot be empty' + elsif list_affinity_groups.map(&:name).include?(name) + raise Azure::Error::Error.new( + 'ConflictError', + 409, + "An affinity group #{name}"\ + " already exists in the current subscription." + ) else validate_location(location) - body = Serialization.affinity_group_to_xml(name, location, label, options) - request_path = "/affinitygroups" + body = Serialization.affinity_group_to_xml(name, + location, + label, + options) + request_path = '/affinitygroups' request = ManagementHttpRequest.new(:post, request_path, body) request.call Loggerx.info "Affinity Group #{name} is created." @@ -119,18 +142,20 @@ def create_affinity_group(name, location, label, options={}) # ==== Attributes # # * +name+ - String. Affinity Group name. - # * +label+ - String. Name for the affinity specified as a base-64 encoded string. + # * +label+ - String. Name for the affinity specified as a + # base-64 encoded string. # # ==== Options # # Accepted key/value pairs are: - # * +:description+ - String. A description for the affinity group. (optional) + # * +:description+ - String. A description for the affinity group. + # (optional) # # See http://msdn.microsoft.com/en-us/library/windowsazure/gg715316.aspx # # Returns: None - def update_affinity_group(name, label, options={}) - raise "Label name cannot be empty" if label.nil? or label.empty? + def update_affinity_group(name, label, options = {}) + raise 'Label name cannot be empty' if label.nil? || label.empty? if affinity_group(name) body = Serialization.resource_to_xml(label, options) request_path = "/affinitygroups/#{name}" @@ -178,9 +203,16 @@ def get_affinity_group(name) end private + def affinity_group(affinity_group_name) - if affinity_group_name.nil? or affinity_group_name.empty? or !list_affinity_groups.map{|x| x.name.downcase}.include?(affinity_group_name.downcase) - error = Azure::Error::Error.new("AffinityGroupNotFound", 404, "The affinity group does not exist.") + if affinity_group_name.nil? ||\ + affinity_group_name.empty? ||\ + !list_affinity_groups.map { |x| x.name.downcase }.include?( + affinity_group_name.downcase + ) + error = Azure::Error::Error.new('AffinityGroupNotFound', + 404, + 'The affinity group does not exist.') raise error else true @@ -188,13 +220,15 @@ def affinity_group(affinity_group_name) end def validate_location(location_name) - locations = Azure::BaseManagementService.new.list_locations.collect(&:name) - if !locations.map(&:downcase).include?(location_name.downcase) - error = "Value '#{location_name}' specified for parameter 'location' is invalid. Allowed values are #{locations.join(',')}" + base_mgmt_service = Azure::BaseManagementService.new + locations = base_mgmt_service.list_locations.map(&:name) + unless locations.map(&:downcase).include?(location_name.downcase) + error = "Value '#{location_name}' specified for parameter"\ + " 'location' is invalid."\ + " Allowed values are #{locations.join(',')}" raise error end end - end end end diff --git a/lib/azure/base_management/serialization.rb b/lib/azure/base_management/serialization.rb index 53b5b04601..8f04d3c87b 100644 --- a/lib/azure/base_management/serialization.rb +++ b/lib/azure/base_management/serialization.rb @@ -16,83 +16,114 @@ module Azure module BaseManagement module Serialization - def self.locations_from_xml(locationXML) location_objs = [] xml = locationXML.css('Locations Location') xml.each do |meta_node| loc = Location.new loc.name = xml_content(meta_node, 'Name') - loc.available_services = meta_node.css('AvailableServices').children.to_ary.join(", ") + available_services = meta_node.css('AvailableServices').children + loc.available_services = available_services.to_ary.join(', ') location_objs << loc end location_objs end - def self.affinity_group_to_xml(name, location, label, options={}) + def self.affinity_group_to_xml(name, location, label, options = {}) builder = Nokogiri::XML::Builder.new do |xml| - xml.CreateAffinityGroup('xmlns'=>'http://schemas.microsoft.com/windowsazure') { + xml.CreateAffinityGroup( + 'xmlns' => 'http://schemas.microsoft.com/windowsazure' + ) do xml.Name name xml.Label Base64.encode64(label).strip xml.Description options[:description] xml.Location location - } + end end builder.doc.to_xml end - def self.affinity_groups_from_xml(affinityXML) + def self.affinity_groups_from_xml(affinity_xml) affinity_groups = [] - affinity_group_servicesXML = affinityXML.css('AffinityGroups AffinityGroup') - affinity_group_servicesXML.each do |ag_xml| + affinity_group_services_xml = affinity_xml.css( + 'AffinityGroups AffinityGroup' + ) + + affinity_group_services_xml.each do |ag_xml| affinity_group = AffinityGroup.new affinity_group.name = xml_content(ag_xml, 'Name') affinity_group.label = Base64.decode64(xml_content(ag_xml, 'Label')) affinity_group.description = xml_content(ag_xml, 'Description') affinity_group.location = xml_content(ag_xml, 'Location') - affinity_group.capability = ag_xml.css('Capabilities Capability').collect {|x| x.content} + + capabilities = ag_xml.css('Capabilities Capability') + affinity_group.capability = capabilities.map { |x| x.content } affinity_groups << affinity_group end affinity_groups.compact end - def self.affinity_group_from_xml(affinityXML) - hosted_services_xml = affinityXML.css('AffinityGroup HostedServices HostedService') - storage_services_xml = affinityXML.css('AffinityGroup StorageServices StorageService') - capabilities_xml = affinityXML.css('AffinityGroup Capabilities Capability') + def self.affinity_group_from_xml(affinity_xml) + hosted_services_xml = affinity_xml.css( + 'AffinityGroup HostedServices HostedService' + ) + + storage_services_xml = affinity_xml.css( + 'AffinityGroup StorageServices StorageService' + ) + + capability_xml = affinity_xml.css( + 'AffinityGroup Capabilities Capability' + ) + AffinityGroup.new do |affinity_group| - affinity_group.name = xml_content(affinityXML, 'AffinityGroup Name') - affinity_group.label = Base64.decode64(xml_content(affinityXML, 'AffinityGroup Label')) - affinity_group.description = xml_content(affinityXML, 'AffinityGroup Description') - affinity_group.location = xml_content(affinityXML, 'AffinityGroup Location') + affinity_group.name = xml_content( + affinity_xml, + 'AffinityGroup Name' + ) + affinity_group.label = Base64.decode64( + xml_content( + affinity_xml, + 'AffinityGroup Label' + ) + ) + affinity_group.description = xml_content( + affinity_xml, + 'AffinityGroup Description' + ) + affinity_group.location = xml_content( + affinity_xml, + 'AffinityGroup Location' + ) affinity_group.hosted_services = [] hosted_services_xml.each do |hosted_service_xml| affinity_group.hosted_services << { - :url=>xml_content(hosted_service_xml, 'Url'), - :service_name=>xml_content(hosted_service_xml,'ServiceName') + url: xml_content(hosted_service_xml, 'Url'), + service_name: xml_content(hosted_service_xml, 'ServiceName') } end affinity_group.storage_services = [] storage_services_xml.each do |storage_service_xml| affinity_group.storage_services << { - :url => xml_content(storage_service_xml, 'Url'), - :service_name => xml_content(storage_service_xml, 'ServiceName') + url: xml_content(storage_service_xml, 'Url'), + service_name: xml_content(storage_service_xml, 'ServiceName') } end - affinity_group.capability = capabilities_xml.collect {|x| x.content} + affinity_group.capability = capability_xml.map { |x| x.content } end end - def self.resource_to_xml(label, options={}) + def self.resource_to_xml(label, options = {}) builder = Nokogiri::XML::Builder.new do |xml| - xml.UpdateAffinityGroup('xmlns'=>'http://schemas.microsoft.com/windowsazure') { + xml.UpdateAffinityGroup( + 'xmlns' => 'http://schemas.microsoft.com/windowsazure' + ) do xml.Label Base64.encode64(label).strip xml.Description options[:description] if options[:description] - } + end end builder.doc.to_xml end - end end end diff --git a/lib/azure/cloud_service_management/cloud_service.rb b/lib/azure/cloud_service_management/cloud_service.rb index a16fee41e7..c8be4cd49e 100644 --- a/lib/azure/cloud_service_management/cloud_service.rb +++ b/lib/azure/cloud_service_management/cloud_service.rb @@ -24,6 +24,7 @@ def initialize attr_accessor :name attr_accessor :description attr_accessor :location + attr_accessor :affinity_group end end diff --git a/lib/azure/cloud_service_management/serialization.rb b/lib/azure/cloud_service_management/serialization.rb index 924c0467d2..45e3470afa 100644 --- a/lib/azure/cloud_service_management/serialization.rb +++ b/lib/azure/cloud_service_management/serialization.rb @@ -24,8 +24,12 @@ def self.cloud_services_to_xml(name, options={}) xml.CreateHostedService('xmlns'=>'http://schemas.microsoft.com/windowsazure') { xml.ServiceName name xml.Label Base64.encode64(name) - xml.Description options[:description] || 'Explicitly created cloud service' - xml.Location options[:location] + xml.Description options[:description] || 'Explicitly created cloud service' + if !options[:affinity_group_name].nil? + xml.AffinityGroup options[:affinity_group_name] + else + xml.Location options[:location] + end } end builder.doc.to_xml @@ -37,6 +41,7 @@ def self.cloud_services_from_xml(cloudXML) cloud_services_xml.each do |cloud_service_xml| cloud = CloudService.new cloud.name = xml_content(cloud_service_xml, 'ServiceName') + cloud.affinity_group = xml_content(cloud_service_xml, 'HostedServiceProperties AffinityGroup') clouds << cloud end clouds.compact diff --git a/lib/azure/core/utility.rb b/lib/azure/core/utility.rb index b94fb7cb48..80570d9b4d 100644 --- a/lib/azure/core/utility.rb +++ b/lib/azure/core/utility.rb @@ -19,18 +19,18 @@ begin require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32|mingw32/ rescue LoadError - puts 'WARNING: Output will look weird on Windows unless you install the "win32console" gem.' + puts 'WARNING: Output will look weird on Windows unless'\ + ' you install the "win32console" gem.' end end module Azure module Error + # Azure Error class Error < Azure::Core::Error - attr :description - - attr :status_code - - attr :type + attr_reader :description + attr_reader :status_code + attr_reader :type def initialize(type, status, description) @type = type @@ -43,17 +43,14 @@ def initialize(type, status, description) module Core module Utility - - def random_string(str='azure',no_of_char=5) - str+(0...no_of_char).map{ ('a'..'z').to_a[rand(26)] }.join + def random_string(str = 'azure', no_of_char = 5) + str + (0...no_of_char).map { ('a'..'z').to_a[rand(26)] }.join end - def xml_content(xml, key, default='') + def xml_content(xml, key, default = '') content = default node = xml.at_css(key) - if node - content = node.text - end + content = node.text if node content end @@ -67,14 +64,12 @@ def locate_file(name) end end - def export_der(cert, key, pass=nil, name=nil) - begin - pkcs12 = OpenSSL::PKCS12.create(pass, name, key, cert) - Base64.encode64(pkcs12.to_der) - rescue Exception => e - puts e.message - abort - end + def export_der(cert, key, pass = nil, name = nil) + pkcs12 = OpenSSL::PKCS12.create(pass, name, key, cert) + Base64.encode64(pkcs12.to_der) + rescue Exception => e + puts e.message + abort end def export_fingerprint(certificate) @@ -82,11 +77,11 @@ def export_fingerprint(certificate) end def enable_winrm?(winrm_transport) - (!winrm_transport.nil? && (winrm_transport.select{|x| x.downcase == 'http' or x.downcase == 'https'}.size > 0)) + (!winrm_transport.nil? && (winrm_transport.select { |x| x.downcase == 'http' || x.downcase == 'https' }.size > 0)) end - end + # Logger module Logger class << self def info(msg) @@ -95,7 +90,7 @@ def info(msg) def error_with_exit(msg) puts msg.bold.red - raise RuntimeError, msg.bold.red + raise msg.bold.red end def warn(msg) @@ -110,57 +105,55 @@ def error(msg) def exception_message(msg) print msg.bold.red - raise RuntimeError, msg.bold.red + raise msg.bold.red end def success(msg) - msg_with_new_line = msg+"\n" + msg_with_new_line = msg + "\n" print msg_with_new_line.green end end end - end end class String - { :reset => 0, - :bold => 1, - :dark => 2, - :underline => 4, - :blink => 5, - :orange => 6, - :negative => 7, - :black => 30, - :red => 31, - :green => 32, - :yellow => 33, - :blue => 34, - :magenta => 35, - :cyan => 36, - :white => 37, + { reset: 0, + bold: 1, + dark: 2, + underline: 4, + blink: 5, + orange: 6, + negative: 7, + black: 30, + red: 31, + green: 32, + yellow: 33, + blue: 34, + magenta: 35, + cyan: 36, + white: 37, }.each do |key, value| define_method key do "\e[#{value}m" + self + "\e[0m" end end - end +# Code validate private/public IP acceptable ranges. class IPAddr - - PrivateRanges = [ - IPAddr.new("10.0.0.0/8"), - IPAddr.new("172.16.0.0/12"), - IPAddr.new("192.168.0.0/16") + PRIVATE_RANGES = [ + IPAddr.new('10.0.0.0/8'), + IPAddr.new('172.16.0.0/12'), + IPAddr.new('192.168.0.0/16') ] def private? return false unless self.ipv4? - PrivateRanges.each do |ipr| + PRIVATE_RANGES.each do |ipr| return true if ipr.include?(self) end - return false + false end def public? @@ -187,12 +180,11 @@ def validate_address_space(ip) end def address_prefix(ip, cidr) - ip+'/'+cidr.to_s + ip + '/' + cidr.to_s end - def valid? ip + def valid?(ip) (IPAddr.new(ip) rescue nil).nil? end end end - diff --git a/lib/azure/service_bus/brokered_message_serializer.rb b/lib/azure/service_bus/brokered_message_serializer.rb index 8d6a6d3f15..6deb7786bd 100644 --- a/lib/azure/service_bus/brokered_message_serializer.rb +++ b/lib/azure/service_bus/brokered_message_serializer.rb @@ -12,17 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "rubygems" -require "json" -require "time" -require "uri" +require 'rubygems' +require 'json' +require 'time' +require 'uri' -require "azure/service_bus/brokered_message" +require 'azure/service_bus/brokered_message' module Azure module ServiceBus + # BrokeredMessageSerializer class BrokeredMessageSerializer - PROPERTIES = { 'ContentType' => 'content_type', 'CorrelationId' => 'correlation_id', @@ -41,7 +41,7 @@ class BrokeredMessageSerializer 'ReplyToSessionId' => 'reply_to_session_id' }.freeze - attr :message + attr_reader :message def initialize(msg) @message = msg @@ -50,45 +50,57 @@ def initialize(msg) def self.get_from_http_response(response) props = JSON.parse(response.headers['brokerproperties']) BrokeredMessage.new(response.body) do |m| - m.location = URI(response.headers['location']) unless response.headers['location'].nil? - m.content_type = response.headers['content-type'] + loc_header = response.headers['location'] + m.location = URI(loc_header) unless loc_header.nil? + m.content_type = response.headers['content-type'] # String based properties - m.lock_token = props['LockToken'] - m.message_id = props['MessageId'] - m.label = props['Label'] - m.to = props['To'] - m.session_id = props['SessionID'] - m.correlation_id = props['CorrelationId'] - m.reply_to = props['ReplyTo'] - m.reply_to = props['ReplyTo'] - m.reply_to_session_id = props['ReplyToSessionId'] + m.lock_token = props['LockToken'] + m.message_id = props['MessageId'] + m.label = props['Label'] + m.to = props['To'] + m.session_id = props['SessionID'] + m.correlation_id = props['CorrelationId'] + m.reply_to = props['ReplyTo'] + m.reply_to = props['ReplyTo'] + m.reply_to_session_id = props['ReplyToSessionId'] # Time based properties - m.locked_until_utc = Time.parse(props['LockedUntilUtc']) unless props['LockedUntilUtc'].nil? - enqueued_time_utc = self.parse_dot_net_serialized_datetime(props['EnqueuedTimeUtc']) unless props['EnqueuedTimeUtc'].nil? - m.enqueued_time_utc = enqueued_time_utc unless enqueued_time_utc.nil? - m.scheduled_enqueue_time_utc = Time.parse(props['ScheduledEnqueueTimeUtc']) unless props['ScheduledEnqueueTimeUtc'].nil? + utc_lock = props['LockedUntilUtc'] + m.locked_until_utc = Time.parse(utc_lock) unless utc_lock.nil? + + enqueued_time_utc = parse_dot_net_serialized_datetime( + props['EnqueuedTimeUtc'] + ) unless props['EnqueuedTimeUtc'].nil? + m.enqueued_time_utc = enqueued_time_utc unless enqueued_time_utc.nil? + + m.scheduled_enqueue_time_utc = Time.parse( + props['ScheduledEnqueueTimeUtc'] + ) unless props['ScheduledEnqueueTimeUtc'].nil? # Numeric based properties - m.delivery_count = props['DeliveryCount'].to_i - m.sequence_number = props['SequenceNumber'].to_i - m.time_to_live = props['TimeToLive'].to_f + m.delivery_count = props['DeliveryCount'].to_i + m.sequence_number = props['SequenceNumber'].to_i + m.time_to_live = props['TimeToLive'].to_f # Custom Properties - header_names_black_list = [ - 'brokerproperties', - 'date', - 'transfer-encoding', - 'location', - 'server', - 'connection', - 'content-type', - 'content-length' - ] - props = response.headers.reject {|k,_| header_names_black_list.include?(k.downcase) } + header_names_black_list = %w( + brokerproperties + date + transfer-encoding + location + server + connection + content-type + content-length + ) + + props = response.headers.reject do |k, _| + header_names_black_list.include?(k.downcase) + end + props.each do |prop_name, value| - parsed = JSON.parse("{ \"" + prop_name + "\" : " + value + "}") + parsed = JSON.parse('{ "' + prop_name + '" : ' + value + '}') m.properties[prop_name] = parsed[prop_name] end end @@ -100,9 +112,9 @@ def self.get_from_http_response(response) def to_json hash = {} PROPERTIES.each do |p, u| - attr_name = u.encode("UTF-8") + attr_name = u.encode('UTF-8') value = @message.send(attr_name) - hash[p] = value.to_s.encode("UTF-8") unless value.nil? + hash[p] = value.to_s.encode('UTF-8') unless value.nil? end hash.to_json end @@ -114,12 +126,10 @@ def to_json def get_property_headers hash = {} @message.properties.each do |name, value| - if value != nil && value.class == Time - value = value.httpdate - end + value = value.httpdate if !value.nil? && value.class == Time - tmp = JSON.generate [ value ] - hash[name] = tmp[1..(tmp.length-2)] + tmp = JSON.generate [value] + hash[name] = tmp[1..(tmp.length - 2)] end hash end @@ -131,17 +141,13 @@ def get_property_headers # # Returns a Time instance def self.parse_dot_net_serialized_datetime(datetime) - begin - Time.parse(datetime) - rescue - milliseconds_in_second = 1000 - match = /\/Date\((\d+)\)\//.match(datetime) - if !match.nil? - ticks = match[1].to_i - time = Time.at(ticks / milliseconds_in_second) - else - nil - end + milliseconds_in_second = 1000 + match = /\/Date\((\d+)\)\//.match(datetime) + if !match.nil? + ticks = match[1].to_i + Time.at(ticks / milliseconds_in_second) + else + nil end end end diff --git a/lib/azure/storage_management/serialization.rb b/lib/azure/storage_management/serialization.rb index 840643e958..1a82a636cc 100644 --- a/lib/azure/storage_management/serialization.rb +++ b/lib/azure/storage_management/serialization.rb @@ -24,7 +24,11 @@ def self.storage_services_to_xml(name, options={}) xml.ServiceName name xml.Label Base64.encode64(name) xml.Description options[:description] || 'Explicitly created storage service' - xml.Location options[:location] + if !options[:affinity_group_name].nil? + xml.AffinityGroup options[:affinity_group_name] + else + xml.Location options[:location] + end } end builder.doc.to_xml diff --git a/lib/azure/virtual_machine_management/serialization.rb b/lib/azure/virtual_machine_management/serialization.rb index 472b62f848..f6ccf1ebf6 100644 --- a/lib/azure/virtual_machine_management/serialization.rb +++ b/lib/azure/virtual_machine_management/serialization.rb @@ -55,6 +55,9 @@ def self.deployment_to_xml(params, options) xml.DeploymentSlot 'Production' xml.Label Base64.encode64(options[:deployment_name]).strip xml.RoleList { xml.Role('i:type'=>'PersistentVMRole') } + if options[:virtual_network_name] + xml.VirtualNetworkName options[:virtual_network_name] + end } end builder.doc.at_css('Role') << role_to_xml(params, options).at_css('PersistentVMRole').children.to_s @@ -79,6 +82,11 @@ def self.role_to_xml(params, options) default_endpoints_to_xml(xml, options) tcp_endpoints_to_xml(xml, options[:tcp_endpoints]) if options[:tcp_endpoints] } + if options[:virtual_network_name] && options[:subnet_name] + xml.SubnetNames { + xml.SubnetName options[:subnet_name] + } + end } } xml.Label Base64.encode64(params[:vm_name]).strip @@ -131,7 +139,7 @@ def self.provisioning_configuration_to_xml(xml, params, options) if options[:winrm_transport].include?('https') xml.Listener { xml.Protocol 'Https' - xml.CertificateThumbprint params[:certificate][:fingerprint] + xml.CertificateThumbprint params[:certificate][:fingerprint] if params[:certificate][:fingerprint] } end } @@ -177,10 +185,11 @@ def self.tcp_endpoints_to_xml(xml, tcp_endpoints) ports = endpoint.split(':') xml.InputEndpoint { xml.LocalPort ports[0] - xml.Name 'TCP-PORT-' + ports[0] if ports.length > 1 + xml.Name 'TCP-PORT-' + ports[1] xml.Port ports[1] else + xml.Name 'TCP-PORT-' + ports[0] xml.Port ports[0] end xml.Protocol 'TCP' @@ -226,6 +235,8 @@ def self.virtual_machines_from_xml(deployXML, cloud_service_name) end end vm.ipaddress = xml_content(rolesXML, 'IpAddress') unless vm.ipaddress + vm.virtual_network_name = xml_content(deployXML.css('Deployment'), + 'VirtualNetworkName') vm end end diff --git a/lib/azure/virtual_machine_management/virtual_machine.rb b/lib/azure/virtual_machine_management/virtual_machine.rb index 421e54ac2a..ca0171c7cd 100644 --- a/lib/azure/virtual_machine_management/virtual_machine.rb +++ b/lib/azure/virtual_machine_management/virtual_machine.rb @@ -37,6 +37,8 @@ def initialize attr_accessor :image attr_accessor :os_type attr_accessor :disk_name + attr_accessor :virtual_network_name + attr_accessor :virtual_network def cloud_service_name @cloud_service_name ||= (random_string(@vm_name+'-service-') if @vm_name) diff --git a/lib/azure/virtual_machine_management/virtual_machine_management_service.rb b/lib/azure/virtual_machine_management/virtual_machine_management_service.rb index c1ca00b3f0..f7c96e5540 100644 --- a/lib/azure/virtual_machine_management/virtual_machine_management_service.rb +++ b/lib/azure/virtual_machine_management/virtual_machine_management_service.rb @@ -37,6 +37,19 @@ def list_virtual_machines response = request.call roles << Serialization.virtual_machines_from_xml(response,cloud_service.name) end + + vnet_service = Azure::VirtualNetworkManagementService.new + virtual_networks = vnet_service.list_virtual_networks + + roles.each do |role| + next if role.nil? + vnet = virtual_networks.select do |network| + network.name == role.virtual_network_name + end + + role.virtual_network = vnet.first unless vnet.nil? || vnet.empty? + end + roles.compact end @@ -75,7 +88,8 @@ def get_virtual_machine(name, cloud_service_name) # * +:storage_account_name+ - String. Name of storage account. # * +:cloud_service_name+ - String. Name of cloud service. # * +:deployment_name+ - String. A name for the deployment. - # * +:tcp_endpoints+ - String. Specifies the external port and internal port separated by a colon. + # * +:tcp_endpoints+ - String. Specifies the internal port and external/public port separated by a colon. + # You can map multiple internal and external ports by separating them with a comma. # * +:ssh_private_key_file+ - String. Path of private key file. # * +:ssh_certificate_file+ - String. Path of certificate file. # * +:ssh_port+ - Integer. Specifies the SSH port number. @@ -88,10 +102,24 @@ def create_virtual_machine(params, options={}) validate_deployment_params(params, options) options[:cloud_service_name] = generate_cloud_service_name(params[:vm_name]) unless options[:cloud_service_name] options[:storage_account_name] = generate_storage_account_name(params[:vm_name]) unless options[:storage_account_name] + optionals = {} + if options[:virtual_network_name] + virtual_network_service = Azure::VirtualNetworkManagementService.new + virtual_networks = virtual_network_service.list_virtual_networks.select{|x| x.name == options[:virtual_network_name]} + if virtual_networks.empty? + Loggerx.error_with_exit "Virtual network #{options[:virtual_network_name]} doesn't exists" + else + optionals[:affinity_group_name] = virtual_networks.first.affinity_group + end + elsif options[:affinity_group_name] + optionals[:affinity_group_name] = options[:affinity_group_name] + else + optionals[:location] = params[:location] + end cloud_service = Azure::CloudServiceManagementService.new - cloud_service.create_cloud_service(options[:cloud_service_name], :location => params[:location]) + cloud_service.create_cloud_service(options[:cloud_service_name], optionals) cloud_service.upload_certificate(options[:cloud_service_name],params[:certificate]) unless params[:certificate].empty? - Azure::StorageManagementService.new.create_storage_account(options[:storage_account_name], :location=> params[:location]) + Azure::StorageManagementService.new.create_storage_account(options[:storage_account_name], optionals) body = Serialization.deployment_to_xml(params,options) path = "/services/hostedservices/#{options[:cloud_service_name]}/deployments" @@ -250,7 +278,7 @@ def certificate_required?(params, options) def winrm_with_https(options) if options[:os_type] == 'Windows' - !options[:winrm_transport].nil? && options[:winrm_transport].include?('https') + options[:winrm_transport] && options[:winrm_transport].include?('https') && options[:certificate_file] && options[:private_key_file] end end diff --git a/lib/azure/virtual_network_management/serialization.rb b/lib/azure/virtual_network_management/serialization.rb index 27745866ab..6f28f24265 100644 --- a/lib/azure/virtual_network_management/serialization.rb +++ b/lib/azure/virtual_network_management/serialization.rb @@ -65,6 +65,7 @@ def self.virtual_network_to_xml(vnet_name, affinity_group, address_spaces, optio } } end + puts builder.doc.to_xml builder.doc.to_xml end @@ -100,10 +101,11 @@ def self.dns_server_to_xml(xml, dns_servers, new_dns_servers) xml.DnsServer('name' => dns[:name], 'IPAddress' => dns[:ip_address]) end dns_servers.each do |dns| - xml.DnsServer('name' => dns[:name], 'IPAddress' => dns[:ip_address]) unless dns_names.include?(dns[:name]) + xml.DnsServer('name' => dns[:name], + 'IPAddress' => dns[:ip_address]) unless\ + dns_names.include?(dns[:name]) end end - end end end diff --git a/lib/azure/virtual_network_management/virtual_network.rb b/lib/azure/virtual_network_management/virtual_network.rb index b0f6ad0f90..68aa5cd721 100644 --- a/lib/azure/virtual_network_management/virtual_network.rb +++ b/lib/azure/virtual_network_management/virtual_network.rb @@ -15,8 +15,8 @@ module Azure module VirtualNetworkManagement + # Represents a Virtual Network class VirtualNetwork - def initialize yield self if block_given? @address_space = [] @@ -31,7 +31,6 @@ def initialize attr_accessor :subnets attr_accessor :dns_servers attr_accessor :affinity_group - end end end diff --git a/lib/azure/virtual_network_management/virtual_network_management_service.rb b/lib/azure/virtual_network_management/virtual_network_management_service.rb index 091414768c..2d35f8fe80 100644 --- a/lib/azure/virtual_network_management/virtual_network_management_service.rb +++ b/lib/azure/virtual_network_management/virtual_network_management_service.rb @@ -16,17 +16,19 @@ module Azure module VirtualNetworkManagement + # VirtualNetworkManagementService class VirtualNetworkManagementService < BaseManagementService - def initialize super() end - # Public: Gets a list of virtual network services available under the current subscription. + # Public: Gets a list of virtual network services available under the + # current subscription. # # See http://msdn.microsoft.com/en-us/library/windowsazure/jj157185.aspx # - # Returns an array of Azure::VirtualNetworkServiceManagement::VirtualNetwork objects + # Returns an array of + # Azure::VirtualNetworkServiceManagement::VirtualNetwork objects def list_virtual_networks request_path = '/services/networking/virtualnetwork' request = ManagementHttpRequest.new(:get, request_path, nil) @@ -38,7 +40,7 @@ def set_network_configuration(*args) if args.length == 1 set_virtual_network_using_xml(args[0]) elsif args.length == 4 || args.length == 3 - args[3] ||= { :subnet => [], :dns => [] } + args[3] ||= { subnet: [], dns: [] } set_virtual_network(args[0], args[1], args[2], args[3]) else raise 'Wrong number of arguments' @@ -62,16 +64,26 @@ def set_network_configuration(*args) # # Accepted key/value pairs in options parameter are: # * +:subnets+ - Array. A hash of the name/value pairs. - # Contains the name, IPv4 address and Cidr of the DNS server. (optional) + # Contains the name, IPv4 address and Cidr of the DNS server. + # (optional) # * +dns_servers+ - Array. A hash of the name/value pairs. # Contains the name and IPv4 address of the DNS server. (optional) # # See http://msdn.microsoft.com/en-us/library/windowsazure/jj157181.aspx # # Returns None - def set_virtual_network(vnet, affinity_group, address_space, options={ :subnet => [], :dns => [] }) + def set_virtual_network(vnet, + affinity_group, + address_space, + options = { + subnet: [], + dns: [] + }) request_path = '/services/networking/media' - body = Serialization.virtual_network_to_xml(vnet, affinity_group, address_space, options) + body = Serialization.virtual_network_to_xml(vnet, + affinity_group, + address_space, + options) request = ManagementHttpRequest.new(:put, request_path, body) request.headers['Content-Type'] = 'text/plain' Loggerx.info "Creating virtual network #{vnet}." @@ -92,7 +104,6 @@ def set_virtual_network_using_xml(file) Loggerx.info 'Creating virtual network.' request.call end - end end end diff --git a/test/fixtures/list_virtual_networks.xml b/test/fixtures/list_virtual_networks.xml index e14d7872e7..bee61f0256 100644 --- a/test/fixtures/list_virtual_networks.xml +++ b/test/fixtures/list_virtual_networks.xml @@ -90,4 +90,3 @@ - diff --git a/test/fixtures/virtual_machine.xml b/test/fixtures/virtual_machine.xml index 36aace3ef0..3448974287 100644 --- a/test/fixtures/virtual_machine.xml +++ b/test/fixtures/virtual_machine.xml @@ -95,6 +95,7 @@ kZS1mYWNlNSI+DQogICAgPEluc3RhbmNlcyBjb3VudD0iMSIgLz4NCiAgPC9Sb2xlPg0KPC9TZXJ2aWN false false + test-virtual-network 2013-04-29T06:12:01Z 2013-04-29T10:45:19Z diff --git a/test/integration/affinity_group/Affinity_test.rb b/test/integration/affinity_group/Affinity_test.rb index 0dbb848347..f2c9f8d212 100644 --- a/test/integration/affinity_group/Affinity_test.rb +++ b/test/integration/affinity_group/Affinity_test.rb @@ -12,23 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "integration/test_helper" +require 'integration/test_helper' describe Azure::BaseManagementService do subject { Azure::BaseManagementService.new } let(:affinity_group_name) { AffinityGroupNameHelper.name } - describe "#affinity_group" do + describe '#affinity_group' do let(:label_name) { 'Label' } - let(:options) { {:description => 'Some Description'} } + let(:options) { { description: 'Some Description' } } - before{ - Loggerx.expects(:puts).returns(nil).at_least(0) - } + before do + Loggerx.expects(:puts).returns(nil).at_least(0) + end - it "get affinity group properties for an existing group" do - subject.create_affinity_group(affinity_group_name, 'West US', label_name, options) + it 'get affinity group properties for an existing group' do + subject.create_affinity_group(affinity_group_name, + WindowsImageLocation, + label_name, + options) affinity = subject.get_affinity_group(affinity_group_name) affinity.must_be_kind_of Azure::BaseManagement::AffinityGroup affinity.name.must_equal affinity_group_name @@ -38,16 +41,15 @@ affinity.capability.wont_equal [] AffinityGroupNameHelper.clean end - - it "get affinity group properties for an non existing affinity group name" do - affinity_group_name = 'unknown' + + it 'gets properties for an non existing affinity group name' do + affinity_group_name = 'unknown' begin - affinity = subject.get_affinity_group(affinity_group_name) + subject.get_affinity_group(affinity_group_name) rescue Azure::Error::Error => error error.status_code.must_equal 404 - error.type.must_equal "AffinityGroupNotFound" + error.type.must_equal 'AffinityGroupNotFound' end end - - end + end end diff --git a/test/integration/affinity_group/Create_Affinity_test.rb b/test/integration/affinity_group/Create_Affinity_test.rb index f749f277e6..74d6bd0b6c 100644 --- a/test/integration/affinity_group/Create_Affinity_test.rb +++ b/test/integration/affinity_group/Create_Affinity_test.rb @@ -12,24 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "integration/test_helper" +require 'integration/test_helper' describe Azure::BaseManagementService do subject { Azure::BaseManagementService.new } - let(:affinity_group_name) { random_string('affinity-group-',10) } - let(:location) { 'West US' } - - before { + let(:affinity_group_name) { random_string('affinity-group-', 10) } + let(:location) { WindowsImageLocation } + + before do Loggerx.expects(:puts).returns(nil).at_least(0) - } - let(:label){ 'Label Name' } - let(:options) { {:description => 'sample description'} } - - describe "#create_affinity_group" do - - it "create new affinity group with valid params" do - subject.create_affinity_group(affinity_group_name, location, label, options) + end + + let(:label) { 'Label Name' } + let(:options) { { description: 'sample description' } } + + describe '#create_affinity_group' do + + it 'create new affinity group with valid params' do + subject.create_affinity_group(affinity_group_name, + location, + label, + options) affinity_group = subject.get_affinity_group(affinity_group_name) affinity_group.must_be_kind_of Azure::BaseManagement::AffinityGroup affinity_group.name.wont_be_nil @@ -39,14 +43,14 @@ affinity_group.description.must_equal options[:description] end - it "errors if the affinity group location is not valid" do + it 'errors if the affinity group location is not valid' do exception = assert_raises(RuntimeError) do subject.create_affinity_group(affinity_group_name, 'North West', label) end assert_match(/Allowed values are East Asia/i, exception.message) end - - it "create new affinity group without optional params" do + + it 'create new affinity group without optional params' do subject.create_affinity_group(affinity_group_name, location, label) affinity_group = subject.get_affinity_group(affinity_group_name) affinity_group.must_be_kind_of Azure::BaseManagement::AffinityGroup diff --git a/test/integration/affinity_group/Delete_Affinity_test.rb b/test/integration/affinity_group/Delete_Affinity_test.rb index d5ba97242b..f1f7f6b4e7 100644 --- a/test/integration/affinity_group/Delete_Affinity_test.rb +++ b/test/integration/affinity_group/Delete_Affinity_test.rb @@ -12,42 +12,45 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "integration/test_helper" +require 'integration/test_helper' describe Azure::BaseManagementService do - before { + before do Loggerx.expects(:puts).returns(nil).at_least(0) - } + end subject { Azure::BaseManagementService.new } let(:affinity_group_name) { AffinityGroupNameHelper.name } - let(:location) { 'West US' } + let(:location) { WindowsImageLocation } let(:label) { 'Label Name' } - describe "#delete_affinity_group" do + describe '#delete_affinity_group' do - it "delete affinity group" do + it 'delete affinity group' do subject.create_affinity_group(affinity_group_name, location, label) subject.delete_affinity_group(affinity_group_name) - affinity_list = subject.list_affinity_groups.select{|x| x.name == affinity_group_name}.first + affinity_list = subject.list_affinity_groups.select do |x| + x.name == affinity_group_name + end.first affinity_list.must_be_nil end - it "errors if the affinity group does not exist" do - affinity_group_name = "unknown-server" + it 'errors if the affinity group does not exist' do + affinity_group_name = 'unknown-server' begin subject.delete_affinity_group(affinity_group_name) rescue Azure::Error::Error => error error.status_code.must_equal 404 - error.type.must_equal "AffinityGroupNotFound" + error.type.must_equal 'AffinityGroupNotFound' end end - - # Pending Test Case. - # it "errors while deleting an affinity group is associated with other services" do - # skip("Need API to associate hosted services and storage account with affinity group.") - # end - + +# Pending Test Case. +# it 'errors while deleting an affinity group associated with services' do +# skip('Need API to associate hosted services and'\ +# => ' storage account with affinity group.') +# end + end end diff --git a/test/integration/affinity_group/List_Affinity_test.rb b/test/integration/affinity_group/List_Affinity_test.rb index 44e8932b36..bd286f4f58 100644 --- a/test/integration/affinity_group/List_Affinity_test.rb +++ b/test/integration/affinity_group/List_Affinity_test.rb @@ -12,22 +12,24 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "integration/test_helper" +require 'integration/test_helper' describe Azure::BaseManagementService do subject { Azure::BaseManagementService.new } let(:affinity_group_name) { AffinityGroupNameHelper.name } - - before{ + + before do Loggerx.expects(:puts).returns(nil).at_least(0) - subject.create_affinity_group(affinity_group_name, 'West US', 'Label Name') - } - after { AffinityGroupNameHelper.clean } + subject.create_affinity_group(affinity_group_name, + WindowsImageLocation, + 'Label Name') + end - describe "#list_affinity_groups" do + after { AffinityGroupNameHelper.clean } - it "list affinity groups" do + describe '#list_affinity_groups' do + it 'list affinity groups' do affinity_groups = subject.list_affinity_groups affinity_group = affinity_groups.first affinity_groups.wont_be_nil @@ -35,6 +37,5 @@ affinity_group.must_be_kind_of Azure::BaseManagement::AffinityGroup assert_operator affinity_groups.size, :>=, 1 end - end end diff --git a/test/integration/affinity_group/Update_Affinity_test.rb b/test/integration/affinity_group/Update_Affinity_test.rb index 5c3d003cef..6ffbdf87fd 100644 --- a/test/integration/affinity_group/Update_Affinity_test.rb +++ b/test/integration/affinity_group/Update_Affinity_test.rb @@ -12,31 +12,35 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "integration/test_helper" +require 'integration/test_helper' describe Azure::BaseManagementService do - AffinityGroupName = random_string('affinity-group-',10) - Azure::BaseManagementService.new.create_affinity_group(AffinityGroupName, 'West US', 'Label') + AffinityGroupName = random_string('affinity-group-', 10) + Azure::BaseManagementService.new.create_affinity_group( + AffinityGroupName, + WindowsImageLocation, + 'Label' + ) - before { + before do Loggerx.expects(:puts).returns(nil).at_least(0) - } + end subject { Azure::BaseManagementService.new } let(:affinity_group_name) { AffinityGroupName } - let(:location) {'West US'} - - describe "#update_affinity_group" do - let(:options) { {:description =>'sample description'} } - - it "update affinity group" do + let(:location) { 'Central US' } + + describe '#update_affinity_group' do + let(:options) { { description: 'sample description' } } + + it 'update affinity group' do label = 'updated label' subject.update_affinity_group(affinity_group_name, label) affinity_group = subject.get_affinity_group(affinity_group_name) affinity_group.wont_be_nil affinity_group.label.must_equal label end - + it "update affinity group's description with valid input" do label = 'Updated Label' subject.update_affinity_group(affinity_group_name, label, options) @@ -56,21 +60,23 @@ affinity_group.label.must_equal label end - it "error in case of description content exceeds allowed limit of 1024 chars" do - options = {:description => 'a'*1025} + it 'error if description content exceeds allowed limit of 1024 chars' do + options = { description: 'a' * 1025 } exception = assert_raises(RuntimeError) do - subject.update_affinity_group(affinity_group_name, 'this is update operation', options) + subject.update_affinity_group(affinity_group_name, + 'this is update operation', + options) end - assert_match(/The description for the affinity group is invalid./i, exception.message) + assert_match(/The description for the affinity group is invalid./i, + exception.message) end - - it "error in case of updating non existing affinity group" do + + it 'error in case of updating non existing affinity group' do affinity_name = 'unkown-affinity-group' exception = assert_raises(Azure::Error::Error) do subject.update_affinity_group(affinity_name, 'updated label', options) end assert_match(/The affinity group does not exist./i, exception.message) end - - end + end end diff --git a/test/integration/database/create_sql_server_firewall_test.rb b/test/integration/database/create_sql_server_firewall_test.rb index c1d3dd65a4..926a6438b7 100644 --- a/test/integration/database/create_sql_server_firewall_test.rb +++ b/test/integration/database/create_sql_server_firewall_test.rb @@ -18,7 +18,7 @@ subject { Azure::SqlDatabaseManagementService.new } let(:login_name) {'ms_open_tech'} - let(:sql_server) { subject.create_server(login_name, 'User1@123', 'West US') } + let(:sql_server) { subject.create_server(login_name, 'User1@123', WindowsImageLocation) } describe "#set_sql_server_firewall_rule" do before { diff --git a/test/integration/database/create_sql_server_test.rb b/test/integration/database/create_sql_server_test.rb index 92f4ab3385..adc58ccdeb 100644 --- a/test/integration/database/create_sql_server_test.rb +++ b/test/integration/database/create_sql_server_test.rb @@ -30,9 +30,9 @@ } it "should be able to create a new sql database server." do - sql_server = subject.create_server(login_name, 'User@123', 'West US') + sql_server = subject.create_server(login_name, 'User@123', WindowsImageLocation) sql_server.name.wont_be_nil - sql_server.location.must_equal "West US" + sql_server.location.must_equal WindowsImageLocation sql_server.administrator_login.must_equal login_name end @@ -47,7 +47,7 @@ it "errors if the sql server passsword is invalid" do password = 'weak' exception = assert_raises(RuntimeError) do - subject.create_server(login_name, password, 'West US') + subject.create_server(login_name, password, WindowsImageLocation) end assert_match(/Password validation failed/i, exception.message) end diff --git a/test/integration/database/delete_sql_server_firewall_test.rb b/test/integration/database/delete_sql_server_firewall_test.rb index 55b82c65ca..1924ec1db2 100644 --- a/test/integration/database/delete_sql_server_firewall_test.rb +++ b/test/integration/database/delete_sql_server_firewall_test.rb @@ -17,7 +17,7 @@ describe Azure::SqlDatabaseManagementService do let(:login_name) {'ms_open_tech'} - let(:sql_server) { subject.create_server(login_name, 'User1@123', 'West US') } + let(:sql_server) { subject.create_server(login_name, 'User1@123', WindowsImageLocation) } subject { Azure::SqlDatabaseManagementService.new } before { diff --git a/test/integration/database/delete_sql_server_test.rb b/test/integration/database/delete_sql_server_test.rb index d594977442..36f076c092 100644 --- a/test/integration/database/delete_sql_server_test.rb +++ b/test/integration/database/delete_sql_server_test.rb @@ -27,7 +27,7 @@ subject { Azure::SqlDatabaseManagementService.new } let(:login_name) {'ms_open_tech'} - let(:sql_server) { subject.create_server(login_name, 'User1@123', 'West US') } + let(:sql_server) { subject.create_server(login_name, 'User1@123', WindowsImageLocation) } describe "#delete_sql_server" do diff --git a/test/integration/database/list_sql_server_firewall_test.rb b/test/integration/database/list_sql_server_firewall_test.rb index 61d18634e5..afdf50d3b8 100644 --- a/test/integration/database/list_sql_server_firewall_test.rb +++ b/test/integration/database/list_sql_server_firewall_test.rb @@ -18,7 +18,7 @@ subject { Azure::SqlDatabaseManagementService.new } let(:login_name) {'ms_open_tech'} - let(:sql_server) { subject.create_server(login_name, 'User1@123', 'West US') } + let(:sql_server) { subject.create_server(login_name, 'User1@123', WindowsImageLocation) } describe "#list_sql_server_firewall_rules" do before { diff --git a/test/integration/database/list_sql_servers_test.rb b/test/integration/database/list_sql_servers_test.rb index 1912e52d55..50dd116a0c 100644 --- a/test/integration/database/list_sql_servers_test.rb +++ b/test/integration/database/list_sql_servers_test.rb @@ -23,7 +23,7 @@ before { Loggerx.expects(:puts).returns(nil).at_least(0) Azure.config.management_endpoint = SqlServerEndpoint - subject.create_server(login_name, 'User1@123', 'West US') + subject.create_server(login_name, 'User1@123', WindowsImageLocation) subject.create_server(login_name, 'User2@123', 'East US') } diff --git a/test/integration/database/reset_password_sql_server_test.rb b/test/integration/database/reset_password_sql_server_test.rb index f3104539e5..6b267402a3 100644 --- a/test/integration/database/reset_password_sql_server_test.rb +++ b/test/integration/database/reset_password_sql_server_test.rb @@ -18,7 +18,7 @@ subject { Azure::SqlDatabaseManagementService.new } let(:login_name) {'ms_open_tech'} - let(:sql_server) { subject.create_server(login_name, 'User1@123', 'West US') } + let(:sql_server) { subject.create_server(login_name, 'User1@123', WindowsImageLocation) } describe "#reset_password" do diff --git a/test/integration/service_bus/scenario_test.rb b/test/integration/service_bus/scenario_test.rb index 7e141c5a91..eaeb121679 100644 --- a/test/integration/service_bus/scenario_test.rb +++ b/test/integration/service_bus/scenario_test.rb @@ -12,33 +12,31 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "integration/test_helper" +require 'integration/test_helper' -require "azure/service_bus/brokered_message" +require 'azure/service_bus/brokered_message' class ScenarioHelper - def ScenarioHelper.out (s) - if (false) - print s + "\n" - end + def self.out(s) + print s + "\n" if false end - def ScenarioHelper.get_custom_properties(i) + def self.get_custom_properties(i) custom_properties = {} - custom_properties['i'] = i; + custom_properties['i'] = i custom_properties['test'] = Time.local(2001, i, 1) - custom_properties['name'] = 'Test' + i.to_s; - custom_properties['meanname'] = "'\"Me`\\'&*<>!@#%^*)\n" + i.to_s; - custom_properties['int'] = 50 + i; - custom_properties['float'] = 3.14159 + i; - custom_properties['even'] = (i % 2 == 0); - custom_properties; + custom_properties['name'] = 'Test' + i.to_s + custom_properties['meanname'] = "'\"Me`\\'&*<>!@#%^*)\n" + i.to_s + custom_properties['int'] = 50 + i + custom_properties['float'] = 3.14159 + i + custom_properties['even'] = (i % 2 == 0) + custom_properties end - def ScenarioHelper.create_issue_message(issue_id, issue_body, label) + def self.create_issue_message(issue_id, issue_body, label) message = Azure::ServiceBus::BrokeredMessage.new(issue_body) - message.correlation_id = "correlationid" + label + message.correlation_id = 'correlationid' + label message.delivery_count = 1 message.sequence_number = 12 message.content_type = 'text/xml' @@ -47,14 +45,16 @@ def ScenarioHelper.create_issue_message(issue_id, issue_body, label) message.message_id = issue_id custom_properties = ScenarioHelper.get_custom_properties issue_id.to_i - custom_properties.each { |key, value| + custom_properties.each do |key, value| message.properties[key] = value - } + end message end - def ScenarioHelper.compare_messages(expected, actual, custom_properties = nil) + def self.compare_messages(expected, + actual, + custom_properties = nil) actual.body.must_equal expected.body actual.content_type.must_equal expected.content_type actual.correlation_id.must_equal expected.correlation_id @@ -62,41 +62,40 @@ def ScenarioHelper.compare_messages(expected, actual, custom_properties = nil) actual.message_id.must_equal expected.message_id actual.reply_to.must_equal expected.reply_to actual.reply_to_session_id.must_equal expected.reply_to_session_id - actual.scheduled_enqueue_time_utc.must_equal expected.scheduled_enqueue_time_utc + actual.scheduled_enqueue_time_utc.must_equal( + expected.scheduled_enqueue_time_utc + ) actual.session_id.must_equal expected.session_id actual.to.must_equal expected.to # Note: The following properties are controlled by the server, # so we cannot verify as much - actual.delivery_count.must_be :kind_of?, Integer - actual.sequence_number.must_be :kind_of?, Integer - actual.enqueued_time_utc.must_be :kind_of?, Time - if actual.lock_token != nil - actual.lock_token.must_be :kind_of?, String - end - if actual.locked_until_utc != nil - actual.locked_until_utc.must_be :kind_of?, Time - end + actual.delivery_count.must_be_kind_of Integer + actual.sequence_number.must_be_kind_of Integer + actual.enqueued_time_utc.must_be_kind_of Time + actual.lock_token.must_be_kind_of String unless actual.lock_token.nil? - if custom_properties == nil - expected_properties = expected.properties; - index = expected_properties["i"]; - if (index == nil) - index = 1; - end - custom_properties = get_custom_properties(index.to_i); + actual.locked_until_utc.must_be_kind_of Time\ + unless actual.locked_until_utc.nil? + + if custom_properties.nil? + expected_properties = expected.properties + index = expected_properties['i'] + index = 1 if index.nil? + custom_properties = get_custom_properties(index.to_i) end - custom_properties.each { |key, value| - ScenarioHelper.out key + ":" + value.to_s - ScenarioHelper.out key.downcase + ":" + (actual.properties[key.downcase] == nil ? "" : actual.properties[key.downcase].to_s) - if value != "GUID" - if value != nil && value.class == Time - value.httpdate.must_equal actual.properties[key.downcase] + custom_properties.each do |key, value| + ScenarioHelper.out key + ':' + value.to_s + k_d = actual.properties[key.downcase] + ScenarioHelper.out("#{k_d}:#{k_d.nil? ? '' : k_d.to_s}") + if value != 'GUID' + if !value.nil? && value.class == Time + value.httpdate.must_equal k_d else - value.must_equal actual.properties[key.downcase] + value.must_equal(k_d) end end - } + end end end diff --git a/test/integration/vm/VM_Create_test.rb b/test/integration/vm/VM_Create_test.rb index 8c893b4906..a8cd28df42 100644 --- a/test/integration/vm/VM_Create_test.rb +++ b/test/integration/vm/VM_Create_test.rb @@ -45,9 +45,13 @@ } } + let(:in_vnet_name) { 'vm-test-virtual-network-3' } + let(:options){ { - :storage_account_name => storage_account_name + :storage_account_name => storage_account_name, + :virtual_network_name => in_vnet_name, + :subnet_name => 'Subnet-1' } } @@ -58,13 +62,26 @@ } } + let(:in_affinity_name) { 'integration-test-affinity-group' } + let(:in_address_space) { ['172.16.0.0/12'] } + inputoptions = { + :subnet => [{ :name => 'Subnet-1', :ip_address => '172.16.0.0', :cidr => 12 }], + :dns => [{ :name => 'DNS', :ip_address => '1.2.3.4' }] + } + before{ Loggerx.expects(:puts).returns(nil).at_least(0) + + affinity_group_service = Azure::BaseManagementService.new + affinity_group_service.create_affinity_group(in_affinity_name, WindowsImageLocation, 'Label') rescue nil + + virtual_network_service = Azure::VirtualNetworkManagementService.new + virtual_network_service.set_network_configuration(in_vnet_name, in_affinity_name, in_address_space, inputoptions) } describe "#deployment" do - it "should set options hash with valid cloud_service_name, deployment_name, storage_account_name." do + it "should set options hash with valid cloud_service_name, deployment_name, storage_account_name and virtual network" do subject.create_virtual_machine(params, options) cloud_name = options[:cloud_service_name] virtual_machine = subject.get_virtual_machine(virtual_machine_name, cloud_name) @@ -76,10 +93,12 @@ virtual_machine.os_type.must_equal 'Linux' options[:storage_account_name].wont_be_nil assert_match(/^#{params[:vm_name]+'-service'}*/, cloud_name) + virtual_machine.virtual_network_name.must_equal in_vnet_name + virtual_machine.virtual_network.must_be_kind_of Azure::VirtualNetworkManagement::VirtualNetwork end - it "should creates http and https enabled winrm virtual machine." do - default_options.merge!(:winrm_transport => ['https','http'], :private_key_file => private_key, :certificate_file => certificate) + it "should creates http and https enabled winrm virtual machine without certificate." do + default_options.merge!(:winrm_transport => ['https','http']) subject.create_virtual_machine(windows_params, default_options) result = subject.get_virtual_machine(virtual_machine_name, cloud_service_name) result.must_be_kind_of Azure::VirtualMachineManagement::VirtualMachine @@ -93,7 +112,7 @@ sleep 30 end - it "should creates only https enabled winrm virtual machine." do + it "should creates https enabled winrm virtual machine using certificate." do default_options.merge!(:winrm_transport => ['https'], :private_key_file => private_key, :certificate_file => certificate) subject.create_virtual_machine(windows_params, default_options) result = subject.get_virtual_machine(virtual_machine_name, cloud_service_name) @@ -120,7 +139,7 @@ assert (not tcp_endpoints_names.include? "WinRm-Http") end - it "created vm should be accessible using password and certificate" do + it "created linux virtual machine should be accessible using password and certificate" do default_options.merge!(:private_key_file => private_key, :certificate_file => certificate) subject.create_virtual_machine(params, default_options) result = subject.get_virtual_machine(virtual_machine_name, cloud_service_name) diff --git a/test/integration/vnet/Virtual_Network_Create_test.rb b/test/integration/vnet/Virtual_Network_Create_test.rb index fb874be73f..d69d52ef54 100644 --- a/test/integration/vnet/Virtual_Network_Create_test.rb +++ b/test/integration/vnet/Virtual_Network_Create_test.rb @@ -22,71 +22,101 @@ let(:invalid_address_space) { ['', ''] } let(:invalid_cidr_address_space) { ['10.0.0.0'] } let(:created_state) { 'Created' } + let(:xml_err_msg) { 'XML Schema validation error in network configuration' } inputoptions = { - :subnet => [{ :name => 'Subnet-1', :ip_address => '172.16.0.0', :cidr => 12 }], - :dns => [{ :name => 'DNS', :ip_address => '1.2.3.4' }] + subnet: [{ name: 'Subnet-1', ip_address: '172.16.0.0', cidr: 12 }], + dns: [{ name: 'DNS', ip_address: '1.2.3.4' }] } - subject { Azure::VirtualNetworkManagement::VirtualNetworkManagementService.new } + subject { Azure::VirtualNetworkManagementService.new } before do Loggerx.expects(:puts).returns(nil).at_least(0) affinity_group_service = Azure::BaseManagementService.new - affinity_group_service.create_affinity_group(in_affinity_name, WindowsImageLocation, 'Label') rescue nil + affinity_group_service.create_affinity_group(in_affinity_name, + WindowsImageLocation, + 'Label' + ) rescue nil end - describe "Create virtual network valid params " do - it "Create virtual network with valid params" do - subject.set_network_configuration(in_vnet_name, in_affinity_name, in_address_space, inputoptions) + describe 'Create virtual network valid params' do + it 'Create virtual network with valid params' do + subject.set_network_configuration(in_vnet_name, + in_affinity_name, + in_address_space, + inputoptions) - VirtualNetworkHelper.check_config(subject.list_virtual_networks, in_vnet_name, in_affinity_name, created_state, in_address_space, inputoptions) + VirtualNetworkHelper.check_config(subject.list_virtual_networks, + in_vnet_name, + in_affinity_name, + created_state, + in_address_space, + inputoptions) end end - describe "Create virtual network with invalid subnet" do - it "checks for invalid subnet and throws an exception" do + describe 'Create virtual network with invalid subnet' do + it 'checks for invalid subnet and throws an exception' do options = { - :subnet => [{ :name => 'Subnet-1', :ip_address => '10.0.0.0/8', :cidr => 12 }], - :dns => [{ :name => 'demodns', :ip_address => '2.3.4.5' }] + subnet: [{ name: 'Subnet-1', ip_address: '10.0.0.0/8', cidr: 12 }], + dns: [{ name: 'demodns', ip_address: '2.3.4.5' }] } exception = assert_raises(RuntimeError) do - subject.set_network_configuration(in_vnet_name, in_affinity_name, in_address_space, options) + subject.set_network_configuration(in_vnet_name, + in_affinity_name, + in_address_space, + options) end - assert_match("XML Schema validation error in network configuration", exception.message) + assert_match(xml_err_msg, exception.message) end end - describe "Create virtual network with no address space provided options " do - it "Create virtual network with invalid options" do + describe 'Create virtual network with no address space provided options ' do + it 'Create virtual network with invalid options' do exception = assert_raises(RuntimeError) do - subject.set_network_configuration(in_vnet_name, in_affinity_name, invalid_address_space, inputoptions) + subject.set_network_configuration(in_vnet_name, + in_affinity_name, + invalid_address_space, + inputoptions) end - assert_match("XML Schema validation error in network configuration", exception.message) + assert_match(xml_err_msg, exception.message) end end - describe "Update virtual network valid params " do - it "Update virtual network with valid params" do + describe 'Update virtual network valid params ' do + it 'Update virtual network with valid params' do update_address_space = ['10.0.0.0/8'] update_options = { - :subnet => [{ :name => 'Subnet-1', :ip_address => '10.0.0.0', :cidr => 16 }], - :dns => [{ :name => 'DNS', :ip_address => '1.2.3.4' }] + subnet: [{ name: 'Subnet-1', ip_address: '10.0.0.0', cidr: 16 }], + dns: [{ name: 'DNS', ip_address: '1.2.3.4' }] } - subject.set_network_configuration(in_vnet_name, in_affinity_name, update_address_space, update_options) - VirtualNetworkHelper.check_config(subject.list_virtual_networks, in_vnet_name, in_affinity_name, created_state, update_address_space, update_options) + subject.set_network_configuration(in_vnet_name, + in_affinity_name, + update_address_space, + update_options) + VirtualNetworkHelper.check_config(subject.list_virtual_networks, + in_vnet_name, + in_affinity_name, + created_state, + update_address_space, + update_options) end end - describe "Create virtual network invalid cidr " do - it "Create virtual network with invalid options" do + describe 'Create virtual network invalid cidr' do + it 'Create virtual network with invalid options' do exception = assert_raises(RuntimeError) do - subject.set_network_configuration(in_vnet_name, in_affinity_name, invalid_cidr_address_space, inputoptions) + subject.set_network_configuration(in_vnet_name, + in_affinity_name, + invalid_cidr_address_space, + inputoptions) end - assert_match("Cidr is invalid for IP #{invalid_cidr_address_space[0]}", exception.message) + assert_match("Cidr is invalid for IP #{invalid_cidr_address_space[0]}", + exception.message) end end end diff --git a/test/integration/vnet/Virtual_Network_list_test.rb b/test/integration/vnet/Virtual_Network_list_test.rb index 65fd4781c4..7ff6159e4f 100644 --- a/test/integration/vnet/Virtual_Network_list_test.rb +++ b/test/integration/vnet/Virtual_Network_list_test.rb @@ -16,31 +16,38 @@ describe Azure::VirtualNetworkManagement::VirtualNetwork do - subject { Azure::VirtualNetworkManagement::VirtualNetworkManagementService.new } + subject { Azure::VirtualNetworkManagementService.new } let(:affinity_group_name) { 'my-affinity-group' } let(:location) { 'West US' } - let(:vnet_name) { 'vnet-integration-test' } + let(:vnet_name) { 'vnet-integration-test-2' } - before { + before do Loggerx.expects(:puts).returns(nil).at_least(0) affinity_group_service = Azure::BaseManagementService.new - affinity_group_service.create_affinity_group(affinity_group_name, location, 'Label Name') rescue nil - address_space = ['172.16.0.0/12', '10.0.0.0/8', '192.168.0.0/24'] - unless subject.list_virtual_networks.collect(&:name).include?(vnet_name) - subject.set_network_configuration(vnet_name, affinity_group_name, address_space) + affinity_group_service.create_affinity_group(affinity_group_name, + location, + 'Label Name' + ) rescue nil + address_space = %w(172.16.0.0/12 10.0.0.0/8 192.168.0.0/24) + unless subject.list_virtual_networks.map(&:name).include?(vnet_name) + subject.set_network_configuration( + vnet_name, + affinity_group_name, + address_space + ) end - } + end - describe "#list_virtual_networks" do - it "Gets a list of virtual network services available under the current subscription." do + describe '#list_virtual_networks' do + it 'Gets a list of virtual networks for the current subscription.' do virtual_networks = subject.list_virtual_networks virtual_networks.wont_be_nil virtual_networks.must_be_kind_of Array - virtual_networks.first.must_be_kind_of Azure::VirtualNetworkManagement::VirtualNetwork + virtual_networks.first.must_be_kind_of( + Azure::VirtualNetworkManagement::VirtualNetwork + ) assert_operator virtual_networks.size, :>=, 1 end end - -end - +end diff --git a/test/support/name_generator.rb b/test/support/name_generator.rb index 4dfdc6ac70..ebb955119b 100644 --- a/test/support/name_generator.rb +++ b/test/support/name_generator.rb @@ -19,8 +19,8 @@ def initialize(&cleanup_proc) end def name - alpha = ("a".."z").to_a - name = 10.times.map { alpha[Random.rand(alpha.size)]}.join + alpha = ('a'..'z').to_a + name = 10.times.map { alpha[Random.rand(alpha.size)] }.join @names << name name end @@ -84,53 +84,56 @@ def clean class GB18030TestStringsClass def get { - "Chinese2B1" => [0x2488, 0x2460, 0x216B, - 0x3128, 0x3129].pack('U*'), - "Chinese2B2" => [0x554A, 0x963F, 0x9F3E, - 0x9F44].pack('U*'), - "Chinese2B3" => [0x4E02, 0x4E04, 0x72DA, - 0x72DB].pack('U*'), - "Chinese2B4" => [0x72DC, 0x72DD, 0xFA28, - 0xFA29].pack('U*'), - "Chinese2B5" => [0x02CA, 0x02CB, 0x02D9, - 0x2013, 0xE7F3, 0x3007].pack('U*'), - "Chinese4BExtA" => [0x3400, 0x3401, 0x4DB4, - 0x4DB5].pack('U*'), - "ChineseExtB" => [0x020000, 0x020001, - 0x02A6D5, 0x02A6D6].pack('U*'), - "Tibetan" => [0x0F56, 0x0F7C, 0x0F51, - 0x0F0B, 0x0F61, 0x0F72, 0x0F42, 0x0020, 0x0020, - 0x0020, 0x0F00, 0x0F01, 0x0F02, 0x0F03, 0x0F04, - 0x0F05, 0x0FA2, 0x0FA3, 0x0FA4, 0x0FA5].pack('U*'), - "Uyghur" => [0x0626, 0x06C7, 0x064A, - 0x063A, 0x06C7, 0x0631, 0x0020, 0x062A, 0x0649, - 0x0644, 0x0649, 0x0020, 0x0020, 0x0020, 0x003B, - 0x060B, 0x0020, 0x060C, 0x0020, 0x060D, 0x0020, - 0x060E, 0x0020, 0x060F, 0x0610, 0x0020, 0x0020, - 0x0020, 0xFEF2, 0x0020, 0xFEF3, 0x0020, 0xFEF4, - 0x0020, 0xFEF5, 0x0020, 0xFEF6, 0x0020, 0xFEF7, - 0x0020, 0xFEF8, 0x0020, 0xFEF9, 0x0020, 0xFEFA, - 0x0020, 0xFEFB].pack('U*'), - "Mongolian" => [0x182E, 0x1823, 0x1829, - 0x182D, 0x1823, 0x182F, 0x0020, 0x0020, 0x1826, - 0x1830, 0x1826, 0x182D, 0x003B, 0x0020, 0x17F8, - 0x17F9, 0x0020, 0x188E, 0x188F, 0x1890, 0x1891, - 0x1892, 0x1893, 0x1894, 0x1895, 0x1896, 0x1897 - ].pack('U*'), - "Yi" => [0xA188, 0xA320, 0xA071, - 0xA0B7, 0x003B, 0xA000, 0xA001, 0xA002, 0xA003, - 0xA004, 0xA005, 0xA006, 0xA4C1, 0xA4C2, 0xA4C3, - 0xA4C4, 0xA4C5, 0xA4C6].pack('U*'), - "DehongDai" => [0x1958, 0x1963, 0x196D, - 0x1970, 0x0020, 0x1956, 0x196D, 0x1970, 0x0020, - 0x1956, 0x196C, 0x1972, 0x1951, 0x1968, 0x1952, - 0x1970].pack('U*'), - "XishuangbannaDai" => [0x198E, 0x19B7, 0x1991, - 0x19BA, 0x1996, 0x19BA, 0x19C8, 0x1989, 0x19B2, - 0x19C7, 0x1989, 0x19B8, 0x19C2, 0x1997, 0x19C3, - 0x1993, 0x19B1].pack('U*') + 'Chinese2B1' => [0x2488, 0x2460, 0x216B, 0x3128, + 0x3129].pack('U*'), + 'Chinese2B2' => [0x554A, 0x963F, 0x9F3E, 0x9F44].pack('U*'), + 'Chinese2B3' => [0x4E02, 0x4E04, 0x72DA, 0x72DB].pack('U*'), + 'Chinese2B4' => [0x72DC, 0x72DD, 0xFA28, 0xFA29].pack('U*'), + 'Chinese2B5' => [0x02CA, 0x02CB, 0x02D9, 0x2013, + 0xE7F3, 0x3007].pack('U*'), + 'Chinese4BExtA' => [0x3400, 0x3401, 0x4DB4, 0x4DB5].pack('U*'), + 'ChineseExtB' => [0x020000, 0x020001, 0x02A6D5, + 0x02A6D6].pack('U*'), + 'Tibetan' => [0x0F56, 0x0F7C, 0x0F51, 0x0F0B, 0x0F61, + 0x0F72, 0x0F42, 0x0020, 0x0020, 0x0020, + 0x0F00, 0x0F01, 0x0F02, 0x0F03, 0x0F04, + 0x0F05, 0x0FA2, 0x0FA3, 0x0FA4, + 0x0FA5].pack('U*'), + 'Uyghur' => [0x0626, 0x06C7, 0x064A, 0x063A, + 0x06C7, 0x0631, 0x0020, 0x062A, + 0x0649, 0x0644, 0x0649, 0x0020, + 0x0020, 0x0020, 0x003B, 0x060B, + 0x0020, 0x060C, 0x0020, 0x060D, + 0x0020, 0x060E, 0x0020, 0x060F, + 0x0610, 0x0020, 0x0020, 0x0020, + 0xFEF2, 0x0020, 0xFEF3, 0x0020, + 0xFEF4, 0x0020, 0xFEF5, 0x0020, + 0xFEF6, 0x0020, 0xFEF7, 0x0020, + 0xFEF8, 0x0020, 0xFEF9, 0x0020, + 0xFEFA, 0x0020, 0xFEFB].pack('U*'), + 'Mongolian' => [0x182E, 0x1823, 0x1829, 0x182D, + 0x1823, 0x182F, 0x0020, 0x0020, + 0x1826, 0x1830, 0x1826, 0x182D, + 0x003B, 0x0020, 0x17F8, 0x17F9, + 0x0020, 0x188E, 0x188F, 0x1890, + 0x1891, 0x1892, 0x1893, 0x1894, + 0x1895, 0x1896, 0x1897].pack('U*'), + 'Yi' => [0xA188, 0xA320, 0xA071, 0xA0B7, + 0x003B, 0xA000, 0xA001, 0xA002, + 0xA003, 0xA004, 0xA005, 0xA006, + 0xA4C1, 0xA4C2, 0xA4C3, 0xA4C4, + 0xA4C5, 0xA4C6].pack('U*'), + 'DehongDai' => [0x1958, 0x1963, 0x196D, 0x1970, + 0x0020, 0x1956, 0x196D, 0x1970, + 0x0020, 0x1956, 0x196C, 0x1972, + 0x1951, 0x1968, 0x1952, 0x1970].pack('U*'), + 'XishuangbannaDai' => [0x198E, 0x19B7, 0x1991, 0x19BA, + 0x1996, 0x19BA, 0x19C8, 0x1989, + 0x19B2, 0x19C7, 0x1989, 0x19B8, + 0x19C2, 0x1997, 0x19C3, 0x1993, 0x19B1].pack('U*') } end + def get_xml_10_fourth_ed_identifiers # No characters from the following sets are valid, # according to the XML library that the service uses: @@ -142,18 +145,14 @@ def get_xml_10_fourth_ed_identifiers # * Xishuangbanna_Dai { 'Chinese_2B1' => [0x3128, 0x3129].pack('U*'), - 'Chinese_2B2' => [0x554A, 0x963F, 0x9F3E, 0x9F44 - ].pack('U*'), - 'Chinese_2B3' => [0x4E02, 0x4E04, 0x72DA, 0x72DB - ].pack('U*'), + 'Chinese_2B2' => [0x554A, 0x963F, 0x9F3E, 0x9F44].pack('U*'), + 'Chinese_2B3' => [0x4E02, 0x4E04, 0x72DA, 0x72DB].pack('U*'), 'Chinese_2B4' => [0x72DC, 0x72DD].pack('U*'), 'Chinese_2B5' => [0x3007].pack('U*'), - 'Tibetan' => [0x0F56, 0x0F7C, 0x0F51, 0x0F61, - 0x0F72, 0x0F42, 0x0FA2, 0x0FA3, 0x0FA4, 0x0FA5 - ].pack('U*'), - 'Uyghur' => [0x0626, 0x06C7, 0x064A, 0x063A, - 0x06C7, 0x0631, 0x062A, 0x0649, 0x0644, 0x0649 - ].pack('U*'), + 'Tibetan' => [0x0F56, 0x0F7C, 0x0F51, 0x0F61, 0x0F72, 0x0F42, + 0x0FA2, 0x0FA3, 0x0FA4, 0x0FA5].pack('U*'), + 'Uyghur' => [0x0626, 0x06C7, 0x064A, 0x063A, 0x06C7, 0x0631, + 0x062A, 0x0649, 0x0644, 0x0649].pack('U*'), } end end diff --git a/test/support/virtual_machine_name_generator.rb b/test/support/virtual_machine_name_generator.rb index 7d76dab3dd..30093802be 100644 --- a/test/support/virtual_machine_name_generator.rb +++ b/test/support/virtual_machine_name_generator.rb @@ -12,9 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -#require 'name_generator' class VirtualMachineNameGenerator - def initialize(&cleanup_proc) @cleanup_proc = cleanup_proc @names = [] @@ -22,14 +20,14 @@ def initialize(&cleanup_proc) end def random_text(text) - @count+=1 - text+@count.to_s+"-#{Array.new(32){rand(36).to_s(36)}.join[0..5]}" + @count += 1 + text + @count.to_s + "-#{Array.new(32) { rand(36).to_s(36) }.join[0..5]}" end def name vm_name = random_text('test-') - cloud_name = random_text(vm_name+'-service-') - @names << [vm_name,cloud_name] + cloud_name = random_text(vm_name + '-service-') + @names << [vm_name, cloud_name] return vm_name, cloud_name end @@ -41,68 +39,82 @@ def clean end def self.cleanup - puts "Running after test cleanup." - #Delete virtual machines + puts 'Running after test cleanup.' + # Delete virtual machines virtual_machine_service = Azure::VirtualMachineManagementService.new virtualmachines = virtual_machine_service.list_virtual_machines azure_cloud_service = Azure::CloudServiceManagementService.new virtualmachines.each do |virtualmachine| - if(virtualmachine.vm_name.include?("test-") && virtualmachine.cloud_service_name.include?(virtualmachine.vm_name+'-service-')) + if virtualmachine.vm_name.include?('test-') &&\ + virtualmachine.cloud_service_name.include?( + virtualmachine.vm_name + '-service-' + ) begin - azure_cloud_service.delete_cloud_service_deployment(virtualmachine.cloud_service_name) - azure_cloud_service.delete_cloud_service(virtualmachine.cloud_service_name) + azure_cloud_service.delete_cloud_service_deployment( + virtualmachine.cloud_service_name + ) + azure_cloud_service.delete_cloud_service( + virtualmachine.cloud_service_name + ) rescue end end end - #Delete cloud services + # Delete cloud services Azure::BaseManagementService.new cloud_services = azure_cloud_service.list_cloud_services cloud_services.each do |cloud_service| - if cloud_service.name.include?("test-") && cloud_service.name.include?("-service-") - azure_cloud_service.delete_cloud_service(cloud_service.name) rescue nil + if cloud_service.name.include?('test-') &&\ + cloud_service.name.include?('-service-') + azure_cloud_service.delete_cloud_service( + cloud_service.name + ) rescue nil end end - #Delete SQL servers + # Delete SQL servers Azure.config.management_endpoint = SqlServerEndpoint sql_database_service = Azure::SqlDatabaseManagementService.new sql_database_servers = sql_database_service.list_servers sql_database_servers.each do |sql_server| - if(sql_server.administrator_login == 'ms_open_tech') + if sql_server.administrator_login == 'ms_open_tech' sql_database_service.delete_server(sql_server.name) rescue nil end end Azure.config.management_endpoint = ManagementServiceEndpoint - #Delete disks + # Delete disks disk_management_service = Azure::VirtualMachineImageManagement::VirtualMachineDiskManagementService.new disks = disk_management_service.list_virtual_machine_disks disks.each do |disk| - if(disk.name.include?('-service-') && disk.name.include?('test-') && !disk.attached) - disk_management_service.delete_virtual_machine_disk(disk.name) rescue nil + if disk.name.include?('-service-') &&\ + disk.name.include?('test-') && !disk.attached + disk_management_service.delete_virtual_machine_disk( + disk.name + ) rescue nil end end - #Delete affinity groups + # Delete affinity groups affinity_group_service = Azure::BaseManagementService.new affinity_groups = affinity_group_service.list_affinity_groups affinity_groups.each do |affinity_group| - if(affinity_group.name.include?('affinity-group-')) - affinity_group_service.delete_affinity_group(affinity_group.name) rescue nil + if affinity_group.name.include?('affinity-group-') + affinity_group_service.delete_affinity_group( + affinity_group.name + ) rescue nil end end - #Delete storage account - storage_service = Azure::StorageManagement::StorageManagementService.new + # Delete storage account + storage_service = Azure::StorageManagementService.new storage_accounts = storage_service.list_storage_accounts storage_accounts.each do |storage_account| - if(storage_account.name.include?('storagetest')) + if storage_account.name.include?('storagetest') storage_service.delete_storage_account(storage_account.name) rescue nil end end end - end VirtualMachineNameHelper = VirtualMachineNameGenerator.new do |name, cloud_name| diff --git a/test/support/virtual_network_helper.rb b/test/support/virtual_network_helper.rb index a786d85eba..cf263c533c 100644 --- a/test/support/virtual_network_helper.rb +++ b/test/support/virtual_network_helper.rb @@ -14,39 +14,60 @@ #-------------------------------------------------------------------------- class VirtualNetworkHelper - def self.check_config(virtual_networks, in_vnet_name, in_affinity_name, created_state, in_address_space, inputoptions) + def self.check_config(virtual_networks, + in_vnet_name, + in_affinity_name, + created_state, + in_address_space, + inputoptions) virtual_networks.wont_be_nil virtual_networks.must_be_kind_of Array - virtual_networks[0].must_be_kind_of Azure::VirtualNetworkManagement::VirtualNetwork + virtual_networks[0].must_be_kind_of( + Azure::VirtualNetworkManagement::VirtualNetwork + ) - created_virtual_network = virtual_networks.select { |x| x.name == in_vnet_name } + created_virtual_network = virtual_networks.select do |x| + x.name == in_vnet_name + end created_virtual_network.wont_be_nil created_virtual_network.size.must_equal 1 - created_virtual_network[0].must_be_kind_of Azure::VirtualNetworkManagement::VirtualNetwork + created_virtual_network[0].must_be_kind_of( + Azure::VirtualNetworkManagement::VirtualNetwork + ) - #Verify global properties + # Verify global properties created_virtual_network[0].name.must_equal in_vnet_name created_virtual_network[0].affinity_group.must_equal in_affinity_name created_virtual_network[0].state.must_equal created_state - #Verify address_space + # Verify address_space created_virtual_network[0].address_space.must_be_kind_of Array created_virtual_network[0].address_space.length.must_equal 1 created_virtual_network[0].address_space.first.must_be_kind_of String - created_virtual_network[0].address_space.first.must_equal in_address_space[0] + created_virtual_network[0].address_space.first.must_equal( + in_address_space[0] + ) - #Verify subnets + # Verify subnets created_virtual_network[0].subnets.must_be_kind_of Array created_virtual_network[0].subnets.length.must_equal 1 - created_virtual_network[0].subnets.first[:name].must_equal inputoptions[:subnet][0][:name] - created_virtual_network[0].subnets.first[:address_prefix].must_equal "#{inputoptions[:subnet][0][:ip_address]}/#{inputoptions[:subnet][0][:cidr]}" + created_virtual_network[0].subnets.first[:name].must_equal( + inputoptions[:subnet][0][:name] + ) + created_virtual_network[0].subnets.first[:address_prefix].must_equal( + "#{inputoptions[:subnet][0][:ip_address]}/"\ + "#{inputoptions[:subnet][0][:cidr]}" + ) - #Verify DNS Servers + # Verify DNS Servers created_virtual_network[0].dns_servers.must_be_kind_of Array created_virtual_network[0].dns_servers.length.must_equal 1 - created_virtual_network[0].dns_servers.first[:name].must_equal inputoptions[:dns][0][:name] - created_virtual_network[0].dns_servers.first[:ip_address].must_equal inputoptions[:dns][0][:ip_address] + created_virtual_network[0].dns_servers.first[:name].must_equal( + inputoptions[:dns][0][:name] + ) + created_virtual_network[0].dns_servers.first[:ip_address].must_equal( + inputoptions[:dns][0][:ip_address] + ) end end - diff --git a/test/unit/affinity_group/affinity_group_test.rb b/test/unit/affinity_group/affinity_group_test.rb index 2c4bf219b1..6fe2187aaf 100644 --- a/test/unit/affinity_group/affinity_group_test.rb +++ b/test/unit/affinity_group/affinity_group_test.rb @@ -12,31 +12,33 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::BaseManagementService do subject { Azure::BaseManagementService.new } - - let(:mock_request){ mock() } - let(:response) { mock() } + + let(:mock_request) { mock } + let(:response) { mock } let(:response_xml) { nil } - before { + before do Loggerx.expects(:puts).returns(nil).at_least(0) mock_request.expects(:call).returns(Nokogiri::XML response_xml).at_least(0) - } + end - describe "#list_affinity_groups" do - let(:request_path) {'/affinitygroups'} - let(:response_xml) { Fixtures["list_affinity_groups"] } + describe '#list_affinity_groups' do + let(:request_path) { '/affinitygroups' } + let(:response_xml) { Fixtures['list_affinity_groups'] } let(:method) { :get } - - before{ - ManagementHttpRequest.stubs(:new).with(method, request_path, nil).returns(mock_request) - } - - it "returns a list of affinity groups for the subscription" do + + before do + ManagementHttpRequest.stubs(:new).with(method, + request_path, + nil).returns(mock_request) + end + + it 'returns a list of affinity groups for the subscription' do results = subject.list_affinity_groups results.must_be_kind_of Array results.length.must_equal 2 @@ -45,26 +47,27 @@ affinity_group.name.must_equal 'AG1' affinity_group.description.must_equal 'First Affinity Group' affinity_group.location.must_equal 'West US' - affinity_group.capability.must_equal ["PersistentVMRole", "HighMemory"] - + affinity_group.capability.must_equal %w(PersistentVMRole HighMemory) end end - describe "#delete_affinity_group" do - before{ - Azure::BaseManagementService.any_instance.stubs(:list_affinity_groups).returns([]) - } - - it "error if affinity group does not exists" do - affinity_group_name = "unknown-affinity-group" + describe '#delete_affinity_group' do + before do + Azure::BaseManagementService.any_instance.stubs( + :list_affinity_groups + ).returns([]) + end + + it 'error if affinity group does not exists' do + affinity_group_name = 'unknown-affinity-group' exception = assert_raises(Azure::Error::Error) do subject.delete_affinity_group affinity_group_name end assert_match(/The affinity group does not exist./i, exception.message) end - it "error if affinity group name is empty" do - affinity_group_name = "" + it 'error if affinity group name is empty' do + affinity_group_name = '' exception = assert_raises(Azure::Error::Error) do subject.delete_affinity_group affinity_group_name end @@ -72,62 +75,88 @@ end end - describe "#create_affinity_group" do + describe '#create_affinity_group' do let(:method) { :post } - let(:request_path) { "/affinitygroups" } - let(:name) { "AG1" } - let(:location) { "West US" } - let(:label) {'Label Name'} + let(:request_path) { '/affinitygroups' } + let(:name) { 'AG1' } + let(:location) { 'West US' } + let(:label) { 'Label Name' } let(:location_request_path) { '/locations' } let(:locations_xml) { Fixtures['list_locations'] } - let(:location_response) { - response = mock() + let(:location_response) do + response = mock response.stubs(:body).returns(locations_xml) response - } + end let(:location_response_body) { Nokogiri::XML location_response.body } - - before{ - ManagementHttpRequest.stubs(:new).with(method, request_path, anything).returns(mock_request) - mock_request.expects(:call).returns(Nokogiri::XML response_xml).at_least(0) - mock_request = mock() - ManagementHttpRequest.stubs(:new).with(:get, location_request_path, nil).returns(mock_request) + + before do + ManagementHttpRequest.stubs(:new).with(method, + request_path, + anything).returns(mock_request) + mock_request.expects(:call).returns( + Nokogiri::XML response_xml + ).at_least(0) + mock_request = mock + + ManagementHttpRequest.stubs(:new).with(:get, + location_request_path, + nil).returns(mock_request) mock_request.expects(:call).returns(location_response_body).at_least(0) - mock_request = mock() - ManagementHttpRequest.stubs(:new).with(:get, request_path, nil).returns(mock_request) - mock_request.expects(:call).returns(Nokogiri::XML Fixtures["list_affinity_groups"]).at_least(0) - } + mock_request = mock + + ManagementHttpRequest.stubs(:new).with(:get, + request_path, + nil).returns(mock_request) + mock_request.expects(:call).returns( + Nokogiri::XML Fixtures['list_affinity_groups'] + ).at_least(0) + end - it "error if affinity group already exists" do + it 'error if affinity group already exists' do exception = assert_raises(Azure::Error::Error) do - subject.create_affinity_group(name, location, label ) + subject.create_affinity_group(name, location, label) end - assert_match(/An affinity group #{name} already exists in the current subscription/i, exception.message) + assert_match( + /An affinity group #{name} already exists in the current subscription/i, + exception.message + ) end - it "error if location is invalid" do + it 'error if location is invalid' do location = 'invalid-location' exception = assert_raises(RuntimeError) do - subject.create_affinity_group('AGN', location, label ) + subject.create_affinity_group('AGN', location, label) end - assert_match(/Value '#{location}' specified for parameter 'location' is invalid/i, exception.message) + assert_match( + /Value '#{location}' specified for parameter 'location' is invalid/i, + exception.message + ) end end - describe "#get_affinity_group" do + describe '#get_affinity_group' do let(:affinity_group_name) { 'AG1' } - let(:request_path) { "/affinitygroups/"+affinity_group_name } - let(:response_xml) { Fixtures["affinity_group"] } + let(:request_path) { '/affinitygroups/' + affinity_group_name } + let(:response_xml) { Fixtures['affinity_group'] } let(:method) { :get } - - before{ - Azure::BaseManagementService.any_instance.stubs(:affinity_group).returns(true) - ManagementHttpRequest.stubs(:new).with(:get, request_path, nil).returns(mock_request) - mock_request.expects(:call).returns(Nokogiri::XML response_xml).at_least(0) - } - - it "returns a properties of given affinity group" do + + before do + Azure::BaseManagementService.any_instance.stubs( + :affinity_group + ).returns(true) + ManagementHttpRequest.stubs(:new).with( + :get, + request_path, + nil + ).returns(mock_request) + mock_request.expects(:call).returns( + Nokogiri::XML response_xml + ).at_least(0) + end + + it 'returns a properties of given affinity group' do affinity_group = subject.get_affinity_group affinity_group_name affinity_group.must_be_kind_of Azure::BaseManagement::AffinityGroup affinity_group.name.must_equal 'AG1' @@ -138,21 +167,21 @@ affinity_group.hosted_services.size.must_equal 1 affinity_group.storage_services.wont_be_nil affinity_group.storage_services.size.must_equal 1 - affinity_group.capability.must_equal ["PersistentVMRole", "HighMemory"] + affinity_group.capability.must_equal %w[PersistentVMRole HighMemory] end end - describe "#update_affinity_group" do + describe '#update_affinity_group' do let(:affinity_group_name) { 'AG1' } - it "error if affinity group label is invalid" do + it 'error if affinity group label is invalid' do exception = assert_raises(RuntimeError) do subject.update_affinity_group(affinity_group_name, nil) end assert_match(/Label name cannot be empty/i, exception.message) end - it "error if affinity group does not exist" do + it 'error if affinity group does not exist' do exception = assert_raises(Azure::Error::Error) do subject.update_affinity_group(nil, 'label') end diff --git a/test/unit/affinity_group/serialization_test.rb b/test/unit/affinity_group/serialization_test.rb index aee839df9c..bacb9410e3 100644 --- a/test/unit/affinity_group/serialization_test.rb +++ b/test/unit/affinity_group/serialization_test.rb @@ -12,19 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::BaseManagement::Serialization do subject { Azure::BaseManagement::Serialization } let(:location) { 'West Us' } - describe "#affinity_group_to_xml" do + describe '#affinity_group_to_xml' do let(:affinity_group_name) { 'AG1' } let(:label) { 'Label Name' } - it "serializes the argument to xml" do - result = subject.affinity_group_to_xml affinity_group_name, location, label + it 'serializes the argument to xml' do + result = subject.affinity_group_to_xml(affinity_group_name, + location, + label) doc = Nokogiri::XML result doc.css('Name').text.must_equal affinity_group_name doc.css('Label').text.must_equal Base64.encode64(label).strip @@ -32,9 +34,12 @@ result.must_be_kind_of String end - it "serializes the argument to xml when optional parameter description is given" do - options = {:description => 'Affinity Group Description'} - result = subject.affinity_group_to_xml affinity_group_name, location, label, options + it 'serializes the argument to xml when optional description is given' do + options = { description: 'Affinity Group Description' } + result = subject.affinity_group_to_xml(affinity_group_name, + location, + label, + options) doc = Nokogiri::XML result doc.css('Name').text.must_equal affinity_group_name doc.css('Label').text.must_equal Base64.encode64(label).strip @@ -44,15 +49,16 @@ end end - describe "#affinity_groups_from_xml" do + describe '#affinity_groups_from_xml' do + let(:affinity_groups_xml) do + Nokogiri::XML(Fixtures['list_affinity_groups']) + end - let(:affinity_groups_xml) { Nokogiri::XML(Fixtures["list_affinity_groups"]) } - - it "accepts an XML string" do + it 'accepts an XML string' do subject.affinity_groups_from_xml affinity_groups_xml end - it "returns an Array of Azure::BaseManagementt::AffinityGroup instances" do + it 'returns an Array of Azure::BaseManagementt::AffinityGroup instances' do results = subject.affinity_groups_from_xml affinity_groups_xml results.must_be_kind_of Array results[0].must_be_kind_of Azure::BaseManagement::AffinityGroup @@ -60,22 +66,23 @@ end end - describe "#affinity_group_from_xml" do + describe '#affinity_group_from_xml' do - let(:affinity_group_xml) { Nokogiri::XML(Fixtures["affinity_group"]) } + let(:affinity_group_xml) { Nokogiri::XML(Fixtures['affinity_group']) } - it "accepts an XML string" do + it 'accepts an XML string' do subject.affinity_group_from_xml affinity_group_xml end - it "returns an Azure::BaseManagement::AffinityGroup instances" do + it 'returns an Azure::BaseManagement::AffinityGroup instances' do affinity_group = subject.affinity_group_from_xml affinity_group_xml affinity_group.must_be_kind_of Azure::BaseManagement::AffinityGroup affinity_group.hosted_services.size.must_equal 1 affinity_group.storage_services.size.must_equal 1 - affinity_group.capability.must_equal ["PersistentVMRole", "HighMemory"] + affinity_group.capability.must_equal %w[PersistentVMRole HighMemory] affinity_group.name.must_equal affinity_group_xml.css('Name').text - affinity_group.description.must_equal affinity_group_xml.css('Description').text + affinity_group.description.must_equal affinity_group_xml.css( + 'Description').text end end end diff --git a/test/unit/cloud_service_management/serialization_test.rb b/test/unit/cloud_service_management/serialization_test.rb index bd2becc42e..4cf9876305 100644 --- a/test/unit/cloud_service_management/serialization_test.rb +++ b/test/unit/cloud_service_management/serialization_test.rb @@ -12,48 +12,65 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::CloudServiceManagement::Serialization do subject { Azure::CloudServiceManagement::Serialization } - let(:cloud_services_from_xml) { Fixtures["list_cloud_services"] } + let(:cloud_services_from_xml) { Fixtures['list_cloud_services'] } - describe "#cloud_services_from_xml" do + describe '#cloud_services_from_xml' do - it "accepts an XML string" do + it 'accepts an XML string' do subject.cloud_services_from_xml Nokogiri::XML(cloud_services_from_xml) end - it "returns an Array of CloudService instances" do - results = subject.cloud_services_from_xml Nokogiri::XML(cloud_services_from_xml) + it 'returns an Array of CloudService instances' do + results = subject.cloud_services_from_xml( + Nokogiri::XML(cloud_services_from_xml) + ) results.must_be_kind_of Array - results[0].must_be_kind_of Azure::CloudServiceManagement::CloudService + results[0].must_be_kind_of(Azure::CloudServiceManagement::CloudService) results.count.must_equal 2 end end - describe "#cloud_services_to_xml" do + describe '#cloud_services_to_xml' do let(:cloud_service_name) {'cloud-service'} - it "accepts an name and options hash" do + it 'accepts an name and options hash' do subject.cloud_services_to_xml cloud_service_name end - it "serializes the argument to xml" do - results = subject.cloud_services_to_xml(cloud_service_name, { :location => "West US"} ) + it 'serializes the argument to xml' do + results = subject.cloud_services_to_xml( + cloud_service_name, { location: 'West US' } + ) + results.must_be_kind_of String doc = Nokogiri::XML results doc.css('ServiceName').text.must_equal cloud_service_name - doc.css('Location').text.must_equal "West US" + doc.css('Location').text.must_equal 'West US' + end + + it 'uses affinity_group if provided' do + results = subject.cloud_services_to_xml( + cloud_service_name, + { affinity_group_name: 'my-affinity-group', location: 'West US' } + ) results.must_be_kind_of String + doc = Nokogiri::XML results + doc.css('ServiceName').text.must_equal cloud_service_name + doc.css('AffinityGroup').text.must_equal 'my-affinity-group' end - it "serializes the options hash to xml" do - results = subject.cloud_services_to_xml(cloud_service_name, { :location => 'East US'} ) + it 'serializes the options hash to xml' do + results = subject.cloud_services_to_xml( + cloud_service_name, + { location: 'East US' } + ) doc = Nokogiri::XML results doc.css('Location').text.must_equal 'East US' results.must_be_kind_of String end - end end \ No newline at end of file diff --git a/test/unit/database/serialization_test.rb b/test/unit/database/serialization_test.rb index fcade1cc08..a7e97c10ee 100644 --- a/test/unit/database/serialization_test.rb +++ b/test/unit/database/serialization_test.rb @@ -12,20 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::SqlDatabaseManagement::Serialization do subject { Azure::SqlDatabaseManagement::Serialization } - let(:sql_servers_xml) { Fixtures["list_sql_database"] } + let(:sql_servers_xml) { Fixtures['list_sql_database'] } - describe "#databases_from_xml" do + describe '#databases_from_xml' do - it "accepts an XML string" do + it 'accepts an XML string' do subject.databases_from_xml Nokogiri::XML(sql_servers_xml) end - it "returns an Array of SqlDatabaseServer instances" do + it 'returns an Array of SqlDatabaseServer instances' do results = subject.databases_from_xml Nokogiri::XML(sql_servers_xml) results.must_be_kind_of Array results[0].must_be_kind_of Azure::SqlDatabaseManagement::SqlDatabase @@ -33,15 +33,16 @@ end end - describe "#database_to_xml" do - let(:login) {'administrator'} - let(:password) {'Password@123'} - let(:location) {'West US'} - it "accepts an name and options hash" do + describe '#database_to_xml' do + let(:login) { 'administrator' } + let(:password) { 'Password@123' } + let(:location) { 'West US' } + + it 'accepts an name and options hash' do subject.database_to_xml(login, password, location) end - it "serializes the argument to xml" do + it 'serializes the argument to xml' do results = subject.database_to_xml login, password, location doc = Nokogiri::XML results doc.css('AdministratorLogin').text.must_equal login @@ -52,14 +53,19 @@ end - describe "#firewall_rule_to_xml" do - let(:options) { {:start_ip_address => '10.0.0.1',:end_ip_address => '10.0.0.255'} } - - it "accepts a ip range" do + describe '#firewall_rule_to_xml' do + let(:options) do + { + start_ip_address: '10.0.0.1', + end_ip_address: '10.0.0.255' + } + end + + it 'accepts a ip range' do subject.firewall_rule_to_xml(options) end - it "serializes the argument to xml" do + it 'serializes the argument to xml' do results = subject.firewall_rule_to_xml options doc = Nokogiri::XML results doc.css('StartIpAddress').text.must_equal options[:start_ip_address] @@ -69,15 +75,17 @@ end - describe "#database_firewall_from_xml" do - let(:sql_server_firewall_xml) { Fixtures["list_sql_server_firewall"] } - - it "accepts an XML string" do + describe '#database_firewall_from_xml' do + let(:sql_server_firewall_xml) { Fixtures['list_sql_server_firewall'] } + + it 'accepts an XML string' do subject.database_firewall_from_xml Nokogiri::XML(sql_server_firewall_xml) end - it "returns an Array of SqlDatabaseServer instances with firewall" do - results = subject.database_firewall_from_xml Nokogiri::XML(sql_server_firewall_xml) + it 'returns an Array of SqlDatabaseServer instances with firewall' do + results = subject.database_firewall_from_xml( + Nokogiri::XML(sql_server_firewall_xml) + ) results.must_be_kind_of Array results[0].must_be_kind_of Hash results.count.must_equal 4 diff --git a/test/unit/database/sql_database_server_service_test.rb b/test/unit/database/sql_database_server_service_test.rb index d1f53aed0a..bd5191d517 100644 --- a/test/unit/database/sql_database_server_service_test.rb +++ b/test/unit/database/sql_database_server_service_test.rb @@ -12,35 +12,39 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::SqlDatabaseManagementService do subject { Azure::SqlDatabaseManagementService.new } let(:response_headers) { {} } - let(:mock_request){ mock() } - let(:response) { mock() } + let(:mock_request) { mock } + let(:response) { mock } let(:response_xml) { nil } - before { + before do Loggerx.expects(:puts).returns(nil).at_least(0) mock_request.stubs(:headers).returns(response_headers) mock_request.expects(:call).returns(Nokogiri::XML response_xml).at_least(0) - } + end - describe "#list_servers" do - let(:response_xml) { Fixtures["list_sql_database"] } + describe '#list_servers' do + let(:response_xml) { Fixtures['list_sql_database'] } let(:method) { :get } - let(:request_path) {'/servers'} - - before{ - ManagementHttpRequest.stubs(:new).with(method, request_path, nil).returns(mock_request) - } + let(:request_path) { '/servers' } + + before do + ManagementHttpRequest.stubs(:new).with( + method, + request_path, + nil + ).returns(mock_request) + end - it "assembles a URI for the request" do + it 'assembles a URI for the request' do subject.list_servers end - - it "returns a list of sql servers for the account" do + + it 'returns a list of sql servers for the account' do results = subject.list_servers results.must_be_kind_of Array results.length.must_equal 3 @@ -50,44 +54,55 @@ sql_server.administrator_login.must_equal 'SqlServer2' sql_server.location.must_equal 'West US' sql_server.feature_name.must_equal 'Premium Mode' - sql_server.feature_value.must_equal "false" - end + sql_server.feature_value.must_equal 'false' + end end - describe "#delete_server" do - before{ - Azure::SqlDatabaseManagementService.any_instance.stubs(:list_servers).returns([]) - } + describe '#delete_server' do + before do + Azure::SqlDatabaseManagementService.any_instance.stubs( + :list_servers + ).returns([]) + end - it "error if sql server does not exists" do - server_name = "unknown-server" + it 'error if sql server does not exists' do + s_name = 'unknown-server' exception = assert_raises(Azure::Error::Error) do - subject.delete_server server_name + subject.delete_server s_name end - assert_match(/Subscription #{Azure.config.subscription_id} does not have server #{server_name}./i, exception.message) + s_id = Azure.config.subscription_id + assert_match(/Subscription #{s_id} does not have server #{s_name}./i, + exception.message) end end - describe "#list_sql_server_firewall_rules" do - let(:response_xml) { Fixtures["list_sql_server_firewall"] } + describe '#list_sql_server_firewall_rules' do + let(:response_xml) { Fixtures['list_sql_server_firewall'] } let(:method) { :get } let(:sql_server_name) { 'server1' } - let(:request_path) {"/servers/#{sql_server_name}/firewallrules"} + let(:request_path) { "/servers/#{sql_server_name}/firewallrules" } - before{ + before do sql_server = Azure::SqlDatabaseManagement::SqlDatabase.new do |server| server.name = sql_server_name end - Azure::SqlDatabaseManagementService.any_instance.stubs(:list_servers).returns([sql_server]) - ManagementHttpRequest.stubs(:new).with(method, request_path, nil).returns(mock_request) - } + Azure::SqlDatabaseManagementService.any_instance.stubs( + :list_servers + ).returns([sql_server]) + + ManagementHttpRequest.stubs(:new).with( + method, + request_path, + nil + ).returns(mock_request) + end - it "assembles a URI for the sql server firewall request" do + it 'assembles a URI for the sql server firewall request' do subject.list_sql_server_firewall_rules sql_server_name end - it "returns a list of firewall of given sql servers" do + it 'returns a list of firewall of given sql servers' do results = subject.list_sql_server_firewall_rules sql_server_name results.must_be_kind_of Array results.length.must_equal 4 @@ -95,22 +110,28 @@ end end - describe "#create_server" do - let(:response_xml) { Fixtures["create_sql_database_server"] } + describe '#create_server' do + let(:response_xml) { Fixtures['create_sql_database_server'] } let(:method) { :post } - let(:request_path) { "/servers" } - let(:password) { "User@123" } - let(:login) { "ms_open_tech" } - let(:location) { "West US" } + let(:request_path) { '/servers' } + let(:password) { 'User@123' } + let(:login) { 'ms_open_tech' } + let(:location) { 'West US' } + + before do + ManagementHttpRequest.stubs(:new).with( + method, + request_path, + anything + ).returns(mock_request) - - before{ - ManagementHttpRequest.stubs(:new).with(method, request_path, anything).returns(mock_request) mock_request.stubs(:headers).returns(response_headers) - mock_request.expects(:call).returns(Nokogiri::XML response_xml).at_least(0) - } + mock_request.expects(:call).returns( + Nokogiri::XML response_xml + ).at_least(0) + end - it "create sql server" do + it 'create sql server' do sql_server = subject.create_server(login, password, location) sql_server.name.must_equal 'gxyfzrhx2c' sql_server.administrator_login.must_equal login @@ -118,14 +139,14 @@ end end - describe "#set_sql_server_firewall_rule" do - it "create sql server" do - ip_range = {:start_ip_address => "0.0.0.1", :end_ip_address => "0.0.0.5"} + describe '#set_sql_server_firewall_rule' do + it 'create sql server' do + ip_range = { start_ip_address: '0.0.0.1', end_ip_address: '0.0.0.5' } exception = assert_raises(RuntimeError) do - subject.set_sql_server_firewall_rule("zv2nfoah2t1", ip_range) + subject.set_sql_server_firewall_rule('zv2nfoah2t1', ip_range) end - assert_match(/Missing parameter server_name or rule_name/i, exception.message) + assert_match(/Missing parameter server_name or rule_name/i, + exception.message) end end - end diff --git a/test/unit/storage_management/serialization_test.rb b/test/unit/storage_management/serialization_test.rb index 11b07d09e9..eae7547869 100644 --- a/test/unit/storage_management/serialization_test.rb +++ b/test/unit/storage_management/serialization_test.rb @@ -55,5 +55,11 @@ results.must_be_kind_of String end + it "uses affinity_group from the hash instead of location" do + results = subject.storage_services_to_xml(storage_service_name, {:affinity_group_name => 'my-affinity-group', :location => 'East US'}) + doc = Nokogiri::XML results + doc.css('AffinityGroup').text.must_equal 'my-affinity-group' + results.must_be_kind_of String + end end end \ No newline at end of file diff --git a/test/unit/virtual_machine_management/serialization_test.rb b/test/unit/virtual_machine_management/serialization_test.rb index aa91b3f7d6..0f0874bdf6 100644 --- a/test/unit/virtual_machine_management/serialization_test.rb +++ b/test/unit/virtual_machine_management/serialization_test.rb @@ -107,7 +107,7 @@ end result.must_be_kind_of String tcp_endpoints.must_include({"Name"=>"TCP-PORT-80", "PublicPort"=>"80", "LocalPort"=>"80"}) - tcp_endpoints.must_include({"Name"=>"TCP-PORT-3389", "PublicPort"=>"3390", "LocalPort"=>"3389"}) + tcp_endpoints.must_include({"Name"=>"TCP-PORT-3390", "PublicPort"=>"3390", "LocalPort"=>"3389"}) tcp_endpoints.must_include({"Name"=>"TCP-PORT-85", "PublicPort"=>"85", "LocalPort"=>"85"}) end diff --git a/test/unit/virtual_machine_management/virtual_machine_management_service_test.rb b/test/unit/virtual_machine_management/virtual_machine_management_service_test.rb index 2dc4dff817..886b1226a0 100644 --- a/test/unit/virtual_machine_management/virtual_machine_management_service_test.rb +++ b/test/unit/virtual_machine_management/virtual_machine_management_service_test.rb @@ -16,7 +16,6 @@ describe Azure::VirtualMachineManagementService do VirtualMachine = Azure::VirtualMachineManagement::VirtualMachine - CloudService = Azure::CloudServiceManagement::CloudService subject do Azure::VirtualMachineManagementService.new @@ -60,9 +59,14 @@ let(:cloud_services_xml) { Fixtures["list_cloud_services"] } let(:virtual_machine_xml) { Fixtures["virtual_machine"] } let(:deployment_error_xml) { Fixtures["deployment_error"] } + let(:virtual_networks_xml) { Fixtures["list_virtual_networks"] } + let(:method) { :get } + let(:mock_cloud_service_request){ mock() } let(:mock_virtual_machine_request){ mock() } + let(:mock_virtual_network_request){ mock() } + let(:cloud_service_response) { cloud_service_response = mock() cloud_service_response.stubs(:body).returns(cloud_services_xml) @@ -80,8 +84,16 @@ http_error_response.stubs(:body).returns(deployment_error_xml) http_error_response } + + let(:virtual_networks_response) { + virtual_networks_response = mock() + virtual_networks_response.stubs(:body).returns(virtual_networks_xml) + virtual_networks_response + } + let(:cloud_service_response_body) { Nokogiri::XML cloud_service_response.body } let(:virtual_machine_response_body) { Nokogiri::XML virtual_machine_response.body } + let(:virtual_networks_response_body) { Nokogiri::XML virtual_networks_response.body } before { ManagementHttpRequest.stubs(:new).with(method, request_path, nil).returns(mock_cloud_service_request) @@ -90,6 +102,8 @@ mock_virtual_machine_request.stubs(:warn=).returns(true).twice ManagementHttpRequest.stubs(:new).with(method, "/services/hostedservices/cloud-service-2/deploymentslots/production").returns(mock_virtual_machine_request) mock_virtual_machine_request.expects(:call).twice.returns(virtual_machine_response_body).returns(Nokogiri::XML deployment_error_response.body) + ManagementHttpRequest.stubs(:new).with(method, '/services/networking/virtualnetwork', nil).returns(mock_virtual_network_request) + mock_virtual_network_request.expects(:call).returns(virtual_networks_response_body) } it "assembles a URI for the request" do @@ -104,6 +118,9 @@ virtual_machine.cloud_service_name.must_equal 'cloud-service-1' virtual_machine.deployment_name.must_equal 'deployment-name' virtual_machine.ipaddress.must_equal '137.116.17.187' + virtual_machine.virtual_network_name.must_equal 'test-virtual-network' + virtual_machine.virtual_network.wont_be_nil + virtual_machine.virtual_network.must_be_kind_of Azure::VirtualNetworkManagement::VirtualNetwork end it "returns a list of virtual machines for the subscription" do @@ -182,7 +199,7 @@ Azure::VirtualMachineManagementService.stubs(:get_virtual_machine).returns(virtual_machine) } - it "should set options hash with valid cloud_service_name, deployment_name, storage_account_name." do + it "should set options hash with valid cloud_service_name, deployment_name, storage_account_name." do options = {} virtual_machine = subject.create_virtual_machine(params, options) options[:cloud_service_name].wont_be_nil @@ -255,12 +272,12 @@ assert_match(/You did not provide a valid 'vm_user' value*/, virtual_machine) end - it "throws error when certificate path is not given." do + it "self-signed certificate is generated by vm and used for the virtual machine when certificate path is not given." do options = { :winrm_transport => ['https','http'] } virtual_machine = subject.create_virtual_machine(windows_params, options) - assert_match(/You did not provide a valid 'private_key_file, certificate_file' value*/, virtual_machine) + virtual_machine.wont_be_nil end it "throws error when certificate path is not invalid." do diff --git a/test/unit/vnet/serialization_test.rb b/test/unit/vnet/serialization_test.rb index 2fa6de693a..cf23167cc8 100644 --- a/test/unit/vnet/serialization_test.rb +++ b/test/unit/vnet/serialization_test.rb @@ -12,12 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::VirtualNetworkManagement::Serialization do subject { Azure::VirtualNetworkManagement::Serialization } - let(:virtual_networks_xml) { Nokogiri::XML Fixtures['list_virtual_networks'] } + let(:virtual_networks_xml) do + Nokogiri::XML Fixtures['list_virtual_networks'] + end let(:virtual_networks_string) { Fixtures ['list_virtual_networks'] } let(:vnet_name) { 'test-vnet-name' } @@ -25,29 +27,43 @@ let(:address_space) { ['172.16.0.0/12', '10.0.0.1/12'] } input_options = { - :subnet => [ - { :name => 'vnet-test-unit-subnet-1', :ip_address => '172.16.0.0', :cidr => 12 }, - { :name => 'vnet-test-unit-subnet-2', :ip_address => '192.168.0.0', :cidr => 12 } + subnet: [ + { + name: 'vnet-test-unit-subnet-1', + ip_address: '172.16.0.0', + cidr: 12 + }, { + name: 'vnet-test-unit-subnet-2', + ip_address: '192.168.0.0', + cidr: 12 + } ], - :dns => [ - { :name => 'vnet-test-unit-dns-1', :ip_address => '1.2.3.4' }, - { :name => 'vnet-test-unit-dns-2', :ip_address => '2.3.4.5' } + dns: [ + { + name: 'vnet-test-unit-dns-1', + ip_address: '1.2.3.4' + }, { + name: 'vnet-test-unit-dns-2', + ip_address: '2.3.4.5' + } ] } - describe "#virtual_network_from_xml" do - it "accepts an XML string as input" do + describe '#virtual_network_from_xml' do + it 'accepts an XML string as input' do subject.virtual_network_from_xml virtual_networks_xml end - it "returns an Array of VirtualNetwork instances" do + it 'returns an Array of VirtualNetwork instances' do result = subject.virtual_network_from_xml virtual_networks_xml result.must_be_kind_of Array result.length.must_equal 3 end - it "returns a virtual network with all attributes populated" do - virtual_network = subject.virtual_network_from_xml(virtual_networks_xml).first + it 'returns a virtual network with all attributes populated' do + virtual_network = subject.virtual_network_from_xml( + virtual_networks_xml + ).first # Verify global properties virtual_network.name.must_equal 'test-virtual-network' @@ -75,63 +91,91 @@ end end - describe "#virtual_network_to_xml" do + describe '#virtual_network_to_xml' do before do - ManagementHttpRequest.any_instance.expects(:call).returns virtual_networks_xml + ManagementHttpRequest.any_instance.expects( + :call + ).returns(virtual_networks_xml) end - let(:result_string) { subject.virtual_network_to_xml(vnet_name, affinity_group, address_space, input_options) } + let(:result_string) do + subject.virtual_network_to_xml(vnet_name, + affinity_group, + address_space, + input_options) + end let(:result) { Nokogiri::XML result_string } - let(:virtual_network) { result.css('VirtualNetworkSites VirtualNetworkSite').last } + let(:virtual_network) do + result.css('VirtualNetworkSites VirtualNetworkSite').last + end - it "accepts parameters without providing subnet/dns" do - subject.virtual_network_to_xml vnet_name, affinity_group, address_space + it 'accepts parameters without providing subnet/dns' do + subject.virtual_network_to_xml(vnet_name, affinity_group, address_space) end - it "accepts parameters including subnet/dns" do - subject.virtual_network_to_xml vnet_name, affinity_group, address_space, input_options + it 'accepts parameters including subnet/dns' do + subject.virtual_network_to_xml(vnet_name, + affinity_group, + address_space, + input_options) end - it "appends the new VirtualNetwork to the existing" do + it 'appends the new VirtualNetwork to the existing' do result_string.must_be_kind_of String result.css('VirtualNetworkSites VirtualNetworkSite').size.must_equal 4 end - it "sets the name and affinity group for the new VirtualNetwork" do + it 'sets the name and affinity group for the new VirtualNetwork' do virtual_network.attr('name').must_equal vnet_name virtual_network.attr('AffinityGroup').must_equal affinity_group end - it "adds new dns servers on the top of existing dns servers" do + it 'adds new dns servers on the top of existing dns servers' do # Verify DnsServers dns_servers = result.css 'DnsServers DnsServer' dns_servers.size.must_equal 6 - dns_servers[0].attr('name').must_equal input_options[:dns][0][:name] - dns_servers[0].attr('IPAddress').must_equal input_options[:dns][0][:ip_address] - dns_servers[1].attr('name').must_equal input_options[:dns][1][:name] - dns_servers[1].attr('IPAddress').must_equal input_options[:dns][1][:ip_address] + dns_servers[0].attr('name').must_equal(input_options[:dns][0][:name]) + dns_servers[0].attr('IPAddress').must_equal( + input_options[:dns][0][:ip_address] + ) + dns_servers[1].attr('name').must_equal( + input_options[:dns][1][:name] + ) + dns_servers[1].attr('IPAddress').must_equal( + input_options[:dns][1][:ip_address] + ) end - it "adds DnsServerRef to VirtualNetworkSite" do + it 'adds DnsServerRef to VirtualNetworkSite' do # Verify DnsServerRef vnet_dns_servers = virtual_network.css 'DnsServersRef DnsServerRef' vnet_dns_servers.size.must_equal 2 - vnet_dns_servers[0].attr('name').must_equal input_options[:dns][0][:name] - vnet_dns_servers[1].attr('name').must_equal input_options[:dns][1][:name] + vnet_dns_servers[0].attr('name').must_equal( + input_options[:dns][0][:name] + ) + vnet_dns_servers[1].attr('name').must_equal( + input_options[:dns][1][:name] + ) end - it "adds Subnets to VirtualNetworkSite" do + it 'adds Subnets to VirtualNetworkSite' do # Verify Subnets vnet_subnets = virtual_network.css 'Subnets Subnet' vnet_subnets.size.must_equal 2 - vnet_subnets[0].attr('name').must_equal input_options[:subnet][0][:name] - vnet_subnets[0].css('AddressPrefix')[0].content.must_equal "#{input_options[:subnet][0][:ip_address]}/#{input_options[:subnet][0][:cidr]}" - vnet_subnets[1].attr('name').must_equal input_options[:subnet][1][:name] - vnet_subnets[1].css('AddressPrefix')[0].content.must_equal "#{input_options[:subnet][1][:ip_address]}/#{input_options[:subnet][1][:cidr]}" + vnet_subnets[0].attr('name').must_equal(input_options[:subnet][0][:name]) + vnet_subnets[0].css('AddressPrefix')[0].content.must_equal( + "#{input_options[:subnet][0][:ip_address]}/"\ + "#{input_options[:subnet][0][:cidr]}" + ) + vnet_subnets[1].attr('name').must_equal(input_options[:subnet][1][:name]) + vnet_subnets[1].css('AddressPrefix')[0].content.must_equal( + "#{input_options[:subnet][1][:ip_address]}/"\ + "#{input_options[:subnet][1][:cidr]}" + ) end - it "adds AddressPrefix to VirtualNetworkSite" do + it 'adds AddressPrefix to VirtualNetworkSite' do # Verify AddressSpaces vnet_address_space = virtual_network.css 'AddressSpace AddressPrefix' vnet_address_space.size.must_equal 2 diff --git a/test/unit/vnet/virtual_network_management_service_test.rb b/test/unit/vnet/virtual_network_management_service_test.rb index f9b7a731bb..c3f4a0ce46 100644 --- a/test/unit/vnet/virtual_network_management_service_test.rb +++ b/test/unit/vnet/virtual_network_management_service_test.rb @@ -12,22 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- -require "test_helper" +require 'test_helper' describe Azure::VirtualNetworkManagementService do subject { Azure::VirtualNetworkManagementService.new } - #Setup mock parameters and return values + # Setup mock parameters and return values let(:vmname) { 'vnet-test-unit-1' } let(:affinity_group) { 'RubyTest' } let(:address_space) { ['172.16.0.0/12'] } - input_options = { - :subnet => [{ :name => 'vnet-test-unit-subnet-1', :ip_address => '172.16.0.0', :cidr => 12 }], - :dns => [{ :name => 'vnet-test-unit-dns-1', :ipaddress => '1.2.3.4' }] - } - let(:non_existent_file) { '/this/file/doesnt/exist.xml' } let(:empty_xml_file) { './test/fixtures/empty_xml_file' } @@ -39,35 +34,45 @@ let(:getmethod) { :get } let(:putmethod) { :put } - let(:mock_list_virtual_networks_request) { mock() } + let(:mock_list_virtual_networks_request) { mock } let(:list_networks_response) do - list_networks_response = mock() + list_networks_response = mock list_networks_response.stubs(:body).returns(virtual_networks) list_networks_response end - let(:list_networks_response_body) { Nokogiri::XML list_networks_response.body } + let(:list_networks_response_body) do + Nokogiri::XML list_networks_response.body + end - let(:mock_set_network_request) { mock() } + let(:mock_set_network_request) { mock } let(:set_network_response) do - set_network_response = mock() + set_network_response = mock set_network_response.stubs(:body).returns(empty_xml_file) set_network_response end - let(:set_network_response_body) { Nokogiri::XML set_network_response.body } + let(:set_network_response_body) do + Nokogiri::XML set_network_response.body + end - describe "#list_virtual_networks" do + describe '#list_virtual_networks' do before do - ManagementHttpRequest.stubs(:new).with(getmethod, list_networks_path, nil).returns(mock_list_virtual_networks_request) - mock_list_virtual_networks_request.expects(:call).returns(list_networks_response_body) + ManagementHttpRequest.stubs(:new).with( + getmethod, + list_networks_path, + nil + ).returns(mock_list_virtual_networks_request) + mock_list_virtual_networks_request.expects(:call).returns( + list_networks_response_body + ) end - it "returns list of virtual networks" do + it 'returns list of virtual networks' do results = subject.list_virtual_networks results.must_be_kind_of Array results.length.must_equal 3 end - it "sets the properties of the virtual network" do + it 'sets the properties of the virtual network' do virtual_network = subject.list_virtual_networks.first # Verify global properties @@ -96,8 +101,8 @@ end end - describe "#set_network_configuration" do - it "throws error if wrong number of arguments (0) passed" do + describe '#set_network_configuration' do + it 'throws error if wrong number of arguments (0) passed' do exception = assert_raises(RuntimeError) do subject.set_network_configuration end @@ -105,15 +110,16 @@ assert_match 'Wrong number of arguments', exception.message end - it "checks if the file exists" do + it 'checks if the file exists' do exception = assert_raises(RuntimeError) do subject.set_network_configuration non_existent_file end - assert_match "Could not read from file '#{non_existent_file}'.", exception.message + assert_match("Could not read from file '#{non_existent_file}'.", + exception.message) end - it "checks if the file name ends in .xml" do + it 'checks if the file name ends in .xml' do exception = assert_raises(RuntimeError) do subject.set_network_configuration empty_xml_file end