From 91248fe23acbbe5ad980ab63691a38f0cb173252 Mon Sep 17 00:00:00 2001 From: Jake Spain Date: Thu, 23 Feb 2023 15:53:35 -0500 Subject: [PATCH] Added spec tests for Vmpooler::Dns --- lib/vmpooler/dns.rb | 41 +++++-------- spec/unit/dns_spec.rb | 62 ++++++++++++++++++++ spec/unit/util/parsing_spec.rb | 103 ++------------------------------- 3 files changed, 83 insertions(+), 123 deletions(-) create mode 100644 spec/unit/dns_spec.rb diff --git a/lib/vmpooler/dns.rb b/lib/vmpooler/dns.rb index 4534a78..823fe17 100644 --- a/lib/vmpooler/dns.rb +++ b/lib/vmpooler/dns.rb @@ -38,13 +38,9 @@ module Vmpooler pool = config[:pools].find { |p| p['name'] == pool_name } pool_dns_config = pool['dns_plugin'] dns_configs = config[:dns_configs].keys - pool_domain = '' - dns_configs.map do |dns_config_name| - pool_domain = config[:dns_configs][dns_config_name]['domain'] if dns_config_name.to_s == pool_dns_config + return config[:dns_configs][dns_config_name]['domain'] if dns_config_name.to_s == pool_dns_config end - - pool_domain end # Returns the plugin domain for the specified dns config by name @@ -54,34 +50,30 @@ module Vmpooler # @return [String] The domain for the specifid dns config def self.get_dns_plugin_domain_by_name(config, name) dns_configs = config[:dns_configs].keys - plugin_domain = '' - dns_configs.map do |dns_config_name| - plugin_domain = config[:dns_configs][dns_config_name]['domain'] if dns_config_name.to_s == name + return config[:dns_configs][dns_config_name]['domain'] if dns_config_name.to_s == name end - - plugin_domain end # Returns a list of DNS plugin classes specified in the vmpooler configuration # # @param config [Object] The entire VMPooler config object - # @return [Array] A list of DNS plugin classes def self.get_dns_plugin_config_classes(config) - if config[:dns_configs] - dns_configs = config[:dns_configs].keys - dns_plugins = dns_configs.map do |dns_config_name| - if config[:dns_configs][dns_config_name] && config[:dns_configs][dns_config_name]['dns_class'] - config[:dns_configs][dns_config_name]['dns_class'].to_s - else - dns_config_name.to_s - end - end.compact.uniq + return nil unless config[:dns_configs] - # dynamic-dns is not actually a class, it's just used as a value to denote - # that dynamic dns is used so no loading or record management is needed - dns_plugins.delete('dynamic-dns') - end + dns_configs = config[:dns_configs].keys + dns_plugins = dns_configs.map do |dns_config_name| + if config[:dns_configs][dns_config_name] && config[:dns_configs][dns_config_name]['dns_class'] + config[:dns_configs][dns_config_name]['dns_class'].to_s + else + dns_config_name.to_s + end + end.compact.uniq + + # dynamic-dns is not actually a class, it's just used as a value to denote + # that dynamic dns is used so no loading or record management is needed + dns_plugins.delete('dynamic-dns') dns_plugins end @@ -93,7 +85,6 @@ module Vmpooler def load_from_gems(name = nil) require_path = "vmpooler/dns/#{name.gsub('-', '/')}" require require_path - $logger.log('d', "[*] [dns_manager] Loading DNS plugins from dns_configs: #{name}") require_path end end diff --git a/spec/unit/dns_spec.rb b/spec/unit/dns_spec.rb new file mode 100644 index 0000000..3e02704 --- /dev/null +++ b/spec/unit/dns_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper' + +describe 'Vmpooler::Dns' do + let(:dns_class) { 'mock-dnsservice' } + let(:dns_config_name) { 'mock' } + let(:pool) { 'pool1' } + let(:config) { YAML.load(<<~EOT + --- + :dns_configs: + :mock: + dns_class: 'mock' + domain: 'example.com' + :pools: + - name: 'pool1' + dns_plugin: 'mock' +EOT + )} + subject { Vmpooler::Dns.new } + + describe '.get_dns_plugin_class_by_name' do + it 'returns the plugin class for the specified config' do + result = Vmpooler::Dns.get_dns_plugin_class_by_name(config, dns_config_name) + expect(result).to eq('mock') + end + end + + describe '.get_domain_for_pool' do + it 'returns the domain for the specified pool' do + result = Vmpooler::Dns.get_domain_for_pool(config, pool) + expect(result).to eq('example.com') + end + end + + describe '.get_dns_plugin_domain_by_name' do + it 'returns the domain for the specified config' do + result = Vmpooler::Dns.get_dns_plugin_domain_by_name(config, dns_config_name) + expect(result).to eq('example.com') + end + end + + describe '.get_dns_plugin_config_classes' do + it 'returns the list of dns plugin classes' do + result = Vmpooler::Dns.get_dns_plugin_config_classes(config) + expect(result).to eq(['mock']) + end + end + + describe '#load_from_gems' do + let(:gem_name) { 'mock-dnsservice' } + let(:translated_gem_name) { 'mock/dnsservice' } + + before(:each) do + allow(subject).to receive(:require).with(gem_name).and_return(true) + end + + it 'loads the specified gem' do + expect(subject).to receive(:require).with("vmpooler/dns/#{translated_gem_name}") + result = subject.load_from_gems(gem_name) + expect(result).to eq("vmpooler/dns/#{translated_gem_name}") + end + end +end diff --git a/spec/unit/util/parsing_spec.rb b/spec/unit/util/parsing_spec.rb index 3546600..c0277f9 100644 --- a/spec/unit/util/parsing_spec.rb +++ b/spec/unit/util/parsing_spec.rb @@ -1,104 +1,11 @@ require 'spec_helper' -describe 'Parser' do +# The only method previously tested here was '#get_domain_for_pool' +# which was moved to Vmpooler::Dns as the more appropriate class +# +# TODO: Add tests for last remaining method, or move to more appropriate class +describe 'Vmpooler::Parsing' do let(:pool) { 'pool1' } subject { Vmpooler::Parsing } - describe '.get_domain_for_pool' do - let(:provider_name) { 'mock_provider' } - context 'No provider is set' do - let(:config) { YAML.load(<<~EOT - --- - :config: - :providers: - :mock_provider: - :pools: - - name: '#{pool}' - size: 1 - EOT - )} - it 'should return nil' do - result = subject.get_domain_for_pool(config, pool) - expect(result).to be_nil - end - end - context 'Provider is vsphere by default' do - let(:config) { YAML.load(<<~EOT - --- - :config: - :providers: - :vsphere: - domain: myown.com - :pools: - - name: '#{pool}' - size: 1 - EOT - )} - - it 'should return the domain set for vsphere' do - result = subject.get_domain_for_pool(config, pool) - expect(result).to eq('myown.com') - end - end - context 'No domain is set' do - let(:config) { YAML.load(<<~EOT - --- - :config: - :providers: - :mock_provider: - :pools: - - name: '#{pool}' - size: 1 - provider: #{provider_name} - EOT - )} - - it 'should return nil' do - result = subject.get_domain_for_pool(config, pool) - expect(result).to be_nil - end - end - - context 'Only a global domain is set' do - let(:config) { YAML.load(<<~EOT - --- - :config: - domain: example.com - :providers: - :mock_provider: - :pools: - - name: '#{pool}' - size: 1 - provider: #{provider_name} - EOT - )} - - it 'should return the domain set in the config section' do - result = subject.get_domain_for_pool(config, pool) - expect(result).to_not be_nil - expect(result).to eq('example.com') - end - end - - context 'A provider specified a domain to use' do - let(:config) { YAML.load(<<~EOT - --- - :config: - :providers: - :mock_provider: - domain: m.example.com - :pools: - - name: '#{pool}' - size: 1 - provider: #{provider_name} - EOT - )} - - it 'should return the domain set in the config section' do - result = subject.get_domain_for_pool(config, pool) - expect(result).to_not be_nil - expect(result).to eq('m.example.com') - end - end - end end