<commit_before>require "spec_helper"

$:.push(File.expand_path(File.join(File.dirname(__FILE__), %w{.. .. lib})))
require "appliance_console/service_group"
require "linux_admin"

describe ApplianceConsole::ServiceGroup do
  let(:group)             { described_class.new }
  let(:common_services)   { %w(evminit memcached miqtop evmserverd) }
  let(:postgres_service)  { %w(postgresql92-postgresql) }

  describe "#postgresql?" do
    it { expect(group).not_to be_postgresql }

    context "when internal postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }
      it { expect(group).to be_postgresql }
    end

    context "when not internal postgres" do
      let(:group) { described_class.new(:internal_postgresql => false) }
      it { expect(group).not_to be_postgresql }
    end
  end

  describe "#enable" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "#to_enable" do
      expect(group.to_enable).to eq(common_services)
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }
      it "#to_enable" do
        expect(group.to_enable).to eq(common_services | postgres_service)
      end
    end
  end

  describe "#start" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "#to_start" do
      expect(group.to_start).to eq(common_services)
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "#to_start" do
        expect(group.to_start).to eq(common_services)
      end
    end
  end

  describe "#disable" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "#to_disable" do
      expect(group.to_disable).to eq(postgres_service)
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "#to_disable" do
        expect(group.to_disable).to eq([])
      end
    end
  end

  describe "#stop" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "#to_stop" do
      expect(group.to_stop).to eq(postgres_service)
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "#to_stop" do
        expect(group.to_stop).to eq([])
      end
    end
  end

  # this is private, but since we are stubbing it, make sure it works
  context "#enable_miqtop" do
    it "calls chkconfig" do
      expect(LinuxAdmin).to receive(:run).with("chkconfig", :params => {"--add" => "miqtop"})
      group.send(:enable_miqtop)
    end
  end

  # this is private, but since we are stubbing it, make sure it works
  context "#run_service" do
    it "invokes LinuxAdmin service call" do
      stub = double
      expect(stub).to receive('start').and_return(true)
      expect(LinuxAdmin::Service).to receive(:new).with('service').and_return(stub)
      group.send(:run_service, 'service', 'start')
    end
  end

  # this is private, but since we are stubbing it, make sure it works
  context "#detached_service" do
    it "invokes Spawn" do
      spwn = double
      expect(Kernel).to receive(:spawn).with(
        "/sbin/service service start", [:out, :err] => ["/dev/null", "w"]
      ).and_return(spwn)
      expect(Process).to receive(:detach).with(spwn)

      group.send(:run_detached_service, "service", "start")
    end
  end

  shared_examples_for "service management" do |command|
    it "##{command}" do
      expect(group).to receive(:enable_miqtop) if command == "enable"

      expected_calls = group.send("to_#{command}")

      # Hack until LinuxAdmin::Service start handles detaching.
      if command == "start"
        group.should_receive(:start_command)
      else
        expected_calls.each do |service|
          expect(group).to receive(:run_service).with(service, command).and_return(true)
        end
      end

      group.send(command)
    end
  end

  include_examples "service management", "enable"
  include_examples "service management", "disable"
  include_examples "service management", "stop"
  include_examples "service management", "start"
end<commit_msg>Don't test internal ServiceGroup#to_* methods<commit_after>require "spec_helper"

$:.push(File.expand_path(File.join(File.dirname(__FILE__), %w{.. .. lib})))
require "appliance_console/service_group"
require "linux_admin"

describe ApplianceConsole::ServiceGroup do
  let(:group)             { described_class.new }
  let(:common_services)   { %w(evminit memcached miqtop evmserverd) }
  let(:postgres_service)  { "postgresql92-postgresql" }

  describe "#postgresql?" do
    it { expect(group).not_to be_postgresql }

    context "when internal postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }
      it { expect(group).to be_postgresql }
    end

    context "when not internal postgres" do
      let(:group) { described_class.new(:internal_postgresql => false) }
      it { expect(group).not_to be_postgresql }
    end
  end

  describe "#enable" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "enables all but postgres" do
      expect(group).to receive(:enable_miqtop)
      common_services.each do |service|
        expect_run_service(service, "enable")
      end

      group.enable
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "enables all including postgres" do
        expect(group).to receive(:enable_miqtop)
        common_services.each do |service|
          expect_run_service(service, "enable")
        end
        expect_run_service(postgres_service, "enable")

        group.enable
      end
    end
  end

  describe "#start" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "starts all but postgres" do
      common_services.each do |service|
        expect_run_detached_service(service, "start")
      end

      group.start
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "starts all including postgres" do
        common_services.each do |service|
          expect_run_detached_service(service, "start")
        end

        group.start
      end
    end
  end

  describe "#disable" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "disables postgres" do
      expect_run_service(postgres_service, "disable")

      group.disable
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "does nothing" do
        expect_no_service_calls

        group.disable
      end
    end
  end

  describe "#stop" do
    let(:group) { described_class.new(:internal_postgresql => false) }

    it "stops postgres" do
      expect_run_service(postgres_service, "stop")

      group.stop
    end

    context "with postgres" do
      let(:group) { described_class.new(:internal_postgresql => true) }

      it "stops nothing" do
        expect_no_service_calls

        group.stop
      end
    end
  end

  # this is private, but since we are stubbing it, make sure it works
  context "#enable_miqtop" do
    it "calls chkconfig" do
      expect(LinuxAdmin).to receive(:run).with("chkconfig", :params => {"--add" => "miqtop"})
      group.send(:enable_miqtop)
    end
  end

  # this is private, but since we are stubbing it, make sure it works
  context "#run_service" do
    it "invokes LinuxAdmin service call" do
      stub = double
      expect(stub).to receive('start').and_return(true)
      expect(LinuxAdmin::Service).to receive(:new).with('service').and_return(stub)
      group.send(:run_service, 'service', 'start')
    end
  end

  # this is private, but since we are stubbing it, make sure it works
  context "#detached_service" do
    it "invokes Spawn" do
      spwn = double
      expect(Kernel).to receive(:spawn).with(
        "/sbin/service service start", [:out, :err] => ["/dev/null", "w"]
      ).and_return(spwn)
      expect(Process).to receive(:detach).with(spwn)

      group.send(:run_detached_service, "service", "start")
    end
  end

  private

  def expect_no_service_calls
    expect(group).not_to receive(:run_service)
  end

  def expect_run_detached_service(service, command)
    expect(group).to receive(:run_detached_service).with(service, command)
  end

  def expect_run_service(service, command)
    expect(group).to receive(:run_service).with(service, command)
  end
end
{'fraction_non_alphanumeric': 0.11447260834014718, 'fraction_numerical': 0.0004672351360822334, 'mean_word_length': 3.2094395280235988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209851', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2666, 'n_words': 645}