a lot of fixes to make tests pass.

This commit is contained in:
Anton Chuchkalov 2015-07-27 17:27:52 +02:00
parent beca955f60
commit 7ebceaa8ec
30 changed files with 63 additions and 213 deletions

View File

@ -17,7 +17,7 @@ module Devops
Image.get_available_provider_images(Devops::Db.connector, provider)
end
def get_image id
def image id
Devops::Db.connector.image(id)
end
@ -29,7 +29,7 @@ module Devops
Devops::Db.connector.image id
obj = Devops::Model::Image.new(image)
obj.id = id
Devops::Db.connector.image_update image
Devops::Db.connector.image_update obj
end
def delete_image id

View File

@ -7,7 +7,7 @@ module Devops
class Key
def keys
keys = Devops::DB.connector.keys.map {|i| i.to_hash}
keys = Devops::Db.connector.keys.map {|i| i.to_hash}
keys.each {|k| k.delete("path")} # We should not return path to the key
end
@ -18,11 +18,11 @@ module Devops
end
key = Devops::Model::Key.new({"path" => file_name, "id" => body["key_name"]})
Devops::DB.connector.key_insert key
Devops::Db.connector.key_insert key
end
def delete key_id
mongo = Devops::DB.connector
mongo = Devops::Db.connector
servers = mongo.servers_by_key key_id
unless servers.empty?
s_str = servers.map{|s| s.id}.join(", ")
@ -33,7 +33,7 @@ module Devops
begin
FileUtils.rm(k.path)
rescue
logger.error "Missing key file for #{key_id} - #{k.filename}"
DevopsLogger.logger.error "Missing key file for #{key_id} - #{k.filename}"
end
mongo.key_delete key_id
end

View File

@ -3,12 +3,14 @@ require "commands/status"
require "commands/server"
require "db/mongo/models/project"
require "workers/project_test_worker"
require_relative "../helpers/version_2.rb"
require_relative "request_handler"
module Devops
module API2_0
module Handler
class Project < RequestHandler
include Devops::API2_0::Helpers
extend DeployCommands
extend StatusCommands
@ -30,20 +32,30 @@ module Devops
end
def project_servers id
# check if project exists
Devops::Db.connector.project(id)
Devops::Db.connector.servers(id, @params[:deploy_env])
end
def project_stacks id
# check if project exists
Devops::Db.connector.project(id)
options = {project: @params[:project]}
options[:deploy_env] = @params[:deploy_env] if @params[:deploy_env]
Devops::Db.connector.stacks(options)
end
# TODO: multi project
def create_project body
p = Devops::Model::Project.new(body)
halt_response("Project '#{p.id}' already exist") if Devops::Db.connector.is_project_exists?(p)
raise InvalidRecord.new("Project '#{p.id}' already exist") if Devops::Db.connector.is_project_exists?(p)
p.add_authorized_user [@request.env['REMOTE_USER']]
p.create
if p.multi?
"Project '#{p.id}' with type 'multi' created"
else
roles = create_roles p.id, p.deploy_envs, logger
roles = create_roles p.id, p.deploy_envs, DevopsLogger.logger
"Project '#{p.id}' created. " + create_roles_response(roles)
end
end
@ -54,7 +66,7 @@ module Devops
project.id = id
old_project = Devops::Db.connector.project id
Devops::Db.connector.project_update project
roles = create_new_roles(old_project, project, logger)
roles = create_new_roles(old_project, project, DevopsLogger.logger)
create_roles_response(roles)
end
@ -134,7 +146,7 @@ module Devops
next
end
jid = DeployWorker.perform_async(dir, s.to_hash, [], DevopsConfig.config)
#logger.info "Job '#{jid}' has been started"
#DevopsLogger.logger.info "Job '#{jid}' has been started"
uri.path = "#{DevopsConfig[:url_prefix]}/v2.0/report/" + jid
files.push uri.to_s
end

View File

