switch ec2 to new validation system

This commit is contained in:
Anton Chuchkalov 2014-11-28 17:12:51 +04:00
parent f6aad954e2
commit 8fd2129a4d
5 changed files with 255 additions and 17 deletions

View File

@ -5,6 +5,8 @@ module DeployEnvCommands
include ImageCommands include ImageCommands
# All these commands should be removed when all deploy envs are switched to new validation system
def check_expires! val def check_expires! val
raise InvalidRecord.new "Parameter 'expires' is invalid" if val.match(/^[0-9]+[smhdw]$/).nil? raise InvalidRecord.new "Parameter 'expires' is invalid" if val.match(/^[0-9]+[smhdw]$/).nil?
end end

View File

@ -15,6 +15,14 @@ class DeployEnvEc2 < DeployEnvBase
:subnets => {:type => Array, :empty => true}, :subnets => {:type => Array, :empty => true},
:groups => {:type => Array, :empty => false} :groups => {:type => Array, :empty => false}
set_validators ::Validators::DeployEnv::RunList,
::Validators::DeployEnv::Expiration,
::Validators::DeployEnv::Users,
::Validators::DeployEnv::Flavor,
::Validators::DeployEnv::Image,
::Validators::DeployEnv::SubnetBelongsToProvider,
::Validators::DeployEnv::Groups
def initialize d={} def initialize d={}
super(d) super(d)
self.flavor = d["flavor"] self.flavor = d["flavor"]
@ -29,23 +37,6 @@ class DeployEnvEc2 < DeployEnvBase
self.groups = (b.is_a?(Array) ? b.uniq : b) self.groups = (b.is_a?(Array) ? b.uniq : b)
end end
def validate!
super
p = ::Provider::ProviderFactory.get(self.provider)
check_flavor!(p, self.flavor)
check_image!(p, self.image)
check_subnets_and_groups!(p, self.subnets, self.groups) do |networks|
unless self.subnets.empty?
{"vpc-id" => networks.detect{|n| n["name"] == self.subnets[0]}["vpcId"] }
end
end
true
rescue InvalidRecord => e
raise InvalidRecord.new "Deploy environment '#{self.identifier}'. " + e.message
end
def to_hash def to_hash
h = super h = super
h.merge!({ h.merge!({
@ -60,4 +51,14 @@ class DeployEnvEc2 < DeployEnvBase
DeployEnvEc2.new(hash) DeployEnvEc2.new(hash)
end end
private
def subnets_filter
networks = provider_instance.networks
unless self.subnets.empty?
{"vpc-id" => networks.detect{|n| n["name"] == self.subnets[0]}["vpcId"] }
end
end
end end

View File

@ -0,0 +1,67 @@
# Stub some methods in Ec2 Provider
puts '!!! WARNING !!!'
puts '!!! Some EC2 methods are stubbed !!!'
class Provider::Ec2
def groups filter=nil
{
'test' => {
'description' => 'Description',
'rules' => [{
"protocol" => "ip_protocol",
"from" => "from_port",
"to" => "to_port",
"cidr" => "cidr"
}]
},
'default' => {
'description' => 'Description',
'rules' => [{
"protocol" => "ip_protocol",
"from" => "from_port",
"to" => "to_port",
"cidr" => "cidr"
}]
}
}
end
def flavors
[{
"id" => 'test_flavor',
"v_cpus" => 2,
"ram" => 256,
"disk" => 1000
}]
end
def images filters
[
{
"id" => 'test_image_ec2',
"name" => 'test image',
"status" => 'test status'
}
]
end
def networks
networks_detail
end
def networks_detail
[
{
'cidr' => '192.0.2.32/27',
'vpcId' => 'test_network_id',
'subnetId' => 'test_network_id',
'name' => 'test_network_id',
'zone' => 'test_zone'
}
]
end
end

View File

@ -142,6 +142,133 @@ Feature: Create deploy env
""" """
Then response should be '200' Then response should be '200'
#---------------------
# EC2
#---------------------
<% ec2_project_name = @config["ec2"]["project"]["name"] %>
<% precreated_ec2_deploy_env = @formatter.json('deploy_env/ec2/valid', spaces: 8) %>
@ec2
Scenario: Send deploy env with invalid runlist for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= @formatter.json('deploy_env/ec2/valid', spaces: 8) %>,
<%= @formatter.json('deploy_env/ec2/invalid/run_list', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "Invalid run list elements: 'wrong_role'. Each element should be role or recipe."
@ec2
Scenario: Send deploy env with invalid expiration for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/invalid/expires', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "Parameter 'expires' is invalid. Valid format: [0-9]+[smhdw] or null."
@ec2
Scenario: Send deploy env with invalid user (missing in mongo) for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/invalid/users', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "These users are missing in mongo: 'non_existing_user'."
@ec2
Scenario: Send deploy env with invalid flavor for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/invalid/flavor', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "Invalid flavor 'wrong_flavor'."
@ec2
Scenario: Send deploy env with invalid image for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/invalid/image', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "Invalid image 'wrong_image'."
@ec2
Scenario: Send deploy env with invalid subnets for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/invalid/subnets_invalid', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "Invalid subnets 'wrong_subnet'."
@ec2
Scenario: Send deploy env with invalid groups for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/invalid/groups', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '400'
And response error should be "Invalid groups 'wrong_group'."
@ec2
Scenario: Create deploy env for project <%= ec2_project_name %>
When I send PUT '/v2.0/project/<%= ec2_project_name %>' query with JSON body
"""
{
"deploy_envs": [
<%= precreated_ec2_deploy_env %>,
<%= @formatter.json('deploy_env/ec2/valid_double', spaces: 8) %>
],
"name": "<%= ec2_project_name %>"
}
"""
Then response should be '200'
#--------------------- #---------------------
# STATIC # STATIC
#--------------------- #---------------------

View File

@ -40,6 +40,47 @@ openstack:
<<: *invalid_base <<: *invalid_base
groups: ['wrong_group'] groups: ['wrong_group']
ec2:
valid: &valid
identifier: test
run_list: []
expires:
provider: ec2
users: ['test']
flavor: test_flavor
image: test_image_ec2
subnets: ['test_network_id']
valid_double:
<<: *valid
identifier: test2
invalid:
base: &invalid_base
<<: *valid
identifier: test2
run_list:
<<: *invalid_base
run_list: ['wrong_role']
expires:
<<: *invalid_base
expires: 10_wrong
users:
<<: *invalid_base
users: [ 'non_existing_user' ]
flavor:
<<: *invalid_base
flavor: wrong_flavor
image:
<<: *invalid_base
image: wrong_image
subnets_empty:
<<: *invalid_base
subnets:
subnets_invalid:
<<: *invalid_base
subnets: ['wrong_subnet']
groups:
<<: *invalid_base
groups: ['wrong_group']
static: static:
valid: &valid valid: &valid