Ejemplo n.º 1
0
def test_fingerprint_version():
    """Changing the FINGERPRINT_VERSION modify the fingerprint sha1."""
    import e3.anod.fingerprint

    f1 = Fingerprint()

    e3.anod.fingerprint.FINGERPRINT_VERSION = '0.0'
    f2 = Fingerprint()

    assert f1 != f2

    f3 = Fingerprint()

    assert f2 == f3
Ejemplo n.º 2
0
    def update_status(self,
                      kind,
                      status=ReturnValue.failure,
                      fingerprint=None):
        """Update meta information on disk.

        :param kind: the primitive name
        :type kind: str
        :param status: the last action return code
        :type status: ReturnValue
        :param fingerprint: the anod fingerprint
        :type fingerprint: Fingerprint
        """
        if fingerprint is None:
            fingerprint = Fingerprint()
        self.save_fingerprint(kind, fingerprint)
        self.save_last_status(kind, status)

        if kind == 'build':
            self.update_status('install', status, fingerprint)
Ejemplo n.º 3
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
Ejemplo n.º 4
0
    def load_fingerprint(self, kind, sha1_only=False):
        """Load the content of the fingerprint from disc.

        :param kind: the primitive name
        :type kind: str
        :param sha1_only: if true returns only the checksum of the
            fingerprint file
        :type sha1_only: bool

        :return: if sha1_only is True, returns a sha1 hexdigest else returns
            a Fingerprint object (the content of the fingerprint file or an
            empty Fingerprint when the fingerprint is invalid or does not
            exist)
        :rtype: str | Fingerprint
        """
        fingerprint_file = os.path.join(self.meta_dir,
                                        kind + '_fingerprint.yaml')
        if sha1_only:
            return sha1(fingerprint_file)

        result = None
        if os.path.exists(fingerprint_file):
            with open(fingerprint_file) as f:
                try:
                    result = yaml.load(f)
                except (ParserError, ReaderError) as e:
                    logger.warning(e)
                    # Invalid fingerprint
                    logger.warning('invalid fingerprint, discard it')
                    result = None

        if not isinstance(result, Fingerprint):
            # The fingerprint file did not exist or was invalid
            # returns an empty fingerprint
            result = Fingerprint()
        return result
Ejemplo n.º 5
0
def test_invalid_fingerprint():
    """A fingerprint value should be hashable."""
    with pytest.raises(AnodError):
        f1 = Fingerprint()
        f1.add('invalid', {})
Ejemplo n.º 6
0
def test_fingerprint():
    f1 = Fingerprint()
    f1.add('foo', '2')

    f2 = Fingerprint()
    f2.add('foo', '4')

    f12_diff = f2.compare_to(f1)
    assert f12_diff['new'] == set([])
    assert f12_diff['updated'] == {'foo'}
    assert f12_diff['obsolete'] == set([])

    f3 = Fingerprint()
    f3.add_file(__file__)

    f23_diff = f3.compare_to(f2)
    assert f23_diff['new'] == {'foo'}
    assert f23_diff['updated'] == set([])
    assert f23_diff['obsolete'] == {os.path.basename(__file__)}

    assert f1.sha1() != f2.sha1() != f3.sha1()

    assert Env().build.os.version in str(f3)

    f4 = Fingerprint()
    f4.add_file(__file__)
    assert f4 == f3

    f5 = Fingerprint()
    with pytest.raises(AnodError) as err:
        f5.add('f4', f4)
    assert 'f4 should be a string' in str(err.value)
Ejemplo n.º 7
0
def test_invalid_fingerprint():
    """A fingerprint value should be hashable."""
    with pytest.raises(AnodError):
        f1 = Fingerprint()
        f1.add('invalid', {})
Ejemplo n.º 8
0
def test_fingerprint():
    f1 = Fingerprint()
    f1.add('foo', '2')

    f2 = Fingerprint()
    f2.add('foo', '4')

    f12_diff = f2.compare_to(f1)
    assert f12_diff['new'] == set([])
    assert f12_diff['updated'] == {'foo'}
    assert f12_diff['obsolete'] == set([])

    f3 = Fingerprint()
    f3.add_file(__file__)

    f23_diff = f3.compare_to(f2)
    assert f23_diff['new'] == {'foo'}
    assert f23_diff['updated'] == set([])
    assert f23_diff['obsolete'] == {os.path.basename(__file__)}

    assert f1.sha1() != f2.sha1() != f3.sha1()

    assert Env().build.os.version in str(f3)

    f4 = Fingerprint()
    f4.add_file(__file__)
    assert f4 == f3

    f5 = Fingerprint()
    with pytest.raises(AnodError) as err:
        f5.add('f4', f4)
    assert 'f4 should be a string' in str(err.value)