require 'chef_helper'

RSpec.describe 'gitlab::sidekiq' do
  let(:chef_run) do
    runner = ChefSpec::SoloRunner.new(
      step_into: %w(sidekiq_service runit_service),
      path: 'spec/chef/fixtures/fauxhai/ubuntu/16.04.json'
    )
    runner.converge('gitlab::default')
  end

  before do
    allow(Gitlab).to receive(:[]).and_call_original
  end

  context 'with default values' do
    it 'correctly renders out the sidekiq service file' do
      expect(chef_run).to render_file("/opt/gitlab/sv/sidekiq/run")
        .with_content { |content|
          expect(content).not_to match(/export prometheus_run_dir=\'\'/)
          expect(content).to match(/mkdir -p \/run\/gitlab\/sidekiq/)
          expect(content).to match(/rm \/run\/gitlab\/sidekiq/)
          expect(content).to match(/chmod 0700 \/run\/gitlab\/sidekiq/)
          expect(content).to match(/chown git \/run\/gitlab\/sidekiq/)
          expect(content).to match(/export prometheus_run_dir=\'\/run\/gitlab\/sidekiq\'/)
          expect(content).to match(/rubyopt=\"-W:no-experimental\"/)
          expect(content).to include(%(RUBYOPT="${rubyopt}"))
          expect(content).to match(%r{bin/sidekiq-cluster})
          expect(content).to match(/-c 20/) # concurrency
          expect(content).to match(/--timeout 25/) # shutdown timeout
          expect(content).to match(/\*/) # all queues
        }
    end

    it_behaves_like "enabled runit service", "sidekiq", "root", "root"
  end

  describe 'log_format' do
    context 'by default' do
      it 'does not pass timestamp flag to svlogd' do
        expect(chef_run).not_to render_file("/opt/gitlab/sv/sidekiq/log/run").with_content(/-tt/)
      end
    end

    context 'when user specifies text log format' do
      before do
        stub_gitlab_rb(
          sidekiq: { log_format: 'text' }
        )
      end

      it 'passes timestamp flag to svlogd' do
        expect(chef_run).to render_file("/opt/gitlab/sv/sidekiq/log/run").with_content(/-tt/)
      end
    end
  end

  context 'log directory and runit group' do
    context 'default values' do
      it_behaves_like 'enabled logged service', 'sidekiq', true, { log_directory_owner: 'git' }
    end

    context 'custom values' do
      before do
        stub_gitlab_rb(
          sidekiq: {
            log_group: 'fugee',
            concurrency: 42
          }
        )
      end
      it_behaves_like 'enabled logged service', 'sidekiq', true, { log_directory_owner: 'git', log_group: 'fugee' }

      it 'correctly renders out the sidekiq service file' do
        expect(chef_run).to render_file("/opt/gitlab/sv/sidekiq/run")
          .with_content { |content|
            expect(content).to match(/-c 42/) # concurrency
          }
      end
    end

    it_behaves_like 'a service with proper supervise directories', 'sidekiq'
  end

  include_examples "consul service discovery", "sidekiq", "sidekiq"
end
