Skip to content

Commit

Permalink
feat: add configurable vm prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Barnett <jason.w.barnett@gmail.com>
  • Loading branch information
jasonwbarnett committed Jul 11, 2023
1 parent bbb1bba commit da25a26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/kitchen/driver/azurerm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
19 changes: 19 additions & 0 deletions spec/unit/kitchen/driver/azurerm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit da25a26

Please sign in to comment.