initial tests for template rendering

This commit is contained in:
David Justice 2017-04-12 16:27:13 -07:00
Родитель 28f458e8b2
Коммит 712a1c69f7
7 изменённых файлов: 297 добавлений и 71 удалений

120
.rubocop.yml Normal file
Просмотреть файл

@ -0,0 +1,120 @@
AllCops:
TargetRubyVersion: 2.2
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
# to ignore them, so only the ones explicitly set in this file are enabled.
DisabledByDefault: true
# Prefer &&/|| over and/or.
Style/AndOr:
Enabled: true
# Do not use braces for hash literals when they are the last argument of a
# method call.
Style/BracesAroundHashParameters:
Enabled: true
# Align `when` with `case`.
Style/CaseIndentation:
Enabled: true
# Align comments with method definitions.
Style/CommentIndentation:
Enabled: true
# No extra empty lines.
Style/EmptyLines:
Enabled: true
# In a regular class definition, no empty lines around the body.
Style/EmptyLinesAroundClassBody:
Enabled: true
# In a regular method definition, no empty lines around the body.
Style/EmptyLinesAroundMethodBody:
Enabled: true
# In a regular module definition, no empty lines around the body.
Style/EmptyLinesAroundModuleBody:
Enabled: true
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
Style/HashSyntax:
Enabled: true
# Method definitions after `private` or `protected` isolated calls need one
# extra level of indentation.
Style/IndentationConsistency:
Enabled: true
EnforcedStyle: rails
# Two spaces, no tabs (for indentation).
Style/IndentationWidth:
Enabled: true
Style/SpaceAfterColon:
Enabled: true
Style/SpaceAfterComma:
Enabled: true
Style/SpaceAroundEqualsInParameterDefault:
Enabled: true
Style/SpaceAroundKeyword:
Enabled: true
Style/SpaceAroundOperators:
Enabled: true
Style/SpaceBeforeFirstArg:
Enabled: true
# Defining a method with parameters needs parentheses.
Style/MethodDefParentheses:
Enabled: true
# Use `foo {}` not `foo{}`.
Style/SpaceBeforeBlockBraces:
Enabled: true
# Use `foo { bar }` not `foo {bar}`.
Style/SpaceInsideBlockBraces:
Enabled: true
# Use `{ a: 1 }` not `{a:1}`.
Style/SpaceInsideHashLiteralBraces:
Enabled: true
Style/SpaceInsideParens:
Enabled: true
# Check quotes usage according to lint rule below.
Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
# Detect hard tabs, no hard tabs.
Style/Tab:
Enabled: true
# Blank lines should not have any spaces.
Style/TrailingBlankLines:
Enabled: true
# No trailing whitespace.
Style/TrailingWhitespace:
Enabled: true
# Use quotes for string literals when they are enough.
Style/UnneededPercentQ:
Enabled: true
# Align `end` with the matching keyword or starting expression except for
# assignments, where it should be aligned with the LHS.
Lint/EndAlignment:
Enabled: true
EnforcedStyleAlignWith: variable
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
Lint/RequireParentheses:
Enabled: true

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

