Exemplo n.º 1
0
    def test_archive01(self):
        """
        StateArchiver should archive all history directories by

          1. Creating a .zip of a timestamped directory's files
          2. Saving the .zip to /var/lib/waagent/history/
          2. Deleting the timestamped directory
        """
        temp_files = [
            'Prod.0.manifest.xml',
            'Prod.0.agentsManifest',
            'Microsoft.Azure.Extensions.CustomScript.0.xml'
        ]

        for f in temp_files:
            self._write_file(f)

        flusher = StateFlusher(self.tmp_dir)
        flusher.flush(datetime.utcnow())

        test_subject = StateArchiver(self.tmp_dir)
        test_subject.archive()

        timestamp_zips = os.listdir(self.history_dir)
        self.assertEqual(1, len(timestamp_zips))

        zip_fn = timestamp_zips[0]          # 2000-01-01T00:00:00.000000.zip
        ts_s = os.path.splitext(zip_fn)[0]  # 2000-01-01T00:00:00.000000

        self.assertIsIso8601(ts_s)
        ts = self.parse_isoformat(ts_s)
        self.assertDateTimeCloseTo(ts, datetime.utcnow(), timedelta(seconds=30))

        zip_full = os.path.join(self.history_dir, zip_fn)
        self.assertZipContains(zip_full, temp_files)
Exemplo n.º 2
0
    def test_archive03(self):
        """
        All archives should be purged, both with the new naming (with incarnation number) and with the old naming.
        """
        start = datetime.now()
        timestamp1 = start + timedelta(seconds=5)
        timestamp2 = start + timedelta(seconds=10)

        dir_old = timestamp1.isoformat()
        dir_new = "{0}_incarnation_1".format(timestamp2.isoformat())

        archive_old = "{0}.zip".format(timestamp1.isoformat())
        archive_new = "{0}_incarnation_1.zip".format(timestamp2.isoformat())

        self._write_file(
            os.path.join("history", dir_old, "Prod.0.manifest.xml"))
        self._write_file(
            os.path.join("history", dir_new, "Prod.1.manifest.xml"))
        self._write_file(os.path.join("history", archive_old))
        self._write_file(os.path.join("history", archive_new))

        self.assertEqual(4, len(os.listdir(self.history_dir)),
                         "Not all entries were archived!")

        test_subject = StateArchiver(self.tmp_dir)
        with patch("azurelinuxagent.common.utils.archive._MAX_ARCHIVED_STATES",
                   0):
            test_subject.purge()

        archived_entries = os.listdir(self.history_dir)
        self.assertEqual(0, len(archived_entries),
                         "Not all entries were purged!")
Exemplo n.º 3
0
    def test_archive04(self):
        """
        The archive directory is created if it does not exist.

        This failure was caught when .purge() was called before .archive().
        """
        test_subject = StateArchiver(os.path.join(self.tmp_dir, 'does-not-exist'))
        test_subject.purge()
Exemplo n.º 4
0
 def __init__(self):
     self.osutil = get_osutil()
     self.dhcp_handler = get_dhcp_handler()
     self.protocol_util = get_protocol_util()
     self.stopped = True
     self.hostname = None
     self.dhcp_id_list = []
     self.server_thread = None
     self.dhcp_warning_enabled = True
     self.last_archive = None
     self.archiver = StateArchiver(conf.get_lib_dir())