@ -80,13 +80,13 @@ module Devops
end
def create_script file_name
file = File.join(settings.scripts_dir, file_name)
file = File.join(Devops::Api2.settings.scripts_dir, file_name)
raise RecordNotFound.new("File '#{file_name}' already exist") if File.exists?(file)
File.open(file, "w") {|f| f.write(@request.body.read)}
end
def delete_script file_name
file = File.join(settings.scripts_dir, file_nsme)
file = File.join(Devops::Api2.settings.scripts_dir, file_name)
raise RecordNotFound.new("File '#{file_name}' does not exist", 404) unless File.exists?(file)
FileUtils.rm(file)
end

View File

@ -18,20 +18,22 @@ module Devops
end
def change_user_privileges user_id, cmd, privileges
change_user(user_id) do
change_user(user_id) do |user|
user.grant(cmd, privileges)
end
end
def change_email user_id, val
change_user(user_id) do
user.email = val
def change_email options
user_id, email = options.values_at('user_id', 'email')
change_user(user_id) do |user|
user.email = email
end
end
def change_password user_id, val
change_user(user_id) do
user.password = val
def change_password options
user_id, password = options.values_at('user_id', 'password')
change_user(user_id) do |user|
user.password = password
end
end

View File

@ -143,7 +143,7 @@ module Devops
# 200 - Deleted
hash["DELETE"] = lambda {|image_id|
check_privileges("image", "w")
Devops::API2_0::Handler::Image.delete_image
Devops::API2_0::Handler::Image.new(request, params).delete_image(image_id)
create_response("Image '#{image_id}' has been removed")
}

View File

@ -64,22 +64,9 @@ module Devops
# 200 - Deleted
app.delete_with_headers "/key/:key", :headers => [:accept] do
check_privileges("key", "w")
servers = settings.mongo.servers_by_key params[:key]
unless servers.empty?
s_str = servers.map{|s| s.id}.join(", ")
raise DependencyError.new "Deleting is forbidden: Key is used in servers: #{s_str}"
end
k = settings.mongo.key params[:key]
begin
FileUtils.rm(k.path)
rescue
logger.error "Missing key file for #{params[:key]} - #{k.filename}"
end
r = settings.mongo.key_delete params[:key]
r = Devops::API2_0::Handler::Key.new.delete params[:key]
return [500, r["err"].inspect] unless r["err"].nil?
return [500, r["err"].inspect] if r["err"]
create_response("Key '#{params[:key]}' removed")
end

View File

@ -100,7 +100,7 @@ module Devops
check_privileges("project", "w")
body = create_object_from_json_body
r = Devops::API2_0::Handler::Project.new(request, params).update_project project, body
info = "Project '#{project.id}' has been updated." + r
info = "Project '#{project}' has been updated." + r
create_response(info)
}
@ -185,7 +185,10 @@ module Devops
# "stack_status": null,
# "id": "openstack_stack"
# ]
app.get_with_headers "/project/:project/stacks", :headers => [:accept], &Devops::Version2_0::Handler::Project.get_project_stacks
app.get_with_headers "/project/:project/stacks", :headers => [:accept] do |project|
check_privileges("project", "r")
json Devops::API2_0::Handler::Project.new(request, params).project_stacks(project).map(&:to_hash)
end
# Create project and chef roles
#
@ -227,9 +230,13 @@ module Devops
body = create_object_from_json_body
check_string(body["name"], "Parameter 'name' must be a not empty string")
check_array(body["deploy_envs"], "Parameter 'deploy_envs' must be a not empty array of objects", Hash)
res = Devops::API2_0::Handler::Project.new(request, params).create_project body
res = "Created. " + res
create_response(res, nil, 201)
begin
res = Devops::API2_0::Handler::Project.new(request, params).create_project body
res = "Created. " + res
create_response(res, nil, 201)
rescue InvalidRecord => e
halt_response(e.message)
end
end
users_hash = {}

View File

@ -1,6 +1,5 @@
module Devops
module Version2_0
module API2_0
module Routes
module ScriptRoutes