@ -1,13 +1,13 @@
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'json'
require 'azure_mgmt_resources'
require 'vagrant/util/template_renderer'
require 'vagrant-azure/util/timer'
require 'vagrant-azure/util/machine_id_helper'
require 'haikunator'
require "log4r"
require "json"
require "azure_mgmt_resources"
require "vagrant/util/template_renderer"
require "vagrant-azure/util/timer"
require "vagrant-azure/util/machine_id_helper"
require "haikunator"
module VagrantPlugins
module Azure
@ -18,7 +18,7 @@ module VagrantPlugins
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_azure::action::run_instance')
@logger = Log4r::Logger.new("vagrant_azure::action::run_instance")
end
def call(env)
@ -38,7 +38,6 @@ module VagrantPlugins
location = config.location
ssh_user_name = machine.config.ssh.username
vm_name = config.vm_name
vm_password = config.vm_password
vm_size = config.vm_size
vm_image_urn = config.vm_image_urn
virtual_network_name = config.virtual_network_name
@ -70,14 +69,14 @@ module VagrantPlugins
env[:ui].info(" -- Availability Set Name: #{availability_set_name}") if availability_set_name
env[:ui].info(" -- DNS Label Prefix: #{dns_label_prefix}")
image_publisher, image_offer, image_sku, image_version = vm_image_urn.split(':')
image_publisher, image_offer, image_sku, image_version = vm_image_urn.split(":")
azure = env[:azure_arm_service]
image_details = nil
env[:metrics]['get_image_details'] = Util::Timer.time do
env[:metrics]["get_image_details"] = Util::Timer.time do
image_details = get_image_details(azure, location, image_publisher, image_offer, image_sku, image_version)
end
@logger.info("Time to fetch os image details: #{env[:metrics]['get_image_details']}")
@logger.info("Time to fetch os image details: #{env[:metrics]["get_image_details"]}")
deployment_params = {
dnsLabelPrefix: dns_label_prefix,
@ -106,23 +105,23 @@ module VagrantPlugins
deployment_template: deployment_template
}
if operating_system != 'Windows'
if operating_system != "Windows"
private_key_paths = machine.config.ssh.private_key_path
if private_key_paths.nil? || private_key_paths.empty?
raise I18n.t('vagrant_azure.private_key_not_specified')
end
paths_to_pub = private_key_paths.map{ |k| File.expand_path( k + '.pub') }.select{ |p| File.exists?(p) }
paths_to_pub = private_key_paths.map { |k| File.expand_path(k + ".pub") }.select { |p| File.exists?(p) }
raise I18n.t('vagrant_azure.public_key_path_private_key', private_key_paths.join(', ')) if paths_to_pub.empty?
deployment_params.merge!(adminUsername: ssh_user_name)
deployment_params.merge!(sshKeyData: File.read(paths_to_pub.first))
communicator_message = 'vagrant_azure.waiting_for_ssh'
communicator_message = "vagrant_azure.waiting_for_ssh"
else
env[:machine].config.vm.communicator = :winrm
machine.config.winrm.port = winrm_port
machine.config.winrm.username = admin_user_name
machine.config.winrm.password = admin_password
communicator_message = 'vagrant_azure.waiting_for_winrm'
communicator_message = "vagrant_azure.waiting_for_winrm"
windows_params = {
adminUsername: admin_user_name,
adminPassword: admin_password,
@ -131,44 +130,28 @@ module VagrantPlugins
deployment_params.merge!(windows_params)
end
unless tcp_endpoints.nil?
if tcp_endpoints.is_a?(Array)
# https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-nsg#Nsg-rules
if tcp_endpoints.length + 133 > 4096
raise I18n.t('vagrant_azure.too_many_tcp_endpoints', count: tcp_endpoints.length)
end
endpoints = tcp_endpoints
elsif tcp_endpoints.is_a?(String) || (tcp_endpoints.is_a?(Integer) && tcp_endpoints > 0)
endpoints = [tcp_endpoints]
else
raise I18n.t('vagrant_azure.unknown_type_as_tcp_endpoints', input: tcp_endpoints)
end
else
endpoints = []
end
template_params.merge!(endpoints: endpoints)
template_params.merge!(endpoints: get_endpoints(tcp_endpoints))
env[:ui].info(" -- Create or Update of Resource Group: #{resource_group_name}")
env[:metrics]['put_resource_group'] = Util::Timer.time do
env[:metrics]["put_resource_group"] = Util::Timer.time do
put_resource_group(azure, resource_group_name, location)
end
@logger.info("Time to create resource group: #{env[:metrics]['put_resource_group']}")
deployment_params = build_deployment_params(template_params, deployment_params.reject{|_,v| v.nil?})
deployment_params = build_deployment_params(template_params, deployment_params.reject { |_, v| v.nil? })
env[:ui].info(' -- Starting deployment')
env[:metrics]['deployment_time'] = Util::Timer.time do
env[:ui].info(" -- Starting deployment")
env[:metrics]["deployment_time"] = Util::Timer.time do
put_deployment(azure, resource_group_name, deployment_params)
end
env[:ui].info(' -- Finished deploying')
env[:ui].info(" -- Finished deploying")
# Immediately save the ID since it is created at this point.
env[:machine].id = serialize_machine_id(resource_group_name, vm_name, location)
@logger.info("Time to deploy: #{env[:metrics]['deployment_time']}")
unless env[:interrupted]
env[:metrics]['instance_ssh_time'] = Util::Timer.time do
env[:metrics]["instance_ssh_time"] = Util::Timer.time do
# Wait for SSH/WinRM to be ready.
env[:ui].info(I18n.t(communicator_message))
network_ready_retries = 0
@ -201,12 +184,30 @@ module VagrantPlugins
@app.call(env)
end
def get_endpoints(tcp_endpoints)
endpoints = []
unless tcp_endpoints.nil?
if tcp_endpoints.is_a?(Array)
# https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-nsg#Nsg-rules
if tcp_endpoints.length + 133 > 4096
raise I18n.t("vagrant_azure.too_many_tcp_endpoints", count: tcp_endpoints.length)
end
endpoints = tcp_endpoints
elsif tcp_endpoints.is_a?(String) || (tcp_endpoints.is_a?(Integer) && tcp_endpoints > 0)
endpoints = [tcp_endpoints]
else
raise I18n.t("vagrant_azure.unknown_type_as_tcp_endpoints", input: tcp_endpoints)
end
end
endpoints
end
def get_image_os(image_details)
image_details.os_disk_image.operating_system
end
def get_image_details(azure, location, publisher, offer, sku, version)
if version == 'latest'
if version == "latest"
images = azure.compute.virtual_machine_images.list(location, publisher, offer, sku)
latest = images.sort_by(&:name).last
azure.compute.virtual_machine_images.get(location, publisher, offer, sku, latest.name)
@ -231,15 +232,15 @@ module VagrantPlugins
# This method generates the deployment template
def render_deployment_template(options)
self_signed_cert_resource = nil
if options[:operating_system] == 'Windows' && options[:winrm_install_self_signed_cert]
setup_winrm_powershell = Vagrant::Util::TemplateRenderer.render('arm/setup-winrm.ps1', options.merge({template_root: template_root}))
if options[:operating_system] == "Windows" && options[:winrm_install_self_signed_cert]
setup_winrm_powershell = Vagrant::Util::TemplateRenderer.render("arm/setup-winrm.ps1", options.merge({template_root: template_root}))
encoded_setup_winrm_powershell = setup_winrm_powershell.
gsub("'", "', variables('singleQuote'), '").
gsub("\r\n", "\n").
gsub("\n", "; ")
self_signed_cert_resource = Vagrant::Util::TemplateRenderer.render('arm/selfsignedcert.json', options.merge({template_root: template_root, setup_winrm_powershell: encoded_setup_winrm_powershell}))
self_signed_cert_resource = Vagrant::Util::TemplateRenderer.render("arm/selfsignedcert.json", options.merge({template_root: template_root, setup_winrm_powershell: encoded_setup_winrm_powershell}))
end
Vagrant::Util::TemplateRenderer.render('arm/deployment.json', options.merge({ template_root: template_root, self_signed_cert_resource: self_signed_cert_resource}))
Vagrant::Util::TemplateRenderer.render("arm/deployment.json", options.merge({ template_root: template_root, self_signed_cert_resource: self_signed_cert_resource}))
end
def build_deployment_params(template_params, deployment_params)
@ -256,12 +257,12 @@ module VagrantPlugins
end
def build_parameters(options)
Hash[*options.map{ |k, v| [k, {value: v}] }.flatten]
Hash[*options.map { |k, v| [k, { value: v } ] }.flatten]
end
# Used to find the base location of aws-vagrant templates
def template_root
Azure.source_root.join('templates')
Azure.source_root.join("templates")
end
def terminate(env)

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

@ -1,6 +1,7 @@
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'vagrant-azure'
# import all the support files

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

@ -0,0 +1,124 @@
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require "spec_helper"
require "vagrant/util/template_renderer"
module VagrantPlugins
module Azure
describe "DeploymentTemplate" do
let(:options) {
{
operating_system: "linux",
location: "location",
endpoints: [22],
template_root: Azure.source_root.join("templates")
}
}
describe "the basics" do
let(:subject) {
render(options)
}
it "should specify schema" do
expect(subject["$schema"]).to eq("http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json")
end
it "should specify content version" do
expect(subject["contentVersion"]).to eq("1.0.0.0")
end
it "should have 13 parameters" do
expect(subject["parameters"].count).to eq(13)
end
it "should have 17 variables" do
expect(subject["variables"].count).to eq(17)
end
it "should have 7 resources" do
expect(subject["resources"].count).to eq(6)
end
end
describe "resources" do
describe "the virtual machine" do
let(:subject) {
render(options)["resources"].detect { |vm| vm["type"] == "Microsoft.Compute/virtualMachines" }
}
it "should depend on 2 resources without an AV Set" do
expect(subject["dependsOn"].count).to eq(2)
end
describe "with AV Set" do
let(:subject) {
template = render(options.merge(availability_set_name: "avSet"))
template["resources"].detect { |vm| vm["type"] == "Microsoft.Compute/virtualMachines" }
}
it "should depend on 3 resources with an AV Set" do
expect(subject["dependsOn"].count).to eq(3)
end
end
end
end
describe "parameters" do
let(:base_keys) {
%w( adminUserName dnsLabelPrefix nsgLabelPrefix vmSize vmName imagePublisher imageOffer imageSku imageVersion subnetName virtualNetworkName winRmPort )
}
let(:nix_keys) {
base_keys + ["sshKeyData"]
}
let(:subject) {
render(options)["parameters"]
}
it "should include all the *nix parameter keys" do
expect(subject.keys).to contain_exactly(*nix_keys)
end
describe "with Windows" do
let(:subject) {
render(options.merge(operating_system: "Windows"))["parameters"]
}
let(:win_keys) {
base_keys + ["adminPassword"]
}
it "should include all the windows parameter keys" do
expect(subject.keys).to contain_exactly(*win_keys)
end
end
end
describe "variables" do
let(:keys) {
%w(storageAccountName location osDiskName addressPrefix subnetPrefix vmStorageAccountContainerName nicName
publicIPAddressName publicIPAddressType storageAccountType networkSecurityGroupName sshKeyPath vnetID
subnetRef apiVersion singleQuote doubleQuote)
}
let(:subject) {
render(options)["variables"]
}
it "should include all the windows parameter keys" do
expect(subject.keys).to contain_exactly(*keys)
end
end
def render(options)
JSON.parse(Vagrant::Util::TemplateRenderer.render("arm/deployment.json", options))
end
end
end
end

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

@ -2,14 +2,14 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'vagrant'
require 'vagrant-azure/config'
require "spec_helper"
require "vagrant-azure/config"
module VagrantPlugins
module Azure
describe Config do
it 'should config' do
it "should config" do
end

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

@ -1,19 +0,0 @@
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'vagrant-azure/services/azure_resource_manager'
module VagrantPlugins
module Azure
module Services
describe AzureResourceManager do
it 'should be hello world' do
end
end
end
end
end

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

@ -163,7 +163,7 @@
"priority": 124,
"direction": "Inbound"
}
}
},
<% else %>
{
"name": "ssh_rule",
@ -178,10 +178,9 @@
"priority": 123,
"direction": "Inbound"
}
}
},
<% end %>
<% endpoints.each_with_index do |ports, index| %>
,
{
"name": "custom_rule_<%= index %>",
"properties": {