| 
4 | 4 | describe GitlabPostReceive do  | 
5 | 5 |  let(:repository_path) { "/home/git/repositories" }  | 
6 | 6 |  let(:repo_name) { 'dzaporozhets/gitlab-ci' }  | 
 | 7 | + let(:actor) { 'key-123' }  | 
 | 8 | + let(:changes) { 'wow' }  | 
7 | 9 |  let(:repo_path) { File.join(repository_path, repo_name) + ".git" }  | 
8 |  | - let(:gitlab_post_receive) { GitlabPostReceive.new(repo_path, 'key-123', 'wow') }  | 
 | 10 | + let(:gitlab_post_receive) { GitlabPostReceive.new(repo_path, actor, changes) }  | 
 | 11 | + let(:message) { "test " * 10 + "message " * 10 }  | 
9 | 12 | 
 
  | 
10 | 13 |  before do  | 
11 | 14 |  GitlabConfig.any_instance.stub(repos_path: repository_path)  | 
12 |  | - Kernel.stub(system: true)  | 
13 |  | - GitlabNet.any_instance.stub(broadcast_message: { "message" => "test " * 10 + "message " * 10 })  | 
 | 15 | + GitlabNet.any_instance.stub(broadcast_message: { "message" => message })  | 
14 | 16 |  end  | 
15 | 17 | 
 
  | 
16 |  | - describe :initialize do  | 
17 |  | - it { gitlab_post_receive.repo_path.should == repo_path }  | 
18 |  | - it { gitlab_post_receive.changes.should == 'wow' }  | 
19 |  | - it { gitlab_post_receive.exec }  | 
 | 18 | + describe "#exec" do  | 
 | 19 | + | 
 | 20 | + before do  | 
 | 21 | + GitlabConfig.any_instance.stub(redis_command: %w(env -i redis-cli))  | 
 | 22 | + allow(gitlab_post_receive).to receive(:system).and_return(true)  | 
 | 23 | + end  | 
 | 24 | + | 
 | 25 | + it "resets the GL_ID environment variable" do  | 
 | 26 | + ENV["GL_ID"] = actor  | 
 | 27 | + | 
 | 28 | + gitlab_post_receive.exec  | 
 | 29 | + | 
 | 30 | + expect(ENV["GL_ID"]).to be_nil  | 
 | 31 | + end  | 
 | 32 | + | 
 | 33 | + it "prints the broadcast message" do  | 
 | 34 | + expect(gitlab_post_receive).to receive(:puts).ordered  | 
 | 35 | + expect(gitlab_post_receive).to receive(:puts).with(  | 
 | 36 | + "========================================================================"  | 
 | 37 | + ).ordered  | 
 | 38 | + expect(gitlab_post_receive).to receive(:puts).ordered  | 
 | 39 | + | 
 | 40 | + expect(gitlab_post_receive).to receive(:puts).with(  | 
 | 41 | + " test test test test test test test test test test message message"  | 
 | 42 | + ).ordered  | 
 | 43 | + expect(gitlab_post_receive).to receive(:puts).with(  | 
 | 44 | + " message message message message message message message message"  | 
 | 45 | + ).ordered  | 
 | 46 | + | 
 | 47 | + expect(gitlab_post_receive).to receive(:puts).ordered   | 
 | 48 | + expect(gitlab_post_receive).to receive(:puts).with(  | 
 | 49 | + "========================================================================"  | 
 | 50 | + ).ordered  | 
 | 51 | + | 
 | 52 | + gitlab_post_receive.exec  | 
 | 53 | + end  | 
 | 54 | + | 
 | 55 | + it "pushes a Sidekiq job onto the queue" do  | 
 | 56 | + expect(gitlab_post_receive).to receive(:system).with(  | 
 | 57 | + *[  | 
 | 58 | + *%w(env -i redis-cli rpush resque:gitlab:queue:post_receive),   | 
 | 59 | + %Q/{"class":"PostReceive","args":["#{repo_path}","#{actor}","#{changes}"]}/,  | 
 | 60 | + { err: "/dev/null", out: "/dev/null" }  | 
 | 61 | + ]  | 
 | 62 | + ).and_return(true)  | 
 | 63 | + | 
 | 64 | + gitlab_post_receive.exec  | 
 | 65 | + end  | 
 | 66 | + | 
 | 67 | + context "when the redis command succeeds" do  | 
 | 68 | + | 
 | 69 | + before do  | 
 | 70 | + allow(gitlab_post_receive).to receive(:system).and_return(true)  | 
 | 71 | + end  | 
 | 72 | + | 
 | 73 | + it "returns true" do  | 
 | 74 | + expect(gitlab_post_receive.exec).to eq(true)  | 
 | 75 | + end  | 
 | 76 | + end  | 
 | 77 | + | 
 | 78 | + context "when the redis command fails" do  | 
 | 79 | + | 
 | 80 | + before do  | 
 | 81 | + allow(gitlab_post_receive).to receive(:system).and_return(false)  | 
 | 82 | + allow($?).to receive(:exitstatus).and_return(nil)  | 
 | 83 | + end  | 
 | 84 | + | 
 | 85 | + it "returns false" do  | 
 | 86 | + expect(gitlab_post_receive.exec).to eq(false)  | 
 | 87 | + end  | 
 | 88 | + end  | 
20 | 89 |  end  | 
21 | 90 | end  | 
0 commit comments