Unit tests: improve fake/mock content to be more realistic
Give a bit more realistic content for the tests and centralize them in the factories with helper methods to retrieve compressed versions so we don't have bytestrings laying around everywhere. Change-Id: Ie5cd5bf1948778451d6a9d1c748943f2ea3a4f91
This commit is contained in:
parent
50f322932c
commit
c91ff18a8c
|
@ -1,6 +1,43 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import factory
|
||||
|
||||
from ara.api import models
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
# constants for things like compressed byte strings or objects
|
||||
FILE_CONTENTS = '---\n# Example file'
|
||||
HOST_FACTS = {
|
||||
'ansible_fqdn': 'hostname',
|
||||
'ansible_distribution': 'CentOS'
|
||||
}
|
||||
PLAYBOOK_PARAMETERS = {
|
||||
'ansible_version': '2.5.5',
|
||||
'inventory': '/etc/ansible/hosts'
|
||||
}
|
||||
RESULT_CONTENTS = {
|
||||
'results': [{
|
||||
'msg': 'something happened'
|
||||
}]
|
||||
}
|
||||
REPORT_DESCRIPTION = 'report description'
|
||||
TASK_TAGS = ['always', 'never']
|
||||
|
||||
|
||||
class FileContentFactory(factory.DjangoModelFactory):
|
||||
|
@ -8,8 +45,8 @@ class FileContentFactory(factory.DjangoModelFactory):
|
|||
model = models.FileContent
|
||||
django_get_or_create = ('sha1',)
|
||||
|
||||
sha1 = '1e58ead094c920fad631d2c22df34dc0314dab0c'
|
||||
contents = b'x\x9cSV(\xc8I\xacL\xca\xcf\xcf\x06\x00\x11\xbd\x03\xa5' # '# playbook'
|
||||
sha1 = utils.sha1(FILE_CONTENTS)
|
||||
contents = utils.compressed_str(FILE_CONTENTS)
|
||||
|
||||
|
||||
class FileFactory(factory.DjangoModelFactory):
|
||||
|
@ -25,7 +62,7 @@ class ReportFactory(factory.DjangoModelFactory):
|
|||
model = models.Report
|
||||
|
||||
name = 'test report'
|
||||
description = b'x\x9cKI-N.\xca,(\xc9\xcc\xcf\x03\x00\x1b\x87\x04\xa5' # 'description'
|
||||
description = utils.compressed_str(REPORT_DESCRIPTION)
|
||||
|
||||
|
||||
class PlaybookFactory(factory.DjangoModelFactory):
|
||||
|
@ -34,7 +71,7 @@ class PlaybookFactory(factory.DjangoModelFactory):
|
|||
|
||||
ansible_version = '2.4.0'
|
||||
completed = True
|
||||
parameters = b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo': 'bar'}
|
||||
parameters = utils.compressed_obj(PLAYBOOK_PARAMETERS)
|
||||
file = factory.SubFactory(FileFactory)
|
||||
|
||||
|
||||
|
@ -56,7 +93,7 @@ class TaskFactory(factory.DjangoModelFactory):
|
|||
action = 'setup'
|
||||
lineno = 2
|
||||
handler = False
|
||||
tags = b'x\x9c\x8bVJ\xcb\xcfW\xd2QPJJ,R\x8a\x05\x00\x1eH\x04\x06' # ['foo', 'bar']
|
||||
tags = utils.compressed_obj(TASK_TAGS)
|
||||
play = factory.SubFactory(PlayFactory)
|
||||
file = factory.SubFactory(FileFactory)
|
||||
|
||||
|
@ -65,7 +102,7 @@ class HostFactory(factory.DjangoModelFactory):
|
|||
class Meta:
|
||||
model = models.Host
|
||||
|
||||
facts = b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo', 'bar'}
|
||||
facts = utils.compressed_obj(HOST_FACTS)
|
||||
name = 'hostname'
|
||||
changed = 1
|
||||
failed = 0
|
||||
|
@ -79,7 +116,7 @@ class ResultFactory(factory.DjangoModelFactory):
|
|||
class Meta:
|
||||
model = models.Result
|
||||
|
||||
content = b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo', 'bar'}
|
||||
content = utils.compressed_obj(RESULT_CONTENTS)
|
||||
status = 'ok'
|
||||
host = factory.SubFactory(HostFactory)
|
||||
task = factory.SubFactory(TaskFactory)
|
||||
|
|
|
@ -1,7 +1,25 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models, serializers
|
||||
from ara.api.tests import factories
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
class HostTestCase(APITestCase):
|
||||
|
@ -25,18 +43,20 @@ class HostTestCase(APITestCase):
|
|||
play = factories.PlayFactory()
|
||||
serializer = serializers.HostSerializer(data={
|
||||
'name': 'compress',
|
||||
'facts': {'foo': 'bar'},
|
||||
'facts': factories.HOST_FACTS,
|
||||
'play': play.id,
|
||||
})
|
||||
serializer.is_valid()
|
||||
host = serializer.save()
|
||||
host.refresh_from_db()
|
||||
self.assertEqual(host.facts, b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T') # {'foo': 'bar'}
|
||||
self.assertEqual(host.facts, utils.compressed_obj(factories.HOST_FACTS))
|
||||
|
||||
def test_host_serializer_decompress_facts(self):
|
||||
host = factories.HostFactory(facts=b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T') # {'foo': 'bar'}
|
||||
host = factories.HostFactory(
|
||||
facts=utils.compressed_obj(factories.HOST_FACTS)
|
||||
)
|
||||
serializer = serializers.HostSerializer(instance=host)
|
||||
self.assertEqual(serializer.data['facts'], {'foo': 'bar'})
|
||||
self.assertEqual(serializer.data['facts'], factories.HOST_FACTS)
|
||||
|
||||
def test_get_no_hosts(self):
|
||||
request = self.client.get('/api/v1/hosts/')
|
||||
|
|
|
@ -1,7 +1,25 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models, serializers
|
||||
from ara.api.tests import factories
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
class FileTestCase(APITestCase):
|
||||
|
@ -14,19 +32,17 @@ class FileTestCase(APITestCase):
|
|||
def test_file_serializer(self):
|
||||
serializer = serializers.FileSerializer(data={
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
})
|
||||
serializer.is_valid()
|
||||
file = serializer.save()
|
||||
file.refresh_from_db()
|
||||
self.assertEqual(file.content.sha1, '1e58ead094c920fad631d2c22df34dc0314dab0c')
|
||||
self.assertEqual(file.content.sha1, utils.sha1(factories.FILE_CONTENTS))
|
||||
|
||||
def test_create_file_with_same_content_create_only_one_file_content(self):
|
||||
content = '# playbook'
|
||||
|
||||
serializer = serializers.FileSerializer(data={
|
||||
'path': '/path/1/playbook.yml',
|
||||
'content': content
|
||||
'content': factories.FILE_CONTENTS
|
||||
})
|
||||
serializer.is_valid()
|
||||
file_content = serializer.save()
|
||||
|
@ -34,7 +50,7 @@ class FileTestCase(APITestCase):
|
|||
|
||||
serializer2 = serializers.FileSerializer(data={
|
||||
'path': '/path/2/playbook.yml',
|
||||
'content': content
|
||||
'content': factories.FILE_CONTENTS
|
||||
})
|
||||
serializer2.is_valid()
|
||||
file_content = serializer2.save()
|
||||
|
@ -47,7 +63,7 @@ class FileTestCase(APITestCase):
|
|||
self.assertEqual(0, models.File.objects.count())
|
||||
request = self.client.post('/api/v1/files/', {
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
})
|
||||
self.assertEqual(201, request.status_code)
|
||||
self.assertEqual(1, models.File.objects.count())
|
||||
|
|
|
@ -1,3 +1,20 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api.tests import factories
|
||||
|
|
|
@ -1,3 +1,20 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import datetime
|
||||
from django.utils import timezone
|
||||
from rest_framework.test import APITestCase
|
||||
|
|
|
@ -1,9 +1,27 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import datetime
|
||||
from django.utils import timezone
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models, serializers
|
||||
from ara.api.tests import factories
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
class PlaybookTestCase(APITestCase):
|
||||
|
@ -16,7 +34,7 @@ class PlaybookTestCase(APITestCase):
|
|||
'ansible_version': '2.4.0',
|
||||
'file': {
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
}
|
||||
})
|
||||
serializer.is_valid()
|
||||
|
@ -29,24 +47,23 @@ class PlaybookTestCase(APITestCase):
|
|||
'ansible_version': '2.4.0',
|
||||
'file': {
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
},
|
||||
'parameters': {'foo': 'bar'}
|
||||
'parameters': factories.PLAYBOOK_PARAMETERS
|
||||
})
|
||||
serializer.is_valid()
|
||||
playbook = serializer.save()
|
||||
playbook.refresh_from_db()
|
||||
self.assertEqual(
|
||||
playbook.parameters,
|
||||
b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo': 'bar'}
|
||||
playbook.parameters, utils.compressed_obj(factories.PLAYBOOK_PARAMETERS)
|
||||
)
|
||||
|
||||
def test_playbook_serializer_decompress_parameters(self):
|
||||
playbook = factories.PlaybookFactory(
|
||||
parameters=b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo': 'bar'}
|
||||
parameters=utils.compressed_obj(factories.PLAYBOOK_PARAMETERS)
|
||||
)
|
||||
serializer = serializers.PlaybookSerializer(instance=playbook)
|
||||
self.assertEqual(serializer.data['parameters'], {'foo': 'bar'})
|
||||
self.assertEqual(serializer.data['parameters'], factories.PLAYBOOK_PARAMETERS)
|
||||
|
||||
def test_get_no_playbooks(self):
|
||||
request = self.client.get('/api/v1/playbooks/')
|
||||
|
@ -71,7 +88,7 @@ class PlaybookTestCase(APITestCase):
|
|||
"ansible_version": "2.4.0",
|
||||
'file': {
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
}
|
||||
})
|
||||
self.assertEqual(201, request.status_code)
|
||||
|
|
|
@ -1,4 +1,20 @@
|
|||
import time
|
||||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models
|
||||
|
@ -13,11 +29,11 @@ class PlaybookFileTestCase(APITestCase):
|
|||
'ansible_version': '2.4.0',
|
||||
'file': {
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
},
|
||||
'files': [{
|
||||
'path': '/path/host',
|
||||
'content': '# host'
|
||||
'content': 'Another file'
|
||||
}],
|
||||
})
|
||||
self.assertEqual(1, models.Playbook.objects.all().count())
|
||||
|
@ -28,7 +44,7 @@ class PlaybookFileTestCase(APITestCase):
|
|||
self.assertEqual(1, models.File.objects.all().count())
|
||||
self.client.post('/api/v1/playbooks/%s/files/' % playbook.id, {
|
||||
'path': '/path/playbook.yml',
|
||||
'content': '# playbook'
|
||||
'content': factories.FILE_CONTENTS
|
||||
})
|
||||
self.assertEqual(2, models.File.objects.all().count())
|
||||
self.assertEqual(1, models.FileContent.objects.all().count())
|
||||
|
@ -37,7 +53,7 @@ class PlaybookFileTestCase(APITestCase):
|
|||
playbook = factories.PlaybookFactory()
|
||||
number_playbooks = models.File.objects.all().count()
|
||||
number_file_contents = models.FileContent.objects.all().count()
|
||||
content = '# playbook %s' % time.time()
|
||||
content = '# %s' % factories.FILE_CONTENTS
|
||||
self.client.post('/api/v1/playbooks/%s/files/' % playbook.id, {
|
||||
'path': '/path/1/playbook.yml',
|
||||
'content': content
|
||||
|
|
|
@ -1,7 +1,25 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models, serializers
|
||||
from ara.api.tests import factories
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
class ReportTestCase(APITestCase):
|
||||
|
@ -21,25 +39,25 @@ class ReportTestCase(APITestCase):
|
|||
def test_report_serializer_compress_description(self):
|
||||
serializer = serializers.ReportSerializer(data={
|
||||
'name': 'compress',
|
||||
'description': 'description'
|
||||
'description': factories.REPORT_DESCRIPTION
|
||||
})
|
||||
serializer.is_valid()
|
||||
report = serializer.save()
|
||||
report.refresh_from_db()
|
||||
self.assertEqual(report.description, b'x\x9cKI-N.\xca,(\xc9\xcc\xcf\x03\x00\x1b\x87\x04\xa5') # 'description'
|
||||
self.assertEqual(report.description, utils.compressed_str(factories.REPORT_DESCRIPTION))
|
||||
|
||||
def test_report_serializer_decompress_parameters(self):
|
||||
def test_report_serializer_decompress_description(self):
|
||||
report = factories.ReportFactory(
|
||||
description=b'x\x9cKI-N.\xca,(\xc9\xcc\xcf\x03\x00\x1b\x87\x04\xa5' # 'description'
|
||||
description=utils.compressed_str(factories.REPORT_DESCRIPTION)
|
||||
)
|
||||
serializer = serializers.ReportSerializer(instance=report)
|
||||
self.assertEqual(serializer.data['description'], 'description')
|
||||
self.assertEqual(serializer.data['description'], factories.REPORT_DESCRIPTION)
|
||||
|
||||
def test_create_report(self):
|
||||
self.assertEqual(0, models.Report.objects.count())
|
||||
request = self.client.post('/api/v1/reports/', {
|
||||
'name': 'compress',
|
||||
'description': 'description'
|
||||
'description': factories.REPORT_DESCRIPTION
|
||||
})
|
||||
self.assertEqual(201, request.status_code)
|
||||
self.assertEqual(1, models.Report.objects.count())
|
||||
|
|
|
@ -1,7 +1,25 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models, serializers
|
||||
from ara.api.tests import factories
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
class ResultTestCase(APITestCase):
|
||||
|
@ -30,21 +48,17 @@ class ResultTestCase(APITestCase):
|
|||
serializer = serializers.ResultSerializer(data={
|
||||
'host': host.id,
|
||||
'task': task.id,
|
||||
'content': {'foo': 'bar'}
|
||||
'content': factories.RESULT_CONTENTS
|
||||
})
|
||||
serializer.is_valid()
|
||||
result = serializer.save()
|
||||
result.refresh_from_db()
|
||||
self.assertEqual(
|
||||
result.content, b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo': 'bar'}
|
||||
)
|
||||
self.assertEqual(result.content, utils.compressed_obj(factories.RESULT_CONTENTS))
|
||||
|
||||
def test_result_serializer_decompress_content(self):
|
||||
result = factories.ResultFactory(
|
||||
content=b'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' # {'foo': 'bar'}
|
||||
)
|
||||
result = factories.ResultFactory(content=utils.compressed_obj(factories.RESULT_CONTENTS))
|
||||
serializer = serializers.ResultSerializer(instance=result)
|
||||
self.assertEqual(serializer.data['content'], {'foo': 'bar'})
|
||||
self.assertEqual(serializer.data['content'], factories.RESULT_CONTENTS)
|
||||
|
||||
def test_get_no_results(self):
|
||||
request = self.client.get('/api/v1/results/')
|
||||
|
@ -71,7 +85,7 @@ class ResultTestCase(APITestCase):
|
|||
'status': 'ok',
|
||||
'host': host.id,
|
||||
'task': task.id,
|
||||
'content': {'foo': 'bar'}
|
||||
'content': factories.RESULT_CONTENTS
|
||||
})
|
||||
self.assertEqual(201, request.status_code)
|
||||
self.assertEqual(1, models.Result.objects.count())
|
||||
|
|
|
@ -1,9 +1,27 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import datetime
|
||||
from django.utils import timezone
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from ara.api import models, serializers
|
||||
from ara.api.tests import factories
|
||||
from ara.api.tests import utils
|
||||
|
||||
|
||||
class TaskTestCase(APITestCase):
|
||||
|
@ -39,17 +57,17 @@ class TaskTestCase(APITestCase):
|
|||
'handler': False,
|
||||
'play': play.id,
|
||||
'file': file.id,
|
||||
'tags': ['foo', 'bar']
|
||||
'tags': factories.TASK_TAGS
|
||||
})
|
||||
serializer.is_valid()
|
||||
task = serializer.save()
|
||||
task.refresh_from_db()
|
||||
self.assertEqual(task.tags, b'x\x9c\x8bVJ\xcb\xcfW\xd2QPJJ,R\x8a\x05\x00\x1eH\x04\x06') # ['foo', 'bar']
|
||||
self.assertEqual(task.tags, utils.compressed_obj(factories.TASK_TAGS))
|
||||
|
||||
def test_task_serializer_decompress_tags(self):
|
||||
task = factories.TaskFactory(tags=b'x\x9c\x8bVJ\xcb\xcfW\xd2QPJJ,R\x8a\x05\x00\x1eH\x04\x06') # ['foo', 'bar']
|
||||
task = factories.TaskFactory(tags=utils.compressed_obj(factories.TASK_TAGS))
|
||||
serializer = serializers.TaskSerializer(instance=task)
|
||||
self.assertEqual(serializer.data['tags'], ['foo', 'bar'])
|
||||
self.assertEqual(serializer.data['tags'], factories.TASK_TAGS)
|
||||
|
||||
def test_get_no_tasks(self):
|
||||
request = self.client.get('/api/v1/tasks/')
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
# Copyright (c) 2018 Red Hat, Inc.
|
||||
#
|
||||
# This file is part of ARA Records Ansible.
|
||||
#
|
||||
# ARA is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# ARA is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import zlib
|
||||
|
||||
|
||||
def compressed_obj(obj):
|
||||
"""
|
||||
Returns a zlib compressed representation of an object
|
||||
"""
|
||||
return zlib.compress(json.dumps(obj).encode('utf-8'))
|
||||
|
||||
|
||||
def compressed_str(obj):
|
||||
"""
|
||||
Returns a zlib compressed representation of a string
|
||||
"""
|
||||
return zlib.compress(obj.encode('utf-8'))
|
||||
|
||||
|
||||
def sha1(obj):
|
||||
"""
|
||||
Returns the sha1 of a compressed string or an object
|
||||
"""
|
||||
contents = zlib.compress(obj.encode('utf8'))
|
||||
return hashlib.sha1(contents).hexdigest()
|
Loading…
Reference in New Issue