simplify stubbing in validators
This commit is contained in:
		
							parent
							
								
									df44b5b269
								
							
						
					
					
						commit
						4bab257970
					
				| @ -53,7 +53,7 @@ module Devops | ||||
|       def subnets_filter | ||||
|         networks = provider_instance.networks | ||||
| 
 | ||||
|         unless self.subnets.empty? | ||||
|         if subnets && !subnets.empty? | ||||
|           network = networks.detect {|n| n["name"] == self.subnets[0]} | ||||
|           if network | ||||
|             {"vpc-id" => network["vpcId"] } | ||||
|  | ||||
| @ -12,6 +12,7 @@ module Validators | ||||
|       raise InvalidRecord.new("An error raised during validation with #{self.class}: #{e.class}: #{e.message}") | ||||
|     end | ||||
| 
 | ||||
|     # :nocov: | ||||
|     def valid? | ||||
|       raise 'override me' | ||||
|     end | ||||
| @ -19,6 +20,7 @@ module Validators | ||||
|     def message | ||||
|       raise 'override me' | ||||
|     end | ||||
|     # :nocov: | ||||
| 
 | ||||
|     class << self | ||||
|       private | ||||
|  | ||||
| @ -3,7 +3,7 @@ module Validators | ||||
| 
 | ||||
|     def valid? | ||||
|       return true unless @model.flavor | ||||
|       available_flavors.detect do |flavor| | ||||
|       @model.provider_instance.flavors.detect do |flavor| | ||||
|         flavor['id'] == @model.flavor | ||||
|       end | ||||
|     end | ||||
| @ -11,11 +11,5 @@ module Validators | ||||
|     def message | ||||
|       "Invalid flavor '#{@model.flavor}'." | ||||
|     end | ||||
| 
 | ||||
|     private | ||||
| 
 | ||||
|     def available_flavors | ||||
|       @model.provider_instance.flavors | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -3,6 +3,9 @@ module Validators | ||||
| 
 | ||||
|     def valid? | ||||
|       return true if @model.groups.nil? | ||||
|       subnets_filter = @model.subnets_filter | ||||
|       available_groups = @model.provider_instance.groups(subnets_filter).keys | ||||
| 
 | ||||
|       @invalid_groups = @model.groups - available_groups | ||||
|       @invalid_groups.empty? | ||||
|     end | ||||
| @ -10,12 +13,5 @@ module Validators | ||||
|     def message | ||||
|       "Invalid groups '#{@invalid_groups.join("', '")}'." | ||||
|     end | ||||
| 
 | ||||
|     private | ||||
| 
 | ||||
|     def available_groups | ||||
|       subnets_filter = @model.subnets_filter | ||||
|       @model.provider_instance.groups(subnets_filter).keys | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -6,7 +6,7 @@ module Validators | ||||
| 
 | ||||
|     def valid? | ||||
|       return true unless @model.image | ||||
|       available_images.detect do |image| | ||||
|       get_available_provider_images(::Devops::Db.connector, @model.provider).detect do |image| | ||||
|         image["id"] == @model.image | ||||
|       end | ||||
|     end | ||||
| @ -14,11 +14,5 @@ module Validators | ||||
|     def message | ||||
|       "Invalid image '#{@model.image}'." | ||||
|     end | ||||
| 
 | ||||
|     private | ||||
| 
 | ||||
|     def available_images | ||||
|       get_available_provider_images(::Devops::Db.connector, @model.provider) | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -4,21 +4,13 @@ module Validators | ||||
|     def valid? | ||||
|       return true unless @model.stack_template | ||||
| 
 | ||||
|       available_stack_templates.detect do |template| | ||||
|         template['id'] == @model.stack_template | ||||
|       Devops::Db.connector.stack_templates.detect do |template| | ||||
|         template.id == @model.stack_template | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|     def message | ||||
|       "Invalid stack template '#{@model.stack_template}'." | ||||
|     end | ||||
| 
 | ||||
|     private | ||||
| 
 | ||||
|     def available_stack_templates | ||||
|       # map to hash to simplify mocks. Later replace this method with something more suitable | ||||
|       Devops::Db.connector.stack_templates.map(&:to_hash) | ||||
|     end | ||||
| 
 | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -4,7 +4,7 @@ module Validators | ||||
|     class Flavor < Base | ||||
| 
 | ||||
