Misc fixes for unit tests

Resolve issues with compatibility with SIMULATE_CAN_CONNECT changes
in ops.testing.

Add Python 3.10 testing.
This commit is contained in:
James Page 2022-04-20 06:57:28 +01:00
parent 492dd40871
commit 91864d8d07
5 changed files with 17 additions and 9 deletions

View File

@ -9,8 +9,10 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
python-version: [3.8, 3.9] python-version:
- "3.8"
- "3.9"
- "3.10"
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }} - name: Set up Python ${{ matrix.python-version }}

View File

@ -35,7 +35,12 @@ sys.path.append("src") # noqa
from ops import framework, model from ops import framework, model
from ops.testing import Harness, _TestingModelBackend, _TestingPebbleClient from ops.testing import (
Harness,
_TestingModelBackend,
_TestingPebbleClient,
SIMULATE_CAN_CONNECT,
)
TEST_CA = '''-----BEGIN CERTIFICATE----- TEST_CA = '''-----BEGIN CERTIFICATE-----
@ -552,6 +557,7 @@ def get_harness(
# /charm/containers/placement-api/pebble.socket # /charm/containers/placement-api/pebble.socket
client.container_name = socket_path.split('/')[3] client.container_name = socket_path.split('/')[3]
self._pebble_clients[socket_path] = client self._pebble_clients[socket_path] = client
self._pebble_clients_can_connect[client] = not SIMULATE_CAN_CONNECT
return client return client
def network_get( def network_get(

View File

@ -47,17 +47,17 @@ basepython = python3
deps = -r{toxinidir}/cookie-requirements.txt deps = -r{toxinidir}/cookie-requirements.txt
commands = /bin/true commands = /bin/true
[testenv:py38] [testenv:py3.8]
basepython = python3.8 basepython = python3.8
deps = -r{toxinidir}/requirements.txt deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
[testenv:py39] [testenv:py3.9]
basepython = python3.9 basepython = python3.9
deps = -r{toxinidir}/requirements.txt deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
[testenv:py310] [testenv:py3.10]
basepython = python3.10 basepython = python3.10
deps = -r{toxinidir}/requirements.txt deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt

View File

@ -13,3 +13,5 @@
# limitations under the License. # limitations under the License.
"""Unit tests for aso.""" """Unit tests for aso."""
import ops.testing
ops.testing.SIMULATE_CAN_CONNECT = True

View File

@ -47,9 +47,7 @@ class TestOSBaseOperatorCharm(test_utils.CharmTestCase):
def set_pebble_ready(self) -> None: def set_pebble_ready(self) -> None:
"""Set pebble ready event.""" """Set pebble ready event."""
container = self.harness.model.unit.get_container("my-service") self.harness.container_pebble_ready('my-service')
# Emit the PebbleReadyEvent
self.harness.charm.on.my_service_pebble_ready.emit(container)
def test_pebble_ready_handler(self) -> None: def test_pebble_ready_handler(self) -> None:
"""Test is raised and observed.""" """Test is raised and observed."""