simplify stubbing in validators
This commit is contained in:
parent
df44b5b269
commit
4bab257970
@ -53,7 +53,7 @@ module Devops
|
|||||||
def subnets_filter
|
def subnets_filter
|
||||||
networks = provider_instance.networks
|
networks = provider_instance.networks
|
||||||
|
|
||||||
unless self.subnets.empty?
|
if subnets && !subnets.empty?
|
||||||
network = networks.detect {|n| n["name"] == self.subnets[0]}
|
network = networks.detect {|n| n["name"] == self.subnets[0]}
|
||||||
if network
|
if network
|
||||||
{"vpc-id" => network["vpcId"] }
|
{"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}")
|
raise InvalidRecord.new("An error raised during validation with #{self.class}: #{e.class}: #{e.message}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# :nocov:
|
||||||
def valid?
|
def valid?
|
||||||
raise 'override me'
|
raise 'override me'
|
||||||
end
|
end
|
||||||
@ -19,6 +20,7 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
raise 'override me'
|
raise 'override me'
|
||||||
end
|
end
|
||||||
|
# :nocov:
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
private
|
private
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module Validators
|
|||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
return true unless @model.flavor
|
return true unless @model.flavor
|
||||||
available_flavors.detect do |flavor|
|
@model.provider_instance.flavors.detect do |flavor|
|
||||||
flavor['id'] == @model.flavor
|
flavor['id'] == @model.flavor
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -11,11 +11,5 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
"Invalid flavor '#{@model.flavor}'."
|
"Invalid flavor '#{@model.flavor}'."
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_flavors
|
|
||||||
@model.provider_instance.flavors
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -3,6 +3,9 @@ module Validators
|
|||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
return true if @model.groups.nil?
|
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 = @model.groups - available_groups
|
||||||
@invalid_groups.empty?
|
@invalid_groups.empty?
|
||||||
end
|
end
|
||||||
@ -10,12 +13,5 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
"Invalid groups '#{@invalid_groups.join("', '")}'."
|
"Invalid groups '#{@invalid_groups.join("', '")}'."
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_groups
|
|
||||||
subnets_filter = @model.subnets_filter
|
|
||||||
@model.provider_instance.groups(subnets_filter).keys
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -6,7 +6,7 @@ module Validators
|
|||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
return true unless @model.image
|
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
|
image["id"] == @model.image
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -14,11 +14,5 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
"Invalid image '#{@model.image}'."
|
"Invalid image '#{@model.image}'."
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_images
|
|
||||||
get_available_provider_images(::Devops::Db.connector, @model.provider)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,21 +4,13 @@ module Validators
|
|||||||
def valid?
|
def valid?
|
||||||
return true unless @model.stack_template
|
return true unless @model.stack_template
|
||||||
|
|
||||||
available_stack_templates.detect do |template|
|
Devops::Db.connector.stack_templates.detect do |template|
|
||||||
template['id'] == @model.stack_template
|
template.id == @model.stack_template
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def message
|
def message
|
||||||
"Invalid stack template '#{@model.stack_template}'."
|
"Invalid stack template '#{@model.stack_template}'."
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module Validators
|
|||||||
class Flavor < Base
|
class Flavor < Base
|
||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
available_flavors.detect do |flavor|
|
@model.provider_instance.flavors.detect do |flavor|
|
||||||
flavor['id'] == @value
|
flavor['id'] == @value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -12,12 +12,6 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
"Invalid flavor '#{@value}'."
|
"Invalid flavor '#{@value}'."
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_flavors
|
|
||||||
@model.provider_instance.flavors
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -7,7 +7,7 @@ module Validators
|
|||||||
include ::ImageCommands
|
include ::ImageCommands
|
||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
available_images.detect do |image|
|
get_available_provider_images(::Devops::Db.connector, @model.provider).detect do |image|
|
||||||
image["id"] == @value
|
image["id"] == @value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -15,12 +15,6 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
"Invalid image '#{@value}'."
|
"Invalid image '#{@value}'."
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_images
|
|
||||||
get_available_provider_images(::Devops::Db.connector, @model.provider)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,6 +2,7 @@ module Validators
|
|||||||
class Helpers::Users < Base
|
class Helpers::Users < Base
|
||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
|
available_users = ::Devops::Db.connector.users_names(@model)
|
||||||
@nonexistent_users = (@model || []) - available_users
|
@nonexistent_users = (@model || []) - available_users
|
||||||
@nonexistent_users.empty?
|
@nonexistent_users.empty?
|
||||||
end
|
end
|
||||||
@ -9,11 +10,5 @@ module Validators
|
|||||||
def message
|
def message
|
||||||
Devops::Messages.t("project.deploy_env.validation.users.not_exist", users: @nonexistent_users.join("', '"))
|
Devops::Messages.t("project.deploy_env.validation.users.not_exist", users: @nonexistent_users.join("', '"))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_users
|
|
||||||
::Devops::Db.connector.users_names(@model)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,21 @@ require_relative 'shared_cloud_deploy_env_specs'
|
|||||||
RSpec.describe Devops::Model::DeployEnvEc2, type: :model do
|
RSpec.describe Devops::Model::DeployEnvEc2, type: :model do
|
||||||
let(:env) { build(:deploy_env_ec2) }
|
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 'deploy env'
|
||||||
it_behaves_like 'cloud deploy env'
|
it_behaves_like 'cloud deploy env'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,21 @@ require_relative 'shared_cloud_deploy_env_specs'
|
|||||||
RSpec.describe Devops::Model::DeployEnvOpenstack, type: :model do
|
RSpec.describe Devops::Model::DeployEnvOpenstack, type: :model do
|
||||||
let(:env) { build(:deploy_env_openstack) }
|
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 'deploy env'
|
||||||
it_behaves_like 'cloud deploy env'
|
it_behaves_like 'cloud deploy env'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,10 +5,10 @@ RSpec.describe Devops::Model::DeployEnvStatic, type: :model do
|
|||||||
let(:env) { build(:deploy_env_static) }
|
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
|
before do
|
||||||
allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(static))
|
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
|
end
|
||||||
|
|
||||||
it_behaves_like 'deploy env'
|
it_behaves_like 'deploy env'
|
||||||
|
|||||||
@ -3,7 +3,21 @@ require 'db/mongo/models/project'
|
|||||||
RSpec.describe Devops::Model::Project, type: :model do
|
RSpec.describe Devops::Model::Project, type: :model do
|
||||||
let(:project) { build(:project) }
|
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', :id, :not_nil, :non_empty_string
|
||||||
include_examples 'field type validation', :deploy_envs, :not_nil, :non_empty_array
|
include_examples 'field type validation', :deploy_envs, :not_nil, :non_empty_array
|
||||||
include_examples 'field type validation', :description, :maybe_nil, :maybe_empty_string
|
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
|
RSpec.shared_context 'stubbed calls to connector in env validators', stubbed_env_validators: true do
|
||||||
before do
|
before do
|
||||||
allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(ec2 openstack))
|
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::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::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::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::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'}])
|
||||||
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