コード例 #1
0
def test_unexpected_exception_during_fingerprint_load():
    """Check that we catch all exceptions raised during fingerprint load.
    """
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    fingerprint_filename = bs.fingerprint_filename(kind='build')

    # Scenario: The fingerprint file is actually not a file...

    mkdir(fingerprint_filename)
    fp = bs.load_fingerprint(kind='build')
    assert fp == Fingerprint()
    os.rmdir(fingerprint_filename)

    # Scenario: The fingerprint file is not readable (lack of permissions
    # in this case).
    #
    # Note that we do not have an easy way to remove read permission
    # to a file when on Windows, so we simply avoid that test when
    # on that platform. This test exercises the same code as in
    # the previous scenario, so this is not a big loss.

    if sys.platform != 'win32':
        ref_fp = Fingerprint()
        ref_fp.add('key1', 'val1')
        bs.save_fingerprint(kind='build', fingerprint=ref_fp)
        os.chmod(fingerprint_filename, 0)

        fp = bs.load_fingerprint(kind='build')
        assert fp == Fingerprint()
        os.chmod(fingerprint_filename, 0o600)
        os.remove(fingerprint_filename)
コード例 #2
0
def test_build_space_exists():
    """Test the BuildSpace.exists method."""
    bs_name = os.path.abspath("foo")
    bs = BuildSpace(bs_name)

    # First, verify the behavior when the buildspace directory
    # doesn't even exist.
    assert not os.path.exists(bs_name), bs_name
    assert bs.exists() is False

    # Next, create the directory, but without anything in it.
    # In particular, the marker file isn't present, so
    # is_buildspace should still return False for that directory.
    mkdir(bs_name)
    assert bs.exists() is False

    # Create the buildspace, and then verify that is_buildspace
    # then returns True.
    bs.create()
    assert bs.exists() is True

    # Verify that we also return False if one of the subdirectories
    # is missing. To do that, first verify that the subdirectory
    # we picked does exist, then delete it, before observing
    # whether BuildSpace.exists now return False or not.
    one_subdir = bs.subdir(bs.DIRS[0])
    assert os.path.isdir(one_subdir)
    rm(one_subdir, recursive=True)
    assert bs.exists() is False
コード例 #3
0
def test_reset_tmp_dir():
    """Check that the tmp_dir is reset when the build space is created.

    REQ-EC19.
    """
    bs = BuildSpace(root_dir=os.getcwd())
    marker = os.path.join(bs.subdir(name="tmp"), "deleteme")
    mkdir(bs.tmp_dir)
    touch(marker)
    assert os.path.exists(marker)
    bs.create()
    assert not os.path.exists(marker)
コード例 #4
0
def test_logs():
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    bs.set_logging()
    bs.end()
    assert os.path.isfile(bs.log_file)
    with open(bs.log_file) as f:
        assert 'anod primitive: build' in f.read()
コード例 #5
0
def test_live_logs():
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    bs.set_logging(stdout_logs=True)
    bs.log_stream.write('here')
    bs.log_stream.flush()
    bs.log_stream.write('there')
    time.sleep(0.2)  # let enough time to cover the tail_thread code
    bs.end()
    with open(bs.log_file) as f:
        assert 'here' in f.read()
コード例 #6
0
def test_status():
    """Check dump/restore of status."""
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    bs.save_last_status(kind='test', status=ReturnValue.notready)

    last_status, timestamp = bs.get_last_status(kind='test')
    assert last_status == ReturnValue.notready
    assert timestamp is not None
    assert (datetime.datetime.now() - timestamp).total_seconds() < 5
    last_status, timestamp = bs.get_last_status(kind='install')
    assert last_status == ReturnValue.missing
    assert timestamp is None
コード例 #7
0
    def get_build_space(self,
                        name: str,
                        platform: Optional[str] = None) -> BuildSpace:
        """Get build space.

        :param name: build space name
        :param platform: platform name (if None use the default platform)

        :return: A BuildSpace object
        """
        if platform is None:
            platform = Env().platform
        return BuildSpace(root_dir=os.path.join(self.root_dir, platform, name))
コード例 #8
0
def test_reset():
    """Verify that BuildSpace reset() delete expected content."""
    bs = BuildSpace(root_dir=os.getcwd())
    bs.create()
    for name in bs.dirs:
        touch(os.path.join(bs.subdir(name), "a"))

    # Reset delete all dirs except log and result by default
    bs.reset()
    for name in bs.dirs:
        exists = os.path.exists(os.path.join(bs.subdir(name), "a"))
        if name in ("log", "results"):
            assert exists
        else:
            assert not exists