View File

@ -144,7 +144,7 @@ module Devops
body = create_object_from_json_body
p = check_string(body[action], "Parameter '#{action}' must be a not empty string")
h = Devops::API2_0::Handler::User.new
h.send("change_#{action}=", p)
h.send("change_#{action}", body.merge('user_id' => u))
create_response("Updated")
end

View File

@ -38,7 +38,7 @@ module Devops
Devops::Api2.settings.mongo.create_root_user
::Provider::ProviderFactory.init(config)
Stubber.stub_providers! if config[:stub_classes]
Stubber.stub_providers!(config[:stub_providers])
end
def routes

View File

@ -74,7 +74,10 @@ module Devops
unless PRIVILEGES.include?(required_privelege)
raise InvalidPrivileges.new("Access internal problem with privilege '#{required_privelege}'")
end
can?(cmd, required_privelege)
# can?(cmd, required_privelege)
unless can?(cmd, required_privelege)
raise InvalidPrivileges.new("Access denied for '#{id}'")
end
end
def self.create_root

View File

@ -1,8 +1,8 @@
module Validators
class Helpers; end
class DeployEnv; end
class Key; end
class Image; end
module Helpers; end
module DeployEnv; end
module Key; end
module Image; end
end
require "db/validators/base"

View File

@ -5,10 +5,6 @@ Feature: Delete script
When I send DELETE '/v2.0/script/cucumber_test_script' query with user without privileges
Then response should be '401'
Scenario: Delete script without header 'Accept'
When I send DELETE '/v2.0/script/cucumber_test_script' query without header 'Accept'
Then response should be '406'
Scenario: Delete script with id 'cucumber_test_script'
When I send DELETE '/v2.0/script/cucumber_test_script' query
Then response should be '200'

View File

@ -6,11 +6,6 @@ Feature: delete project
When I send DELETE '/v2.0/project/test_openstack' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack project without header 'Accept'
When I send DELETE '/v2.0/project/test_openstack' query without header 'Accept'
Then response should be '406'
@openstack
Scenario: Delete openstack project
When I send DELETE '/v2.0/project/test_openstack' query
@ -29,11 +24,6 @@ Feature: delete project
When I send DELETE '/v2.0/project/test_ec2' query with user without privileges
Then response should be '401'
@ec2
Scenario: Delete ec2 project without header 'Accept'
When I send DELETE '/v2.0/project/test_ec2' query without header 'Accept'
Then response should be '406'
@ec2
Scenario: Delete ec2 project
When I send DELETE '/v2.0/project/test_ec2' query

View File

@ -6,11 +6,6 @@ Feature: delete image
When I send DELETE '/v2.0/image/08093b30-8393-42c3-8fb3-c4df56deb967' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack image without header 'Accept'
When I send DELETE '/v2.0/image/08093b30-8393-42c3-8fb3-c4df56deb967' query without header 'Accept'
Then response should be '406'
@openstack
Scenario: Delete openstack image
When I send DELETE '/v2.0/image/08093b30-8393-42c3-8fb3-c4df56deb967' query
@ -29,11 +24,6 @@ Feature: delete image
When I send DELETE '/v2.0/image/ami-63071b0a' query with user without privileges
Then response should be '401'
@ec2
Scenario: Delete ec2 image without header 'Accept'
When I send DELETE '/v2.0/image/ami-63071b0a' query without header 'Accept'
Then response should be '406'
@ec2
Scenario: Delete ec2 image
When I send DELETE '/v2.0/image/ami-63071b0a' query

View File

