|
| 1 | +# Copyright 2018 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 | +import json |
| 16 | +import os |
| 17 | +import sys |
| 18 | + |
| 19 | +from asynctest import ANY, TestCase, main, mock, patch |
| 20 | + |
| 21 | +from .config_exception import ConfigException |
| 22 | +from .exec_provider import ExecProvider |
| 23 | + |
| 24 | + |
| 25 | +class ExecProviderTest(TestCase): |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self.input_ok = { |
| 29 | + 'command': 'aws-iam-authenticator token -i dummy', |
| 30 | + 'apiVersion': 'client.authentication.k8s.io/v1beta1' |
| 31 | + } |
| 32 | + self.output_ok = """ |
| 33 | + { |
| 34 | + "apiVersion": "client.authentication.k8s.io/v1beta1", |
| 35 | + "kind": "ExecCredential", |
| 36 | + "status": { |
| 37 | + "token": "dummy" |
| 38 | + } |
| 39 | + } |
| 40 | + """ |
| 41 | + |
| 42 | + process_patch = patch('kubernetes_asyncio.config.exec_provider.asyncio.create_subprocess_exec') |
| 43 | + self.exec_mock = process_patch.start() |
| 44 | + self.process_mock = self.exec_mock.return_value |
| 45 | + self.process_mock.stdout.read = mock.CoroutineMock(return_value=self.output_ok) |
| 46 | + self.process_mock.stderr.read = mock.CoroutineMock(return_value='') |
| 47 | + self.process_mock.wait = mock.CoroutineMock(return_value=0) |
| 48 | + |
| 49 | + def tearDown(self): |
| 50 | + patch.stopall() |
| 51 | + |
| 52 | + def test_missing_input_keys(self): |
| 53 | + exec_configs = [{}, {'command': ''}, {'apiVersion': ''}] |
| 54 | + for exec_config in exec_configs: |
| 55 | + with self.assertRaises(ConfigException) as context: |
| 56 | + ExecProvider(exec_config) |
| 57 | + self.assertIn('exec: malformed request. missing key', |
| 58 | + context.exception.args[0]) |
| 59 | + |
| 60 | + async def test_error_code_returned(self): |
| 61 | + self.process_mock.stdout.read.return_value = '' |
| 62 | + self.process_mock.wait.return_value = 1 |
| 63 | + with self.assertRaisesRegex(ConfigException, 'exec: process returned 1'): |
| 64 | + ep = ExecProvider(self.input_ok) |
| 65 | + await ep.run() |
| 66 | + |
| 67 | + async def test_nonjson_output_returned(self): |
| 68 | + self.process_mock.stdout.read.return_value = '' |
| 69 | + with self.assertRaisesRegex(ConfigException, 'exec: failed to decode process output'): |
| 70 | + ep = ExecProvider(self.input_ok) |
| 71 | + await ep.run() |
| 72 | + |
| 73 | + async def test_missing_output_keys(self): |
| 74 | + outputs = [ |
| 75 | + """ |
| 76 | + { |
| 77 | + "kind": "ExecCredential", |
| 78 | + "status": { |
| 79 | + "token": "dummy" |
| 80 | + } |
| 81 | + } |
| 82 | + """, """ |
| 83 | + { |
| 84 | + "apiVersion": "client.authentication.k8s.io/v1beta1", |
| 85 | + "status": { |
| 86 | + "token": "dummy" |
| 87 | + } |
| 88 | + } |
| 89 | + """, """ |
| 90 | + { |
| 91 | + "apiVersion": "client.authentication.k8s.io/v1beta1", |
| 92 | + "kind": "ExecCredential" |
| 93 | + } |
| 94 | + """ |
| 95 | + ] |
| 96 | + for output in outputs: |
| 97 | + self.process_mock.stdout.read.return_value = output |
| 98 | + with self.assertRaisesRegex(ConfigException, 'exec: malformed response. missing key'): |
| 99 | + ep = ExecProvider(self.input_ok) |
| 100 | + await ep.run() |
| 101 | + |
| 102 | + async def test_mismatched_api_version(self): |
| 103 | + wrong_api_version = 'client.authentication.k8s.io/v1' |
| 104 | + output = """ |
| 105 | + { |
| 106 | + "apiVersion": "%s", |
| 107 | + "kind": "ExecCredential", |
| 108 | + "status": { |
| 109 | + "token": "dummy" |
| 110 | + } |
| 111 | + } |
| 112 | + """ % wrong_api_version |
| 113 | + self.process_mock.stdout.read.return_value = output |
| 114 | + with self.assertRaisesRegex(ConfigException, 'exec: plugin api version {} does not match'.format(wrong_api_version)): |
| 115 | + ep = ExecProvider(self.input_ok) |
| 116 | + await ep.run() |
| 117 | + |
| 118 | + async def test_ok_01(self): |
| 119 | + ep = ExecProvider(self.input_ok) |
| 120 | + result = await ep.run() |
| 121 | + self.assertTrue(isinstance(result, dict)) |
| 122 | + self.assertTrue('token' in result) |
| 123 | + self.exec_mock.assert_called_once_with('aws-iam-authenticator', 'token', '-i', 'dummy', |
| 124 | + env=ANY, stderr=-1, stdin=None, stdout=-1) |
| 125 | + self.process_mock.stdout.read.assert_awaited_once() |
| 126 | + self.process_mock.stderr.read.assert_awaited_once() |
| 127 | + self.process_mock.wait.assert_awaited_once() |
| 128 | + |
| 129 | + async def test_ok_with_args(self): |
| 130 | + self.input_ok['args'] = ['--mock', '90'] |
| 131 | + ep = ExecProvider(self.input_ok) |
| 132 | + result = await ep.run() |
| 133 | + self.assertTrue(isinstance(result, dict)) |
| 134 | + self.assertTrue('token' in result) |
| 135 | + self.exec_mock.assert_called_once_with('aws-iam-authenticator', 'token', '-i', 'dummy', '--mock', '90', |
| 136 | + env=ANY, stderr=-1, stdin=None, stdout=-1) |
| 137 | + self.process_mock.stdout.read.assert_awaited_once() |
| 138 | + self.process_mock.stderr.read.assert_awaited_once() |
| 139 | + self.process_mock.wait.assert_awaited_once() |
| 140 | + |
| 141 | + async def test_ok_with_env(self): |
| 142 | + |
| 143 | + self.input_ok['env'] = [{'name': 'EXEC_PROVIDER_ENV_NAME', |
| 144 | + 'value': 'EXEC_PROVIDER_ENV_VALUE'}] |
| 145 | + |
| 146 | + ep = ExecProvider(self.input_ok) |
| 147 | + result = await ep.run() |
| 148 | + self.assertTrue(isinstance(result, dict)) |
| 149 | + self.assertTrue('token' in result) |
| 150 | + |
| 151 | + env_used = self.exec_mock.await_args_list[0][1]['env'] |
| 152 | + self.assertEqual(env_used['EXEC_PROVIDER_ENV_NAME'], 'EXEC_PROVIDER_ENV_VALUE') |
| 153 | + self.assertEqual(json.loads(env_used['KUBERNETES_EXEC_INFO']), {'apiVersion': |
| 154 | + 'client.authentication.k8s.io/v1beta1', |
| 155 | + 'kind': 'ExecCredential', |
| 156 | + 'spec': {'interactive': sys.stdout.isatty()}}) |
| 157 | + self.exec_mock.assert_called_once_with('aws-iam-authenticator', 'token', '-i', 'dummy', |
| 158 | + env=ANY, stderr=-1, stdin=None, stdout=-1) |
| 159 | + self.process_mock.stdout.read.assert_awaited_once() |
| 160 | + self.process_mock.stderr.read.assert_awaited_once() |
| 161 | + self.process_mock.wait.assert_awaited_once() |
0 commit comments