elastic_stack_spec.rb 6.38 KB
Newer Older
1
2
3
4
5
# frozen_string_literal: true

require 'spec_helper'

describe Clusters::Applications::ElasticStack do
6
7
  include KubernetesHelpers

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
  include_examples 'cluster application core specs', :clusters_applications_elastic_stack
  include_examples 'cluster application status specs', :clusters_applications_elastic_stack
  include_examples 'cluster application version specs', :clusters_applications_elastic_stack
  include_examples 'cluster application helm specs', :clusters_applications_elastic_stack

  describe '#can_uninstall?' do
    let(:ingress) { create(:clusters_applications_ingress, :installed, external_hostname: 'localhost.localdomain') }
    let(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: ingress.cluster) }

    subject { elastic_stack.can_uninstall? }

    it { is_expected.to be_truthy }
  end

  describe '#set_initial_status' do
    before do
      elastic_stack.set_initial_status
    end

    context 'when ingress is not installed' do
      let(:cluster) { create(:cluster, :provided_by_gcp) }
      let(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: cluster) }

      it { expect(elastic_stack).to be_not_installable }
    end

    context 'when ingress is installed and external_ip is assigned' do
      let(:ingress) { create(:clusters_applications_ingress, :installed, external_ip: '127.0.0.1') }
      let(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: ingress.cluster) }

      it { expect(elastic_stack).to be_installable }
    end

    context 'when ingress is installed and external_hostname is assigned' do
      let(:ingress) { create(:clusters_applications_ingress, :installed, external_hostname: 'localhost.localdomain') }
      let(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: ingress.cluster) }

      it { expect(elastic_stack).to be_installable }
    end
  end

  describe '#install_command' do
    let!(:ingress) { create(:clusters_applications_ingress, :installed, external_ip: '127.0.0.1') }
    let!(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: ingress.cluster) }

    subject { elastic_stack.install_command }

    it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::Helm::InstallCommand) }

    it 'is initialized with elastic stack arguments' do
      expect(subject.name).to eq('elastic-stack')
      expect(subject.chart).to eq('stable/elastic-stack')
      expect(subject.version).to eq('1.8.0')
      expect(subject).to be_rbac
      expect(subject.files).to eq(elastic_stack.files)
    end

    context 'on a non rbac enabled cluster' do
      before do
        elastic_stack.cluster.platform_kubernetes.abac!
      end

      it { is_expected.not_to be_rbac }
    end

    context 'application failed to install previously' do
      let(:elastic_stack) { create(:clusters_applications_elastic_stack, :errored, version: '0.0.1') }

      it 'is initialized with the locked version' do
        expect(subject.version).to eq('1.8.0')
      end
    end
  end

  describe '#uninstall_command' do
    let!(:ingress) { create(:clusters_applications_ingress, :installed, external_ip: '127.0.0.1') }
    let!(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: ingress.cluster) }

    subject { elastic_stack.uninstall_command }

    it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::Helm::DeleteCommand) }

    it 'is initialized with elastic stack arguments' do
      expect(subject.name).to eq('elastic-stack')
      expect(subject).to be_rbac
      expect(subject.files).to eq(elastic_stack.files)
    end

    it 'specifies a post delete command to remove custom resource definitions' do
      expect(subject.postdelete).to eq([
        'kubectl delete pvc --selector release\\=elastic-stack'
      ])
    end
  end

  describe '#files' do
    let!(:ingress) { create(:clusters_applications_ingress, :installed, external_ip: '127.0.0.1') }
    let!(:elastic_stack) { create(:clusters_applications_elastic_stack, cluster: ingress.cluster) }

    let(:values) { subject[:'values.yaml'] }

    subject { elastic_stack.files }

    it 'includes elastic stack specific keys in the values.yaml file' do
      expect(values).to include('ELASTICSEARCH_HOSTS')
    end
  end
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

  describe '#elasticsearch_client' do
    context 'cluster is nil' do
      it 'returns nil' do
        expect(subject.cluster).to be_nil
        expect(subject.elasticsearch_client).to be_nil
      end
    end

    context "cluster doesn't have kubeclient" do
      let(:cluster) { create(:cluster) }
      subject { create(:clusters_applications_elastic_stack, cluster: cluster) }

      it 'returns nil' do
        expect(subject.elasticsearch_client).to be_nil
      end
    end

    context 'cluster has kubeclient' do
      let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
      let(:kubernetes_url) { subject.cluster.platform_kubernetes.api_url }
      let(:kube_client) { subject.cluster.kubeclient.core_client }

      subject { create(:clusters_applications_elastic_stack, cluster: cluster) }

      before do
        subject.cluster.platform_kubernetes.namespace = 'a-namespace'
        stub_kubeclient_discover(cluster.platform_kubernetes.api_url)

        create(:cluster_kubernetes_namespace,
               cluster: cluster,
               cluster_project: cluster.cluster_project,
               project: cluster.cluster_project.project)
      end

      it 'creates proxy elasticsearch_client' do
        expect(subject.elasticsearch_client).to be_instance_of(Elasticsearch::Transport::Client)
      end

      it 'copies proxy_url, options and headers from kube client to elasticsearch_client' do
        expect(Elasticsearch::Client)
          .to(receive(:new))
          .with(url: a_valid_url)
          .and_call_original

        client = subject.elasticsearch_client
        faraday_connection = client.transport.connections.first.connection

        expect(faraday_connection.headers["Authorization"]).to eq(kube_client.headers[:Authorization])
        expect(faraday_connection.ssl.cert_store).to be_instance_of(OpenSSL::X509::Store)
        expect(faraday_connection.ssl.verify).to eq(1)
      end

      context 'when cluster is not reachable' do
        before do
          allow(kube_client).to receive(:proxy_url).and_raise(Kubeclient::HttpError.new(401, 'Unauthorized', nil))
        end

        it 'returns nil' do
          expect(subject.elasticsearch_client).to be_nil
        end
      end
    end
  end
179
end