Manage a pool of nodes for a distributed test infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_driver_aws.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2018 Red Hat
  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
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import fixtures
  16. import logging
  17. import os
  18. import tempfile
  19. from unittest.mock import patch
  20. import boto3
  21. from moto import mock_ec2
  22. import yaml
  23. from nodepool import tests
  24. from nodepool import zk
  25. class TestDriverAws(tests.DBTestCase):
  26. log = logging.getLogger("nodepool.TestDriverAws")
  27. @mock_ec2
  28. def test_ec2_machine(self):
  29. aws_id = 'AK000000000000000000'
  30. aws_key = '0123456789abcdef0123456789abcdef0123456789abcdef'
  31. self.useFixture(
  32. fixtures.EnvironmentVariable('AWS_ACCESS_KEY_ID', aws_id))
  33. self.useFixture(
  34. fixtures.EnvironmentVariable('AWS_SECRET_ACCESS_KEY', aws_key))
  35. ec2 = boto3.client('ec2', region_name='us-west-2')
  36. # TEST-NET-3
  37. vpc = ec2.create_vpc(CidrBlock='203.0.113.0/24')
  38. subnet = ec2.create_subnet(
  39. CidrBlock='203.0.113.128/25', VpcId=vpc['Vpc']['VpcId'])
  40. subnet_id = subnet['Subnet']['SubnetId']
  41. sg = ec2.create_security_group(
  42. GroupName='zuul-nodes', VpcId=vpc['Vpc']['VpcId'],
  43. Description='Zuul Nodes')
  44. sg_id = sg['GroupId']
  45. ec2_template = os.path.join(
  46. os.path.dirname(__file__), '..', 'fixtures', 'aws.yaml')
  47. raw_config = yaml.safe_load(open(ec2_template))
  48. raw_config['zookeeper-servers'][0] = {
  49. 'host': self.zookeeper_host,
  50. 'port': self.zookeeper_port,
  51. 'chroot': self.zookeeper_chroot,
  52. }
  53. raw_config['providers'][0]['pools'][0]['subnet-id'] = subnet_id
  54. raw_config['providers'][0]['pools'][0]['security-group-id'] = sg_id
  55. with tempfile.NamedTemporaryFile() as tf:
  56. tf.write(yaml.safe_dump(
  57. raw_config, default_flow_style=False).encode('utf-8'))
  58. tf.flush()
  59. configfile = self.setup_config(tf.name)
  60. pool = self.useNodepool(configfile, watermark_sleep=1)
  61. pool.start()
  62. req = zk.NodeRequest()
  63. req.state = zk.REQUESTED
  64. req.node_types.append('ubuntu1404')
  65. with patch('nodepool.driver.aws.handler.nodescan') as nodescan:
  66. nodescan.return_value = 'MOCK KEY'
  67. self.zk.storeNodeRequest(req)
  68. self.log.debug("Waiting for request %s", req.id)
  69. req = self.waitForNodeRequest(req)
  70. self.assertEqual(req.state, zk.FULFILLED)
  71. self.assertNotEqual(req.nodes, [])
  72. node = self.zk.getNode(req.nodes[0])
  73. self.assertEqual(node.allocated_to, req.id)
  74. self.assertEqual(node.state, zk.READY)
  75. self.assertIsNotNone(node.launcher)
  76. self.assertEqual(node.connection_type, 'ssh')
  77. nodescan.assert_called_with(
  78. node.interface_ip, port=22, timeout=180, gather_hostkeys=True)
  79. node.state = zk.DELETING
  80. self.zk.storeNode(node)
  81. self.waitForNodeDeletion(node)