Exemplo n.º 5
0
    def test_archive02(self):
        """
        StateArchiver should purge the MAX_ARCHIVED_STATES oldest files
        or directories.  The oldest timestamps are purged first.

        This test case creates a mixture of archive files and directories.
        It creates 5 more values than MAX_ARCHIVED_STATES to ensure that
        5 archives are cleaned up.  It asserts that the files and
        directories are properly deleted from the disk.
        """
        count = 6
        total = _MAX_ARCHIVED_STATES + count

        start = datetime.now()
        timestamps = []

        for i in range(0, total):
            timestamp = start + timedelta(seconds=i)
            timestamps.append(timestamp)

            if i % 2 == 0:
                filename = os.path.join(
                    'history',
                    "{0}_incarnation_0".format(timestamp.isoformat()),
                    'Prod.0.manifest.xml')
            else:
                filename = os.path.join(
                    'history',
                    "{0}_incarnation_0.zip".format(timestamp.isoformat()))

            self._write_file(filename)

        self.assertEqual(total, len(os.listdir(self.history_dir)))

        test_subject = StateArchiver(self.tmp_dir)
        test_subject.purge()

        archived_entries = os.listdir(self.history_dir)
        self.assertEqual(_MAX_ARCHIVED_STATES, len(archived_entries))

        archived_entries.sort()

        for i in range(0, _MAX_ARCHIVED_STATES):
            timestamp = timestamps[i + count].isoformat()
            if i % 2 == 0:
                filename = "{0}_incarnation_0".format(timestamp)
            else:
                filename = "{0}_incarnation_0.zip".format(timestamp)
            self.assertTrue(
                filename in archived_entries,
                "'{0}' is not in the list of unpurged entires".format(
                    filename))
Exemplo n.º 6
0
    def test_archive01(self):
        """
        StateArchiver should archive all history directories by

          1. Creating a .zip of a timestamped directory's files
          2. Saving the .zip to /var/lib/waagent/history/
          2. Deleting the timestamped directory
        """
        temp_files = [
            'GoalState.0.xml', 'Prod.0.manifest.xml', 'Prod.0.agentsManifest',
            'Microsoft.Azure.Extensions.CustomScript.0.xml'
        ]

        for current_file in temp_files:
            self._write_file(current_file)

        flusher = StateFlusher(self.tmp_dir)
        flusher.flush()

        test_subject = StateArchiver(self.tmp_dir)
        test_subject.archive()

        timestamp_zips = os.listdir(self.history_dir)
        self.assertEqual(1, len(timestamp_zips))

        zip_fn = timestamp_zips[
            0]  # 2000-01-01T00:00:00.000000_incarnation_N.zip
        timestamp_str, incarnation = self._parse_archive_name(zip_fn)

        self.assert_is_iso8601(timestamp_str)
        timestamp = self.parse_isoformat(timestamp_str)
        self.assert_datetime_close_to(timestamp, datetime.utcnow(),
                                      timedelta(seconds=30))
        self.assertEqual("0", incarnation)

        zip_full = os.path.join(self.history_dir, zip_fn)
        self.assertEqual(self.assert_zip_contains(zip_full, temp_files), None)
Exemplo n.º 7
0
    def __init__(self):
        self.osutil = get_osutil()
        self.dhcp_handler = get_dhcp_handler()
        self.protocol_util = None
        self._protocol = None
        self.stopped = True
        self.hostname = None
        self.dhcp_id_list = []
        self.server_thread = None
        self.dhcp_warning_enabled = True
        self.archiver = StateArchiver(conf.get_lib_dir())
        self._reset_firewall_rules = False

        self._periodic_operations = [
            PeriodicOperation("_remove_persistent_net_rules", self._remove_persistent_net_rules_period, conf.get_remove_persistent_net_rules_period()),
            PeriodicOperation("_monitor_dhcp_client_restart", self._monitor_dhcp_client_restart, conf.get_monitor_dhcp_client_restart_period()),
            PeriodicOperation("_cleanup_goal_state_history", self._cleanup_goal_state_history, conf.get_goal_state_history_cleanup_period())
        ]
        if conf.enable_firewall():
            self._periodic_operations.append(PeriodicOperation("_enable_firewall", self._enable_firewall, conf.get_enable_firewall_period()))
        if conf.get_root_device_scsi_timeout() is not None:
            self._periodic_operations.append(PeriodicOperation("_set_root_device_scsi_timeout", self._set_root_device_scsi_timeout, conf.get_root_device_scsi_timeout_period()))
        if conf.get_monitor_hostname():
            self._periodic_operations.append(PeriodicOperation("_monitor_hostname", self._monitor_hostname_changes, conf.get_monitor_hostname_period()))
Exemplo n.º 8
0
 def __init__(self):
     super(CleanupGoalStateHistory,
           self).__init__(conf.get_goal_state_history_cleanup_period())
     self.archiver = StateArchiver(conf.get_lib_dir())