|       def valid? | ||||
|         available_flavors.detect do |flavor| | ||||
|         @model.provider_instance.flavors.detect do |flavor| | ||||
|           flavor['id'] == @value | ||||
|         end | ||||
|       end | ||||
| @ -12,12 +12,6 @@ module Validators | ||||
|       def message | ||||
|         "Invalid flavor '#{@value}'." | ||||
|       end | ||||
| 
 | ||||
|       private | ||||
| 
 | ||||
|       def available_flavors | ||||
|         @model.provider_instance.flavors | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -7,7 +7,7 @@ module Validators | ||||
|       include ::ImageCommands | ||||
| 
 | ||||
|       def valid? | ||||
|         available_images.detect do |image| | ||||
|         get_available_provider_images(::Devops::Db.connector, @model.provider).detect do |image| | ||||
|           image["id"] == @value | ||||
|         end | ||||
|       end | ||||
| @ -15,12 +15,6 @@ module Validators | ||||
|       def message | ||||
|         "Invalid image '#{@value}'." | ||||
|       end | ||||
| 
 | ||||
|       private | ||||
| 
 | ||||
|       def available_images | ||||
|         get_available_provider_images(::Devops::Db.connector, @model.provider) | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -2,6 +2,7 @@ module Validators | ||||
|   class Helpers::Users < Base | ||||
| 
 | ||||
|     def valid? | ||||
|       available_users = ::Devops::Db.connector.users_names(@model) | ||||
|       @nonexistent_users = (@model || []) - available_users | ||||
|       @nonexistent_users.empty? | ||||
|     end | ||||
| @ -9,11 +10,5 @@ module Validators | ||||
|     def message | ||||
|       Devops::Messages.t("project.deploy_env.validation.users.not_exist", users: @nonexistent_users.join("', '")) | ||||
|     end | ||||
| 
 | ||||
|     private | ||||
| 
 | ||||
|     def available_users | ||||
|       ::Devops::Db.connector.users_names(@model) | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -5,7 +5,21 @@ require_relative 'shared_cloud_deploy_env_specs' | ||||
| RSpec.describe Devops::Model::DeployEnvEc2, type: :model do | ||||
|   let(:env) { build(:deploy_env_ec2) } | ||||
| 
 | ||||
|   describe 'it inherits from cloud deploy_env', stubbed_env_validators: true do | ||||
|   describe 'it inherits from cloud deploy_env', stubbed_connector: true do | ||||
|     before do | ||||
|       provider_double = instance_double('Provider::Ec2', | ||||
|         flavors: [{'id' => 'flavor'}], | ||||
|         networks: [{'default' => {'vpcId' => 'foo'}}], | ||||
|         groups: {'default' => nil}, | ||||
|         images: [{'id' => 'image'}] | ||||
|       ) | ||||
|       allow(Provider::ProviderFactory).to receive(:providers) { %w(ec2) } | ||||
|       allow(Provider::ProviderFactory).to receive(:get) { provider_double } | ||||
|       allow(stubbed_connector).to receive(:users_names) { %w(root) } | ||||
|       allow(stubbed_connector).to receive(:available_images) { %w(image) } | ||||
|       allow(stubbed_connector).to receive(:stack_templates) { [build(:stack_template_ec2, id: 'template')] } | ||||
|     end | ||||
| 
 | ||||
|     it_behaves_like 'deploy env' | ||||
|     it_behaves_like 'cloud deploy env' | ||||
|   end | ||||
|  | ||||
| @ -5,7 +5,21 @@ require_relative 'shared_cloud_deploy_env_specs' | ||||
| RSpec.describe Devops::Model::DeployEnvOpenstack, type: :model do | ||||
|   let(:env) { build(:deploy_env_openstack) } | ||||
| 
 | ||||
|   describe 'it inherits from cloud deploy_env', stubbed_env_validators: true do | ||||
|   describe 'it inherits from cloud deploy_env', stubbed_connector: true do | ||||
|     before do | ||||
|       provider_double = instance_double('Provider::Openstack', | ||||
|         flavors: [{'id' => 'flavor'}], | ||||
|         networks: [{'default' => {'vpcId' => 'foo'}}], | ||||
|         groups: {'default' => nil}, | ||||
|         images: [{'id' => 'image'}] | ||||
|       ) | ||||
|       allow(Provider::ProviderFactory).to receive(:providers) { %w(openstack) } | ||||
|       allow(Provider::ProviderFactory).to receive(:get) { provider_double } | ||||
|       allow(stubbed_connector).to receive(:users_names) { %w(root) } | ||||
|       allow(stubbed_connector).to receive(:available_images) { %w(image) } | ||||
|       allow(stubbed_connector).to receive(:stack_templates) { [build(:stack_template_openstack, id: 'template')] } | ||||
|     end | ||||
| 
 | ||||
