diff options
Diffstat (limited to 'spec/githubrepo_spec.rb')
-rw-r--r-- | spec/githubrepo_spec.rb | 109 |
1 files changed, 90 insertions, 19 deletions
diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 9ad43e9..ab5e251 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -2,19 +2,32 @@ require 'spec_helper' require 'rugged' require 'oxidized/hook/githubrepo' -describe Oxidized::Node do +describe GithubRepo do let(:credentials) { mock() } let(:remote) { mock() } + let(:remotes) { 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.config.log = '/dev/null' 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' @@ -68,31 +81,89 @@ 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 + 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}) + 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) + + repo.expects(:remotes).twice.returns(remotes) + remotes.expects(:[]).with('origin').returns(nil) + 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 'and there are several repositories' do + let(:create_remote) { 'ggrroouupp#remote_repo' } + 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 + + 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 'and has a single repository' do + let(:create_remote) { 'github_repo_hook#remote_repo' } + 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 + + 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 |