Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Make testability changes to make it easier to test:
* Move constants to class methods that are easier to stub
* Move chown call to a method that's easy to stub
  • Loading branch information
jrafanie committed Apr 3, 2018
1 parent 570999b commit 3bc082b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
39 changes: 25 additions & 14 deletions app/models/embedded_ansible_worker/object_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def ensure_plugin_playbooks_project_seeded(provider, connection)
copy_plugin_ansible_content

commit_git_plugin_content
FileUtils.chown_R('awx', 'awx', CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR)
chown_playbooks_tempdir

project = find_default_project(connection, provider.default_project)
if project
Expand All @@ -78,20 +78,24 @@ def ensure_plugin_playbooks_project_seeded(provider, connection)
private

def clean_consolidated_plugin_directory
FileUtils.rm_rf(CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR)
FileUtils.rm_rf(self.class.consolidated_plugin_directory)
end

def copy_plugin_ansible_content
FileUtils.mkdir_p(CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR)
FileUtils.mkdir_p(self.class.consolidated_plugin_directory)

#TODO: make this a public api via an attr_reader
Vmdb::Plugins.instance.instance_variable_get(:@registered_ansible_content).each do |content|
FileUtils.cp_r(Dir.glob("#{content.path}/*"), CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR)
FileUtils.cp_r(Dir.glob("#{content.path}/*"), self.class.consolidated_plugin_directory)
end
end

def chown_playbooks_tempdir
FileUtils.chown_R('awx', 'awx', self.class.consolidated_plugin_directory)
end

def commit_git_plugin_content
Dir.chdir(CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR) do
Dir.chdir(self.class.consolidated_plugin_directory) do
require 'rugged'
repo = Rugged::Repository.init_at(".")
index = repo.index
Expand All @@ -108,13 +112,6 @@ def commit_git_plugin_content
end
end

PLAYBOOK_PROJECT_ATTRIBUTES = {
:name => "#{I18n.t('product.name')} Default Project".freeze,
:scm_type => "git".freeze,
:scm_url => "file://#{CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR}".freeze,
:scm_update_on_launch => false
}.freeze

def find_default_project(connection, project_id)
return unless project_id
connection.api.projects.find(project_id)
Expand All @@ -124,10 +121,24 @@ def find_default_project(connection, project_id)
end

def update_playbook_project(project, organization)
project.update_attributes!(PLAYBOOK_PROJECT_ATTRIBUTES.merge(:organization => organization))
project.update_attributes!(self.class.playbook_project_attributes.merge(:organization => organization))
end

def create_playbook_project(connection, organization)
connection.api.projects.create!(PLAYBOOK_PROJECT_ATTRIBUTES.merge(:organization => organization).to_json)
connection.api.projects.create!(self.class.playbook_project_attributes.merge(:organization => organization).to_json)
end

class_methods do
def consolidated_plugin_directory
CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR
end

def playbook_project_attributes
{ :name => "#{I18n.t('product.name')} Default Project".freeze,
:scm_type => "git".freeze,
:scm_url => "file://#{consolidated_plugin_directory}".freeze,
:scm_update_on_launch => false
}.freeze
end
end
end
43 changes: 43 additions & 0 deletions spec/models/embedded_ansible_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,49 @@
end
end

describe "#ensure_plugin_playbooks_project_seeded" do
let!(:tmp_dir) { Pathname.new(Dir.mktmpdir("consolidated_ansible_playbooks")) }
let!(:expected_attributes) do
{
:name => "ManageIQ Default Project",
:scm_type => "git",
:scm_url => "file://#{tmp_dir}",
:scm_update_on_launch => false,
:organization => 42
}
end

before do
provider.default_organization = 42
allow(EmbeddedAnsibleWorker).to receive(:consolidated_plugin_directory).and_return(tmp_dir)
allow(subject).to receive(:chown_playbooks_tempdir)
end

after do
FileUtils.rm_rf(tmp_dir)
end

it "creates a git project as the provider's default project" do
expect(proj_collection).to receive(:create!)
.with(expected_attributes.to_json)
.and_return(double(:id => 1234))

subject.ensure_plugin_playbooks_project_seeded(provider, api_connection)
expect(Dir.exists?(tmp_dir.join(".git"))).to be_truthy
expect(provider.default_project).to eq(1234)
end

it "updates an existing project" do
project = double(:id => 1234)
expect(subject).to receive(:find_default_project).and_return(project)
expect(project).to receive(:update_attributes!)
.with(expected_attributes)
.and_return(double(:id => 1234))

subject.ensure_plugin_playbooks_project_seeded(provider, api_connection)
end
end

describe "#start_monitor_thread" do
let(:pool) { double("ConnectionPool") }

Expand Down

0 comments on commit 3bc082b

Please sign in to comment.