コード例 #9
0
def test_reset():
    """Verify that BuildSpace reset() delete expected content."""
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    for name in bs.dirs:
        touch(os.path.join(bs.get_subdir(name), 'a'))

    # Reset delete all dirs except log and result by default
    bs.reset()
    for name in bs.dirs:
        exists = os.path.exists(os.path.join(bs.get_subdir(name), 'a'))
        if name in ('log', 'results'):
            assert exists
        else:
            assert not exists
コード例 #10
0
    def get_build_space(self, name, platform=None):
        """Get build space.

        :param name: build space name
        :type name: str
        :param platform: platform name (if None use the default platform)
        :type platform: str | None

        :return: A BuildSpace object
        :rtype: BuildSpace
        """
        if platform is None:
            platform = Env().platform
        return BuildSpace(root_dir=os.path.join(self.root_dir, platform, name))
コード例 #11
0
def test_dump_traceback():
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    try:
        raise ValueError('dumping traceback now')
    except ValueError:
        with pytest.raises(AnodError) as anod_err:
            bs.dump_traceback('foo', 'test')

    assert 'dumping traceback now' in str(anod_err)

    trace_file = os.path.join(bs.log_dir, 'traceback_test')
    assert os.path.exists(trace_file)
    with open(trace_file) as f:
        assert 'dumping traceback now' in f.read()
コード例 #12
0
def test_fingerprint():
    """Check dump/restore of fingerprint."""
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    fp = Fingerprint()
    fp.add('foo', 'bar')
    bs.save_fingerprint(kind='build', fingerprint=fp)

    # Check that the fingerprint stored on disk is equal to fp
    fp_checksum = bs.load_fingerprint(kind='build', sha1_only=True)
    assert isinstance(fp_checksum, str)

    loaded_fp = bs.load_fingerprint(kind='build')
    assert loaded_fp == fp

    # Now make sure that load_fingerprint does not fail when the bumped
    # value is corrupted
    with open(os.path.join(bs.meta_dir, 'build_fingerprint.yaml'), 'w') as f:
        f.write('[')

    assert bs.load_fingerprint(kind='build') == Fingerprint()

    # Try updating the fingerprint
    fp.add('key', 'value')
    bs.update_status(
        kind='build', status=ReturnValue.notready,
        fingerprint=fp)
    assert bs.get_last_status(kind='build')[0] == ReturnValue.notready
    loaded_fp = bs.load_fingerprint(kind='build')
    assert loaded_fp == fp

    # Updating build fingerprint also update install fingerprint
    loaded_fp = bs.load_fingerprint(kind='install')
    assert loaded_fp == fp

    # Now update install status

    bs.update_status(kind='install', status=ReturnValue.success)
    assert bs.get_last_status(kind='install')[0] == ReturnValue.success

    # build status should not be modified
    assert bs.get_last_status(kind='build')[0] == ReturnValue.notready
コード例 #13
0
def test_subdir():
    """Check error handling of get_subdir."""
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    with pytest.raises(ValueError):
        bs.get_subdir('foo')
コード例 #14
0
def test_subdir():
    """Check error handling of subdir."""
    bs = BuildSpace(root_dir=os.getcwd())
    with pytest.raises(ValueError):
        bs.subdir("foo")
コード例 #15
0
def test_fingerprint():
    """Check dump/restore of fingerprint."""
    bs = BuildSpace(root_dir=os.getcwd(), primitive='build')
    bs.create()
    fp = Fingerprint()
    fp.add('foo', 'bar')
    bs.save_fingerprint(kind='build', fingerprint=fp)

    loaded_fp = bs.load_fingerprint(kind='build')
    assert loaded_fp == fp

    # Now make sure that load_fingerprint does not fail when the bumped
    # value is corrupted
    with open(bs.fingerprint_filename('build'), 'w') as f:
        f.write('[')

    assert bs.load_fingerprint(kind='build') == Fingerprint()

    # Try updating the fingerprint
    fp.add('key', 'value')
    bs.update_status(kind='build', status=ReturnValue.notready, fingerprint=fp)
    assert bs.get_last_status(kind='build')[0] == ReturnValue.notready
    loaded_fp = bs.load_fingerprint(kind='build')
    assert loaded_fp == fp

    # Updating build fingerprint also update install fingerprint
    loaded_fp = bs.load_fingerprint(kind='install')
    assert loaded_fp == fp

    # Now update install status

    bs.update_status(kind='install', status=ReturnValue.success)
    assert bs.get_last_status(kind='install')[0] == ReturnValue.success

    # build status should not be modified
    assert bs.get_last_status(kind='build')[0] == ReturnValue.notready