summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/satellite/action_spec.rb
blob: 28e3d64ee2be0528484216f6dac785cd74de7339 (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
require 'spec_helper'

describe 'Gitlab::Satellite::Action' do
  let(:project) { create(:project) }
  let(:user) { create(:user) }

  describe '#prepare_satellite!' do
    it 'should be able to fetch timeout from conf' do
      expect(Gitlab::Satellite::Action::DEFAULT_OPTIONS[:git_timeout]).to eq(30.seconds)
    end

    it 'create a repository with a parking branch and one remote: origin' do
      repo = project.satellite.repo

      #now lets dirty it up

      starting_remote_count = repo.git.list_remotes.size
      expect(starting_remote_count).to be >= 1
      #kind of hookey way to add a second remote
      origin_uri = repo.git.remote({v: true}).split(" ")[1]
    begin
      repo.git.remote({raise: true}, 'add', 'another-remote', origin_uri)
      repo.git.branch({raise: true}, 'a-new-branch')

      expect(repo.heads.size).to be > (starting_remote_count)
      expect(repo.git.remote().split(" ").size).to be > (starting_remote_count)
    rescue
    end

      repo.git.config({}, "user.name", "#{user.name} -- foo")
      repo.git.config({}, "user.email", "#{user.email} -- foo")
      expect(repo.config['user.name']).to eq("#{user.name} -- foo")
      expect(repo.config['user.email']).to eq("#{user.email} -- foo")


      #These must happen in the context of the satellite directory...
      satellite_action = Gitlab::Satellite::Action.new(user, project)
      project.satellite.lock {
        #Now clean it up, use send to get around prepare_satellite! being protected
        satellite_action.send(:prepare_satellite!, repo)
      }

      #verify it's clean
      heads = repo.heads.map(&:name)
      expect(heads.size).to eq(1)
      expect(heads.include?(Gitlab::Satellite::Satellite::PARKING_BRANCH)).to eq(true)
      remotes = repo.git.remote().split(' ')
      expect(remotes.size).to eq(1)
      expect(remotes.include?('origin')).to eq(true)
      expect(repo.config['user.name']).to eq(user.name)
      expect(repo.config['user.email']).to eq(user.email)
    end
  end

  describe '#in_locked_and_timed_satellite' do

    it 'should make use of a lockfile' do
      repo = project.satellite.repo
      called = false

      #set assumptions
      FileUtils.rm_f(project.satellite.lock_file)

      expect(File.exists?(project.satellite.lock_file)).to be_falsey

      satellite_action = Gitlab::Satellite::Action.new(user, project)
      satellite_action.send(:in_locked_and_timed_satellite) do |sat_repo|
        expect(repo).to eq(sat_repo)
        expect(File.exists? project.satellite.lock_file).to be_truthy
        called = true
      end

      expect(called).to be_truthy

    end

    it 'should be able to use the satellite after locking' do
      repo = project.satellite.repo
      called = false

      # Set base assumptions
      if File.exists? project.satellite.lock_file
        expect(FileLockStatusChecker.new(project.satellite.lock_file).flocked?).to be_falsey
      end

      satellite_action = Gitlab::Satellite::Action.new(user, project)
      satellite_action.send(:in_locked_and_timed_satellite) do |sat_repo|
        called = true
        expect(repo).to eq(sat_repo)
        expect(File.exists? project.satellite.lock_file).to be_truthy
        expect(FileLockStatusChecker.new(project.satellite.lock_file).flocked?).to be_truthy
      end

      expect(called).to be_truthy
      expect(FileLockStatusChecker.new(project.satellite.lock_file).flocked?).to be_falsey

    end

    class FileLockStatusChecker < File
      def flocked?(&block)
        status = flock LOCK_EX|LOCK_NB
        case status
          when false
            return true
          when 0
            begin
              block ? block.call : false
            ensure
              flock LOCK_UN
            end
          else
            raise SystemCallError, status
        end
      end
    end

  end
end