@ -46,7 +46,11 @@ end
When(/^I send POST '(.*)' query with JSON body with user without privileges$/) do |path, body|
JSON.parse(body) unless body.strip.empty?
res = post_without_privileges(path, body, DEFAULT_HEADERS)
post_without_privileges(path, body, DEFAULT_HEADERS)
end
When(/^I send POST '(.*)' query with user without privileges$/) do |path|
post_without_privileges(path, {}, DEFAULT_HEADERS)
end
When(/^I send DELETE '(.*)' query$/) do |path|
@ -102,12 +106,6 @@ When(/^I send PUT '(.*)' query with JSON body with header 'Accept' value '(.*)'$
res = put_body(path, body, headers)
end
When(/^I send PUT '(.*)' query with body without header 'Content-Type'$/) do |path, body|
headers = DEFAULT_HEADERS.clone
headers.delete("Content-Type")
res = put_body(path, body, headers)
end
When(/^I send PUT '(.*)' query with JSON body without header 'Content-Type'$/) do |path, body|
JSON.parse(body) unless body.strip.empty?
headers = DEFAULT_HEADERS.clone

View File

@ -30,10 +30,6 @@ Feature: list user
]
"""
Scenario: Get list of all users without header 'Accept'
When I send GET '/v2.0/users' query without headers 'Accept'
Then response should be '406'
Scenario: Get list of all users without privileges
When I send GET '/v2.0/users' query with user without privileges
Then response should be '401'

View File

@ -46,19 +46,10 @@ Feature: Flavors
And the JSON response should be an array
And response array should be empty
@static
Scenario: Get flavors list of static provider without 'Accept' header
When I send GET '/v2.0/flavors/static' query without headers 'Accept'
Then response should be '406'
Scenario: Get flavors list of unknown provider
When I send GET '/v2.0/flavors/foo' query
Then response should be '404'
Scenario: Get flavors list of unknown provider without 'Accept' header
When I send GET '/v2.0/flavors/foo' query without headers 'Accept'
Then response should be '406'
Scenario: Get flavors list of unknown provider without privileges
When I send GET '/v2.0/flavors/foo' query with user without privileges
Then response should be '401'

View File

@ -12,17 +12,6 @@ Feature: create user
"""
Then response should be '401'
Scenario: create user without header 'Accept'
When I send POST '/v2.0/user' query with JSON body without header 'Accept'
"""
{
"username": "<%= @config["user"]["name"] %>",
"email": "<%= @config["user"]["name"] %>@test.test",
"password": "<%= @config["user"]["name"] %>"
}
"""
Then response should be '406'
Scenario: create user without header 'Content-Type'
When I send POST '/v2.0/user' query with JSON body without header 'Content-Type'
"""

View File

@ -19,10 +19,6 @@ Feature: Manage images
]
"""
Scenario: Get list of all images without header 'Accept'
When I send GET '/v2.0/images' query without headers 'Accept'
Then response should be '406'
Scenario: Get list of all images without privileges
When I send GET '/v2.0/images' query with user without privileges
Then response should be '401'
@ -57,11 +53,6 @@ Feature: Manage images
And the Content-Type header should include 'application/json'
And the JSON response should be an array
@openstack
Scenario: Get list of openstack images (provider) without header 'Accept'
When I send GET '/v2.0/images/provider/openstack' query without headers 'Accept'
Then response should be '406'
@openstack
Scenario: Get images list of openstack without privileges
When I send GET '/v2.0/images/provider/openstack' query with user without privileges
@ -93,11 +84,6 @@ Feature: Manage images
And the Content-Type header should include 'application/json'
And the JSON response should be an array
@ec2
Scenario: Get list of ec2 images (provider) without header 'Accept'
When I send GET '/v2.0/images/provider/ec2' query without headers 'Accept'
Then response should be '406'
@ec2
Scenario: Get images list of ec2 without privileges
When I send GET '/v2.0/images/provider/ec2' query with user without privileges
@ -530,11 +516,6 @@ Feature: Manage images
}
"""
@ec2
Scenario: Get info for single ec2 image without headers 'Accept'
When I send GET '/v2.0/image/<%= @config["ec2"]["image"] %>' query without headers 'Accept'
Then response should be '406'
@ec2
Scenario: Get ec2 image without privileges
When I send GET '/v2.0/image/<%= @config["ec2"]["image"] %>' query with user without privileges
@ -557,11 +538,6 @@ Feature: Manage images
}
"""
@openstack
Scenario: Get info for single openstack image without headers 'Accept'
When I send GET '/v2.0/image/<%= @config["openstack"]["image"] %>' query without headers 'Accept'
Then response should be '406'
@openstack
Scenario: Get openstack image without privileges
When I send GET '/v2.0/image/<%= @config["openstack"]["image"] %>' query with user without privileges

