summaryrefslogtreecommitdiff
path: root/spec/services/clusters/refresh_service_spec.rb
blob: 9e442ebf4e9c8f1d9a3052ab0c7d701c8f12197d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true

require 'spec_helper'

describe Clusters::RefreshService do
  shared_examples 'creates a kubernetes namespace' do
    let(:token) { 'aaaaaa' }
    let(:service_account_creator) { double(Clusters::Gcp::Kubernetes::CreateOrUpdateServiceAccountService, execute: true) }
    let(:secrets_fetcher) { double(Clusters::Gcp::Kubernetes::FetchKubernetesTokenService, execute: token) }

    it 'creates a kubernetes namespace' do
      expect(Clusters::Gcp::Kubernetes::CreateOrUpdateServiceAccountService).to receive(:namespace_creator).and_return(service_account_creator)
      expect(Clusters::Gcp::Kubernetes::FetchKubernetesTokenService).to receive(:new).and_return(secrets_fetcher)

      expect { subject }.to change(project.kubernetes_namespaces, :count)

      kubernetes_namespace = cluster.kubernetes_namespaces.first
      expect(kubernetes_namespace).to be_present
      expect(kubernetes_namespace.project).to eq(project)
    end
  end

  shared_examples 'does not create a kubernetes namespace' do
    it 'does not create a new kubernetes namespace' do
      expect(Clusters::Gcp::Kubernetes::CreateOrUpdateServiceAccountService).not_to receive(:namespace_creator)
      expect(Clusters::Gcp::Kubernetes::FetchKubernetesTokenService).not_to receive(:new)

      expect { subject }.not_to change(Clusters::KubernetesNamespace, :count)
    end
  end

  describe '.create_or_update_namespaces_for_cluster' do
    let(:cluster) { create(:cluster, :provided_by_user, :project) }
    let(:project) { cluster.project }

    subject { described_class.create_or_update_namespaces_for_cluster(cluster) }

    context 'cluster is project level' do
      include_examples 'creates a kubernetes namespace'

      context 'when project already has kubernetes namespace' do
        before do
          create(:cluster_kubernetes_namespace, project: project, cluster: cluster)
        end

        include_examples 'does not create a kubernetes namespace'
      end
    end

    context 'cluster is group level' do
      let(:cluster) { create(:cluster, :provided_by_user, :group) }
      let(:group) { cluster.group }
      let(:project) { create(:project, group: group) }

      include_examples 'creates a kubernetes namespace'

      context 'when project already has kubernetes namespace' do
        before do
          create(:cluster_kubernetes_namespace, project: project, cluster: cluster)
        end

        include_examples 'does not create a kubernetes namespace'
      end
    end
  end

  describe '.create_or_update_namespaces_for_project' do
    let(:project) { create(:project) }

    subject { described_class.create_or_update_namespaces_for_project(project) }

    it 'creates no kubernetes namespaces' do
      expect { subject }.not_to change(project.kubernetes_namespaces, :count)
    end

    context 'project has a project cluster' do
      let!(:cluster) { create(:cluster, :provided_by_gcp, cluster_type: :project_type, projects: [project]) }

      include_examples 'creates a kubernetes namespace'

      context 'when project already has kubernetes namespace' do
        before do
          create(:cluster_kubernetes_namespace, project: project, cluster: cluster)
        end

        include_examples 'does not create a kubernetes namespace'
      end
    end

    context 'project belongs to a group cluster' do
      let!(:cluster) { create(:cluster, :provided_by_gcp, :group) }

      let(:group) { cluster.group }
      let(:project) { create(:project, group: group) }

      context 'when ci_preparing_state feature flag is enabled' do
        include_examples 'does not create a kubernetes namespace'

        context 'when project already has kubernetes namespace' do
          before do
            create(:cluster_kubernetes_namespace, project: project, cluster: cluster)
          end

          include_examples 'does not create a kubernetes namespace'
        end
      end

      context 'when ci_preparing_state feature flag is disabled' do
        before do
          stub_feature_flags(ci_preparing_state: false)
        end

        include_examples 'creates a kubernetes namespace'

        context 'when project already has kubernetes namespace' do
          before do
            create(:cluster_kubernetes_namespace, project: project, cluster: cluster)
          end

          include_examples 'does not create a kubernetes namespace'
        end
      end
    end
  end
end