|
1 | 1 | # Copyright 2023 Canonical Ltd. |
2 | 2 | # Licensed under the Apache V2, see LICENCE file for details. |
3 | 3 |
|
| 4 | +import os |
4 | 5 | from pathlib import Path |
5 | 6 | import tempfile |
6 | 7 | import unittest |
7 | 8 | import pytest |
8 | 9 |
|
9 | | -from juju.secrets import create_secret_data |
| 10 | +from juju.secrets import create_secret_data, read_secret_data |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class TestCreateSecretData(unittest.TestCase): |
@@ -57,3 +58,50 @@ def test_secret_key_from_file(self): |
57 | 58 | 'ICAgICAgICAgIC0tLS0tQkVHSU4gQ0VSVElGSUNBVEUtLS0tLQogICAgICAgICAgTUlJRllqQ0NBMHFnQXdJQkFnSVFLYVBORDlZZ2dJRzYrak9jZ21wazNEQU5CZ2txaGtpRzl3MEJBUXNGQURBegogICAgICAgICAgTVJ3d0dnWURWUVFLRXhOc2FXNTFlR052Ym5SaGFXNWxjbk11YjNKbk1STXdFUVlEVlFRRERBcDBhVzFBWld4MwogICAgICAgICAgLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQ==' |
58 | 59 | ), |
59 | 60 | }) |
| 61 | + |
| 62 | + |
| 63 | +class TestReadSecretData(unittest.TestCase): |
| 64 | + def test_yaml_file(self): |
| 65 | + data = """ |
| 66 | + hello: world |
| 67 | + goodbye#base64: world |
| 68 | + another-key: !!binary | |
| 69 | + R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 |
| 70 | + OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ |
| 71 | + +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC |
| 72 | + AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= |
| 73 | + """ |
| 74 | + |
| 75 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 76 | + file_path = os.path.join(temp_dir, "secret.yaml") |
| 77 | + |
| 78 | + with open(file_path, 'w') as file: |
| 79 | + file.write(data) |
| 80 | + |
| 81 | + attrs = read_secret_data(file_path) |
| 82 | + self.assertDictEqual(attrs, { |
| 83 | + "hello": "d29ybGQ=", |
| 84 | + "goodbye": "world", |
| 85 | + "another-key": "YiJHSUY4OWFceDBjXHgwMFx4MGNceDAwXHg4NFx4MDBceDAwXHhmZlx4ZmZceGY3XHhmNVx4ZjVceGVlXHhlOVx4ZTlceGU1ZmZmXHgwMFx4MDBceDAwXHhlN1x4ZTdceGU3Xl5eXHhmM1x4ZjNceGVkXHg4ZVx4OGVceDhlXHhlMFx4ZTBceGUwXHg5Zlx4OWZceDlmXHg5M1x4OTNceDkzXHhhN1x4YTdceGE3XHg5ZVx4OWVceDllaWlpY2NjXHhhM1x4YTNceGEzXHg4NFx4ODRceDg0XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5XHhmZlx4ZmVceGY5IVx4ZmVceDBlTWFkZSB3aXRoIEdJTVBceDAwLFx4MDBceDAwXHgwMFx4MDBceDBjXHgwMFx4MGNceDAwXHgwMFx4MDUsICBceDhlXHg4MTBceDllXHhlM0BceDE0XHhlOGlceDEwXHhjNFx4ZDFceDhhXHgwOFx4MWNceGNmXHg4ME0kelx4ZWZceGZmMFx4ODVwXHhiOFx4YjAxZlxyXHgxYlx4Y2VceDAxXHhjM1x4MDFceDFlXHgxMCcgXHg4MlxuXHgwMVx4MDA7Ig==" |
| 86 | + }) |
| 87 | + |
| 88 | + def test_json_file(self): |
| 89 | + data = ''' |
| 90 | + { |
| 91 | + "hello": "world", |
| 92 | + "goodbye#base64": "world" |
| 93 | + } |
| 94 | + ''' |
| 95 | + |
| 96 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 97 | + file_path = os.path.join(temp_dir, "secret.json") |
| 98 | + |
| 99 | + with open(file_path, 'w', encoding='utf-8') as file: |
| 100 | + file.write(data) |
| 101 | + |
| 102 | + attrs = read_secret_data(file_path) |
| 103 | + |
| 104 | + self.assertDictEqual(attrs, { |
| 105 | + "hello": "d29ybGQ=", |
| 106 | + "goodbye": "world" |
| 107 | + }) |
0 commit comments