summaryrefslogtreecommitdiff
path: root/spec/services/clusters/kubernetes/fetch_kubernetes_token_service_spec.rb
blob: 03c402fb066126a5b836208d657d45e82629d167 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Kubernetes::FetchKubernetesTokenService do
  include KubernetesHelpers

  describe '#execute' do
    let(:api_url) { 'http://111.111.111.111' }
    let(:namespace) { 'my-namespace' }
    let(:service_account_token_name) { 'gitlab-token' }

    let(:kubeclient) do
      Gitlab::Kubernetes::KubeClient.new(
        api_url,
        auth_options: { username: 'admin', password: 'xxx' }
      )
    end

    subject { described_class.new(kubeclient, service_account_token_name, namespace, token_retry_delay: 0).execute }

    before do
      stub_kubeclient_discover(api_url)
    end

    context 'when params correct' do
      let(:decoded_token) { 'xxx.token.xxx' }
      let(:token) { Base64.encode64(decoded_token) }

      context 'when the secret exists' do
        before do
          stub_kubeclient_get_secret(
            api_url,
            metadata_name: service_account_token_name,
            namespace: namespace,
            token: token
          )
        end

        it { is_expected.to eq(decoded_token) }
      end

      context 'when there is a 500 error' do
        before do
          stub_kubeclient_get_secret_error(api_url, service_account_token_name, namespace: namespace, status: 500)
        end

        it { expect { subject }.to raise_error(Kubeclient::HttpError) }
      end

      context 'when the secret does not exist on the first try' do
        before do
          stub_kubeclient_get_secret_not_found_then_found(
            api_url,
            metadata_name: service_account_token_name,
            namespace: namespace,
            token: token
          )
        end

        it 'retries and finds the token' do
          expect(subject).to eq(decoded_token)
        end
      end

      context 'when the secret permanently does not exist' do
        before do
          stub_kubeclient_get_secret_error(api_url, service_account_token_name, namespace: namespace, status: 404)
        end

        it { is_expected.to be_nil }
      end

      context 'when the secret is missing a token on the first try' do
        before do
          stub_kubeclient_get_secret_missing_token_then_with_token(
            api_url,
            metadata_name: service_account_token_name,
            namespace: namespace,
            token: token
          )
        end

        it 'retries and finds the token' do
          expect(subject).to eq(decoded_token)
        end
      end

      context 'when the secret is permanently missing a token' do
        before do
          stub_kubeclient_get_secret(
            api_url,
            metadata_name: service_account_token_name,
            namespace: namespace,
            token: nil
          )
        end

        it { is_expected.to be_nil }
      end
    end
  end
end