summaryrefslogtreecommitdiff
path: root/tests/unit/test_yamlutil.py
blob: c1292fda9312057c134fc891e50c063c689e25e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2021 Acme Gating, LLC
#
# 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.

from zuul.lib import yamlutil
from tests.base import BaseTestCase

import testtools


class TestYamlDumper(BaseTestCase):
    def test_load_normal_data(self):
        expected = {'foo': 'bar'}
        data = 'foo: bar\n'
        out = yamlutil.safe_load(data)
        self.assertEqual(out, expected)

        out = yamlutil.encrypted_load(data)
        self.assertEqual(out, expected)

    def test_load_encrypted_data(self):
        expected = {'foo': yamlutil.EncryptedPKCS1_OAEP('YmFy')}
        self.assertEqual(expected['foo'].ciphertext, b'bar')
        data = "foo: !encrypted/pkcs1-oaep YmFy\n"

        out = yamlutil.encrypted_load(data)
        self.assertEqual(out, expected)

        with testtools.ExpectedException(
                yamlutil.yaml.constructor.ConstructorError):
            out = yamlutil.safe_load(data)

    def test_dump_normal_data(self):
        data = {'foo': 'bar'}
        expected = 'foo: bar\n'
        out = yamlutil.safe_dump(data, default_flow_style=False)
        self.assertEqual(out, expected)

        out = yamlutil.encrypted_dump(data, default_flow_style=False)
        self.assertEqual(out, expected)

    def test_dump_encrypted_data(self):
        data = {'foo': yamlutil.EncryptedPKCS1_OAEP('YmFy')}
        self.assertEqual(data['foo'].ciphertext, b'bar')
        expected = "foo: !encrypted/pkcs1-oaep YmFy\n"

        out = yamlutil.encrypted_dump(data, default_flow_style=False)
        self.assertEqual(out, expected)

        with testtools.ExpectedException(
                yamlutil.yaml.representer.RepresenterError):
            out = yamlutil.safe_dump(data, default_flow_style=False)

    def test_ansible_dumper(self):
        data = {'foo': 'bar'}
        data = yamlutil.mark_strings_unsafe(data)
        expected = "foo: !unsafe 'bar'\n"
        yaml_out = yamlutil.ansible_unsafe_dump(data, default_flow_style=False)
        self.assertEqual(yaml_out, expected)

        data = {'foo': {'bar': 'baz'}, 'list': ['bar', 1, 3.0, True, None]}
        data = yamlutil.mark_strings_unsafe(data)
        expected = """\
foo:
  bar: !unsafe 'baz'
list:
- !unsafe 'bar'
- 1
- 3.0
- true
- null
"""
        yaml_out = yamlutil.ansible_unsafe_dump(data, default_flow_style=False)
        self.assertEqual(yaml_out, expected)

    def test_ansible_dumper_with_aliases(self):
        foo = {'bar': 'baz'}
        data = {'foo1': foo, 'foo2': foo}
        expected = """\
foo1: &id001
  bar: baz
foo2: *id001
"""
        yaml_out = yamlutil.ansible_unsafe_dump(data, default_flow_style=False)
        self.assertEqual(yaml_out, expected)

    def test_ansible_dumper_ignore_aliases(self):
        foo = {'bar': 'baz'}
        data = {'foo1': foo, 'foo2': foo}
        expected = """\
foo1:
  bar: baz
foo2:
  bar: baz
"""
        yaml_out = yamlutil.ansible_unsafe_dump(
            data,
            ignore_aliases=True,
            default_flow_style=False)
        self.assertEqual(yaml_out, expected)