示例#1
0
    def test_happy_path_without_token_arg(self, action_status, contract_client,
                                          discharge_root_macaroon,
                                          request_updated_contract):
        """A mock-heavy test for the happy path without an argument"""
        # TODO: Improve this test with less general mocking and more
        # post-conditions
        bound_macaroon = b'bound_bytes_macaroon'
        discharge_root_macaroon.return_value = bound_macaroon
        args = mock.MagicMock(token=None)
        cfg = FakeConfig.with_account()
        machine_token = {
            'machineTokenInfo': {
                'contractInfo': {
                    'name': 'mycontract',
                    'resourceEntitlements': []
                }
            }
        }

        def fake_contract_updates(cfg, contract_token, allow_enable):
            cfg.write_cache('machine-token', machine_token)
            return True

        request_updated_contract.side_effect = fake_contract_updates
        ret = action_attach(args, cfg)

        assert 0 == ret
        assert 1 == action_status.call_count
        expected_macaroon = bound_macaroon.decode('utf-8')
        assert expected_macaroon == cfg._cache_contents['bound-macaroon']
示例#2
0
    def test_happy_path_with_token_arg(self, action_status,
                                       contract_machine_attach,
                                       discharge_root_macaroon):
        """A mock-heavy test for the happy path with the contract token arg"""
        # TODO: Improve this test with less general mocking and more
        # post-conditions
        token = 'contract-token'
        args = mock.MagicMock(token=token)
        cfg = FakeConfig.with_account()
        machine_token = {
            'machineTokenInfo': {
                'contractInfo': {
                    'name': 'mycontract',
                    'resourceEntitlements': []
                }
            }
        }

        def fake_contract_attach(contract_token):
            cfg.write_cache('machine-token', machine_token)
            return machine_token

        contract_machine_attach.side_effect = fake_contract_attach

        ret = action_attach(args, cfg)

        assert 0 == ret
        assert 1 == action_status.call_count
        expected_calls = [mock.call(contract_token=token)]
        assert expected_calls == contract_machine_attach.call_args_list
        assert 0 == discharge_root_macaroon.call_count
示例#3
0
    def test_no_discharged_macaroon(self, action_status, contract_client,
                                    discharge_root_macaroon, stdout):
        """If we can't discharge the root macaroon, fail gracefully."""
        discharge_root_macaroon.return_value = None
        args = mock.MagicMock(token=None)
        cfg = FakeConfig.with_account()

        ret = action_attach(args, cfg)

        assert 1 == ret
        expected_msg = ('Could not attach machine. Unable to obtain'
                        ' authenticated user token')
        assert mock.call(expected_msg) in stdout.write.call_args_list