summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/githubrepo_spec.rb98
-rw-r--r--spec/node_spec.rb44
-rw-r--r--spec/nodes_spec.rb35
-rw-r--r--spec/spec_helper.rb14
4 files changed, 179 insertions, 12 deletions
diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb
new file mode 100644
index 0000000..9ad43e9
--- /dev/null
+++ b/spec/githubrepo_spec.rb
@@ -0,0 +1,98 @@
+require 'spec_helper'
+require 'rugged'
+require 'oxidized/hook/githubrepo'
+
+describe Oxidized::Node do
+ let(:credentials) { mock() }
+ let(:remote) { mock() }
+ let(:repo_head) { mock() }
+ let(:repo) { mock() }
+ let(:gr) { GithubRepo.new }
+
+ before(:each) do
+ Oxidized.asetus = Asetus.new
+ Oxidized.config.output.git.repo = 'foo.git'
+ Oxidized.setup_logger
+ end
+
+ describe "#fetch_and_merge_remote" do
+ before(:each) do
+ Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git'
+ Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials)
+ repo_head.expects(:name).returns('refs/heads/master')
+ gr.cfg = Oxidized.config.hooks.github_repo_hook
+ end
+
+ it "should not try to merge when there is no update in remote branch" do
+ repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0))
+ repo.expects(:branches).never
+ repo.expects(:head).returns(repo_head)
+ gr.fetch_and_merge_remote(repo).must_equal nil
+ end
+ describe "when there is update considering conflicts" do
+ let(:merge_index) { mock() }
+ let(:their_branch) { mock() }
+
+ before(:each) do
+ repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns({total_deltas: 1})
+ their_branch.expects(:target_id).returns(1)
+ repo_head.expects(:target_id).returns(2)
+ repo.expects(:merge_commits).with(2, 1).returns(merge_index)
+ repo.expects(:branches).returns({"origin/master" => their_branch})
+ end
+
+ it "should not try merging when there's conflict" do
+ repo.expects(:head).twice.returns(repo_head)
+ their_branch.expects(:name).returns("origin/master")
+ merge_index.expects(:conflicts?).returns(true)
+ Rugged::Commit.expects(:create).never
+ gr.fetch_and_merge_remote(repo).must_equal nil
+ end
+
+ it "should merge when there is no conflict" do
+ repo.expects(:head).times(3).returns(repo_head)
+ their_branch.expects(:target).returns("their_target")
+ their_branch.expects(:name).twice.returns("origin/master")
+ repo_head.expects(:target).returns("our_target")
+ merge_index.expects(:write_tree).with(repo).returns("tree")
+ merge_index.expects(:conflicts?).returns(false)
+ Rugged::Commit.expects(:create).with(repo, {
+ parents: ["our_target", "their_target"],
+ tree: "tree",
+ message: "Merge remote-tracking branch 'origin/master'",
+ update_ref: "HEAD"
+ }).returns(1)
+ gr.fetch_and_merge_remote(repo).must_equal 1
+ end
+ end
+ end
+
+ describe "#run_hook" do
+ before(:each) do
+ remote.expects(:url).returns('https://github.com/username/foo.git')
+ remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true)
+ repo_head.expects(:name).twice.returns('refs/heads/master')
+ repo.expects(:head).twice.returns(repo_head)
+ repo.expects(:path).returns('foo.git')
+ repo.expects(:remotes).returns({'origin' => remote})
+ repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0))
+ Rugged::Repository.expects(:new).with('foo.git').returns(repo)
+ end
+
+ it "will push to the remote repository using https" do
+ Oxidized.config.hooks.github_repo_hook.remote_repo = 'https://github.com/username/foo.git'
+ Oxidized.config.hooks.github_repo_hook.username = 'username'
+ Oxidized.config.hooks.github_repo_hook.password = 'password'
+ Rugged::Credentials::UserPassword.expects(:new).with(username: 'username', password: 'password').returns(credentials)
+ gr.cfg = Oxidized.config.hooks.github_repo_hook
+ gr.run_hook(nil).must_equal true
+ end
+
+ it "will push to the remote repository using ssh" do
+ Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git'
+ Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials)
+ gr.cfg = Oxidized.config.hooks.github_repo_hook
+ gr.run_hook(nil).must_equal true
+ end
+ end
+end
diff --git a/spec/node_spec.rb b/spec/node_spec.rb
new file mode 100644
index 0000000..c568463
--- /dev/null
+++ b/spec/node_spec.rb
@@ -0,0 +1,44 @@
+require 'spec_helper'
+
+describe Oxidized::Node do
+ before(:each) do
+ Oxidized.stubs(:asetus).returns(Asetus.new)
+
+ Oxidized::Node.any_instance.stubs(:resolve_output)
+ @node = Oxidized::Node.new(name: 'example.com',
+ input: 'ssh',
+ output: 'git',
+ model: 'junos',
+ username: 'alma',
+ password: 'armud',
+ prompt: 'test_prompt')
+
+ end
+
+ describe '#new' do
+ it 'should resolve input' do
+ @node.input[0].to_s.split('::')[1].must_equal 'SSH'
+ end
+ it 'should resolve model' do
+ @node.model.class.must_equal JunOS
+ end
+ it 'should resolve username' do
+ @node.auth[:username].must_equal 'alma'
+ end
+ it 'should resolve password' do
+ @node.auth[:password].must_equal 'armud'
+ end
+ it 'should require prompt' do
+ @node.prompt.must_equal 'test_prompt'
+ end
+ end
+
+ describe '#run' do
+ it 'should fetch the configuration' do
+ stub_oxidized_ssh
+
+ status, _ = @node.run
+ status.must_equal :success
+ end
+ end
+end
diff --git a/spec/nodes_spec.rb b/spec/nodes_spec.rb
index 80ed300..5f2ef95 100644
--- a/spec/nodes_spec.rb
+++ b/spec/nodes_spec.rb
@@ -1,33 +1,44 @@
-require 'oxidized'
-Oxidized.mgr = Oxidized::Manager.new
+require 'spec_helper'
describe Oxidized::Nodes do
before(:each) do
+ Resolv.any_instance.stubs(:getaddress)
+ Oxidized.stubs(:asetus).returns(Asetus.new)
+ opts = {
+ input: 'ssh',
+ output: 'git',
+ model: 'junos',
+ username: 'alma',
+ password: 'armud',
+ prompt: 'test_prompt'
+ }
+
+ Oxidized::Node.any_instance.stubs(:resolve_output)
@nodes_org = %w(ltt-pe1.hel kes2-rr1.tku tor-peer1.oul
- hal-p2.tre sav-gr1-sw1.kuo psl-sec-pe1.hel).map { |e| Oxidized::Node.new(:name=>e) }
- @nodes = Oxidized::Nodes.new @nodes_org.dup
+ hal-p2.tre sav-gr1-sw1.kuo psl-sec-pe1.hel).map { |e| Oxidized::Node.new(opts.merge(name: e)) }
+ @node = @nodes_org.delete_at(0)
+ @nodes = Oxidized::Nodes.new(nodes: @nodes_org.dup)
end
describe '#put' do
it 'adds node to top of queue' do
- node = Oxidized::Node.new(:name=>'kst-p1.sto')
- @nodes.put node
- expect(@nodes).to eq [node] + @nodes_org
+ @nodes.put @node
+ @nodes.must_equal [@node] + @nodes_org
end
end
describe '#get' do
it 'returns node from top of queue' do
- expect(@nodes.get).to eq @nodes_org.first
+ @nodes.get.must_equal @nodes_org.first
end
it 'moves node from top to bottom' do
@nodes.get
- expect(@nodes).to end_with [@nodes_org.first]
+ @nodes.last.must_equal @nodes_org.first
end
it 'does not change node count' do
before = @nodes.size
@nodes.get
- expect(before).to eq @nodes.size
+ before.must_equal @nodes.size
end
end
@@ -35,12 +46,12 @@ describe Oxidized::Nodes do
it 'moves node to top of queue' do
node = @nodes[3]
@nodes.next node.name
- expect(@nodes).to start_with [node]
+ @nodes.first.must_equal node
end
it 'does not change node count' do
before = @nodes.size
@nodes.next @nodes[3].name
- expect(before).to eq @nodes.size
+ before.must_equal @nodes.size
end
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..28eb9d4
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,14 @@
+require 'minitest/autorun'
+require 'mocha/mini_test'
+require 'oxidized'
+
+Oxidized.mgr = Oxidized::Manager.new
+
+def stub_oxidized_ssh
+ Oxidized::SSH.any_instance.stubs(:connect).returns(true)
+ Oxidized::SSH.any_instance.stubs(:node).returns(@node)
+ Oxidized::SSH.any_instance.expects(:cmd).at_least(1).returns("this is a command output\nModel: mx960")
+ Oxidized::SSH.any_instance.stubs(:connect_cli).returns(true)
+ Oxidized::SSH.any_instance.stubs(:disconnect).returns(true)
+ Oxidized::SSH.any_instance.stubs(:disconnect_cli).returns(true)
+end