|
| 1 | +# Copyright 2017 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +require 'spec_helper' |
| 16 | +require 'config/matchers' |
| 17 | + |
| 18 | +require 'kubernetes/config/incluster_config' |
| 19 | + |
| 20 | + |
| 21 | +describe Kubernetes::InClusterConfig do |
| 22 | + |
| 23 | + context '#configure' do |
| 24 | + let(:incluster_config) do |
| 25 | + Kubernetes::InClusterConfig.new.tap do |c| |
| 26 | + c.instance_variable_set(:@env, { |
| 27 | + Kubernetes::InClusterConfig::SERVICE_HOST_ENV_NAME => 'localhost', |
| 28 | + Kubernetes::InClusterConfig::SERVICE_PORT_ENV_NAME => '443', |
| 29 | + }) |
| 30 | + c.instance_variable_set(:@ca_cert, file_fixture('certs/ca.crt').to_s) |
| 31 | + c.instance_variable_set(:@token_file, file_fixture('tokens/token').to_s) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + it 'should configure configuration' do |
| 36 | + expected = Kubernetes::Configuration.new do |c| |
| 37 | + c.scheme = 'https' |
| 38 | + c.host = 'localhost:443' |
| 39 | + c.ssl_ca_cert = file_fixture('certs/ca.crt').to_s |
| 40 | + c.api_key['authorization'] = 'Bearer token1' |
| 41 | + end |
| 42 | + actual = Kubernetes::Configuration.new |
| 43 | + |
| 44 | + incluster_config.configure(actual) |
| 45 | + expect(actual).to be_same_configuration_as(expected) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context '#validate' do |
| 50 | + let(:incluster_config) do |
| 51 | + Kubernetes::InClusterConfig.new.tap do |c| |
| 52 | + c.instance_variable_set(:@env, { |
| 53 | + Kubernetes::InClusterConfig::SERVICE_HOST_ENV_NAME => 'localhost', |
| 54 | + Kubernetes::InClusterConfig::SERVICE_PORT_ENV_NAME => '443', |
| 55 | + }) |
| 56 | + c.instance_variable_set(:@ca_cert, file_fixture('certs/ca.crt').to_s) |
| 57 | + c.instance_variable_set(:@token_file, file_fixture('tokens/token').to_s) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'if valid environment' do |
| 62 | + |
| 63 | + it 'shold not raise ConfigError' do |
| 64 | + expect { incluster_config.validate }.not_to raise_error |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'if SERVICE_HOST_ENV_NAME env variable is not set' do |
| 69 | + |
| 70 | + it 'should raise ConfigError' do |
| 71 | + incluster_config.env[Kubernetes::InClusterConfig::SERVICE_HOST_ENV_NAME] = nil |
| 72 | + |
| 73 | + expect { incluster_config.validate }.to raise_error(Kubernetes::ConfigError) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'if SERVICE_PORT_ENV_NAME env variable is not set' do |
| 78 | + |
| 79 | + it 'should raise ConfigError' do |
| 80 | + incluster_config.env[Kubernetes::InClusterConfig::SERVICE_PORT_ENV_NAME] = nil |
| 81 | + |
| 82 | + expect { incluster_config.validate }.to raise_error(Kubernetes::ConfigError) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'if ca_cert file is not exist' do |
| 87 | + |
| 88 | + it 'shold raise ConfigError' do |
| 89 | + incluster_config.instance_variable_set(:@ca_cert, 'certs/no_ca.crt') |
| 90 | + |
| 91 | + expect { incluster_config.validate }.to raise_error(Kubernetes::ConfigError) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + context 'if token file is not exist' do |
| 96 | + |
| 97 | + it 'shold raise ConfigError' do |
| 98 | + incluster_config.instance_variable_set(:@token_file, 'tokens/no_token') |
| 99 | + |
| 100 | + expect { incluster_config.validate }.to raise_error(Kubernetes::ConfigError) |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + context '#ca_cert' do |
| 106 | + let(:incluster_config) { Kubernetes::InClusterConfig.new } |
| 107 | + |
| 108 | + it 'shold return "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"' do |
| 109 | + expect(incluster_config.ca_cert).to eq('/var/run/secrets/kubernetes.io/serviceaccount/ca.crt') |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context '#token_file' do |
| 114 | + let(:incluster_config) { Kubernetes::InClusterConfig.new } |
| 115 | + |
| 116 | + it 'shold return "/var/run/secrets/kubernetes.io/serviceaccount/token"' do |
| 117 | + expect(incluster_config.token_file).to eq('/var/run/secrets/kubernetes.io/serviceaccount/token') |
| 118 | + end |
| 119 | + end |
| 120 | +end |
0 commit comments