summaryrefslogtreecommitdiff
path: root/spec/workers/cluster_configure_worker_spec.rb
blob: 83f7680943581dff0f4921fd95d61fb31e8a150d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

require 'spec_helper'

describe ClusterConfigureWorker, '#perform' do
  let(:worker) { described_class.new }
  let(:ci_preparing_state_enabled) { false }

  before do
    stub_feature_flags(ci_preparing_state: ci_preparing_state_enabled)
  end

  context 'when group cluster' do
    let(:cluster) { create(:cluster, :group, :provided_by_gcp) }
    let(:group) { cluster.group }

    context 'when group has no projects' do
      it 'does not create a namespace' do
        expect_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).not_to receive(:execute)

        worker.perform(cluster.id)
      end
    end

    context 'when group has a project' do
      let!(:project) { create(:project, group: group) }

      it 'creates a namespace for the project' do
        expect_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).to receive(:execute).once

        worker.perform(cluster.id)
      end
    end

    context 'when group has project in a sub-group' do
      let!(:subgroup) { create(:group, parent: group) }
      let!(:project) { create(:project, group: subgroup) }

      it 'creates a namespace for the project' do
        expect_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).to receive(:execute).once

        worker.perform(cluster.id)
      end
    end
  end

  context 'when provider type is gcp' do
    let(:cluster) { create(:cluster, :project, :provided_by_gcp) }

    it 'configures kubernetes platform' do
      expect_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).to receive(:execute)

      described_class.new.perform(cluster.id)
    end
  end

  context 'when provider type is user' do
    let(:cluster) { create(:cluster, :project, :provided_by_user) }

    it 'configures kubernetes platform' do
      expect_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).to receive(:execute)

      described_class.new.perform(cluster.id)
    end
  end

  context 'when cluster does not exist' do
    it 'does not provision a cluster' do
      expect_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).not_to receive(:execute)

      described_class.new.perform(123)
    end
  end

  context 'ci_preparing_state feature is enabled' do
    let(:cluster) { create(:cluster) }
    let(:ci_preparing_state_enabled) { true }

    it 'does not configure the cluster' do
      expect(Clusters::RefreshService).not_to receive(:create_or_update_namespaces_for_cluster)

      described_class.new.perform(cluster.id)
    end
  end
end