Ejemplo n.º 1
0
    def test_commit_refresh_separate_stores(self):
        """Refresh-commit cycle from separate stores restores HostInfo."""
        info = host_info.HostInfo(labels=['labels'],
                                  attributes={'attrib': 'value'})
        store = file_store.FileStore(self._store_file)
        store.commit(info)

        read_store = file_store.FileStore(self._store_file)
        got = read_store.get()
        self.assertEqual(info, got)
Ejemplo n.º 2
0
 def test_commit_to_bad_path_raises(self):
     """Commit to a non-writable path raises StoreError."""
     # file_lock_timeout of 0 forces no retries (speeds up the test)
     store = file_store.FileStore('/rooty/non-writable/path/mostly',
                                  file_lock_timeout_seconds=0)
     with self.assertRaises(host_info.StoreError):
         store.commit(host_info.HostInfo())
Ejemplo n.º 3
0
 def test_commit_refresh_round_trip(self):
     """Refresh-commit cycle from a single store restores HostInfo."""
     info = host_info.HostInfo(labels=['labels'],
                               attributes={'attrib': 'value'})
     store = file_store.FileStore(self._store_file)
     store.commit(info)
     got = store.get(force_refresh=True)
     self.assertEqual(info, got)
Ejemplo n.º 4
0
 def test_refresh_from_non_existent_path_raises(self):
     """Refresh from a non-existent backing file raises StoreError."""
     # file_lock_timeout of 0 forces no retries (speeds up the test)
     store = file_store.FileStore(self._store_file,
                                  file_lock_timeout_seconds=0)
     store.commit(host_info.HostInfo())
     os.unlink(self._store_file)
     with self.assertRaises(host_info.StoreError):
         store.get(force_refresh=True)
Ejemplo n.º 5
0
    def test_refresh_from_unreadable_path_raises(self):
        """Refresh from an unreadable backing file raises StoreError."""
        # file_lock_timeout of 0 forces no retries (speeds up the test)
        store = file_store.FileStore(self._store_file,
                                     file_lock_timeout_seconds=0)
        store.commit(host_info.HostInfo())
        old_mode = os.stat(self._store_file).st_mode
        os.chmod(self._store_file, old_mode & ~stat.S_IRUSR)
        self.addCleanup(os.chmod, self._store_file, old_mode)

        with self.assertRaises(host_info.StoreError):
            store.get(force_refresh=True)
Ejemplo n.º 6
0
    def test_commit_blocks_for_locked_file(self):
        """Commit blocks when the backing file is locked.

        This is a greybox test. We artificially lock the backing file.
        This test intentionally uses a real locking.FileLock to ensure that
        locking API is used correctly.
        """
        # file_lock_timeout of 0 forces no retries (speeds up the test)
        store = file_store.FileStore(self._store_file,
                                     file_lock_timeout_seconds=0)
        file_lock = locking.FileLock(store._lock_path, locktype=locking.FLOCK)
        with file_lock.lock(), self.assertRaises(host_info.StoreError):
            store.commit(host_info.HostInfo())
Ejemplo n.º 7
0
    def test_commit_with_negative_timeout_clips(self, mock_file_lock_class):
        """Commit request with negative timeout is same as 0 timeout.

        @param mock_file_lock_class: A patched version of the locking.FileLock
                class.
        """
        mock_file_lock = mock_file_lock_class.return_value
        mock_file_lock.__enter__.return_value = mock_file_lock
        mock_file_lock.write_lock.side_effect = (
            locking.LockNotAcquiredError('Testing error'))

        store = file_store.FileStore(self._store_file,
                                     file_lock_timeout_seconds=-1)
        with self.assertRaises(host_info.StoreError):
            store.commit(host_info.HostInfo())
        self.assertEqual(1, mock_file_lock.write_lock.call_count)
Ejemplo n.º 8
0
    def test_commit_succeeds_after_lock_retry(self, mock_file_lock_class):
        """Tests that commit succeeds when locking requires retries.

        @param mock_file_lock_class: A patched version of the locking.FileLock
                class.
        """
        mock_file_lock = mock_file_lock_class.return_value
        mock_file_lock.__enter__.return_value = mock_file_lock
        mock_file_lock.write_lock.side_effect = [
            locking.LockNotAcquiredError('Testing error'),
            True,
        ]

        store = file_store.FileStore(self._store_file,
                                     file_lock_timeout_seconds=0.1)
        store.commit(host_info.HostInfo())
        self.assertEqual(2, mock_file_lock.write_lock.call_count)
Ejemplo n.º 9
0
def _parse_hostinfo_keyval(job_dir, hostname):
    """
    Parse host keyvals from hostinfo.

    @param job_dir: The string directory name of the associated job.
    @param hostname: The string hostname.

    @return A dictionary representing the host keyvals.

    """
    # The hostinfo path looks like:
    # host_info_store/chromeos6-row4-rack11-host6.store
    #
    # TODO(ayatane): We should pass hostinfo path explicitly.
    subdir = 'host_info_store'
    hostinfo_path = os.path.join(job_dir, subdir, hostname + '.store')
    store = file_store.FileStore(hostinfo_path)
    hostinfo = store.get()
    # TODO(ayatane): Investigate if urllib.quote is better.
    label_string = ','.join(
        label.replace(':', '%3A') for label in hostinfo.labels)
    return {'labels': label_string, 'platform': hostinfo.model}
Ejemplo n.º 10
0
def _read_store(path):
    """Read a HostInfo from a file at path."""
    store = file_store.FileStore(path)
    return store.get()
Ejemplo n.º 11
0
 def test_empty_store_raises_on_get(self):
     """Refresh from store before commit raises StoreError"""
     store = file_store.FileStore(self._store_file)
     with self.assertRaises(host_info.StoreError):
         store.get()
Ejemplo n.º 12
0
 def test_str(self):
     """Sanity tests the __str__ implementaiton"""
     store = file_store.FileStore('/foo/path')
     self.assertEqual(str(store), 'FileStore[/foo/path]')