# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do
  let(:build) { create(:ci_build) }

  describe '#unmet?' do
    subject { described_class.new(build).unmet? }

    context 'build has no deployment' do
      before do
        expect(build.deployment).to be_nil
      end

      it { is_expected.to be_falsey }
    end

    context 'build has a deployment' do
      let!(:deployment) { create(:deployment, deployable: build, cluster: cluster) }
      let(:cluster) { nil }

      context 'and a cluster to deploy to' do
        let(:cluster) { create(:cluster, :group) }

        it { is_expected.to be_truthy }

        context 'and the cluster is not managed' do
          let(:cluster) { create(:cluster, :not_managed, projects: [build.project]) }

          it { is_expected.to be_falsey }
        end

        context 'and a namespace is already created for this project' do
          let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, :with_token, cluster: cluster, project: build.project) }

          it { is_expected.to be_falsey }

          context 'and the service_account_token is blank' do
            let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, :without_token, cluster: cluster, project: build.project) }

            it { is_expected.to be_truthy }
          end
        end
      end

      context 'and no cluster to deploy to' do
        it { is_expected.to be_falsey }
      end
    end
  end

  describe '#complete!' do
    let!(:deployment) { create(:deployment, deployable: build, cluster: cluster) }
    let(:service) { double(execute: true) }
    let(:cluster) { nil }

    subject { described_class.new(build).complete! }

    context 'completion is required' do
      let(:cluster) { create(:cluster, :group) }

      it 'creates a kubernetes namespace' do
        expect(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService)
          .to receive(:new)
          .with(cluster: cluster, kubernetes_namespace: instance_of(Clusters::KubernetesNamespace))
          .and_return(service)

        expect(service).to receive(:execute).once

        subject
      end
    end

    context 'completion is not required' do
      it 'does not create a namespace' do
        expect(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).not_to receive(:new)

        subject
      end
    end
  end
end
