summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorWild Kat <wk@users.noreply.github.com>2018-05-26 23:49:29 +0200
committerGitHub <noreply@github.com>2018-05-26 23:49:29 +0200
commit39dbb74c527c054007e26ad34c8921d4be6cb638 (patch)
treed55745153153450fb40e1f591eb861495a6068d9 /spec
parentdefdc69f92b3bcf62b1fd3b5ceac63a7a4e637ae (diff)
parent42bf10b7bce34ff577c469f6c2f659f3913cbf4a (diff)
Merge branch 'master' into FortiOS-Push
Diffstat (limited to 'spec')
-rw-r--r--spec/cli_spec.rb12
-rw-r--r--spec/hook/githubrepo_spec.rb (renamed from spec/githubrepo_spec.rb)40
-rw-r--r--spec/input/ssh_spec.rb69
-rw-r--r--spec/node_spec.rb29
-rw-r--r--spec/nodes_spec.rb4
-rw-r--r--spec/spec_helper.rb19
6 files changed, 127 insertions, 46 deletions
diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb
index eb9872e..8b9b792 100644
--- a/spec/cli_spec.rb
+++ b/spec/cli_spec.rb
@@ -2,8 +2,14 @@ require 'spec_helper'
require 'oxidized/cli'
describe Oxidized::CLI do
- before { @original = ARGV }
- after { ARGV.replace @original }
+ before(:each) do
+ @original = ARGV
+ Oxidized.asetus = Asetus.new
+ end
+
+ after(:each) do
+ ARGV.replace @original
+ end
%w[-v --version].each do |option|
describe option do
@@ -15,7 +21,7 @@ describe Oxidized::CLI do
File.expects(:expand_path)
Kernel.expects(:exit)
- assert_output("#{Oxidized::VERSION}\n") { Oxidized::CLI.new }
+ assert_output("#{Oxidized::VERSION_FULL}\n") { Oxidized::CLI.new }
end
end
end
diff --git a/spec/githubrepo_spec.rb b/spec/hook/githubrepo_spec.rb
index 2f84c78..67c99f0 100644
--- a/spec/githubrepo_spec.rb
+++ b/spec/hook/githubrepo_spec.rb
@@ -3,11 +3,11 @@ require 'rugged'
require 'oxidized/hook/githubrepo'
describe GithubRepo do
- let(:credentials) { mock() }
- let(:remote) { mock() }
- let(:remotes) { mock() }
- let(:repo_head) { mock() }
- let(:repo) { mock() }
+ let(:credentials) { mock }
+ let(:remote) { mock }
+ let(:remotes) { mock }
+ let(:repo_head) { mock }
+ let(:repo) { mock }
let(:gr) { GithubRepo.new }
before do
@@ -32,7 +32,7 @@ describe GithubRepo do
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)
+ Proc.expects(:new).returns(credentials)
repo_head.expects(:name).returns('refs/heads/master')
gr.cfg = Oxidized.config.hooks.github_repo_hook
end
@@ -44,15 +44,15 @@ describe GithubRepo do
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() }
+ 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})
+ 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})
+ repo.expects(:branches).returns("origin/master" => their_branch)
end
it "should not try merging when there's conflict" do
@@ -70,12 +70,11 @@ describe GithubRepo do
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)
+ 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
@@ -89,6 +88,7 @@ describe GithubRepo do
end
before do
+ Proc.expects(:new).returns(credentials)
repo_head.expects(:name).twice.returns('refs/heads/master')
repo.expects(:head).twice.returns(repo_head)
repo.expects(:path).returns('/foo.git')
@@ -100,7 +100,7 @@ describe GithubRepo do
Oxidized.config.output.git.repo = '/foo.git'
remote.expects(:url).returns('https://github.com/username/foo.git')
remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true)
- repo.expects(:remotes).returns({'origin' => remote})
+ repo.expects(:remotes).returns('origin' => remote)
Rugged::Repository.expects(:new).with('/foo.git').returns(repo)
end
@@ -108,14 +108,14 @@ describe GithubRepo 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)
+ Proc.expects(:new).returns(credentials)
gr.cfg = Oxidized.config.hooks.github_repo_hook
gr.run_hook(ctx).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)
+ Proc.expects(:new).returns(credentials)
gr.cfg = Oxidized.config.hooks.github_repo_hook
gr.run_hook(ctx).must_equal true
end
@@ -125,7 +125,7 @@ describe GithubRepo do
let(:group) { 'ggrroouupp' }
before do
- Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials)
+ Proc.expects(:new).returns(credentials)
Rugged::Repository.expects(:new).with(repository).returns(repo)
repo.expects(:remotes).twice.returns(remotes)
diff --git a/spec/input/ssh_spec.rb b/spec/input/ssh_spec.rb
index d86ffa0..0a3bd8d 100644
--- a/spec/input/ssh_spec.rb
+++ b/spec/input/ssh_spec.rb
@@ -8,29 +8,66 @@ describe Oxidized::SSH do
Oxidized::Node.any_instance.stubs(:resolve_repo)
Oxidized::Node.any_instance.stubs(:resolve_input)
Oxidized::Node.any_instance.stubs(:resolve_output)
- @node = Oxidized::Node.new(name: 'example.com',
- input: 'ssh',
- output: 'git',
- model: 'junos',
- username: 'alma',
- password: 'armud',
- vars: {ssh_proxy: 'test.com'})
-
end
describe "#connect" do
- it "should use proxy command when proxy host given" do
+ it "should use proxy command when proxy host given and connect by ip if resolve_dns is true" do
+ Oxidized.config.resolve_dns = true
+ @node = Oxidized::Node.new(name: 'example.com',
+ input: 'ssh',
+ output: 'git',
+ model: 'junos',
+ username: 'alma',
+ password: 'armud',
+ vars: { ssh_proxy: 'test.com' })
+
+ ssh = Oxidized::SSH.new
+
+ model = mock
+ model.expects(:cfg).returns('ssh' => [])
+ @node.expects(:model).returns(model).at_least_once
+
+ proxy = mock
+ Net::SSH::Proxy::Command.expects(:new).with("ssh test.com -W %h:%p").returns(proxy)
+ Net::SSH.expects(:start).with('93.184.216.34', 'alma', port: 22,
+ timeout: Oxidized.config.timeout,
+ paranoid: Oxidized.config.input.ssh.secure,
+ keepalive: true,
+ proxy: proxy,
+ password: 'armud',
+ number_of_password_prompts: 0,
+ auth_methods: %w[none publickey password])
+
+ ssh.instance_variable_set("@exec", true)
+ ssh.connect(@node)
+ end
+
+ it "should use proxy command when proxy host given and connect by name if resolve_dns is false" do
+ Oxidized.config.resolve_dns = false
+ @node = Oxidized::Node.new(name: 'example.com',
+ input: 'ssh',
+ output: 'git',
+ model: 'junos',
+ username: 'alma',
+ password: 'armud',
+ vars: { ssh_proxy: 'test.com' })
+
ssh = Oxidized::SSH.new
- model = mock()
- model.expects(:cfg).returns({'ssh' => []})
- @node.expects(:model).returns(model)
+ model = mock
+ model.expects(:cfg).returns('ssh' => [])
+ @node.expects(:model).returns(model).at_least_once
- proxy = mock()
+ proxy = mock
Net::SSH::Proxy::Command.expects(:new).with("ssh test.com -W %h:%p").returns(proxy)
- Net::SSH.expects(:start).with('93.184.216.34', 'alma', {:port => 22, :password => 'armud', :timeout => Oxidized.config.timeout,
- :paranoid => Oxidized.config.input.ssh.secure, :auth_methods => ['none', 'publickey', 'password', 'keyboard-interactive'],
- :number_of_password_prompts => 0, :proxy => proxy})
+ Net::SSH.expects(:start).with('example.com', 'alma', port: 22,
+ timeout: Oxidized.config.timeout,
+ paranoid: Oxidized.config.input.ssh.secure,
+ keepalive: true,
+ proxy: proxy,
+ password: 'armud',
+ number_of_password_prompts: 0,
+ auth_methods: %w[none publickey password])
ssh.instance_variable_set("@exec", true)
ssh.connect(@node)
diff --git a/spec/node_spec.rb b/spec/node_spec.rb
index 829e05a..ba5b515 100644
--- a/spec/node_spec.rb
+++ b/spec/node_spec.rb
@@ -14,7 +14,6 @@ describe Oxidized::Node do
username: 'alma',
password: 'armud',
prompt: 'test_prompt')
-
end
describe '#new' do
@@ -39,9 +38,31 @@ describe Oxidized::Node do
it 'should fetch the configuration' do
stub_oxidized_ssh
- status, _ = @node.run
+ status, = @node.run
status.must_equal :success
end
+ it 'should record the success' do
+ stub_oxidized_ssh
+
+ before_successes = @node.stats.get(:success_count)
+ j = Oxidized::Job.new @node
+ j.join
+ @node.stats.add j
+ after_successes = @node.stats.get(:success_count)
+ successes = after_successes - before_successes
+ successes.must_equal 1
+ end
+ it 'should record a failure' do
+ stub_oxidized_ssh_fail
+
+ before_fails = @node.stats.get(:failure_count)
+ j = Oxidized::Job.new @node
+ j.join
+ @node.stats.add j
+ after_fails = @node.stats.get(:failure_count)
+ fails = after_fails - before_fails
+ fails.must_equal 1
+ end
end
describe '#repo' do
@@ -52,9 +73,9 @@ describe Oxidized::Node do
let(:group) { nil }
let(:node) do
- Oxidized::Node.new({
+ Oxidized::Node.new(
ip: '127.0.0.1', group: group, model: 'junos'
- })
+ )
end
it 'when there are no groups' do
diff --git a/spec/nodes_spec.rb b/spec/nodes_spec.rb
index 6fa4b41..a801107 100644
--- a/spec/nodes_spec.rb
+++ b/spec/nodes_spec.rb
@@ -17,8 +17,8 @@ describe Oxidized::Nodes do
Oxidized::Node.any_instance.stubs(:resolve_repo)
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(opts.merge(name: e)) }
+ @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(opts.merge(name: e)) }
@node = @nodes_org.delete_at(0)
@nodes = Oxidized::Nodes.new(nodes: @nodes_org.dup)
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 28eb9d4..00f46e5 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,5 +1,13 @@
+require 'simplecov'
+SimpleCov.start
+
+if ENV['CI'] == 'true'
+ require 'codecov'
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
+end
+
require 'minitest/autorun'
-require 'mocha/mini_test'
+require 'mocha/minitest'
require 'oxidized'
Oxidized.mgr = Oxidized::Manager.new
@@ -12,3 +20,12 @@ def stub_oxidized_ssh
Oxidized::SSH.any_instance.stubs(:disconnect).returns(true)
Oxidized::SSH.any_instance.stubs(:disconnect_cli).returns(true)
end
+
+def stub_oxidized_ssh_fail
+ Oxidized::SSH.any_instance.stubs(:connect).returns(false)
+ Oxidized::SSH.any_instance.stubs(:node).returns(@node)
+ Oxidized::SSH.any_instance.expects(:cmd).never
+ Oxidized::SSH.any_instance.stubs(:connect_cli).returns(false)
+ Oxidized::SSH.any_instance.stubs(:disconnect).returns(false)
+ Oxidized::SSH.any_instance.stubs(:disconnect_cli).returns(false)
+end