Cosmin Poieana ae15fee086 Normalize all metadata providers and plugins
Every meta data service should return bytes only for these capabilities:
    * get_content
    * get_user_data
While `_get_meta_data` and any other method derrived from it
(including public keys, certificates etc.) should return homogeneous
data types and only strings, not bytes.
The decoding procedure is handled at its roots, not in the plugins
and is done by only using `encoding.get_as_string` function.

Fixed bugs:
    * invalid certificate splitting under maas service which usually
      generated an extra invalid certificate (empty string + footer)
    * text operations on bytes in maas and cloudstack (split, comparing)
    * multiple types for certificates (now only strings)
    * not receiving bytes from opennebula service when using `get_user_data`
      (which leads to crash under later processing through io.BytesIO)
    * erroneous certificate parsing/stripping/replacing under x509 importing
      (footer remains, not all possible EOLs replaced as it should)

Also added new and refined actual misleading unittests.

Change-Id: I704c43f5f784458a881293d761a21e62aed85732
2015-06-21 19:25:20 +03:00

55 lines
2.0 KiB
Python

# Copyright 2014 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import tempfile
import unittest
from cloudbaseinit.tests import testutils
from cloudbaseinit.utils import encoding
class TestEncoding(unittest.TestCase):
def test_get_as_string(self):
content_map = [
("data", "data"),
(b"data", "data"),
("data".encode(), "data"),
("data".encode("utf-16"), None)
]
with testutils.LogSnatcher("cloudbaseinit.utils.encoding") as snatch:
for content, expect in content_map:
self.assertEqual(expect, encoding.get_as_string(content))
self.assertIn("couldn't decode", snatch.output[0].lower())
def test_write_file(self):
mode_map = [
(("w", "r"), "my test\ndata\n\n", False),
(("wb", "rb"), "\r\n".join((chr(x) for x in
(32, 125, 0))).encode(), False),
(("wb", "rb"), "my test\ndata\n\n", True)
]
with testutils.create_tempdir() as temp:
fd, path = tempfile.mkstemp(dir=temp)
os.close(fd)
for (write, read), data, encode in mode_map:
encoding.write_file(path, data, mode=write)
with open(path, read) as stream:
content = stream.read()
if encode:
data = data.encode()
self.assertEqual(data, content)