Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: 'JUJU_MODEL_NAME' if Harness-backed test is executed #17

Closed
exceptorr opened this issue Jun 1, 2020 · 1 comment
Closed

Comments

@exceptorr
Copy link
Contributor

Originally inspired by canonical/operator#309

======================================================================== FAILURES ========================================================================
_____________________________________________ OnConfigChangedHandlerTest.test__it_blocks_until_pod_is_ready ______________________________________________

self = <charm_test.OnConfigChangedHandlerTest testMethod=test__it_blocks_until_pod_is_ready>, mock_pod_spec = <function set_juju_pod_spec at 0x10b794200>
mock_juju_pod_spec = <function build_juju_pod_spec at 0x10b794950>, mock_time = <NonCallableMagicMock name='time' spec_set='module' id='4487497232'>
mock_k8s_mod = <NonCallableMagicMock name='k8s' spec_set='module' id='4482352976'>
mock_build_juju_unit_status_func = <function build_juju_unit_status at 0x10b7a5290>

    @patch('charm.build_juju_unit_status', spec_set=True, autospec=True)
    @patch('charm.k8s', spec_set=True, autospec=True)
    @patch('charm.time', spec_set=True, autospec=True)
    @patch('charm.build_juju_pod_spec', spec_set=True, autospec=True)
    @patch('charm.set_juju_pod_spec', spec_set=True, autospec=True)
    def test__it_blocks_until_pod_is_ready(
            self,
            mock_pod_spec,
            mock_juju_pod_spec,
            mock_time,
            mock_k8s_mod,
            mock_build_juju_unit_status_func):
        # Setup
        mock_fw_adapter_cls = \
            create_autospec(framework.FrameworkAdapter, spec_set=True)
        mock_fw_adapter = mock_fw_adapter_cls.return_value

        mock_juju_unit_states = [
            MaintenanceStatus(str(uuid4())),
            MaintenanceStatus(str(uuid4())),
            ActiveStatus(str(uuid4())),
        ]
        mock_build_juju_unit_status_func.side_effect = mock_juju_unit_states

        mock_event_cls = create_autospec(EventBase, spec_set=True)
        mock_event = mock_event_cls.return_value

        harness = Harness(charm.Charm)
        harness.begin()
        harness.charm._stored.set_default(is_started=False)
>       harness.charm.on.config_changed.emit()

test/charm_test.py:99:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
lib/ops/framework.py:207: in emit
    framework._emit(event)
lib/ops/framework.py:714: in _emit
    self._reemit(event_path)
lib/ops/framework.py:757: in _reemit
    custom_handler(event)
src/charm.py:74: in on_config_changed
    on_config_changed_handler(event, self.fw_adapter, self._stored)
src/charm.py:96: in on_config_changed_handler
    juju_model = fw_adapter.get_model_name()
src/adapters/framework.py:88: in get_model_name
    return os.environ["JUJU_MODEL_NAME"]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = environ({'TMPDIR': '/var/folders/ch/ptmqpg6d4rjbmlgh_6smvrxm0000gn/T/', 'PATH': '/Users/vgrevtsev/Canonical/charm-k8s-...', 'PYTEST_CURRENT_TEST': 'test/charm_test.py::OnConfigChangedHandlerTest::test__it_blocks_until_pod_is_ready (call)'})
key = 'JUJU_MODEL_NAME'

    def __getitem__(self, key):
        try:
            value = self._data[self.encodekey(key)]
        except KeyError:
            # raise KeyError with the original key value
>           raise KeyError(key) from None
E           KeyError: 'JUJU_MODEL_NAME'

/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py:681: KeyError

---------- coverage: platform darwin, python 3.7.7-final-0 -----------
Name                        Stmts   Miss Branch BrPart  Cover
-------------------------------------------------------------
src/adapters/framework.py      58      4      6      0    94%
src/adapters/k8s.py            43      1     10      2    94%
src/charm.py                   73     36     10      1    48%
src/domain.py                 108     10     42      9    87%
src/exceptions.py               5      1      0      0    80%
src/interface_http.py          26      7      2      0    68%
-------------------------------------------------------------
TOTAL                         313     59     70     12    79%
Coverage HTML written to dir coverage-report

================================================================ short test summary info =================================================================
FAILED test/charm_test.py::OnConfigChangedHandlerTest::test__it_blocks_until_pod_is_ready - KeyError: 'JUJU_MODEL_NAME'
exceptorr added a commit to exceptorr/charm-k8s-prometheus that referenced this issue Jun 1, 2020
(using the charm parameters). The only thing here missing
is the dynamic config reload itself.

Also, a charmed-lma#17 issue has been addressed (removing the os.environ
dependency, as Harness is not initializing os.environ as Juju
normally does).
exceptorr added a commit to exceptorr/charm-k8s-prometheus that referenced this issue Jun 1, 2020
(using the charm parameters). The only thing here missing
is the dynamic config reload itself.

Also, a charmed-lma#17 issue has been addressed (removing the os.environ
dependency, as Harness is not initializing os.environ as Juju
normally does).
@exceptorr
Copy link
Contributor Author

Resolved with #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant