Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Add tests for rule generation function
Browse files Browse the repository at this point in the history
- Add test_create_material_rules_with_zero_index
- Add test_create_material_rules_with_nonzero_index
- Add test_create_product_rules
  • Loading branch information
SolidifiedRay committed Oct 7, 2020
1 parent 2e4555a commit 3d36c15
Showing 1 changed file with 120 additions and 50 deletions.
170 changes: 120 additions & 50 deletions tests/test_create_layout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import unittest
import before_after_filesystem_snapshot
import create_layout
import in_toto.models.link

class Test_before_after_filesystem_snapshot(unittest.TestCase):

'''Check whether the output of before_after_filesystem_snapshot is as defined
'''Check whether the output of create_layout is as defined
by each test case.'''

before = {
Expand All @@ -13,6 +14,42 @@ class Test_before_after_filesystem_snapshot(unittest.TestCase):
'bar/bat/four.tgz': '6677889900112233'
}

first_step_link_str = {
'_type': 'link',
'name': 'first_step',
'byproducts': {},
'environment': {},
'materials': {},
'command': [],
'products': {
'one.tgz': {'sha256': '1234567890abcdef'},
'foo/two.tgz': {'sha256': '0000001111112222'},
'three.txt': {'sha256': '1111222233334444'},
'bar/bat/four.tgz': {'sha256': '6677889900112233'}
}
}

second_step_link_str = {
'_type': 'link',
'name': 'second_step',
'byproducts': {},
'environment': {},
'materials': {
'one.tgz': {'sha256': '1234567890abcdef'},
'foo/two.tgz': {'sha256': '0000001111112222'},
'three.txt': {'sha256': '1111222233334444'},
'bar/bat/four.tgz': {'sha256': '6677889900112233'}
},
'command': [],
'products': {
'five.txt': {'sha256': '5555555555555555'},
'one.tgz': {'sha256': '1234567890abcdef'},
'foo/two.tgz': {'sha256': 'ffffffffffffffff'},
'bar/bat/four.tgz': {'sha256': '6677889900112233'},
'baz/six.tgz': {'sha256': '6666666666666666'}
}
}

def test_same_filesystem_snapshot(self):

after = {
Expand All @@ -22,18 +59,18 @@ def test_same_filesystem_snapshot(self):
'bar/bat/four.tgz': '6677889900112233'
}

snapshot = before_after_filesystem_snapshot.snapshot(self.before, after)
self.assertEqual(snapshot, (['bar/bat/four.tgz', 'foo/two.tgz', 'one.tgz',
'three.txt'], [], [], []))
changes = create_layout.changes_between_snapshots(self.before, after)
self.assertEqual(changes, (['bar/bat/four.tgz', 'foo/two.tgz', 'one.tgz',
'three.txt'], [], [], []))


def test_removed_files_filesystem_snapshot(self):

after = {}

snapshot = before_after_filesystem_snapshot.snapshot(self.before, after)
self.assertEqual(snapshot, ([], [], [], ['bar/bat/four.tgz', 'foo/two.tgz',
'one.tgz', 'three.txt']))
changes = create_layout.changes_between_snapshots(self.before, after)
self.assertEqual(changes, ([], [], [], ['bar/bat/four.tgz', 'foo/two.tgz',
'one.tgz', 'three.txt']))


def test_new_filesystem_snapshot(self):
Expand All @@ -43,10 +80,10 @@ def test_new_filesystem_snapshot(self):
'foofoo/seven.txt': '1111222233334555'
}

snapshot = before_after_filesystem_snapshot.snapshot(self.before, after)
self.assertEqual(snapshot, ([], [], ['five.tgz', 'foo/bar/six.tgz',
'foofoo/seven.txt'], ['bar/bat/four.tgz', 'foo/two.tgz', 'one.tgz',
'three.txt']))
changes = create_layout.changes_between_snapshots(self.before, after)
self.assertEqual(changes, ([], [], ['five.tgz', 'foo/bar/six.tgz',
'foofoo/seven.txt'], ['bar/bat/four.tgz', 'foo/two.tgz', 'one.tgz',
'three.txt']))


def test_fully_modified_filesystem_snapshot(self):
Expand All @@ -58,9 +95,9 @@ def test_fully_modified_filesystem_snapshot(self):
'bar/bat/four.tgz': '6677889900123456'
}

snapshot = before_after_filesystem_snapshot.snapshot(self.before, after)
self.assertEqual(snapshot, ([], ['bar/bat/four.tgz', 'foo/two.tgz',
'one.tgz', 'three.txt'], [], []))
changes = create_layout.changes_between_snapshots(self.before, after)
self.assertEqual(changes, ([], ['bar/bat/four.tgz', 'foo/two.tgz',
'one.tgz', 'three.txt'], [], []))


def test_partially_modified_filesystem_snapshot(self):
Expand All @@ -73,41 +110,74 @@ def test_partially_modified_filesystem_snapshot(self):
'baz/six.tgz': '6666666666666666'
}

snapshot = before_after_filesystem_snapshot.snapshot(self.before, after)
self.assertEqual(snapshot, (['one.tgz'], ['bar/bat/four.tgz',
'foo/two.tgz'], ['baz/six.tgz', 'five.txt'], ['three.txt']))

def test_generate_artifact_rules(self):

after = {
'five.txt': '5555555555555555',
'one.tgz': '1234567890abcdef',
'foo/two.tgz': 'ffffffffffffffff',
'bar/bat/four.tgz': '6677889900112233',
'baz/six.tgz': '6666666666666666'
}

artifact_rules = {
'expected_materials': [
['ALLOW', 'bar/bat/four.tgz'],
['ALLOW', 'one.tgz'],
['ALLOW', 'foo/two.tgz'],
['DELETE', 'three.txt'],
['DISALLOW', '*']
],
'expected_products': [
['ALLOW', 'bar/bat/four.tgz'],
['ALLOW', 'one.tgz'],
['MODIFY', 'foo/two.tgz'],
['CREATE', 'baz/six.tgz'],
['CREATE', 'five.txt'],
['DISALLOW', '*']
]
}

snapshot = before_after_filesystem_snapshot.snapshot(self.before, after)
rules = before_after_filesystem_snapshot.generate_artifact_rules(snapshot)
self.assertDictEqual(artifact_rules, rules)
changes = create_layout.changes_between_snapshots(self.before, after)
self.assertEqual(changes, (['one.tgz'], ['bar/bat/four.tgz',
'foo/two.tgz'], ['baz/six.tgz', 'five.txt'], ['three.txt']))


def test_create_material_rules_of_initial_step(self):
# Zero index means that the current step is the initial step,
# so we need to ALLOW all the existing files instead of matching.
second_link = in_toto.models.link.Link.read(self.second_step_link_str)
links = [second_link]
result = True

expected_materials = [
['ALLOW', 'bar/bat/four.tgz'],
['ALLOW', 'one.tgz'],
['ALLOW', 'foo/two.tgz'],
['ALLOW', 'three.txt'],
['DELETE', 'three.txt'],
['DISALLOW', '*']
]

for rule in create_layout.create_material_rules(None, second_link):
if rule not in expected_materials:
result = False
break

self.assertTrue(result)

def test_create_material_rules_of_not_initial_step(self):
# Nonzero index means that the current step is not the initial step,
# so we need to MATCH materials with products of the previous step.
first_link = in_toto.models.link.Link.read(self.first_step_link_str)
second_link = in_toto.models.link.Link.read(self.second_step_link_str)
links = [first_link, second_link]
result = True

expected_materials = [
['MATCH', 'bar/bat/four.tgz', 'WITH', 'PRODUCTS', 'FROM', 'first_step'],
['MATCH', 'one.tgz', 'WITH', 'PRODUCTS', 'FROM', 'first_step'],
['MATCH', 'foo/two.tgz', 'WITH', 'PRODUCTS', 'FROM', 'first_step'],
['MATCH', 'three.txt', 'WITH', 'PRODUCTS', 'FROM', 'first_step'],
['DELETE', 'three.txt']
]

for rule in create_layout.create_material_rules(first_link, second_link):
if rule not in expected_materials:
result = False
break

self.assertTrue(result)

def test_create_product_rules(self):
# Given the changes of second step's materials and product,
# generate the product rules.
second_link = in_toto.models.link.Link.read(self.second_step_link_str)
expected_products = [
['ALLOW', 'bar/bat/four.tgz'],
['ALLOW', 'one.tgz'],
['MODIFY', 'foo/two.tgz'],
['CREATE', 'baz/six.tgz'],
['CREATE', 'five.txt'],
['DISALLOW', '*']
]

self.assertEqual(expected_products,
create_layout.create_product_rules(second_link))

# TODO: missing test for create_layout_from_ordered_links

if __name__ == '__main__':
unittest.main()

0 comments on commit 3d36c15

Please sign in to comment.