From 4b099731f1132739aaaf203cc417d254feb6862e Mon Sep 17 00:00:00 2001 From: Jason Barnett Date: Sat, 15 Jul 2023 12:26:46 -0600 Subject: [PATCH] feat: add configurable vm prefix (#264) Signed-off-by: Jason Barnett --- lib/kitchen/driver/azurerm.rb | 7 ++++++- spec/unit/kitchen/driver/azurerm_spec.rb | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/kitchen/driver/azurerm.rb b/lib/kitchen/driver/azurerm.rb index 5d5ab6d..f898e7c 100644 --- a/lib/kitchen/driver/azurerm.rb +++ b/lib/kitchen/driver/azurerm.rb @@ -84,6 +84,11 @@ class Azurerm < Kitchen::Driver::Base SecureRandom.base64(25) end + # This prefix MUST be no longer than 3 characters + default_config(:vm_prefix) do |_config| + "tk-" + end + default_config :vm_name, nil default_config :store_deployment_credentials_in_state, true @@ -379,7 +384,7 @@ def existing_state_value?(state, property) # @return [Hash] Updated Hash of state values. def validate_state(state = {}) state[:uuid] = SecureRandom.hex(8) unless existing_state_value?(state, :uuid) - state[:vm_name] = config[:vm_name] || "tk-#{state[:uuid][0..11]}" unless existing_state_value?(state, :vm_name) + state[:vm_name] = config[:vm_name] || "#{config[:vm_prefix]}#{state[:uuid][0..11]}" unless existing_state_value?(state, :vm_name) state[:server_id] = "vm#{state[:uuid]}" unless existing_state_value?(state, :server_id) state[:azure_resource_group_name] = azure_resource_group_name unless existing_state_value?(state, :azure_resource_group_name) %i{subscription_id azure_environment use_managed_disks}.each do |config_element| diff --git a/spec/unit/kitchen/driver/azurerm_spec.rb b/spec/unit/kitchen/driver/azurerm_spec.rb index a6938db..198f936 100644 --- a/spec/unit/kitchen/driver/azurerm_spec.rb +++ b/spec/unit/kitchen/driver/azurerm_spec.rb @@ -110,6 +110,10 @@ it "should set store_deployment_credentials_in_state to true" do expect(default_config[:store_deployment_credentials_in_state]).to eq(true) end + + it "Should use tk- vm prefix" do + expect(default_config[:vm_prefix]).to eq("tk-") + end end describe "#validate_state" do @@ -150,6 +154,7 @@ expect(state[:vm_name].length).to eq(15) expect(state[:vm_name]).to be_an_instance_of(String) expect(state[:vm_name]).not_to eq(vm_name) + expect(state[:vm_name]).to start_with("tk-") end it "does not generate vm_name, when one exists in state" do @@ -158,6 +163,20 @@ driver.validate_state(state) expect(state[:vm_name]).to eq(vm_name_in_state) end + + context "when vm_prefix is set in config" do + before do + config[:vm_prefix] = "ab-" + end + + it "generates vm_name with prefix, when one does not exist in state" do + driver.validate_state(state) + expect(state[:vm_name].length).to eq(15) + expect(state[:vm_name]).to be_an_instance_of(String) + expect(state[:vm_name]).not_to eq(vm_name) + expect(state[:vm_name]).to start_with("ab-") + end + end end end