def test_install_bootloader_skips_if_unchanged(self): contents = factory.getRandomString() dest = self.make_file(contents=contents) age_file(dest, 100) original_write_time = get_write_time(dest) loader = self.make_file(contents=contents) install_bootloader(loader, dest) self.assertThat(dest, FileContains(contents)) self.assertEqual(original_write_time, get_write_time(dest))
def test_leaves_install_image_untouched_if_unchanged(self): arch = self.get_arch() release = 'precise' archive = self.make_downloads(arch=arch, release=release) self.call_script(archive, self.tftproot, arch=arch, release=release) tftp_path = compose_tftp_path(self.tftproot, arch, release, 'install', 'linux') backdate(tftp_path) original_timestamp = get_write_time(tftp_path) self.call_script(archive, self.tftproot, arch=arch, release=release) self.assertEqual(original_timestamp, get_write_time(tftp_path))
def test_leaves_install_image_untouched_if_unchanged(self): arch = self.get_arch() release = 'precise' archive = self.make_downloads(arch=arch, release=release) self.call_script(archive, self.tftproot, arch=arch, release=release) tftp_path = compose_tftp_path( self.tftproot, arch, release, 'install', 'linux') backdate(tftp_path) original_timestamp = get_write_time(tftp_path) self.call_script(archive, self.tftproot, arch=arch, release=release) self.assertEqual(original_timestamp, get_write_time(tftp_path))
def test_check_lease_changes_returns_tuple_if_no_state_cached(self): self.set_lease_state() leases = factory.make_random_leases() leases_file = self.fake_leases_file(leases) self.assertEqual( (get_write_time(leases_file), leases), check_lease_changes())
def test_check_lease_changes_does_not_parse_unchanged_leases_file(self): parser = FakeMethod() leases_file = self.fake_leases_file() self.patch(leases_module, 'parse_leases_file', parser) self.set_lease_state(get_write_time(leases_file), {}) update_leases() self.assertSequenceEqual([], parser.calls)
def test_update_leases_does_nothing_without_lease_changes(self): fake_send_leases = FakeMethod() self.patch(leases_module, 'send_leases', fake_send_leases) leases = factory.make_random_leases() leases_file = self.fake_leases_file(leases) self.set_lease_state(get_write_time(leases_file), leases.copy()) self.assertEqual([], leases_module.send_leases.calls)
def test_check_lease_changes_returns_tuple_if_leases_dropped(self): self.set_lease_state( datetime.utcnow() - timedelta(seconds=10), factory.make_random_leases()) leases_file = self.fake_leases_file({}) self.assertEqual( (get_write_time(leases_file), {}), check_lease_changes())
def test_check_lease_changes_returns_tuple_if_lease_added(self): leases = factory.make_random_leases() self.set_lease_state(datetime.utcnow() - timedelta(seconds=10), leases.copy()) leases[factory.getRandomIPAddress()] = factory.getRandomMACAddress() leases_file = self.fake_leases_file(leases) self.assertEqual((get_write_time(leases_file), leases), check_lease_changes())
def test_upload_leases_processes_leases_unconditionally(self): leases = factory.make_random_leases() leases_file = self.fake_leases_file(leases) self.set_lease_state(get_write_time(leases_file), leases.copy()) self.patch(leases_module, 'send_leases', FakeMethod()) upload_leases() self.assertEqual([(leases, )], leases_module.send_leases.extract_args())
def test_upload_leases_processes_leases_unconditionally(self): leases = factory.make_random_leases() leases_file = self.fake_leases_file(leases) self.set_lease_state(get_write_time(leases_file), leases.copy()) self.patch(leases_module, 'send_leases', FakeMethod()) upload_leases() self.assertEqual( [(leases, )], leases_module.send_leases.extract_args())
def test_check_lease_changes_returns_tuple_if_lease_added(self): leases = factory.make_random_leases() self.set_lease_state( datetime.utcnow() - timedelta(seconds=10), leases.copy()) leases[factory.getRandomIPAddress()] = factory.getRandomMACAddress() leases_file = self.fake_leases_file(leases) self.assertEqual( (get_write_time(leases_file), leases), check_lease_changes())
def test_parse_leases_file_parses_leases(self): params = { 'ip': factory.getRandomIPAddress(), 'mac': factory.getRandomMACAddress(), } leases_file = self.write_leases_file("""\ lease %(ip)s { starts 5 2010/01/01 00:00:01; ends never; tstp 6 2010/01/02 05:00:00; tsfp 6 2010/01/02 05:00:00; binding state free; hardware ethernet %(mac)s; } """ % params) self.assertEqual( (get_write_time(leases_file), {params['ip']: params['mac']}), parse_leases_file())
def test_parse_leases_file_parses_leases(self): params = { 'ip': factory.getRandomIPAddress(), 'mac': factory.getRandomMACAddress(), } leases_file = self.write_leases_file("""\ lease %(ip)s { starts 5 2010/01/01 00:00:01; ends never; tstp 6 2010/01/02 05:00:00; tsfp 6 2010/01/02 05:00:00; binding state free; hardware ethernet %(mac)s; } """ % params) self.assertEqual((get_write_time(leases_file), { params['ip']: params['mac'] }), parse_leases_file())
def fake_leases_file(self, leases=None, age=None): """Fake the presence of a leases file. This does not go through the leases parser. It patches out the leases parser with a fake that returns the lease data you pass in here. :param leases: Dict of leases (mapping IP addresses to MACs). :param age: Number of seconds since last modification to leases file. :return: Path/name of temporary file. """ if leases is None: leases = {} leases = leases.copy() leases_file = self.make_file() if age is not None: age_file(leases_file, age) timestamp = get_write_time(leases_file) self.redirect_parser(leases_file) self.patch( leases_module, 'parse_leases_file', lambda: (timestamp, leases)) return leases_file
def fake_leases_file(self, leases=None, age=None): """Fake the presence of a leases file. This does not go through the leases parser. It patches out the leases parser with a fake that returns the lease data you pass in here. :param leases: Dict of leases (mapping IP addresses to MACs). :param age: Number of seconds since last modification to leases file. :return: Path/name of temporary file. """ if leases is None: leases = {} leases = leases.copy() leases_file = self.make_file() if age is not None: age_file(leases_file, age) timestamp = get_write_time(leases_file) self.redirect_parser(leases_file) self.patch(leases_module, 'parse_leases_file', lambda: (timestamp, leases)) return leases_file
def test_check_lease_changes_returns_None_if_no_change(self): leases = factory.make_random_leases() leases_file = self.fake_leases_file(leases) self.set_lease_state(get_write_time(leases_file), leases.copy()) self.assertIsNone(check_lease_changes())
def test_check_lease_changes_returns_tuple_if_no_state_cached(self): self.set_lease_state() leases = factory.make_random_leases() leases_file = self.fake_leases_file(leases) self.assertEqual((get_write_time(leases_file), leases), check_lease_changes())
def test_check_lease_changes_returns_tuple_if_leases_dropped(self): self.set_lease_state(datetime.utcnow() - timedelta(seconds=10), factory.make_random_leases()) leases_file = self.fake_leases_file({}) self.assertEqual((get_write_time(leases_file), {}), check_lease_changes())