From e17356f22505f0aed88fd04c9925b8aeb2520e28 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Tue, 16 Feb 2016 14:18:07 -0200 Subject: `Node#repo` should return the node group repository --- spec/node_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'spec') diff --git a/spec/node_spec.rb b/spec/node_spec.rb index c568463..f9c912e 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe Oxidized::Node do before(:each) do Oxidized.stubs(:asetus).returns(Asetus.new) + Oxidized.config.output.git.repo = '/tmp/repository.git' Oxidized::Node.any_instance.stubs(:resolve_output) @node = Oxidized::Node.new(name: 'example.com', @@ -41,4 +42,28 @@ describe Oxidized::Node do status.must_equal :success end end + + describe '#repo' do + it 'when there is no groups' do + @node.repo.must_equal '/tmp/repository.git' + end + + describe 'when there are groups' do + let(:node) do + Oxidized::Node.new({ + ip: '127.0.0.1', group: 'ggrroouupp', model: 'junos' + }) + end + + it 'with only one repository' do + Oxidized.config.output.git.single_repo = true + node.repo.must_equal '/tmp/repository.git' + end + + it 'with more than one repository' do + Oxidized.config.output.git.single_repo = false + node.repo.must_equal '/tmp/ggrroouupp.git' + end + end + end end -- cgit v1.2.1 From a5b400f83e4cec0704ecda219bf4182e3de059b1 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Tue, 16 Feb 2016 14:52:11 -0200 Subject: supress `Rugged` log output --- spec/githubrepo_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 9ad43e9..71681ed 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -12,6 +12,7 @@ describe Oxidized::Node do before(:each) do Oxidized.asetus = Asetus.new Oxidized.config.output.git.repo = 'foo.git' + Oxidized.config.log = '/dev/null' Oxidized.setup_logger end -- cgit v1.2.1 From a626c212f9e64434b828dadcb0cb3bd879beb612 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Tue, 16 Feb 2016 18:56:30 -0200 Subject: fix `githubrepo` hook for groups repositories remotes when there was a repository per group we could not use the hook for pushing since we only had **one** remote configured. This PR configures each repository its own remote based in the `groups` config, as follows: ``` --- groups: first: remote_repo: git@gitlab.lab:oxidized/first.git second: remote_repo: git@gitlab.lab:oxidized/second.git ``` --- spec/githubrepo_spec.rb | 86 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 18 deletions(-) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 71681ed..36207d3 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -2,9 +2,10 @@ require 'spec_helper' require 'rugged' require 'oxidized/hook/githubrepo' -describe Oxidized::Node do +describe Oxidized::GithubRepo do let(:credentials) { mock() } let(:remote) { mock() } + let(:remotes) { mock() } let(:repo_head) { mock() } let(:repo) { mock() } let(:gr) { GithubRepo.new } @@ -69,31 +70,80 @@ describe Oxidized::Node do 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) + let(:group) { nil } + let(:ctx) { OpenStruct.new(node: node) } + let(:node) do + Oxidized::Node.new(ip: '127.0.0.1', group: group, model: 'junos', output: 'output') + end + + before do 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 + describe 'when there is only one repository and no groups' do + before do + 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}) + 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(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) + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end 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 + describe "when there are groups" do + let(:group) { 'ggrroouupp' } + + before do + Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials) + Rugged::Repository.expects(:new).with(repository).returns(repo) + Oxidized.config.groups.ggrroouupp.remote_repo = 'ggrroouupp#remote_repo' + Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' + Oxidized.config.output.git.single_repo = single_repo + + repo.expects(:remotes).twice.returns(remotes) + remotes.expects(:[]).with('origin').returns(nil) + remotes.expects(:create).with('origin', 'ggrroouupp#remote_repo').returns(remote) + remote.expects(:url).returns('url') + remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) + end + + describe 'when there are several repositories' do + let(:repository) { './ggrroouupp.git' } + let(:single_repo) { nil } + + it 'will push to the node group repository' do + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end + + describe 'when is a single repository' do + let(:repository) { 'foo.git' } + let(:single_repo) { true } + + it 'will push to the correct repository' do + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end end end end -- cgit v1.2.1 From 695883a75783e19de70e60314fab49661c387a22 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Thu, 18 Feb 2016 17:01:25 -0200 Subject: create tests for `GithubRepo#validate_cfg!` method --- spec/githubrepo_spec.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 36207d3..4674035 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' require 'rugged' require 'oxidized/hook/githubrepo' -describe Oxidized::GithubRepo do +describe GithubRepo do let(:credentials) { mock() } let(:remote) { mock() } let(:remotes) { mock() } @@ -17,6 +17,18 @@ describe Oxidized::GithubRepo do Oxidized.setup_logger end + describe '#validate_cfg!' do + before do + gr.expects(:respond_to?).with(:validate_cfg!).returns(false) # `cfg=` call + end + + it 'raise a error when `remote_repo` is not configured' do + Oxidized.config.hooks.github_repo_hook = { type: 'githubrepo' } + gr.cfg = Oxidized.config.hooks.github_repo_hook + proc { gr.validate_cfg! }.must_raise(KeyError) + end + end + describe "#fetch_and_merge_remote" do before(:each) do Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git' -- cgit v1.2.1 From 66a0c3261611f737e5f36527da4ba0d13d70e092 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Fri, 19 Feb 2016 12:08:25 -0200 Subject: fix 'GithubRepo#single_repo?' private method --- spec/githubrepo_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 4674035..a5bbeca 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -132,12 +132,13 @@ describe GithubRepo do repo.expects(:remotes).twice.returns(remotes) remotes.expects(:[]).with('origin').returns(nil) - remotes.expects(:create).with('origin', 'ggrroouupp#remote_repo').returns(remote) + remotes.expects(:create).with('origin', create_remote).returns(remote) remote.expects(:url).returns('url') remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) end describe 'when there are several repositories' do + let(:create_remote) { 'ggrroouupp#remote_repo' } let(:repository) { './ggrroouupp.git' } let(:single_repo) { nil } @@ -148,6 +149,7 @@ describe GithubRepo do end describe 'when is a single repository' do + let(:create_remote) { 'github_repo_hook#remote_repo' } let(:repository) { 'foo.git' } let(:single_repo) { true } -- cgit v1.2.1 From a486e086fc3b52f26ade94e58a16fa2150fc9cae Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Fri, 19 Feb 2016 18:58:47 -0200 Subject: move the groups remote to the hook config thanks to @ElvinEfendi for the idea! :+1: --- spec/githubrepo_spec.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index a5bbeca..8d85761 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -126,8 +126,6 @@ describe GithubRepo do before do Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials) Rugged::Repository.expects(:new).with(repository).returns(repo) - Oxidized.config.groups.ggrroouupp.remote_repo = 'ggrroouupp#remote_repo' - Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' Oxidized.config.output.git.single_repo = single_repo repo.expects(:remotes).twice.returns(remotes) @@ -137,22 +135,30 @@ describe GithubRepo do remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) end - describe 'when there are several repositories' do + describe 'and there are several repositories' do let(:create_remote) { 'ggrroouupp#remote_repo' } let(:repository) { './ggrroouupp.git' } let(:single_repo) { nil } + before do + Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' + end + it 'will push to the node group repository' do gr.cfg = Oxidized.config.hooks.github_repo_hook gr.run_hook(ctx).must_equal true end end - describe 'when is a single repository' do + describe 'and has a single repository' do let(:create_remote) { 'github_repo_hook#remote_repo' } let(:repository) { 'foo.git' } let(:single_repo) { true } + before do + Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' + end + it 'will push to the correct repository' do gr.cfg = Oxidized.config.hooks.github_repo_hook gr.run_hook(ctx).must_equal true -- cgit v1.2.1 From 3a57984ed70d7d217147b4d7341dff86aeceed70 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Fri, 19 Feb 2016 16:37:32 -0200 Subject: fix intermitent failing of version tests --- spec/cli_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 783d9c8..0a6c91b 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -12,7 +12,8 @@ describe Oxidized::CLI do before { ARGV.replace [option] } it 'prints the version and exits' do - Oxidized::Config.expects(:load).returns(asetus) + Oxidized::Config.expects(:load) + Oxidized.expects(:setup_logger) Kernel.expects(:exit) proc { -- cgit v1.2.1 From c098f57f5dfc4588673ac62083611736c8a75136 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Fri, 19 Feb 2016 22:17:37 -0200 Subject: refactor `#remote_repo` to not rely on `#single_repo?` config even in the case we have groups, we can assume is only one repository if the `remote_repo` config is a String. --- spec/githubrepo_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 8d85761..b6a8d9e 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -126,7 +126,6 @@ describe GithubRepo do before do Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials) Rugged::Repository.expects(:new).with(repository).returns(repo) - Oxidized.config.output.git.single_repo = single_repo repo.expects(:remotes).twice.returns(remotes) remotes.expects(:[]).with('origin').returns(nil) @@ -138,7 +137,6 @@ describe GithubRepo do describe 'and there are several repositories' do let(:create_remote) { 'ggrroouupp#remote_repo' } let(:repository) { './ggrroouupp.git' } - let(:single_repo) { nil } before do Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' @@ -153,10 +151,10 @@ describe GithubRepo do describe 'and has a single repository' do let(:create_remote) { 'github_repo_hook#remote_repo' } let(:repository) { 'foo.git' } - let(:single_repo) { true } before do Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' + Oxidized.config.output.git.single_repo = true end it 'will push to the correct repository' do -- cgit v1.2.1 From 69210c7ddf7d77cc9eb7b78f0bc00ad2f1475955 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Mon, 22 Feb 2016 14:16:29 -0300 Subject: follow the hook remotes config also for groups repositories ref.: https://github.com/ytti/oxidized/pull/326/files#r53557892 --- spec/githubrepo_spec.rb | 4 +++- spec/node_spec.rb | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 14 deletions(-) (limited to 'spec') diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index b6a8d9e..ab5e251 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -12,7 +12,6 @@ describe GithubRepo do before(:each) do Oxidized.asetus = Asetus.new - Oxidized.config.output.git.repo = 'foo.git' Oxidized.config.log = '/dev/null' Oxidized.setup_logger end @@ -97,6 +96,7 @@ describe GithubRepo do describe 'when there is only one repository and no groups' do before 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}) @@ -139,6 +139,7 @@ describe GithubRepo do let(:repository) { './ggrroouupp.git' } before do + Oxidized.config.output.git.repo.ggrroouupp = repository Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' end @@ -153,6 +154,7 @@ describe GithubRepo do let(:repository) { 'foo.git' } before do + Oxidized.config.output.git.repo = repository Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' Oxidized.config.output.git.single_repo = true end diff --git a/spec/node_spec.rb b/spec/node_spec.rb index f9c912e..21c6e34 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -3,7 +3,6 @@ require 'spec_helper' describe Oxidized::Node do before(:each) do Oxidized.stubs(:asetus).returns(Asetus.new) - Oxidized.config.output.git.repo = '/tmp/repository.git' Oxidized::Node.any_instance.stubs(:resolve_output) @node = Oxidized::Node.new(name: 'example.com', @@ -44,25 +43,47 @@ describe Oxidized::Node do end describe '#repo' do - it 'when there is no groups' do - @node.repo.must_equal '/tmp/repository.git' + let(:group) { nil } + let(:node) do + Oxidized::Node.new({ + ip: '127.0.0.1', group: group, model: 'junos' + }) + end + + it 'when there are no groups' do + Oxidized.config.output.git.repo = '/tmp/repository.git' + node.repo.must_equal '/tmp/repository.git' end describe 'when there are groups' do - let(:node) do - Oxidized::Node.new({ - ip: '127.0.0.1', group: 'ggrroouupp', model: 'junos' - }) + let(:group) { 'ggrroouupp' } + + before do + Oxidized.config.output.git.single_repo = single_repo end - it 'with only one repository' do - Oxidized.config.output.git.single_repo = true - node.repo.must_equal '/tmp/repository.git' + describe 'with only one repository' do + let(:single_repo) { true } + + before do + Oxidized.config.output.git.repo = '/tmp/repository.git' + end + + it 'should use the correct remote' do + node.repo.must_equal '/tmp/repository.git' + end end - it 'with more than one repository' do - Oxidized.config.output.git.single_repo = false - node.repo.must_equal '/tmp/ggrroouupp.git' + describe 'with more than one repository' do + let(:single_repo) { nil } + + before do + Oxidized.config.output.git.repo.ggrroouupp = '/tmp/ggrroouupp.git' + end + + it 'should use the correct remote' do + node.repo.must_equal '/tmp/ggrroouupp.git' + end end end end -- cgit v1.2.1