2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
require 'spec_helper'
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Clusters::Kubernetes::FetchKubernetesTokenService do
|
2018-12-13 13:39:08 +05:30
|
|
|
include KubernetesHelpers
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
describe '#execute' do
|
|
|
|
let(:api_url) { 'http://111.111.111.111' }
|
2018-12-13 13:39:08 +05:30
|
|
|
let(:namespace) { 'my-namespace' }
|
|
|
|
let(:service_account_token_name) { 'gitlab-token' }
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
let(:kubeclient) do
|
|
|
|
Gitlab::Kubernetes::KubeClient.new(
|
|
|
|
api_url,
|
2018-12-13 13:39:08 +05:30
|
|
|
auth_options: { username: 'admin', password: 'xxx' }
|
2018-12-05 23:21:45 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
subject { described_class.new(kubeclient, service_account_token_name, namespace, token_retry_delay: 0).execute }
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
|
|
|
stub_kubeclient_discover(api_url)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
context 'when params correct' do
|
|
|
|
let(:decoded_token) { 'xxx.token.xxx' }
|
|
|
|
let(:token) { Base64.encode64(decoded_token) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when the secret exists' do
|
2018-12-13 13:39:08 +05:30
|
|
|
before do
|
|
|
|
stub_kubeclient_get_secret(
|
|
|
|
api_url,
|
2021-01-03 14:25:43 +05:30
|
|
|
metadata_name: service_account_token_name,
|
|
|
|
namespace: namespace,
|
|
|
|
token: token
|
2018-12-13 13:39:08 +05:30
|
|
|
)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(decoded_token) }
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
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
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when the secret does not exist on the first try' do
|
|
|
|
before do
|
|
|
|
stub_kubeclient_get_secret_not_found_then_found(
|
|
|
|
api_url,
|
2021-01-03 14:25:43 +05:30
|
|
|
metadata_name: service_account_token_name,
|
|
|
|
namespace: namespace,
|
|
|
|
token: token
|
2019-09-30 21:07:59 +05:30
|
|
|
)
|
|
|
|
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
|
2018-12-13 13:39:08 +05:30
|
|
|
before do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_kubeclient_get_secret_error(api_url, service_account_token_name, namespace: namespace, status: 404)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
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,
|
2021-01-03 14:25:43 +05:30
|
|
|
metadata_name: service_account_token_name,
|
|
|
|
namespace: namespace,
|
|
|
|
token: token
|
2019-09-30 21:07:59 +05:30
|
|
|
)
|
|
|
|
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,
|
2021-01-03 14:25:43 +05:30
|
|
|
metadata_name: service_account_token_name,
|
|
|
|
namespace: namespace,
|
|
|
|
token: nil
|
2019-09-30 21:07:59 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|