diff --git a/devops-service/commands/deploy_env.rb b/devops-service/commands/deploy_env.rb index 43860e8..25f7b8e 100644 --- a/devops-service/commands/deploy_env.rb +++ b/devops-service/commands/deploy_env.rb @@ -5,6 +5,8 @@ module DeployEnvCommands include ImageCommands + # All these commands should be removed when all deploy envs are switched to new validation system + def check_expires! val raise InvalidRecord.new "Parameter 'expires' is invalid" if val.match(/^[0-9]+[smhdw]$/).nil? end diff --git a/devops-service/db/mongo/models/deploy_env_ec2.rb b/devops-service/db/mongo/models/deploy_env_ec2.rb index a1e5f29..5370b70 100644 --- a/devops-service/db/mongo/models/deploy_env_ec2.rb +++ b/devops-service/db/mongo/models/deploy_env_ec2.rb @@ -15,6 +15,14 @@ class DeployEnvEc2 < DeployEnvBase :subnets => {:type => Array, :empty => true}, :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={} super(d) self.flavor = d["flavor"] @@ -29,23 +37,6 @@ class DeployEnvEc2 < DeployEnvBase self.groups = (b.is_a?(Array) ? b.uniq : b) 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 h = super h.merge!({ @@ -60,4 +51,14 @@ class DeployEnvEc2 < DeployEnvBase DeployEnvEc2.new(hash) 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 diff --git a/devops-service/providers/ec2_stub.rb b/devops-service/providers/ec2_stub.rb new file mode 100644 index 0000000..76afba8 --- /dev/null +++ b/devops-service/providers/ec2_stub.rb @@ -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 \ No newline at end of file diff --git a/devops-service/tests/templates/api_v2/10_create/40_deploy_env.feature.erb b/devops-service/tests/templates/api_v2/10_create/40_deploy_env.feature.erb index 1c18bda..a8cafc6 100644 --- a/devops-service/tests/templates/api_v2/10_create/40_deploy_env.feature.erb +++ b/devops-service/tests/templates/api_v2/10_create/40_deploy_env.feature.erb @@ -142,6 +142,133 @@ Feature: Create deploy env """ 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 #--------------------- diff --git a/devops-service/tests/templates/fixtures/deploy_env.yml b/devops-service/tests/templates/fixtures/deploy_env.yml index e93cc2d..3a5f4e5 100644 --- a/devops-service/tests/templates/fixtures/deploy_env.yml +++ b/devops-service/tests/templates/fixtures/deploy_env.yml @@ -40,6 +40,47 @@ openstack: <<: *invalid_base 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: valid: &valid