github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/testcharms/charms/refresher/tests/test_charm.py (about)

     1  # Copyright 2022 Thomas Miller
     2  # See LICENSE file for licensing details.
     3  #
     4  # Learn more about testing at: https://juju.is/docs/sdk/testing
     5  
     6  import unittest
     7  from unittest.mock import Mock
     8  
     9  from charm import RefresherCharm
    10  from ops.model import ActiveStatus
    11  from ops.testing import Harness
    12  
    13  
    14  class TestCharm(unittest.TestCase):
    15      def setUp(self):
    16          self.harness = Harness(RefresherCharm)
    17          self.addCleanup(self.harness.cleanup)
    18          self.harness.begin()
    19  
    20      def test_config_changed(self):
    21          self.assertEqual(list(self.harness.charm._stored.things), [])
    22          self.harness.update_config({"thing": "foo"})
    23          self.assertEqual(list(self.harness.charm._stored.things), ["foo"])
    24  
    25      def test_action(self):
    26          # the harness doesn't (yet!) help much with actions themselves
    27          action_event = Mock(params={"fail": ""})
    28          self.harness.charm._on_fortune_action(action_event)
    29  
    30          self.assertTrue(action_event.set_results.called)
    31  
    32      def test_action_fail(self):
    33          action_event = Mock(params={"fail": "fail this"})
    34          self.harness.charm._on_fortune_action(action_event)
    35  
    36          self.assertEqual(action_event.fail.call_args, [("fail this",)])
    37  
    38      def test_httpbin_pebble_ready(self):
    39          # Simulate making the Pebble socket available
    40          self.harness.set_can_connect("httpbin", True)
    41          # Check the initial Pebble plan is empty
    42          initial_plan = self.harness.get_container_pebble_plan("httpbin")
    43          self.assertEqual(initial_plan.to_yaml(), "{}\n")
    44          # Expected plan after Pebble ready with default config
    45          expected_plan = {
    46              "services": {
    47                  "httpbin": {
    48                      "override": "replace",
    49                      "summary": "httpbin",
    50                      "command": "gunicorn -b 0.0.0.0:80 httpbin:app -k gevent",
    51                      "startup": "enabled",
    52                      "environment": {"thing": "🎁"},
    53                  }
    54              },
    55          }
    56          # Get the httpbin container from the model
    57          container = self.harness.model.unit.get_container("httpbin")
    58          # Emit the PebbleReadyEvent carrying the httpbin container
    59          self.harness.charm.on.httpbin_pebble_ready.emit(container)
    60          # Get the plan now we've run PebbleReady
    61          updated_plan = self.harness.get_container_pebble_plan("httpbin").to_dict()
    62          # Check we've got the plan we expected
    63          self.assertEqual(expected_plan, updated_plan)
    64          # Check the service was started
    65          service = self.harness.model.unit.get_container("httpbin").get_service("httpbin")
    66          self.assertTrue(service.is_running())
    67          # Ensure we set an ActiveStatus with no message
    68          self.assertEqual(self.harness.model.unit.status, ActiveStatus())