Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add configurable vm prefix #264

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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