File: test_charm.py

package info (click to toggle)
golang-github-canonical-candid 1.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,016 kB
  • sloc: python: 1,903; sh: 235; makefile: 81
file content (353 lines) | stat: -rw-r--r-- 12,873 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Copyright 2022 Canonical Ltd
# See LICENSE file for licensing details.
#
# Learn more about testing at: https://juju.is/docs/sdk/testing

import os
import pathlib
import shutil
import tempfile
import textwrap
import unittest
from unittest.mock import MagicMock, patch

from charm import CandidOperatorCharm
from ops.testing import Harness

MINIMAL_CONFIG = {
    "admin-agent-public-key": "test-admin-public-key",
    "api-macaroon-timeout": "10m",
    "discharge-macaroon-timeout": "20m",
    "discharge-token-timeout": "30m",
    "identity-providers": """\
- type: static
  name: static
  description: Default identity provider
  require-mfa: true
  users:
    user1:
      name: User One
      email: user1@example.com
      password: password1
      groups:
       - group1
       - group3""",
    "location": "https://test-location",
    "private-key": "test-private-key",
    "public-key": "test-public-key",
    "rendezvous-timeout": "5m",
}


class test_process:
    def wait_output(self):
        return (
            """{
            "private": "generated-private-key",
            "public": "generated-public-key"
        }""",
            """{'a'}""",
        )


