|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. 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, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +"""Basic integration test that runs update_config.py.""" |
| 19 | + |
| 20 | +from nose.plugins.attrib import attr |
| 21 | +from cuisine import file_write, run |
| 22 | +from fabric.api import hide |
| 23 | +import json |
| 24 | +import random |
| 25 | +import datetime |
| 26 | + |
| 27 | +try: |
| 28 | + from . import SystemVMTestCase, has_line, print_doc |
| 29 | +except (ImportError, ValueError): |
| 30 | + from systemvm import SystemVMTestCase, has_line, print_doc |
| 31 | + |
| 32 | + |
| 33 | +def deep_copy(obj): |
| 34 | + return json.loads(json.dumps(obj)) |
| 35 | + |
| 36 | + |
| 37 | +class UpdateConfigTestCase(SystemVMTestCase): |
| 38 | + basic_config = { |
| 39 | + "ip_address": [ |
| 40 | + { |
| 41 | + "public_ip": "10.0.2.102", |
| 42 | + "source_nat": True, |
| 43 | + "add": True, |
| 44 | + "one_to_one_nat": False, |
| 45 | + "first_i_p": False, |
| 46 | + "gateway": "10.0.2.1", |
| 47 | + "netmask": "255.255.255.0", |
| 48 | + "vif_mac_address": "06:cb:aa:00:00:03", |
| 49 | + "nic_dev_id": 1, |
| 50 | + "new_nic": False |
| 51 | + } |
| 52 | + ], |
| 53 | + "type": "ips" |
| 54 | + } |
| 55 | + |
| 56 | + def update_config(self, config): |
| 57 | + config_json = json.dumps(config, indent=2) |
| 58 | + print_doc('config.json', config_json) |
| 59 | + file_write('/etc/cloudstack/update_config_test.json', config_json) |
| 60 | + with hide("everything"): |
| 61 | + result = run("python /opt/cloud/bin/update_config.py update_config_test.json", |
| 62 | + timeout=600, warn_only=True) |
| 63 | + print result |
| 64 | + assert result.succeeded, 'update_config.py ran without errors' |
| 65 | + assert result.find("Convergence is achieved") >= 0, 'update_config.py should report convergence' |
| 66 | + |
| 67 | + def clear_log(self): |
| 68 | + tstamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
| 69 | + run("test -f /var/log/cloud.log && mv /var/log/cloud.log /var/log/cloud.log.%s || true" % tstamp) |
| 70 | + |
| 71 | + def setUp(self): |
| 72 | + super(UpdateConfigTestCase, self).setUp() |
| 73 | + self.clear_log() |
| 74 | + |
| 75 | + def check_no_errors(self): |
| 76 | + # todo config update should exit 1 on convergence errors! |
| 77 | + found, context = has_line('/var/log/cloud.log', 'cannot be configured') |
| 78 | + if found: |
| 79 | + print_doc('/var/log/cloud.log', context) |
| 80 | + assert not found, 'cloud.log should not contain "cannot be configured"' |
| 81 | + |
| 82 | + @attr(tags=["systemvm"], required_hardware="true") |
| 83 | + def test_basic_config(self): |
| 84 | + self.update_config(self.basic_config) |
| 85 | + self.check_no_errors() |
| 86 | + # should be able to run twice with same config |
| 87 | + self.clear_log() |
| 88 | + self.update_config(self.basic_config) |
| 89 | + self.check_no_errors() |
| 90 | + |
| 91 | + @attr(tags=["systemvm"], required_hardware="true") |
| 92 | + def test_various_random_ip_addresses(self): |
| 93 | + r = random.Random() |
| 94 | + r.seed() |
| 95 | + for i in range(0, 10): |
| 96 | + # todo need to know what kind of configurations are valid! |
| 97 | + config = deep_copy(self.basic_config) |
| 98 | + ip_address = deep_copy(self.basic_config["ip_address"][0]) |
| 99 | + ip_address["public_ip"] = "10.0.2.%d" % (i + 103,) |
| 100 | + ip_address["source_nat"] = r.choice((True, False)) |
| 101 | + ip_address["add"] = r.choice((True, False)) |
| 102 | + ip_address["one_to_one_nat"] = r.choice((True, False)) |
| 103 | + ip_address["first_i_p"] = r.choice((True, False)) |
| 104 | + ip_address["nic_dev_id"] = r.choice((0, 1, 2)) |
| 105 | + if ip_address["nic_dev_id"] > 0: |
| 106 | + ip_address["new_nic"] = True |
| 107 | + else: |
| 108 | + ip_address["new_nic"] = False |
| 109 | + config["ip_address"].append(ip_address) |
| 110 | + # runs a bunch of times adding an IP address each time |
| 111 | + self.update_config(config) |
| 112 | + self.check_no_errors() |
| 113 | + self.clear_log() |
| 114 | + # run again with just the basic config; this should remove the IP addresses? |
| 115 | + self.update_config(self.basic_config) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + import unittest |
| 120 | + unittest.main() |
0 commit comments