Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Add configurable startup command (#18)
Browse files Browse the repository at this point in the history
* adding configurable startup command

* adding configurable startup command

* adding configurable startup command

* addressing comments

* addressing comments
  • Loading branch information
gtrkiller authored Oct 20, 2022
1 parent 52b0926 commit 279d2b6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ options:
type: int
description: "External port for Ingress configuration."
default: 8080
startup_command:
type: string
description: Command required to start up the docker image provided.
default: "/srv/gunicorn/run"
11 changes: 9 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _get_gunicorn_pebble_config(self, event: ops.framework.EventBase) -> dict:
"gunicorn": {
"override": "replace",
"summary": "gunicorn service",
"command": "/srv/gunicorn/run",
"command": self.config["startup_command"],
"startup": "enabled",
}
},
Expand Down Expand Up @@ -202,7 +202,14 @@ def _configure_workload(self, event: ops.charm.EventBase) -> None:
"About to add_layer with pebble_config: %s", yaml.dump(gunicorn_pebble_config)
)
gunicorn_container.add_layer("gunicorn", gunicorn_pebble_config, combine=True)
gunicorn_container.pebble.replan_services()
try:
gunicorn_container.pebble.replan_services()
except ops.pebble.ChangeError:
self.unit.status = BlockedStatus(
"Charm's startup command may be wrong, please check the config"
)
return

statsd_container.add_layer(
"statsd-prometheus-exporter", statsd_pebble_config, combine=True
)
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import unittest
from unittest.mock import MagicMock, patch

from ops import testing
from ops import pebble, testing
from ops.model import BlockedStatus
from scenario import (
JUJU_DEFAULT_CONFIG,
TEST_JUJU_CONFIG,
Expand Down Expand Up @@ -416,6 +417,18 @@ def test_configure_workload_pebble_not_ready(self):
self.assertEqual(r, expected_ret)
self.assertTrue(expected_output in logger.output[0])

def test_configure_workload_exception(self):

mock_event = MagicMock()

with patch("ops.model.Container.pebble", return_value=MagicMock()) as pebble_mock:
pebble_mock.replan_services.side_effect = pebble.ChangeError("abc", "def")
self.harness.charm._configure_workload(mock_event)
self.assertEqual(
self.harness.model.unit.status,
BlockedStatus("Charm's startup command may be wrong, please check the config"),
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 279d2b6

Please sign in to comment.