class TestCharm(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None
        self.harness = Harness(CandidOperatorCharm)
        self.addCleanup(self.harness.cleanup)
        self.harness.disable_hooks()
        self.harness.add_oci_resource("candid-image")
        self.harness.begin()

        rel_id = self.harness.add_relation("ingress", "nginx-ingress")
        self.harness.add_relation_unit(rel_id, "nginx-ingress/0")

        self.tempdir = tempfile.TemporaryDirectory()
        self.addCleanup(self.tempdir.cleanup)
        shutil.copytree(
            os.path.join(self.harness.charm.charm_dir, "templates"),
            os.path.join(self.tempdir.name, "templates"),
        )
        self.harness.charm.framework.charm_dir = pathlib.Path(
            self.tempdir.name
        )

        self.harness.container_pebble_ready("candid")

    def test_on_pebble_ready(self):
        self.harness.update_config(MINIMAL_CONFIG)

        self.harness.update_config(
            {"private-key": "new-private-key", "public-key": "new-public-key"}
        )

        container = self.harness.model.unit.get_container("candid")
        # Emit the pebble-ready event for jimm
        self.harness.charm.on.candid_pebble_ready.emit(container)

        # Check the that the plan was updated
        plan = self.harness.get_container_pebble_plan("candid")
        self.assertEqual(
            plan.to_dict(),
            {
                "services": {
                    "candid": {
                        "summary": "Candid Identity Service",
                        "startup": "disabled",
                        "override": "merge",
                        "command": "/root/candidsrv /root/config.yaml",
                        "environment": {
                            "ADMIN_AGENT_PUBLIC_KEY": "test-admin-public-key",
                            "API_MACAROON_TIMEOUT": "10m",
                            "DISCHARGE_MACAROON_TIMEOUT": "20m",
                            "DISCHARGE_TOKEN_TIMEOUT": "30m",
                            "IDENTITY_PROVIDERS": """\
- type: static
  name: static
  description: Default identity provider
  require-mfa: true
  users:
    user1:
      name: User One
      email: user1@example.com
      password: password1
      groups:
       - group1
       - group3""",
                            "LOCATION": "https://test-location",
                            "LOGGING_CONFIG": "INFO",
                            "PRIVATE_KEY": "new-private-key",
                            "PUBLIC_KEY": "new-public-key",
                            "RENDEZVOUS_TIMEOUT": "5m",
                        },
                    }
                }
            },
        )

        config = container.pull("/root/config.yaml")
        self.assertEqual(
            textwrap.dedent(
                """\
                            access-log: /root/logs/access.log
                            auth-username: admin
                            listen-address: :8081
                            max-mgo-sessions: 300
                            request-timeout: 2s
                            resource-path: /root/www
                            storage:
                                type: memory
                            location: https://test-location
                            private-key: new-private-key
                            public-key: new-public-key
                            private-addr: localhost
                            admin-agent-private-key: test-admin-public-key
                            identity-providers:
                            - type: static
                              name: static
                              description: Default identity provider
                              require-mfa: true
                              users:
                                user1:
                                  name: User One
                                  email: user1@example.com
                                  password: password1
                                  groups:
                                   - group1
                                   - group3"""
            ),
            config.read(),
        )

    def test_on_config_changed(self):
        self.harness.update_config(MINIMAL_CONFIG)

        container = self.harness.model.unit.get_container("candid")
        self.harness.charm.on.candid_pebble_ready.emit(container)

        # Check the that the plan was updated
        plan = self.harness.get_container_pebble_plan("candid")
        self.assertEqual(
            plan.to_dict(),
            {
                "services": {
                    "candid": {
                        "summary": "Candid Identity Service",
                        "startup": "disabled",
                        "override": "merge",
                        "command": "/root/candidsrv /root/config.yaml",
                        "environment": {
                            "ADMIN_AGENT_PUBLIC_KEY": "test-admin-public-key",
                            "API_MACAROON_TIMEOUT": "10m",
                            "DISCHARGE_MACAROON_TIMEOUT": "20m",
                            "DISCHARGE_TOKEN_TIMEOUT": "30m",
                            "IDENTITY_PROVIDERS": """\
- type: static
  name: static
  description: Default identity provider
  require-mfa: true
  users:
    user1:
      name: User One
      email: user1@example.com
      password: password1
      groups:
       - group1
       - group3""",
                            "LOCATION": "https://test-location",
                            "LOGGING_CONFIG": "INFO",
                            "PRIVATE_KEY": "test-private-key",
                            "PUBLIC_KEY": "test-public-key",
                            "RENDEZVOUS_TIMEOUT": "5m",
                        },
                    }
                }
            },
        )

        config = container.pull("/root/config.yaml")
        self.assertEqual(
            textwrap.dedent(
                """\
                            access-log: /root/logs/access.log
                            auth-username: admin
                            listen-address: :8081
                            max-mgo-sessions: 300
                            request-timeout: 2s
                            resource-path: /root/www
                            storage:
                                type: memory
                            location: https://test-location
                            private-key: test-private-key
                            public-key: test-public-key
                            private-addr: localhost
                            admin-agent-private-key: test-admin-public-key
                            identity-providers:
                            - type: static
                              name: static
                              description: Default identity provider
                              require-mfa: true
                              users:
                                user1:
                                  name: User One
                                  email: user1@example.com
                                  password: password1
                                  groups:
                                   - group1
                                   - group3"""
            ),
            config.read(),
        )

    @patch("ops.model.Container.exec")
    def test_on_leader_elected(self, exec):
        exec.return_value = test_process()
        self.harness.charm.db = MagicMock()

        self.harness.update_config(
            {
                "admin-agent-public-key": "test-admin-public-key",
                "api-macaroon-timeout": "10m",
                "discharge-macaroon-timeout": "20m",
                "discharge-token-timeout": "30m",
                "identity-providers": "test-identity-providers",
                "location": "https://test-location",
                "rendezvous-timeout": "5m",
            }
        )

        rel_id = self.harness.add_relation("candid", "candid")
        self.harness.add_relation_unit(rel_id, "candid/1")
        self.harness.set_leader(True)

        self.harness.charm.on.leader_elected.emit()

        # Check the that the plan was updated
        plan = self.harness.get_container_pebble_plan("candid")
        self.assertEqual(
            plan.to_dict(),
            {
                "services": {
                    "candid": {
                        "summary": "Candid Identity Service",
                        "startup": "disabled",
                        "override": "merge",
                        "command": "/root/candidsrv /root/config.yaml",
                        "environment": {
                            "ADMIN_AGENT_PUBLIC_KEY": "test-admin-public-key",
                            "API_MACAROON_TIMEOUT": "10m",
                            "DISCHARGE_MACAROON_TIMEOUT": "20m",
                            "DISCHARGE_TOKEN_TIMEOUT": "30m",
                            "IDENTITY_PROVIDERS": "test-identity-providers",
                            "LOCATION": "https://test-location",
                            "LOGGING_CONFIG": "INFO",
                            "PRIVATE_KEY": "generated-private-key",
                            "PUBLIC_KEY": "generated-public-key",
                            "RENDEZVOUS_TIMEOUT": "5m",
                        },
                    }
                }
            },
        )

        self.assertEqual(
            self.harness.get_relation_data(rel_id, "candid-k8s"),
            {
                "private-key": "generated-private-key",
                "public-key": "generated-public-key",
            },
        )

    def test_keys_from_relation_data(self):
        self.harness.update_config({"no-proxy": "192.168.0.1"})

        rel_id = self.harness.add_relation("candid", "candid-k8s")
        self.harness.add_relation_unit(rel_id, "candid-k8s/1")

        self.harness.set_leader(False)
        self.harness.update_relation_data(
            rel_id,
            "candid-k8s",
            {
                "private-key": "generated-private-key",
                "public-key": "generated-public-key",
            },
        )
        self.harness.update_relation_data(
            rel_id,
            "candid-k8s/1",
            {
                "private-address": "192.168.0.2",
            },
        )

        container = self.harness.model.unit.get_container("candid")
        self.harness.charm.on.candid_pebble_ready.emit(container)

        self.harness.update_config({})

        # Check the that the plan was updated
        plan = self.harness.get_container_pebble_plan("candid")
        self.assertEqual(
            plan.to_dict(),
            {
                "services": {
                    "candid": {
                        "summary": "Candid Identity Service",
                        "startup": "disabled",
                        "override": "merge",
                        "command": "/root/candidsrv /root/config.yaml",
                        "environment": {
                            "API_MACAROON_TIMEOUT": "48h",
                            "DISCHARGE_MACAROON_TIMEOUT": "48h",
                            "DISCHARGE_TOKEN_TIMEOUT": "48h",
                            "LOGGING_CONFIG": "INFO",
                            "NO_PROXY": "192.168.0.1,192.168.0.2",
                            "PRIVATE_KEY": "generated-private-key",
                            "PUBLIC_KEY": "generated-public-key",
                            "RENDEZVOUS_TIMEOUT": "10m",
                        },
                    }
                }
            },
        )