mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-25 21:44:00 -07:00
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.17) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.10) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.12) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.7) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
* Adjust all __future__ imports: for i in $(grep -REl "__future__.*absolute_import" plugins/ tests/); do sed -e 's/from __future__ import .*/from __future__ import annotations/g' -i $i; done * Remove all UTF-8 encoding specifications for Python source files: for i in $(grep -REl '[-][*]- coding: utf-8 -[*]-' plugins/ tests/); do sed -e '/^# -\*- coding: utf-8 -\*-/d' -i $i; done * Remove __metaclass__ = type: for i in $(grep -REl '__metaclass__ = type' plugins/ tests/); do sed -e '/^__metaclass__ = type/d' -i $i; done
52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
# Copyright (c) Ansible project
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import shutil
|
|
import tempfile
|
|
|
|
from ansible_collections.community.internal_test_tools.tests.unit.compat import unittest
|
|
from ansible_collections.community.general.plugins.module_utils.ocapi_utils import OcapiUtils
|
|
|
|
|
|
class TestOcapiUtils(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tempdir = tempfile.mkdtemp()
|
|
self.utils = OcapiUtils(creds={"user": "a_user", "pswd": "a_password"},
|
|
base_uri="fakeUri",
|
|
proxy_slot_number=None,
|
|
timeout=30,
|
|
module=None)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
def test_prepare_multipart_firmware_upload(self):
|
|
# Generate a binary file and save it
|
|
filename = "fake_firmware.bin"
|
|
filepath = os.path.join(self.tempdir, filename)
|
|
file_contents = b'\x00\x01\x02\x03\x04'
|
|
with open(filepath, 'wb+') as f:
|
|
f.write(file_contents)
|
|
|
|
# Call prepare_mutipart_firmware_upload
|
|
content_type, b_form_data = self.utils.prepare_multipart_firmware_upload(filepath)
|
|
|
|
# Check the returned content-type
|
|
content_type_pattern = r"multipart/form-data; boundary=(.*)"
|
|
m = re.match(content_type_pattern, content_type)
|
|
self.assertIsNotNone(m)
|
|
|
|
# Check the returned binary data
|
|
boundary = m.group(1)
|
|
expected_content_text = '--%s\r\n' % boundary
|
|
expected_content_text += 'Content-Disposition: form-data; name="FirmwareFile"; filename="%s"\r\n' % filename
|
|
expected_content_text += 'Content-Type: application/octet-stream\r\n\r\n'
|
|
expected_content_bytes = bytearray(expected_content_text, 'utf-8')
|
|
expected_content_bytes += file_contents
|
|
expected_content_bytes += bytearray('\r\n--%s--' % boundary, 'utf-8')
|
|
self.assertEqual(expected_content_bytes, b_form_data)
|