|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +import unittest |
| 19 | + |
| 20 | +import mock |
| 21 | + |
| 22 | +from .config_exception import ConfigException |
| 23 | +from .exec_provider import ExecProvider |
| 24 | +from .kube_config import ConfigNode |
| 25 | + |
| 26 | + |
| 27 | +class ExecProviderTest(unittest.TestCase): |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + self.input_ok = ConfigNode('test', { |
| 31 | + 'command': 'aws-iam-authenticator', |
| 32 | + 'args': ['token', '-i', 'dummy'], |
| 33 | + 'apiVersion': 'client.authentication.k8s.io/v1beta1', |
| 34 | + 'env': None |
| 35 | + }) |
| 36 | + self.output_ok = """ |
| 37 | + { |
| 38 | + "apiVersion": "client.authentication.k8s.io/v1beta1", |
| 39 | + "kind": "ExecCredential", |
| 40 | + "status": { |
| 41 | + "token": "dummy" |
| 42 | + } |
| 43 | + } |
| 44 | + """ |
| 45 | + |
| 46 | + def test_missing_input_keys(self): |
| 47 | + exec_configs = [ConfigNode('test1', {}), |
| 48 | + ConfigNode('test2', {'command': ''}), |
| 49 | + ConfigNode('test3', {'apiVersion': ''})] |
| 50 | + for exec_config in exec_configs: |
| 51 | + with self.assertRaises(ConfigException) as context: |
| 52 | + ExecProvider(exec_config) |
| 53 | + self.assertIn('exec: malformed request. missing key', |
| 54 | + context.exception.args[0]) |
| 55 | + |
| 56 | + @mock.patch('subprocess.Popen') |
| 57 | + def test_error_code_returned(self, mock): |
| 58 | + instance = mock.return_value |
| 59 | + instance.wait.return_value = 1 |
| 60 | + instance.communicate.return_value = ('', '') |
| 61 | + with self.assertRaises(ConfigException) as context: |
| 62 | + ep = ExecProvider(self.input_ok) |
| 63 | + ep.run() |
| 64 | + self.assertIn('exec: process returned %d' % |
| 65 | + instance.wait.return_value, context.exception.args[0]) |
| 66 | + |
| 67 | + @mock.patch('subprocess.Popen') |
| 68 | + def test_nonjson_output_returned(self, mock): |
| 69 | + instance = mock.return_value |
| 70 | + instance.wait.return_value = 0 |
| 71 | + instance.communicate.return_value = ('', '') |
| 72 | + with self.assertRaises(ConfigException) as context: |
| 73 | + ep = ExecProvider(self.input_ok) |
| 74 | + ep.run() |
| 75 | + self.assertIn('exec: failed to decode process output', |
| 76 | + context.exception.args[0]) |
| 77 | + |
| 78 | + @mock.patch('subprocess.Popen') |
| 79 | + def test_missing_output_keys(self, mock): |
| 80 | + instance = mock.return_value |
| 81 | + instance.wait.return_value = 0 |
| 82 | + outputs = [ |
| 83 | + """ |
| 84 | + { |
| 85 | + "kind": "ExecCredential", |
| 86 | + "status": { |
| 87 | + "token": "dummy" |
| 88 | + } |
| 89 | + } |
| 90 | + """, """ |
| 91 | + { |
| 92 | + "apiVersion": "client.authentication.k8s.io/v1beta1", |
| 93 | + "status": { |
| 94 | + "token": "dummy" |
| 95 | + } |
| 96 | + } |
| 97 | + """, """ |
| 98 | + { |
| 99 | + "apiVersion": "client.authentication.k8s.io/v1beta1", |
| 100 | + "kind": "ExecCredential" |
| 101 | + } |
| 102 | + """ |
| 103 | + ] |
| 104 | + for output in outputs: |
| 105 | + instance.communicate.return_value = (output, '') |
| 106 | + with self.assertRaises(ConfigException) as context: |
| 107 | + ep = ExecProvider(self.input_ok) |
| 108 | + ep.run() |
| 109 | + self.assertIn('exec: malformed response. missing key', |
| 110 | + context.exception.args[0]) |
| 111 | + |
| 112 | + @mock.patch('subprocess.Popen') |
| 113 | + def test_mismatched_api_version(self, mock): |
| 114 | + instance = mock.return_value |
| 115 | + instance.wait.return_value = 0 |
| 116 | + wrong_api_version = 'client.authentication.k8s.io/v1' |
| 117 | + output = """ |
| 118 | + { |
| 119 | + "apiVersion": "%s", |
| 120 | + "kind": "ExecCredential", |
| 121 | + "status": { |
| 122 | + "token": "dummy" |
| 123 | + } |
| 124 | + } |
| 125 | + """ % wrong_api_version |
| 126 | + instance.communicate.return_value = (output, '') |
| 127 | + with self.assertRaises(ConfigException) as context: |
| 128 | + ep = ExecProvider(self.input_ok) |
| 129 | + ep.run() |
| 130 | + self.assertIn( |
| 131 | + 'exec: plugin api version %s does not match' % |
| 132 | + wrong_api_version, |
| 133 | + context.exception.args[0]) |
| 134 | + |
| 135 | + @mock.patch('subprocess.Popen') |
| 136 | + def test_ok_01(self, mock): |
| 137 | + instance = mock.return_value |
| 138 | + instance.wait.return_value = 0 |
| 139 | + instance.communicate.return_value = (self.output_ok, '') |
| 140 | + ep = ExecProvider(self.input_ok) |
| 141 | + result = ep.run() |
| 142 | + self.assertTrue(isinstance(result, dict)) |
| 143 | + self.assertTrue('token' in result) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + unittest.main() |
0 commit comments