require_relative "../support/spec_helper"

module Dalmatian
  RSpec.describe RdsDeployment do
    before do
      allow(Helper).to receive(:change_to)
      allow(Helper).to receive(:run!)
      allow(Logger).to receive(:info)
    end

    let(:rds_params) { double("rds_params") }

    let(:rds) do
      instance_double(
        Rds,
        identifier: "testservice",
        cluster_id: "new-dedicated-cluster-id",
        account_id: 123456789012,
        to_params: rds_params
      )
    end

    let(:deployment) do
      RdsDeployment.new(
        rds: rds,
        env: {name: "staging", details: {"min_servers" => 2, "max_servers" => 4}},
        auto_approve: false,
        plan: false
      )
    end

    describe "#call" do
      before do
        allow(Terraform).to receive(:apply)
        allow(Terraform).to receive(:plan)
        allow(Terraform).to receive(:ensure_presence_of_workspace)
      end

      it "changes to rds infrastructure directory" do
        directory = File.join(
          Infrastructure::APP_ROOT,
          Infrastructure::PATH,
          "rds"
        )

        deployment.call

        expect(Helper).to have_received(:change_to).with(directory)
      end

      it "asks Terraform to ensure that the workspace is in place" do
        deployment.call

        expect(Terraform).to have_received(:ensure_presence_of_workspace)
          .with("new-dedicated-cluster-id-testservice-rds-staging")
      end

      context "when in _plan_ mode" do
        let(:deployment) do
          RdsDeployment.new(
            rds: rds,
            env: {name: "staging", details: {"min_servers" => 2, "max_servers" => 4}},
            auto_approve: false,
            plan: true
          )
        end

        it "invokes Terraform.plan with the _dalmatian-read_ role" do
          deployment.call

          expect(Terraform).to have_received(:plan).with(
            tfvars: {
              "min_servers" => 2,
              "max_servers" => 4,
              "rds" => rds_params,
              "account_id" => 123456789012,
              "infrastructure_name" => "new-dedicated-cluster-id",
              "environment" => "staging",
              "dalmatian_role" => "dalmatian-read"
            },
            verbose: false
          )
        end
      end

      context "when NOT in _plan_ mode" do
        it "invokes Terraform.apply with the _dalmatian-admin_ role" do
          deployment.call

          expect(Terraform).to have_received(:apply).with(
            tfvars: {
              "min_servers" => 2,
              "max_servers" => 4,
              "rds" => rds_params,
              "account_id" => 123456789012,
              "infrastructure_name" => "new-dedicated-cluster-id",
              "environment" => "staging",
              "dalmatian_role" => "dalmatian-admin"
            },
            auto_approve: false,
            verbose: false
          )
        end

        context "when in _auto_approve_ mode" do
          let(:deployment) do
            RdsDeployment.new(
              rds: rds,
              env: {name: "staging", details: {"min_servers" => 2, "max_servers" => 4}},
              auto_approve: true,
              plan: false
            )
          end

          it "asks Terraform to use auto_approve mode" do
            deployment.call

            expect(Terraform).to have_received(:apply).with(
              hash_including(auto_approve: true)
            )
          end
        end
      end
    end
  end
end
