summaryrefslogtreecommitdiff
path: root/spec/githubrepo_spec.rb
blob: 36207d38bc3864c1e4a39f75bc7f45d70a588b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'spec_helper'
require 'rugged'
require 'oxidized/hook/githubrepo'

describe Oxidized::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 "#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
    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(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0))
    end

    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

    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