gitaly_rake_spec.rb 4.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
require 'rake_helper'

describe 'gitlab:gitaly namespace rake task' do
  before :all do
    Rake.application.rake_require 'tasks/gitlab/gitaly'
  end

  describe 'install' do
    let(:repo) { 'https://gitlab.com/gitlab-org/gitaly.git' }
    let(:clone_path) { Rails.root.join('tmp/tests/gitaly').to_s }
11
    let(:version) { File.read(Rails.root.join(Gitlab::GitalyClient::SERVER_VERSION_FILE)).chomp }
12 13 14 15 16 17 18 19 20 21 22

    context 'no dir given' do
      it 'aborts and display a help message' do
        # avoid writing task output to spec progress
        allow($stderr).to receive :write
        expect { run_rake_task('gitlab:gitaly:install') }.to raise_error /Please specify the directory where you want to install gitaly/
      end
    end

    context 'when an underlying Git command fail' do
      it 'aborts and display a help message' do
23
        expect(main_object)
24
          .to receive(:checkout_or_clone_version).and_raise 'Git error'
25 26 27 28 29 30 31 32 33 34

        expect { run_rake_task('gitlab:gitaly:install', clone_path) }.to raise_error 'Git error'
      end
    end

    describe 'checkout or clone' do
      before do
        expect(Dir).to receive(:chdir).with(clone_path)
      end

35
      it 'calls checkout_or_clone_version with the right arguments' do
36
        expect(main_object)
37
          .to receive(:checkout_or_clone_version).with(version: version, repo: repo, target_dir: clone_path)
38 39 40 41 42 43

        run_rake_task('gitlab:gitaly:install', clone_path)
      end
    end

    describe 'gmake/make' do
44
      let(:command_preamble) { %w[/usr/bin/env -u RUBYOPT -u BUNDLE_GEMFILE] }
45

46
      before do
47
        stub_env('CI', false)
48 49
        FileUtils.mkdir_p(clone_path)
        expect(Dir).to receive(:chdir).with(clone_path).and_call_original
50
        allow(Rails.env).to receive(:test?).and_return(false)
51 52 53 54
      end

      context 'gmake is available' do
        before do
55
          expect(main_object).to receive(:checkout_or_clone_version)
56 57 58 59
        end

        it 'calls gmake in the gitaly directory' do
          expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['/usr/bin/gmake', 0])
60
          expect(main_object).to receive(:run_command!).with(command_preamble + %w[gmake]).and_return(true)
61 62 63 64 65 66 67

          run_rake_task('gitlab:gitaly:install', clone_path)
        end
      end

      context 'gmake is not available' do
        before do
68
          expect(main_object).to receive(:checkout_or_clone_version)
69
          expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['', 42])
70 71 72
        end

        it 'calls make in the gitaly directory' do
73
          expect(main_object).to receive(:run_command!).with(command_preamble + %w[make]).and_return(true)
74 75 76

          run_rake_task('gitlab:gitaly:install', clone_path)
        end
77

78
        context 'when Rails.env is test' do
79 80 81 82 83
          let(:command) do
            %W[make
               BUNDLE_FLAGS=--no-deployment
               BUNDLE_PATH=#{Bundler.bundle_path}]
          end
84

85
          before do
86
            allow(Rails.env).to receive(:test?).and_return(true)
87 88
          end

89 90
          it 'calls make in the gitaly directory with --no-deployment flag for bundle' do
            expect(main_object).to receive(:run_command!).with(command_preamble + command).and_return(true)
91

92 93 94
            run_rake_task('gitlab:gitaly:install', clone_path)
          end
        end
95 96 97
      end
    end
  end
98 99 100 101

  describe 'storage_config' do
    it 'prints storage configuration in a TOML format' do
      config = {
102
        'default' => Gitlab::GitalyClient::StorageSettings.new(
103 104
          'path' => '/path/to/default',
          'gitaly_address' => 'unix:/path/to/my.socket'
105 106
        ),
        'nfs_01' => Gitlab::GitalyClient::StorageSettings.new(
107 108
          'path' => '/path/to/nfs_01',
          'gitaly_address' => 'unix:/path/to/my.socket'
109
        )
110 111
      }
      allow(Gitlab.config.repositories).to receive(:storages).and_return(config)
112
      allow(Rails.env).to receive(:test?).and_return(false)
113

114
      expected_output = ''
115
      Timecop.freeze do
116
        expected_output = <<~TOML
117 118
          # Gitaly storage configuration generated from #{Gitlab.config.source} on #{Time.current.to_s(:long)}
          # This is in TOML format suitable for use in Gitaly's config.toml file.
119
          bin_dir = "tmp/tests/gitaly"
120
          socket_path = "/path/to/my.socket"
Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
121 122
          [gitlab-shell]
          dir = "#{Gitlab.config.gitlab_shell.path}"
123 124 125 126 127 128
          [[storage]]
          name = "default"
          path = "/path/to/default"
          [[storage]]
          name = "nfs_01"
          path = "/path/to/nfs_01"
129 130 131
        TOML
      end

132 133
      expect { run_rake_task('gitlab:gitaly:storage_config')}
        .to output(expected_output).to_stdout
134

Ken's avatar
Ken committed
135
      parsed_output = TomlRB.parse(expected_output)
136
      config.each do |name, params|
137
        expect(parsed_output['storage']).to include({ 'name' => name, 'path' => params.legacy_disk_path })
138 139 140
      end
    end
  end
141
end