require "spec_helper"

describe CpEnv::Terraform do
  let(:cluster) { "live-1.cloud-platform.service.justice.gov.uk" }
  let(:vpc_name) { "live-1" }
  let(:eks_cluster_name) { "live" }
  let(:namespace) { "mynamespace" }
  let(:dir) { "namespaces/#{cluster}/#{namespace}" }
  let(:success) { double(success?: true) }
  let(:failure) { double(success?: false) }

  let(:env_vars) {
    {
      "PIPELINE_STATE_BUCKET" => "bucket",
      "PIPELINE_STATE_KEY_PREFIX" => "key-prefix/",
      "PIPELINE_TERRAFORM_STATE_LOCK_TABLE" => "lock-table",
      "PIPELINE_STATE_REGION" => "region",
      "PIPELINE_CLUSTER_STATE" => cluster,
      "TF_VAR_vpc_name" => vpc_name,
      "TF_VAR_eks_cluster_name" => eks_cluster_name,
      "TF_VAR_kubernetes_cluster" => "XIHIUHIU.eks.amazonaws.com"
    }
  }

  let(:params) {
    {
      cluster: cluster,
      namespace: namespace,
      dir: dir
    }
  }

  subject(:tf) { described_class.new(params) }

  describe "plan" do
    it "runs terraform plan" do
      env_vars.each do |key, val|
        expect(ENV).to receive(:fetch).with(key).at_least(:once).and_return(val)
      end
      allow(FileTest).to receive(:directory?).and_return(true)

      tf_dir = "#{dir}/resources"
      kubernetes_cluster = `kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}'`.chomp.gsub(/^https:\/\/(.*)/, '\1').gsub(/^api\.(.*)/, '\1')

      tf_init = "cd #{tf_dir}; export TF_VAR_kubernetes_cluster=#{kubernetes_cluster}; terraform init -backend-config=\"bucket=bucket\" -backend-config=\"key=key-prefix/live-1.cloud-platform.service.justice.gov.uk/mynamespace/terraform.tfstate\" -backend-config=\"dynamodb_table=lock-table\" -backend-config=\"region=region\""

      tf_plan = "cd #{tf_dir}; export TF_VAR_kubernetes_cluster=#{kubernetes_cluster}; terraform plan  "

      expect_execute(tf_init, "", success)
      expect_execute(tf_plan, "", success)
      expect($stdout).to receive(:puts).at_least(:once)

      tf.plan
    end
  end

  describe "apply" do
    it "applies terraform files" do
      env_vars.each do |key, val|
        expect(ENV).to receive(:fetch).with(key).at_least(:once).and_return(val)
      end
      allow(FileTest).to receive(:directory?).and_return(true)
      tf_dir = "#{dir}/resources"
      kubernetes_cluster = `kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}'`.chomp.gsub(/^https:\/\/(.*)/, '\1').gsub(/^api\.(.*)/, '\1')

      tf_init = "cd #{tf_dir}; export TF_VAR_kubernetes_cluster=#{kubernetes_cluster}; terraform init -backend-config=\"bucket=bucket\" -backend-config=\"key=key-prefix/live-1.cloud-platform.service.justice.gov.uk/mynamespace/terraform.tfstate\" -backend-config=\"dynamodb_table=lock-table\" -backend-config=\"region=region\""

      tf_apply = "cd #{tf_dir}; export TF_VAR_kubernetes_cluster=#{kubernetes_cluster}; terraform apply -auto-approve"

      expect_execute(tf_init, "", success)
      expect_execute(tf_apply, "", success)
      expect($stdout).to receive(:puts).at_least(:once)

      tf.apply
    end
  end
end