|     it_behaves_like 'deploy env' | ||||
|     it_behaves_like 'cloud deploy env' | ||||
|   end | ||||
|  | ||||
| @ -5,10 +5,10 @@ RSpec.describe Devops::Model::DeployEnvStatic, type: :model do | ||||
|   let(:env) { build(:deploy_env_static) } | ||||
| 
 | ||||
| 
 | ||||
|   describe 'it inherits from deploy env', stubbed_logger: true do | ||||
|   describe 'it inherits from deploy env', stubbed_logger: true, stubbed_connector: true do | ||||
|     before do | ||||
|       allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(static)) | ||||
|       allow_any_instance_of(Validators::Helpers::Users).to receive(:available_users).and_return(['root']) | ||||
|       allow(stubbed_connector).to receive(:users_names) { %w(root) } | ||||
|     end | ||||
| 
 | ||||
|     it_behaves_like 'deploy env' | ||||
|  | ||||
| @ -3,7 +3,21 @@ require 'db/mongo/models/project' | ||||
| RSpec.describe Devops::Model::Project, type: :model do | ||||
|   let(:project) { build(:project) } | ||||
| 
 | ||||
|   describe 'validation rules:', stubbed_env_validators: true do | ||||
|   describe 'validation rules:', stubbed_connector: true do | ||||
|     before do | ||||
|       provider_double = instance_double('Provider::Ec2', | ||||
|         flavors: [{'id' => 'flavor'}], | ||||
|         networks: [{'default' => {'vpcId' => 'foo'}}], | ||||
|         groups: {'default' => nil}, | ||||
|         images: [{'id' => 'image'}] | ||||
|       ) | ||||
|       allow(Provider::ProviderFactory).to receive(:providers) { %w(ec2) } | ||||
|       allow(Provider::ProviderFactory).to receive(:get) { provider_double } | ||||
|       allow(stubbed_connector).to receive(:users_names) { %w(root) } | ||||
|       allow(stubbed_connector).to receive(:available_images) { %w(image) } | ||||
|       allow(stubbed_connector).to receive(:stack_templates) { [build(:stack_template_ec2, id: 'template')] } | ||||
|     end | ||||
| 
 | ||||
|     include_examples 'field type validation', :id, :not_nil, :non_empty_string | ||||
|     include_examples 'field type validation', :deploy_envs, :not_nil, :non_empty_array | ||||
|     include_examples 'field type validation', :description, :maybe_nil, :maybe_empty_string | ||||
|  | ||||
| @ -1,9 +1,10 @@ | ||||
| RSpec.shared_context 'stubbed calls to connector in env validators', stubbed_env_validators: true do | ||||
|   before do | ||||
|     allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(ec2 openstack)) | ||||
|     allow_any_instance_of(env_class).to receive_message_chain('provider_instance.flavors').and_return [{'id' => 'flavor'}] | ||||
|     allow_any_instance_of(Validators::Helpers::Users).to receive(:available_users).and_return(['root']) | ||||
|     allow_any_instance_of(Validators::DeployEnv::Flavor).to receive(:available_flavors).and_return([{'id' => 'flavor'}]) | ||||
|     allow_any_instance_of(Validators::FieldValidator::Flavor).to receive(:available_flavors).and_return([{'id' => 'flavor'}]) | ||||
|     # allow_any_instance_of(Validators::DeployEnv::Flavor).to receive(:available_flavors).and_return([{'id' => 'flavor'}]) | ||||
|     # allow_any_instance_of(Validators::FieldValidator::Flavor).to receive(:available_flavors).and_return([{'id' => 'flavor'}]) | ||||
|     allow_any_instance_of(Validators::DeployEnv::Groups).to receive(:available_groups).and_return(['default']) | ||||
|     allow_any_instance_of(Validators::DeployEnv::Image).to receive(:available_images).and_return([{'id' => 'image'}]) | ||||
|     allow_any_instance_of(Validators::DeployEnv::Image).to receive(:available_images).and_return([{'id' => 'image'}]) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Anton Chuchkalov
						Anton Chuchkalov