View File

@ -5,13 +5,6 @@ Feature: Add new script
When I send PUT '/v2.0/script/<%= @config["script"]["name"] %>' query with user without privileges
Then response should be '401'
Scenario: Add new script without header 'Accept'
When I send PUT '/v2.0/script/<%= @config["script"]["name"] %>' query with body without header 'Accept'
"""
echo "<%= @config["script"]["name"] %>"
"""
Then response should be '406'
Scenario: Add new script with id '<%= @config["script"]["name"] %>'
When I send PUT '/v2.0/script/<%= @config["script"]["name"] %>' query with body
"""

View File

@ -21,15 +21,6 @@ Feature: change user privileges and password
"""
Then response should be '401'
Scenario: change user <%= k %> without header 'Accept'
When I send PUT '/v2.0/user/<%= @config["user"]["name"] %>/<%= k %>' query with JSON body without header 'Accept'
"""
{
"<%= k %>": "<%= val %>"
}
"""
Then response should be '406'
Scenario: change user <%= k %> without header 'Content-Type'
When I send PUT '/v2.0/user/<%= @config["user"]["name"] %>/<%= k %>' query with JSON body without header 'Content-Type'
"""
@ -91,13 +82,6 @@ Feature: change user privileges and password
"""
Then response should be '401'
Scenario: change user privileges without header 'Accept'
When I send PUT '/v2.0/user/<%= @config["user"]["name"] %>' query with JSON body without header 'Accept'
"""
{}
"""
Then response should be '406'
Scenario: change user privileges without header 'Content-Type'
When I send PUT '/v2.0/user/<%= @config["user"]["name"] %>' query with JSON body without header 'Content-Type'
"""

View File

@ -6,11 +6,6 @@ Feature: Delete stack template
When I send DELETE '/v2.0/stack_template/<%= openstack_template_id %>' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack stack_template without header 'Accept'
When I send DELETE '/v2.0/stack_template/<%= openstack_template_id %>' query without header 'Accept'
Then response should be '406'
@openstack
Scenario: Delete openstack stack_template with id '<%= openstack_template_id %>'
When I send DELETE '/v2.0/stack_template/<%= openstack_template_id %>' query

View File

@ -5,10 +5,6 @@ Feature: Delete script
When I send DELETE '/v2.0/script/<%= @config["script"]["name"] %>' query with user without privileges
Then response should be '401'
Scenario: Delete script without header 'Accept'
When I send DELETE '/v2.0/script/<%= @config["script"]["name"] %>' query without header 'Accept'
Then response should be '406'
Scenario: Delete script with id '<%= @config["script"]["name"] %>'
When I send DELETE '/v2.0/script/<%= @config["script"]["name"] %>' query
Then response should be '200'

View File

@ -6,11 +6,6 @@ Feature: Delete stack
When I send DELETE '/v2.0/stack/<%= openstack_stack_id %>' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack stack without header 'Accept'
When I send DELETE '/v2.0/stack/<%= openstack_stack_id %>' query without header 'Accept'
Then response should be '406'
@openstack
Scenario: Delete stack with id '<%= openstack_stack_id %>'
When I send DELETE '/v2.0/stack/<%= openstack_stack_id %>' query

View File

@ -6,11 +6,6 @@ Feature: delete project
When I send DELETE '/v2.0/project/<%= @config["openstack"]["project"]["name"] %>' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack project without header 'Accept'
When I send DELETE '/v2.0/project/<%= @config["openstack"]["project"]["name"] %>' query without header 'Accept'
Then response should be '406'
@openstack
Scenario: Delete openstack project
When I send DELETE '/v2.0/project/<%= @config["openstack"]["project"]["name"] %>' query
@ -29,11 +24,6 @@ Feature: delete project
When I send DELETE '/v2.0/project/<%= @config["ec2"]["project"]["name"] %>' query with user without privileges
Then response should be '401'
@ec2
Scenario: Delete ec2 project without header 'Accept'
When I send DELETE '/v2.0/project/<%= @config["ec2"]["project"]["name"] %>' query without header 'Accept'
Then response should be '406'
@ec2
Scenario: Delete ec2 project
When I send DELETE '/v2.0/project/<%= @config["ec2"]["project"]["name"] %>' query
@ -53,11 +43,6 @@ Feature: delete project
When I send DELETE '/v2.0/project/<%= @config["static"]["project"]["name"] %>' query with user without privileges
Then response should be '401'
@static
Scenario: Delete static project without header 'Accept'
When I send DELETE '/v2.0/project/<%= @config["static"]["project"]["name"] %>' query without header 'Accept'
Then response should be '406'
@static
Scenario: Delete static project
When I send DELETE '/v2.0/project/<%= @config["static"]["project"]["name"] %>' query

View File

@ -6,11 +6,6 @@ Feature: delete image
When I send DELETE '/v2.0/image/<%= @config["openstack"]["image"] %>' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack image without header 'Accept'
When I send DELETE '/v2.0/image/<%= @config["openstack"]["image"] %>' query without header 'Accept'
Then response should be '406'
@openstack
Scenario: Delete openstack image
When I send DELETE '/v2.0/image/<%= @config["openstack"]["image"] %>' query
@ -29,11 +24,6 @@ Feature: delete image
When I send DELETE '/v2.0/image/<%= @config["ec2"]["image"] %>' query with user without privileges
Then response should be '401'
@ec2
Scenario: Delete ec2 image without header 'Accept'
When I send DELETE '/v2.0/image/<%= @config["ec2"]["image"] %>' query without header 'Accept'
Then response should be '406'
@ec2
Scenario: Delete ec2 image
When I send DELETE '/v2.0/image/<%= @config["ec2"]["image"] %>' query

View File

@ -5,10 +5,6 @@ Feature: delete user
When I send DELETE '/v2.0/user/<%= @config["user"]["name"] %>' query with user without privileges
Then response should be '401'
Scenario: delete user without header 'Accept'
When I send DELETE '/v2.0/user/<%= @config["user"]["name"] %>' query without header 'Accept'
Then response should be '406'
Scenario: delete unknown user
When I send DELETE '/v2.0/user/unknown' query
Then response should be '404'

View File

@ -6,16 +6,6 @@ Feature: Filters
When I send DELETE '/v2.0/filter/openstack/image' query with user without privileges
Then response should be '401'
@openstack
Scenario: Delete openstack image filter with header 'Accept' value 'text/*'
When I send DELETE '/v2.0/filter/openstack/image' query with JSON body with header 'Accept' value 'text/*'
"""
[
"<%= @config["openstack"]["image"] %>"
]
"""
Then response should be '406'
@openstack
Scenario: Delete openstack image filter without header 'Content-Type'
When I send DELETE '/v2.0/filter/openstack/image' query with JSON body without header 'Content-Type'
@ -91,16 +81,6 @@ Feature: Filters
When I send DELETE '/v2.0/filter/ec2/image' query with user without privileges
Then response should be '401'
@ec2
Scenario: Delete ec2 image filter with header 'Accept' value 'text/*'
When I send DELETE '/v2.0/filter/ec2/image' query with JSON body with header 'Accept' value 'text/*'
"""
[
"<%= @config["ec2"]["image"] %>"
]
"""
Then response should be '406'
@ec2
Scenario: Delete ec2 image filter without header 'Content-Type'
When I send DELETE '/v2.0/filter/ec2/image' query with JSON body without header 'Content-Type'