blob: c9c1ebd51300ba10a7808754ae9369db453ff2ec [file] [log] [blame]
Clint Byrum50c69d82017-05-04 11:55:20 -07001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import os
14import subprocess
15
16from tests.base import ZuulTestCase
17from zuul.executor.server import SshAgent
18
19
20class TestSshAgent(ZuulTestCase):
21 tenant_config_file = 'config/single-tenant/main.yaml'
22
23 def test_ssh_agent(self):
24 # Need a private key to add
25 env_copy = dict(os.environ)
26 # DISPLAY and SSH_ASKPASS will cause interactive test runners to get a
27 # surprise
28 if 'DISPLAY' in env_copy:
29 del env_copy['DISPLAY']
30 if 'SSH_ASKPASS' in env_copy:
31 del env_copy['SSH_ASKPASS']
32
33 agent = SshAgent()
34 agent.start()
35 env_copy.update(agent.env)
36
37 pub_key_file = '{}.pub'.format(self.private_key_file)
38 pub_key = None
39 with open(pub_key_file) as pub_key_f:
40 pub_key = pub_key_f.read().split('== ')[0]
41
42 agent.add(self.private_key_file)
43 keys = agent.list()
44 self.assertEqual(1, len(keys))
45 self.assertEqual(keys[0].split('== ')[0], pub_key)
46 agent.remove(self.private_key_file)
47 keys = agent.list()
48 self.assertEqual([], keys)
49 agent.stop()
50 # Agent is now dead and thus this should fail
51 with open('/dev/null') as devnull:
52 self.assertRaises(subprocess.CalledProcessError,
53 subprocess.check_call,
54 ['ssh-add', self.private_key_file],
55 env=env_copy,
56 stderr=devnull)