2018-08-09 19:46:54 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2019-01-29 18:01:20 +00:00
|
|
|
from operator import itemgetter
|
2018-06-10 16:47:49 +00:00
|
|
|
from typing import List, Tuple
|
2018-04-27 17:16:43 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2018-04-30 17:58:19 +00:00
|
|
|
import pytest
|
2018-10-13 12:53:46 +00:00
|
|
|
from boltons import urlutils
|
2018-09-07 10:35:32 +00:00
|
|
|
from teal.db import UniqueViolation
|
2018-09-21 08:43:15 +00:00
|
|
|
from teal.marshmallow import ValidationError
|
2018-08-03 16:15:08 +00:00
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.client import UserClient
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
2019-05-11 14:27:22 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Action, BenchmarkDataStorage, \
|
|
|
|
BenchmarkProcessor, EraseSectors, RateComputer, Snapshot, SnapshotRequest, VisualTest
|
2018-07-14 14:41:22 +00:00
|
|
|
from ereuse_devicehub.resources.device import models as m
|
2018-05-16 13:23:48 +00:00
|
|
|
from ereuse_devicehub.resources.device.exceptions import NeedsId
|
2019-05-02 15:49:15 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import SolidStateDrive
|
2018-11-17 19:21:11 +00:00
|
|
|
from ereuse_devicehub.resources.device.sync import MismatchBetweenProperties, \
|
|
|
|
MismatchBetweenTagsAndHid
|
2018-10-13 12:53:46 +00:00
|
|
|
from ereuse_devicehub.resources.enums import ComputerChassis, SnapshotSoftware
|
2018-05-30 10:49:40 +00:00
|
|
|
from ereuse_devicehub.resources.tag import Tag
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
|
|
|
from tests.conftest import file
|
|
|
|
|
|
|
|
|
2018-04-30 17:58:19 +00:00
|
|
|
@pytest.mark.usefixtures('auth_app_context')
|
|
|
|
def test_snapshot_model():
|
2018-04-27 17:16:43 +00:00
|
|
|
"""
|
|
|
|
Tests creating a Snapshot with its relationships ensuring correct
|
|
|
|
DB mapping.
|
|
|
|
"""
|
2018-07-14 14:41:22 +00:00
|
|
|
device = m.Desktop(serial_number='a1', chassis=ComputerChassis.Tower)
|
2018-04-30 17:58:19 +00:00
|
|
|
# noinspection PyArgumentList
|
|
|
|
snapshot = Snapshot(uuid=uuid4(),
|
2018-08-09 19:46:54 +00:00
|
|
|
end_time=datetime.now(timezone.utc),
|
2018-04-30 17:58:19 +00:00
|
|
|
version='1.0',
|
2018-06-10 16:47:49 +00:00
|
|
|
software=SnapshotSoftware.DesktopApp,
|
2018-04-30 17:58:19 +00:00
|
|
|
elapsed=timedelta(seconds=25))
|
|
|
|
snapshot.device = device
|
|
|
|
snapshot.request = SnapshotRequest(request={'foo': 'bar'})
|
|
|
|
db.session.add(snapshot)
|
|
|
|
db.session.commit()
|
2018-07-14 14:41:22 +00:00
|
|
|
device = m.Desktop.query.one() # type: m.Desktop
|
2019-05-11 14:27:22 +00:00
|
|
|
e1 = device.actions[0]
|
2018-06-10 16:47:49 +00:00
|
|
|
assert isinstance(e1, Snapshot), 'Creation order must be preserved: 1. snapshot, 2. WR'
|
2018-04-30 17:58:19 +00:00
|
|
|
db.session.delete(device)
|
|
|
|
db.session.commit()
|
|
|
|
assert Snapshot.query.one_or_none() is None
|
|
|
|
assert SnapshotRequest.query.one_or_none() is None
|
|
|
|
assert User.query.one() is not None
|
2018-07-14 14:41:22 +00:00
|
|
|
assert m.Desktop.query.one_or_none() is None
|
|
|
|
assert m.Device.query.one_or_none() is None
|
2018-10-13 12:53:46 +00:00
|
|
|
# Check properties
|
|
|
|
assert device.url == urlutils.URL('http://localhost/devices/1')
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_schema(app: Devicehub):
|
|
|
|
with app.app_context():
|
|
|
|
s = file('basic.snapshot')
|
|
|
|
app.resources['Snapshot'].schema.load(s)
|
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_post(user: UserClient):
|
2018-05-11 16:58:48 +00:00
|
|
|
"""
|
2018-06-16 10:41:12 +00:00
|
|
|
Tests the post snapshot endpoint (validation, etc), data correctness,
|
|
|
|
and relationship correctness.
|
2018-05-11 16:58:48 +00:00
|
|
|
"""
|
2019-05-11 14:27:22 +00:00
|
|
|
# TODO add all action_types to check, how to add correctly??
|
2018-06-16 10:41:12 +00:00
|
|
|
snapshot = snapshot_and_check(user, file('basic.snapshot'),
|
2019-05-11 14:27:22 +00:00
|
|
|
action_types=(
|
2019-04-30 00:02:23 +00:00
|
|
|
BenchmarkProcessor.t,
|
2019-05-08 17:12:05 +00:00
|
|
|
VisualTest.t,
|
2019-04-30 00:02:23 +00:00
|
|
|
RateComputer.t
|
2018-07-14 14:41:22 +00:00
|
|
|
),
|
2018-06-16 10:41:12 +00:00
|
|
|
perform_second_snapshot=False)
|
2018-05-11 16:58:48 +00:00
|
|
|
assert snapshot['software'] == 'Workbench'
|
|
|
|
assert snapshot['version'] == '11.0'
|
|
|
|
assert snapshot['uuid'] == 'f5efd26e-8754-46bc-87bf-fbccc39d60d9'
|
|
|
|
assert snapshot['elapsed'] == 4
|
|
|
|
assert snapshot['author']['id'] == user.user['id']
|
2019-05-11 14:27:22 +00:00
|
|
|
assert 'actions' not in snapshot['device']
|
2018-05-11 16:58:48 +00:00
|
|
|
assert 'author' not in snapshot['device']
|
2018-07-14 14:41:22 +00:00
|
|
|
device, _ = user.get(res=m.Device, item=snapshot['device']['id'])
|
2019-01-29 18:01:20 +00:00
|
|
|
key = itemgetter('serialNumber')
|
|
|
|
snapshot['components'].sort(key=key)
|
|
|
|
device['components'].sort(key=key)
|
2018-06-16 10:41:12 +00:00
|
|
|
assert snapshot['components'] == device['components']
|
|
|
|
|
2019-01-29 18:01:20 +00:00
|
|
|
assert {c['type'] for c in snapshot['components']} == {m.GraphicCard.t, m.RamModule.t,
|
2019-02-04 17:20:50 +00:00
|
|
|
m.Processor.t}
|
2019-05-11 14:27:22 +00:00
|
|
|
rate = next(e for e in snapshot['actions'] if e['type'] == RateComputer.t)
|
|
|
|
rate, _ = user.get(res=Action, item=rate['id'])
|
2018-06-16 10:41:12 +00:00
|
|
|
assert rate['device']['id'] == snapshot['device']['id']
|
2019-01-29 18:01:20 +00:00
|
|
|
rate['components'].sort(key=key)
|
2018-06-16 10:41:12 +00:00
|
|
|
assert rate['components'] == snapshot['components']
|
|
|
|
assert rate['snapshot']['id'] == snapshot['id']
|
2018-05-11 16:58:48 +00:00
|
|
|
|
|
|
|
|
2018-05-16 13:23:48 +00:00
|
|
|
def test_snapshot_component_add_remove(user: UserClient):
|
|
|
|
"""
|
|
|
|
Tests adding and removing components and some don't generate HID.
|
|
|
|
All computers generate HID.
|
|
|
|
"""
|
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
def get_actions_info(actions: List[dict]) -> tuple:
|
2018-05-13 13:13:12 +00:00
|
|
|
return tuple(
|
|
|
|
(
|
|
|
|
e['type'],
|
2018-06-15 13:31:03 +00:00
|
|
|
[c['serialNumber'] for c in e['components']]
|
2018-05-13 13:13:12 +00:00
|
|
|
)
|
2019-05-11 14:27:22 +00:00
|
|
|
for e in user.get_many(res=Action, resources=actions, key='id')
|
2018-05-13 13:13:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# We add the first device (2 times). The distribution of components
|
|
|
|
# (represented with their S/N) should be:
|
|
|
|
# PC 1: p1c1s, p1c2s, p1c3s. PC 2: ø
|
2018-05-11 16:58:48 +00:00
|
|
|
s1 = file('1-device-with-components.snapshot')
|
2019-05-08 17:12:05 +00:00
|
|
|
snapshot1 = snapshot_and_check(user,
|
|
|
|
s1,
|
2019-05-11 14:27:22 +00:00
|
|
|
action_types=(BenchmarkProcessor.t,),
|
2019-05-08 17:12:05 +00:00
|
|
|
perform_second_snapshot=False)
|
2018-05-13 13:13:12 +00:00
|
|
|
pc1_id = snapshot1['device']['id']
|
2018-07-14 14:41:22 +00:00
|
|
|
pc1, _ = user.get(res=m.Device, item=pc1_id)
|
2018-05-13 13:13:12 +00:00
|
|
|
# Parent contains components
|
|
|
|
assert tuple(c['serialNumber'] for c in pc1['components']) == ('p1c1s', 'p1c2s', 'p1c3s')
|
|
|
|
# Components contain parent
|
|
|
|
assert all(c['parent'] == pc1_id for c in pc1['components'])
|
2019-05-11 14:27:22 +00:00
|
|
|
# pc has two actions: Snapshot and the BenchmarkProcessor
|
|
|
|
# TODO change assert to len(pc1['actions']) == 2 cause we add BenchmarkProcessor action
|
|
|
|
assert len(pc1['actions']) == 2
|
|
|
|
# TODO pc1['actions'][0]['type'] == BenchmarkProcessor.t
|
|
|
|
assert pc1['actions'][1]['type'] == Snapshot.t
|
2018-05-13 13:13:12 +00:00
|
|
|
# p1c1s has Snapshot
|
2018-07-14 14:41:22 +00:00
|
|
|
p1c1s, _ = user.get(res=m.Device, item=pc1['components'][0]['id'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(e['type'] for e in p1c1s['actions']) == ('Snapshot',)
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
# We register a new device
|
|
|
|
# It has the processor of the first one (p1c2s)
|
|
|
|
# PC 1: p1c1s, p1c3s. PC 2: p2c1s, p1c2s
|
2019-05-11 14:27:22 +00:00
|
|
|
# Actions PC1: Snapshot, Remove. PC2: Snapshot
|
2018-05-11 16:58:48 +00:00
|
|
|
s2 = file('2-second-device-with-components-of-first.snapshot')
|
2019-05-11 14:27:22 +00:00
|
|
|
# num_actions = 2 = Remove, Add
|
|
|
|
snapshot2 = snapshot_and_check(user, s2, action_types=('Remove',),
|
2018-05-13 13:13:12 +00:00
|
|
|
perform_second_snapshot=False)
|
|
|
|
pc2_id = snapshot2['device']['id']
|
2018-07-14 14:41:22 +00:00
|
|
|
pc1, _ = user.get(res=m.Device, item=pc1_id)
|
|
|
|
pc2, _ = user.get(res=m.Device, item=pc2_id)
|
2018-05-13 13:13:12 +00:00
|
|
|
# PC1
|
|
|
|
assert tuple(c['serialNumber'] for c in pc1['components']) == ('p1c1s', 'p1c3s')
|
|
|
|
assert all(c['parent'] == pc1_id for c in pc1['components'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(e['type'] for e in pc1['actions']) == ('BenchmarkProcessor', 'Snapshot', 'Remove')
|
2018-05-13 13:13:12 +00:00
|
|
|
# PC2
|
|
|
|
assert tuple(c['serialNumber'] for c in pc2['components']) == ('p1c2s', 'p2c1s')
|
|
|
|
assert all(c['parent'] == pc2_id for c in pc2['components'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(e['type'] for e in pc2['actions']) == ('Snapshot',)
|
2018-05-13 13:13:12 +00:00
|
|
|
# p1c2s has two Snapshots, a Remove and an Add
|
2018-07-14 14:41:22 +00:00
|
|
|
p1c2s, _ = user.get(res=m.Device, item=pc2['components'][0]['id'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(e['type'] for e in p1c2s['actions']) == (
|
2019-05-10 16:00:38 +00:00
|
|
|
'BenchmarkProcessor', 'Snapshot', 'Snapshot', 'Remove'
|
|
|
|
)
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
# We register the first device again, but removing motherboard
|
|
|
|
# and moving processor from the second device to the first.
|
|
|
|
# We have created 1 Remove (from PC2's processor back to PC1)
|
|
|
|
# PC 0: p1c2s, p1c3s. PC 1: p2c1s
|
2018-05-11 16:58:48 +00:00
|
|
|
s3 = file('3-first-device-but-removing-motherboard-and-adding-processor-from-2.snapshot')
|
2018-05-16 13:23:48 +00:00
|
|
|
snapshot_and_check(user, s3, ('Remove',), perform_second_snapshot=False)
|
2018-07-14 14:41:22 +00:00
|
|
|
pc1, _ = user.get(res=m.Device, item=pc1_id)
|
|
|
|
pc2, _ = user.get(res=m.Device, item=pc2_id)
|
2018-05-13 13:13:12 +00:00
|
|
|
# PC1
|
|
|
|
assert {c['serialNumber'] for c in pc1['components']} == {'p1c2s', 'p1c3s'}
|
|
|
|
assert all(c['parent'] == pc1_id for c in pc1['components'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(get_actions_info(pc1['actions'])) == (
|
2018-05-13 13:13:12 +00:00
|
|
|
# id, type, components, snapshot
|
2019-04-30 00:02:23 +00:00
|
|
|
('BenchmarkProcessor', []), # first BenchmarkProcessor
|
2018-06-15 13:31:03 +00:00
|
|
|
('Snapshot', ['p1c1s', 'p1c2s', 'p1c3s']), # first Snapshot1
|
|
|
|
('Remove', ['p1c2s']), # Remove Processor in Snapshot2
|
|
|
|
('Snapshot', ['p1c2s', 'p1c3s']) # This Snapshot3
|
2018-05-13 13:13:12 +00:00
|
|
|
)
|
|
|
|
# PC2
|
|
|
|
assert tuple(c['serialNumber'] for c in pc2['components']) == ('p2c1s',)
|
|
|
|
assert all(c['parent'] == pc2_id for c in pc2['components'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(e['type'] for e in pc2['actions']) == (
|
2018-05-13 13:13:12 +00:00
|
|
|
'Snapshot', # Second Snapshot
|
|
|
|
'Remove' # the processor we added in 2.
|
|
|
|
)
|
|
|
|
# p1c2s has Snapshot, Remove and Add
|
2018-07-14 14:41:22 +00:00
|
|
|
p1c2s, _ = user.get(res=m.Device, item=pc1['components'][0]['id'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert tuple(get_actions_info(p1c2s['actions'])) == (
|
2019-04-30 00:02:23 +00:00
|
|
|
('BenchmarkProcessor', []), # first BenchmarkProcessor
|
2018-06-15 13:31:03 +00:00
|
|
|
('Snapshot', ['p1c1s', 'p1c2s', 'p1c3s']), # First Snapshot to PC1
|
|
|
|
('Snapshot', ['p1c2s', 'p2c1s']), # Second Snapshot to PC2
|
|
|
|
('Remove', ['p1c2s']), # ...which caused p1c2s to be removed form PC1
|
|
|
|
('Snapshot', ['p1c2s', 'p1c3s']), # The third Snapshot to PC1
|
|
|
|
('Remove', ['p1c2s']) # ...which caused p1c2 to be removed from PC2
|
2018-05-13 13:13:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# We register the first device but without the processor,
|
|
|
|
# adding a graphic card and adding a new component
|
2018-05-11 16:58:48 +00:00
|
|
|
s4 = file('4-first-device-but-removing-processor.snapshot-and-adding-graphic-card')
|
2018-05-13 13:13:12 +00:00
|
|
|
snapshot_and_check(user, s4, perform_second_snapshot=False)
|
2018-07-14 14:41:22 +00:00
|
|
|
pc1, _ = user.get(res=m.Device, item=pc1_id)
|
|
|
|
pc2, _ = user.get(res=m.Device, item=pc2_id)
|
2018-05-13 13:13:12 +00:00
|
|
|
# PC 0: p1c3s, p1c4s. PC1: p2c1s
|
|
|
|
assert {c['serialNumber'] for c in pc1['components']} == {'p1c3s', 'p1c4s'}
|
|
|
|
assert all(c['parent'] == pc1_id for c in pc1['components'])
|
|
|
|
# This last Snapshot only
|
2019-05-11 14:27:22 +00:00
|
|
|
assert get_actions_info(pc1['actions'])[-1] == ('Snapshot', ['p1c3s', 'p1c4s'])
|
2018-05-13 13:13:12 +00:00
|
|
|
# PC2
|
|
|
|
# We haven't changed PC2
|
|
|
|
assert tuple(c['serialNumber'] for c in pc2['components']) == ('p2c1s',)
|
|
|
|
assert all(c['parent'] == pc2_id for c in pc2['components'])
|
2018-05-16 13:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _test_snapshot_computer_no_hid(user: UserClient):
|
|
|
|
"""
|
|
|
|
Tests inserting a computer that doesn't generate a HID, neither
|
|
|
|
some of its components.
|
|
|
|
"""
|
|
|
|
# PC with 2 components. PC doesn't have HID and neither 1st component
|
|
|
|
s = file('basic.snapshot')
|
|
|
|
del s['device']['model']
|
|
|
|
del s['components'][0]['model']
|
|
|
|
user.post(s, res=Snapshot, status=NeedsId)
|
|
|
|
# The system tells us that it could not register the device because
|
|
|
|
# the device (computer) cannot generate a HID.
|
|
|
|
# In such case we need to specify an ``id`` so the system can
|
|
|
|
# recognize the device. The ``id`` can reference to the same
|
|
|
|
# device, it already existed in the DB, or to a placeholder,
|
|
|
|
# if the device is new in the DB.
|
2018-07-14 14:41:22 +00:00
|
|
|
user.post(s, res=m.Device)
|
2018-05-16 13:23:48 +00:00
|
|
|
s['device']['id'] = 1 # Assign the ID of the placeholder
|
|
|
|
user.post(s, res=Snapshot)
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_mismatch_id():
|
|
|
|
"""Tests uploading a device with an ID from another device."""
|
2018-07-14 14:41:22 +00:00
|
|
|
# Note that this won't happen as in this new version
|
2018-05-30 10:49:40 +00:00
|
|
|
# the ID is not used in the Snapshot process
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_tag_inner_tag(tag_id: str, user: UserClient, app: Devicehub):
|
|
|
|
"""Tests a posting Snapshot with a local tag."""
|
|
|
|
b = file('basic.snapshot')
|
|
|
|
b['device']['tags'] = [{'type': 'Tag', 'id': tag_id}]
|
2019-04-23 19:30:08 +00:00
|
|
|
|
2018-07-14 14:41:22 +00:00
|
|
|
snapshot_and_check(user, b,
|
2019-05-11 14:27:22 +00:00
|
|
|
action_types=(RateComputer.t, BenchmarkProcessor.t, VisualTest.t))
|
2018-05-30 10:49:40 +00:00
|
|
|
with app.app_context():
|
2018-09-11 20:51:13 +00:00
|
|
|
tag = Tag.query.one() # type: Tag
|
2018-05-30 10:49:40 +00:00
|
|
|
assert tag.device_id == 1, 'Tag should be linked to the first device'
|
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_tag_inner_tag_mismatch_between_tags_and_hid(user: UserClient, tag_id: str):
|
|
|
|
"""Ensures one device cannot 'steal' the tag from another one."""
|
|
|
|
pc1 = file('basic.snapshot')
|
|
|
|
pc1['device']['tags'] = [{'type': 'Tag', 'id': tag_id}]
|
|
|
|
user.post(pc1, res=Snapshot)
|
|
|
|
pc2 = file('1-device-with-components.snapshot')
|
|
|
|
user.post(pc2, res=Snapshot) # PC2 uploads well
|
|
|
|
pc2['device']['tags'] = [{'type': 'Tag', 'id': tag_id}] # Set tag from pc1 to pc2
|
|
|
|
user.post(pc2, res=Snapshot, status=MismatchBetweenTagsAndHid)
|
2018-06-10 16:47:49 +00:00
|
|
|
|
|
|
|
|
2018-07-19 19:25:06 +00:00
|
|
|
def test_snapshot_different_properties_same_tags(user: UserClient, tag_id: str):
|
2018-11-17 19:21:11 +00:00
|
|
|
"""Tests a snapshot performed to device 1 with tag A and then to
|
2018-07-19 19:25:06 +00:00
|
|
|
device 2 with tag B. Both don't have HID but are different type.
|
|
|
|
Devicehub must fail the Snapshot.
|
|
|
|
"""
|
|
|
|
# 1. Upload PC1 without hid but with tag
|
|
|
|
pc1 = file('basic.snapshot')
|
|
|
|
pc1['device']['tags'] = [{'type': 'Tag', 'id': tag_id}]
|
|
|
|
del pc1['device']['serialNumber']
|
|
|
|
user.post(pc1, res=Snapshot)
|
|
|
|
# 2. Upload PC2 without hid, a different characteristic than PC1, but with same tag
|
|
|
|
pc2 = file('basic.snapshot')
|
|
|
|
pc2['uuid'] = uuid4()
|
|
|
|
pc2['device']['tags'] = pc1['device']['tags']
|
2018-11-17 19:21:11 +00:00
|
|
|
# pc2 model is unknown but pc1 model is set = different property
|
|
|
|
del pc2['device']['model']
|
|
|
|
user.post(pc2, res=Snapshot, status=MismatchBetweenProperties)
|
2018-07-19 19:25:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_upload_twice_uuid_error(user: UserClient):
|
|
|
|
pc1 = file('basic.snapshot')
|
|
|
|
user.post(pc1, res=Snapshot)
|
2018-09-07 10:35:32 +00:00
|
|
|
user.post(pc1, res=Snapshot, status=UniqueViolation)
|
2018-07-19 19:25:06 +00:00
|
|
|
|
|
|
|
|
2018-09-21 08:43:15 +00:00
|
|
|
def test_snapshot_component_containing_components(user: UserClient):
|
|
|
|
"""There is no reason for components to have components and when
|
|
|
|
this happens it is always an error.
|
|
|
|
|
|
|
|
This test avoids this until an appropriate use-case is presented.
|
|
|
|
"""
|
|
|
|
s = file('basic.snapshot')
|
|
|
|
s['device'] = {
|
|
|
|
'type': 'Processor',
|
|
|
|
'serialNumber': 'foo',
|
|
|
|
'manufacturer': 'bar',
|
|
|
|
'model': 'baz'
|
|
|
|
}
|
|
|
|
user.post(s, res=Snapshot, status=ValidationError)
|
|
|
|
|
|
|
|
|
2019-05-10 16:00:38 +00:00
|
|
|
def test_erase_privacy_standards_endtime_sort(user: UserClient):
|
|
|
|
"""Tests a Snapshot with EraseSectors and the resulting privacy
|
|
|
|
properties.
|
|
|
|
|
|
|
|
This tests ensures that only the last erasure is picked up, as
|
|
|
|
erasures have always custom endTime value set.
|
2018-11-09 10:22:13 +00:00
|
|
|
"""
|
2018-06-10 16:47:49 +00:00
|
|
|
s = file('erase-sectors.snapshot')
|
2019-05-11 14:27:22 +00:00
|
|
|
assert s['components'][0]['actions'][0]['endTime'] == '2018-06-01T09:12:06+02:00'
|
|
|
|
snapshot = snapshot_and_check(user, s, action_types=(
|
2019-04-30 00:02:23 +00:00
|
|
|
EraseSectors.t,
|
|
|
|
BenchmarkDataStorage.t,
|
|
|
|
BenchmarkProcessor.t
|
2019-05-10 16:00:38 +00:00
|
|
|
), perform_second_snapshot=False)
|
|
|
|
# Perform a new snapshot changing the erasure time, as if
|
|
|
|
# it is a new erasure performed after.
|
2019-05-11 14:27:22 +00:00
|
|
|
erase = next(e for e in snapshot['actions'] if e['type'] == EraseSectors.t)
|
2019-05-10 16:00:38 +00:00
|
|
|
assert erase['endTime'] == '2018-06-01T07:12:06+00:00'
|
|
|
|
s['uuid'] = uuid4()
|
2019-05-11 14:27:22 +00:00
|
|
|
s['components'][0]['actions'][0]['endTime'] = '2018-06-01T07:14:00+00:00'
|
|
|
|
snapshot = snapshot_and_check(user, s, action_types=(
|
2019-05-10 16:00:38 +00:00
|
|
|
EraseSectors.t,
|
|
|
|
BenchmarkDataStorage.t,
|
|
|
|
BenchmarkProcessor.t
|
|
|
|
), perform_second_snapshot=False)
|
|
|
|
|
|
|
|
# The actual test
|
2019-05-02 15:49:15 +00:00
|
|
|
storage = next(e for e in snapshot['components'] if e['type'] == SolidStateDrive.t)
|
2019-05-11 14:27:22 +00:00
|
|
|
storage, _ = user.get(res=m.Device, item=storage['id']) # Let's get storage actions too
|
2019-05-10 16:00:38 +00:00
|
|
|
# order: endTime ascending
|
2019-05-11 14:27:22 +00:00
|
|
|
# erasure1/2 have an user defined time and others actions endTime = created
|
|
|
|
erasure1, erasure2, benchmark_hdd1, _snapshot1, benchmark_hdd2, _snapshot2 = storage['actions']
|
2018-06-16 10:41:12 +00:00
|
|
|
assert erasure1['type'] == erasure2['type'] == 'EraseSectors'
|
2019-05-10 16:00:38 +00:00
|
|
|
assert benchmark_hdd1['type'] == benchmark_hdd2['type'] == 'BenchmarkDataStorage'
|
2018-06-10 16:47:49 +00:00
|
|
|
assert _snapshot1['type'] == _snapshot2['type'] == 'Snapshot'
|
2019-05-11 14:27:22 +00:00
|
|
|
get_snapshot, _ = user.get(res=Action, item=_snapshot2['id'])
|
|
|
|
assert get_snapshot['actions'][0]['endTime'] == '2018-06-01T07:14:00+00:00'
|
2019-02-04 17:20:50 +00:00
|
|
|
assert snapshot == get_snapshot
|
2019-05-11 14:27:22 +00:00
|
|
|
erasure, _ = user.get(res=Action, item=erasure1['id'])
|
2018-06-10 16:47:49 +00:00
|
|
|
assert len(erasure['steps']) == 2
|
2019-02-04 17:20:50 +00:00
|
|
|
assert erasure['steps'][0]['startTime'] == '2018-06-01T06:15:00+00:00'
|
|
|
|
assert erasure['steps'][0]['endTime'] == '2018-06-01T07:16:00+00:00'
|
|
|
|
assert erasure['steps'][1]['startTime'] == '2018-06-01T06:16:00+00:00'
|
|
|
|
assert erasure['steps'][1]['endTime'] == '2018-06-01T07:17:00+00:00'
|
2018-06-10 16:47:49 +00:00
|
|
|
assert erasure['device']['id'] == storage['id']
|
2018-11-17 17:24:34 +00:00
|
|
|
step1, step2 = erasure['steps']
|
|
|
|
assert step1['type'] == 'StepZero'
|
|
|
|
assert step1['severity'] == 'Info'
|
|
|
|
assert 'num' not in step1
|
|
|
|
assert step2['type'] == 'StepRandom'
|
|
|
|
assert step2['severity'] == 'Info'
|
|
|
|
assert 'num' not in step2
|
|
|
|
assert ['HMG_IS5'] == erasure['standards']
|
2018-11-09 10:22:13 +00:00
|
|
|
assert storage['privacy']['type'] == 'EraseSectors'
|
|
|
|
pc, _ = user.get(res=m.Device, item=snapshot['device']['id'])
|
|
|
|
assert pc['privacy'] == [storage['privacy']]
|
2018-10-13 12:53:46 +00:00
|
|
|
|
|
|
|
# Let's try a second erasure with an error
|
|
|
|
s['uuid'] = uuid4()
|
2019-05-11 14:27:22 +00:00
|
|
|
s['components'][0]['actions'][0]['severity'] = 'Error'
|
2018-10-13 12:53:46 +00:00
|
|
|
snapshot, _ = user.post(s, res=Snapshot)
|
2018-11-09 10:22:13 +00:00
|
|
|
storage, _ = user.get(res=m.Device, item=storage['id'])
|
2019-01-02 16:52:43 +00:00
|
|
|
assert storage['hid'] == 'solidstatedrive-c1mr-c1ml-c1s'
|
2018-11-09 10:22:13 +00:00
|
|
|
assert storage['privacy']['type'] == 'EraseSectors'
|
|
|
|
pc, _ = user.get(res=m.Device, item=snapshot['device']['id'])
|
|
|
|
assert pc['privacy'] == [storage['privacy']]
|
2018-06-20 21:18:15 +00:00
|
|
|
|
|
|
|
|
2018-10-25 10:36:25 +00:00
|
|
|
def test_test_data_storage(user: UserClient):
|
|
|
|
"""Tests a Snapshot with EraseSectors."""
|
|
|
|
s = file('erase-sectors-2-hdd.snapshot')
|
|
|
|
snapshot, _ = user.post(res=Snapshot, data=s)
|
2018-10-27 07:01:13 +00:00
|
|
|
incidence_test = next(
|
2019-05-11 14:27:22 +00:00
|
|
|
ev for ev in snapshot['actions']
|
2018-10-27 07:01:13 +00:00
|
|
|
if ev.get('reallocatedSectorCount', None) == 15
|
|
|
|
)
|
2018-11-09 10:22:13 +00:00
|
|
|
assert incidence_test['severity'] == 'Error'
|
2018-10-25 10:36:25 +00:00
|
|
|
|
|
|
|
|
2019-04-23 19:30:08 +00:00
|
|
|
# TODO change to RateMonitor
|
|
|
|
@pytest.mark.xfail(reason='Not implemented yet, new rate is need it')
|
2018-06-20 21:18:15 +00:00
|
|
|
def test_snapshot_computer_monitor(user: UserClient):
|
|
|
|
s = file('computer-monitor.snapshot')
|
2019-05-11 14:27:22 +00:00
|
|
|
snapshot_and_check(user, s, action_types=('ManualRate',))
|
2018-10-13 12:53:46 +00:00
|
|
|
# todo check that ManualRate has generated an AggregateRate
|
2018-06-20 21:18:15 +00:00
|
|
|
|
|
|
|
|
2019-04-30 08:26:06 +00:00
|
|
|
@pytest.mark.xfail(reason='Not implemented yet, new rate is need it')
|
2018-11-17 18:22:41 +00:00
|
|
|
def test_snapshot_mobile_smartphone_imei_manual_rate(user: UserClient):
|
2018-10-04 08:59:31 +00:00
|
|
|
s = file('smartphone.snapshot')
|
2019-05-11 14:27:22 +00:00
|
|
|
snapshot = snapshot_and_check(user, s, action_types=('VisualTest',))
|
2018-11-17 18:22:41 +00:00
|
|
|
mobile, _ = user.get(res=m.Device, item=snapshot['device']['id'])
|
|
|
|
assert mobile['imei'] == 3568680000414120
|
|
|
|
# todo check that manual rate has been created
|
2018-10-04 08:59:31 +00:00
|
|
|
|
|
|
|
|
2018-10-13 12:53:46 +00:00
|
|
|
@pytest.mark.xfail(reason='Test not developed')
|
2018-06-20 21:18:15 +00:00
|
|
|
def test_snapshot_components_none():
|
|
|
|
"""
|
|
|
|
Tests that a snapshot without components does not
|
|
|
|
remove them from the computer.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-10-13 12:53:46 +00:00
|
|
|
@pytest.mark.xfail(reason='Test not developed')
|
2018-06-20 21:18:15 +00:00
|
|
|
def test_snapshot_components_empty():
|
|
|
|
"""
|
|
|
|
Tests that a snapshot whose components are an empty list remove
|
|
|
|
all its components.
|
|
|
|
"""
|
2018-07-14 14:41:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def assert_similar_device(device1: dict, device2: dict):
|
|
|
|
"""
|
|
|
|
Like :class:`ereuse_devicehub.resources.device.models.Device.
|
|
|
|
is_similar()` but adapted for testing.
|
|
|
|
"""
|
|
|
|
assert isinstance(device1, dict) and device1
|
|
|
|
assert isinstance(device2, dict) and device2
|
|
|
|
for key in 'serialNumber', 'model', 'manufacturer', 'type':
|
2018-09-30 10:29:33 +00:00
|
|
|
assert device1.get(key, '').lower() == device2.get(key, '').lower()
|
2018-07-14 14:41:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def assert_similar_components(components1: List[dict], components2: List[dict]):
|
|
|
|
"""
|
|
|
|
Asserts that the components in components1 are
|
|
|
|
similar than the components in components2.
|
|
|
|
"""
|
|
|
|
assert len(components1) == len(components2)
|
2019-01-29 18:01:20 +00:00
|
|
|
key = itemgetter('serialNumber')
|
|
|
|
components1.sort(key=key)
|
|
|
|
components2.sort(key=key)
|
2018-07-14 14:41:22 +00:00
|
|
|
for c1, c2 in zip(components1, components2):
|
|
|
|
assert_similar_device(c1, c2)
|
|
|
|
|
|
|
|
|
|
|
|
def snapshot_and_check(user: UserClient,
|
|
|
|
input_snapshot: dict,
|
2019-05-11 14:27:22 +00:00
|
|
|
action_types: Tuple[str, ...] = tuple(),
|
2018-07-14 14:41:22 +00:00
|
|
|
perform_second_snapshot=True) -> dict:
|
|
|
|
"""
|
|
|
|
Performs a Snapshot and then checks if the result is ok:
|
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
- There have been performed the types of actions and in the same
|
|
|
|
order as described in the passed-in ``action_types``.
|
2018-07-14 14:41:22 +00:00
|
|
|
- The inputted devices are similar to the resulted ones.
|
2019-05-11 14:27:22 +00:00
|
|
|
- There is no Remove action after the first Add.
|
2018-07-14 14:41:22 +00:00
|
|
|
- All input components are now inside the parent device.
|
|
|
|
|
|
|
|
Optionally, it can perform a second Snapshot which should
|
2019-05-11 14:27:22 +00:00
|
|
|
perform an exact result, except for the actions.
|
2018-07-14 14:41:22 +00:00
|
|
|
|
|
|
|
:return: The last resulting snapshot.
|
|
|
|
"""
|
|
|
|
snapshot, _ = user.post(res=Snapshot, data=input_snapshot)
|
2019-05-11 14:27:22 +00:00
|
|
|
assert all(e['type'] in action_types for e in snapshot['actions'])
|
|
|
|
assert len(snapshot['actions']) == len(action_types)
|
|
|
|
# Ensure there is no Remove action after the first Add
|
2018-07-14 14:41:22 +00:00
|
|
|
found_add = False
|
2019-05-11 14:27:22 +00:00
|
|
|
for action in snapshot['actions']:
|
|
|
|
if action['type'] == 'Add':
|
2018-07-14 14:41:22 +00:00
|
|
|
found_add = True
|
|
|
|
if found_add:
|
2019-05-11 14:27:22 +00:00
|
|
|
assert action['type'] != 'Receive', 'All Remove actions must be before the Add ones'
|
2018-07-14 14:41:22 +00:00
|
|
|
assert input_snapshot['device']
|
|
|
|
assert_similar_device(input_snapshot['device'], snapshot['device'])
|
|
|
|
if input_snapshot.get('components', None):
|
|
|
|
assert_similar_components(input_snapshot['components'], snapshot['components'])
|
|
|
|
assert all(c['parent'] == snapshot['device']['id'] for c in snapshot['components']), \
|
|
|
|
'Components must be in their parent'
|
|
|
|
if perform_second_snapshot:
|
|
|
|
if 'uuid' in input_snapshot:
|
|
|
|
input_snapshot['uuid'] = uuid4()
|
2019-05-11 14:27:22 +00:00
|
|
|
return snapshot_and_check(user, input_snapshot, action_types,
|
|
|
|
perform_second_snapshot=False)
|
2018-07-14 14:41:22 +00:00
|
|
|
else:
|
|
|
|
return snapshot
|
2018-10-23 13:37:37 +00:00
|
|
|
|
|
|
|
|
2019-04-23 19:30:08 +00:00
|
|
|
# TODO change to which Rate??
|
|
|
|
@pytest.mark.xfail(reason='Not implemented yet, new rate is need it')
|
2018-10-23 13:37:37 +00:00
|
|
|
def test_snapshot_keyboard(user: UserClient):
|
|
|
|
s = file('keyboard.snapshot')
|
2019-05-11 14:27:22 +00:00
|
|
|
snapshot = snapshot_and_check(user, s, action_types=('ManualRate',))
|
2018-10-23 13:37:37 +00:00
|
|
|
keyboard = snapshot['device']
|
|
|
|
assert keyboard['layout'] == 'ES'
|
2019-02-13 17:59:14 +00:00
|
|
|
|
|
|
|
|
2019-04-30 00:02:23 +00:00
|
|
|
@pytest.mark.xfail(reason='Debug and rewrite it')
|
2019-02-13 17:59:14 +00:00
|
|
|
def test_pc_rating_rate_none(user: UserClient):
|
|
|
|
"""Tests a Snapshot with EraseSectors."""
|
2019-04-30 00:02:23 +00:00
|
|
|
# TODO this snapshot have a benchmarkprocessor and a benchmarkprocessorsysbench
|
2019-02-13 17:59:14 +00:00
|
|
|
s = file('desktop-9644w8n-lenovo-0169622.snapshot')
|
|
|
|
snapshot, _ = user.post(res=Snapshot, data=s)
|
|
|
|
|
|
|
|
|
|
|
|
def test_pc_2(user: UserClient):
|
|
|
|
s = file('laptop-hp_255_g3_notebook-hewlett-packard-cnd52270fw.snapshot')
|
|
|
|
snapshot, _ = user.post(res=Snapshot, data=s)
|