From 625472df35cbb5499a87efde97fc4a77f7d462a5 Mon Sep 17 00:00:00 2001 From: Brandon High Date: Fri, 1 Nov 2019 09:09:34 -0700 Subject: [PATCH] (QENG-7530) Fix hostname_shorten regex Prior to this commit the hostname_shorten regex wouldn't match the updated human readable hostnames because they contain dashes. This commit updates the regex to capture dashes in the hostname, and adds a few specs to verify that behavior. --- lib/vmpooler/api/helpers.rb | 2 +- spec/unit/api/helpers_spec.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/vmpooler/api/helpers.rb b/lib/vmpooler/api/helpers.rb index 54636ca..6fdd2f6 100644 --- a/lib/vmpooler/api/helpers.rb +++ b/lib/vmpooler/api/helpers.rb @@ -140,7 +140,7 @@ module Vmpooler end def hostname_shorten(hostname, domain=nil) - if domain && hostname =~ /^\w+\.#{domain}$/ + if domain && hostname =~ /^[\w-]+\.#{domain}$/ hostname = hostname[/[^\.]+/] end diff --git a/spec/unit/api/helpers_spec.rb b/spec/unit/api/helpers_spec.rb index e026cf7..62fed34 100644 --- a/spec/unit/api/helpers_spec.rb +++ b/spec/unit/api/helpers_spec.rb @@ -19,6 +19,8 @@ describe Vmpooler::API::Helpers do ['example.com', 'not-example.com', 'example.com'], ['example.com', 'example.com', 'example.com'], ['sub.example.com', 'example.com', 'sub'], + ['adjective-noun.example.com', 'example.com', 'adjective-noun'], + ['abc123.example.com', 'example.com', 'abc123'], ['example.com', nil, 'example.com'] ].each do |hostname, domain, expected| it { expect(subject.hostname_shorten(hostname, domain)).to eq expected }