예제 #1
0
def test_Resource__local_update():
    r = Resource(mock.MagicMock())
    stash = r._meta_data.copy()
    r._local_update({'test': 1})
    assert stash == r._meta_data
    r.__dict__.pop('_meta_data')
    assert r.__dict__ == {'test': 1}
예제 #2
0
 def test_missing_required_creation_parameter(self):
     r = Resource(mock.MagicMock())
     r._meta_data['required_creation_parameters'] = set(['NONEMPTY'])
     with pytest.raises(MissingRequiredCreationParameter) as MRCPEIO:
         r.create(partition="Common", name='CreateTest')
     assert MRCPEIO.value.message ==\
         "Missing required params: set(['NONEMPTY'])"
예제 #3
0
 def test_missing_required_params(self):
     r = Resource(mock.MagicMock())
     r._meta_data['required_refresh_parameters'] = set(['IMPOSSIBLE'])
     with pytest.raises(MissingRequiredReadParameter) as MRREIO:
         r.load(partition='Common', name='test_load')
     assert MRREIO.value.message ==\
         "Missing required params: set(['IMPOSSIBLE'])"
예제 #4
0
 def test_missing_required_params(self):
     r = Resource(mock.MagicMock())
     r._meta_data['required_load_parameters'] = set(['IMPOSSIBLE'])
     with pytest.raises(MissingRequiredReadParameter) as MRREIO:
         r.load(partition='Common', name='test_load')
     assert MRREIO.value.message ==\
         "Missing required params: set(['IMPOSSIBLE'])"
예제 #5
0
def test_Resource__local_update():
    r = Resource(mock.MagicMock())
    stash = r._meta_data.copy()
    r._local_update({'test': 1})
    assert stash == r._meta_data
    r.__dict__.pop('_meta_data')
    assert r.__dict__ == {'test': 1}
예제 #6
0
 def test_missing_required_creation_parameter(self):
     r = Resource(mock.MagicMock())
     r._meta_data['required_creation_parameters'] = set(['NONEMPTY'])
     with pytest.raises(MissingRequiredCreationParameter) as MRCPEIO:
         r.create(partition="Common", name='CreateTest')
     assert MRCPEIO.value.message ==\
         "Missing required params: set(['NONEMPTY'])"
예제 #7
0
def test_Resource_refresh():
    r = Resource(mock.MagicMock())

    r._meta_data['bigip']._meta_data['icr_session'].get.return_value =\
        MockResponse({u"a": 1})
    r._meta_data['uri'] = 'URI'
    r.refresh()
    assert r.a == 1
예제 #8
0
 def test_requests_params_collision(self):
     r = Resource(mock.MagicMock())
     with pytest.raises(RequestParamKwargCollision) as RPKCEIO:
         r.load(partition='Common', name='test_load',
                requests_params={'partition': 'ERROR'})
     assert RPKCEIO.value.message ==\
         "Requests Parameter 'partition' collides with a load parameter"\
         " of the same name."
예제 #9
0
 def test_invalid_force(self):
     r = Resource(mock.MagicMock())
     r._meta_data['uri'] = 'URI'
     mock_session = mock.MagicMock()
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     with pytest.raises(InvalidForceType) as IFTEIO:
         r.delete(force='true')
     assert IFTEIO.value.message == 'force parameter must be type bool'
예제 #10
0
def test__create_with_Collision():
    r = Resource(mock.MagicMock())
    r._meta_data['uri'] = 'URI'
    with pytest.raises(URICreationCollision) as UCCEIO:
        r.create(uri='URI')
    assert UCCEIO.value.message ==\
        "There was an attempt to assign a new uri to this resource,"\
        " the _meta_data['uri'] is URI and it should not be changed."
예제 #11
0
 def test_invalid_force(self):
     r = Resource(mock.MagicMock())
     r._meta_data['uri'] = 'URI'
     mock_session = mock.MagicMock()
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     with pytest.raises(InvalidForceType) as IFTEIO:
         r.delete(force='true')
     assert IFTEIO.value.message == 'force parameter must be type bool'
예제 #12
0
def test__create_with_Collision():
    r = Resource(mock.MagicMock())
    r._meta_data['uri'] = 'URI'
    with pytest.raises(URICreationCollision) as UCCEIO:
        r.create(uri='URI')
    assert UCCEIO.value.message ==\
        "There was an attempt to assign a new uri to this resource,"\
        " the _meta_data['uri'] is URI and it should not be changed."
예제 #13
0
def test_Resource_refresh():
    r = Resource(mock.MagicMock())

    r._meta_data['bigip']._meta_data['icr_session'].get.return_value =\
        MockResponse({u"a": 1})
    r._meta_data['uri'] = 'URI'
    r.refresh()
    assert r.a == 1
예제 #14
0
 def test_requests_params_collision(self):
     r = Resource(mock.MagicMock())
     with pytest.raises(RequestParamKwargCollision) as RPKCEIO:
         r.load(partition='Common',
                name='test_load',
                requests_params={'partition': 'ERROR'})
     assert RPKCEIO.value.message ==\
         "Requests Parameter 'partition' collides with a load parameter"\
         " of the same name."
예제 #15
0
 def test_KindTypeMismatch(self):
     r = Resource(mock.MagicMock())
     r._meta_data['bigip']._meta_data['icr_session'].post.return_value =\
         MockResponse({u"kind": u"tm:"})
     r._meta_data['required_json_kind'] = 'INCORRECT!'
     with pytest.raises(KindTypeMismatch) as KTMmEIO:
         r.create(partition="Common", name="test_create")
     assert KTMmEIO.value.message ==\
         "For instances of type ''Resource'' the corresponding kind must"\
         " be ''INCORRECT!'' but creation returned JSON with kind: u'tm:'"
예제 #16
0
 def test_successful(self):
     r = Resource(mock.MagicMock())
     MRO = MockResponse({u"kind": u"tm:",
                         u"selfLink": u".../~Common~test_create"})
     r._meta_data['bigip']._meta_data['icr_session'].post.return_value = MRO
     r._meta_data['required_json_kind'] = u"tm:"
     r._meta_data['allowed_lazy_attributes'] = []
     r.create(partition="Common", name="test_create")
     assert r.kind == u"tm:"
     assert r.selfLink == u".../~Common~test_create"
예제 #17
0
 def test_KindTypeMismatch(self):
     r = Resource(mock.MagicMock())
     r._meta_data['bigip']._meta_data['icr_session'].post.return_value =\
         MockResponse({u"kind": u"tm:"})
     r._meta_data['required_json_kind'] = 'INCORRECT!'
     with pytest.raises(KindTypeMismatch) as KTMmEIO:
         r.create(partition="Common", name="test_create")
     assert KTMmEIO.value.message ==\
         "For instances of type ''Resource'' the corresponding kind must"\
         " be ''INCORRECT!'' but creation returned JSON with kind: u'tm:'"
예제 #18
0
 def test_not_found(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     response = requests.Response()
     response.status_code = 404
     mock_session = mock.MagicMock()
     mock_session.get.side_effect = requests.HTTPError(response=response)
     r._meta_data['bigip']._meta_data = {
         'icr_session': mock_session,
         'hostname': 'TESTDOMAINNAME'
     }
     assert not r.exists(partition='Common', name='test_exists')
예제 #19
0
 def test_loadable(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     mockuri = "https://localhost/mgmt/tm/ltm/nat/~Common~test_exists"
     attrs = {'get.return_value':
              MockResponse({u"generation": 0, u"selfLink": mockuri})}
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data =\
         {'icr_session': mock_session,
          'hostname': 'TESTDOMAINNAME'}
     r.generation = 0
     assert r.exists(partition='Common', name='test_exists')
예제 #20
0
 def test_not_found(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     response = requests.Response()
     response.status_code = 404
     mock_session = mock.MagicMock()
     mock_session.get.side_effect = requests.HTTPError(response=response)
     r._meta_data['bigip']._meta_data = {
         'icr_session': mock_session,
         'hostname': 'TESTDOMAINNAME'
     }
     assert not r.exists(partition='Common', name='test_exists')
예제 #21
0
 def test_error(self):
     response = requests.Response()
     response.status_code = 400
     mock_session = mock.MagicMock()
     mock_session.get.side_effect = requests.HTTPError(response=response)
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['bigip']._meta_data = {
         'icr_session': mock_session,
         'hostname': 'TESTDOMAINNAME'
     }
     with pytest.raises(requests.HTTPError) as err:
         r.exists(partition='Common', name='test_exists')
         assert err.response.status_code == 400
예제 #22
0
 def test_error(self):
     response = requests.Response()
     response.status_code = 400
     mock_session = mock.MagicMock()
     mock_session.get.side_effect = requests.HTTPError(response=response)
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['bigip']._meta_data = {
         'icr_session': mock_session,
         'hostname': 'TESTDOMAINNAME'
     }
     with pytest.raises(requests.HTTPError) as err:
         r.exists(partition='Common', name='test_exists')
         assert err.response.status_code == 400
예제 #23
0
def test__activate_URI():
    r = Resource(mock.MagicMock())
    r._meta_data['allowed_lazy_attributes'] = []
    r._meta_data['attribute_registry'] = {u"tm:": u"SPAM"}
    r._meta_data['bigip']._meta_data = {'hostname': 'TESTDOMAIN'}
    TURI = 'https://localhost/mgmt/tm/'\
           'ltm/nat/~Common~testnat/?ver=11.5&a=b#FOO'
    assert r._meta_data['allowed_lazy_attributes'] == []
    r._activate_URI(TURI)
    assert r._meta_data['uri'] ==\
        'https://TESTDOMAIN/mgmt/tm/ltm/nat/~Common~testnat/'
    assert r._meta_data['creation_uri_qargs'] ==\
        {'a': ['b'], 'ver': ['11.5']}
    assert r._meta_data['creation_uri_frag'] == 'FOO'
    assert r._meta_data['allowed_lazy_attributes'] == [u"SPAM"]
예제 #24
0
 def test_loadable(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     mockuri = "https://localhost/mgmt/tm/ltm/nat/~Common~test_exists"
     attrs = {
         'get.return_value':
         MockResponse({
             u"generation": 0,
             u"selfLink": mockuri
         })
     }
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data =\
         {'icr_session': mock_session,
          'hostname': 'TESTDOMAINNAME'}
     r.generation = 0
     assert r.exists(partition='Common', name='test_exists')
예제 #25
0
 def test_read_only_removal(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     r._meta_data['read_only_attributes'] = [u"READONLY"]
     attrs = {
         'put.return_value': MockResponse({u"generation": 0}),
         'get.return_value': MockResponse({u"generation": 0})
     }
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     r.generation = 0
     r.READONLY = True
     assert 'READONLY' in r.__dict__
     r.update(a=u"b")
     submitted = r._meta_data['bigip'].\
         _meta_data['icr_session'].put.call_args[1]['json']
     assert 'READONLY' not in submitted
예제 #26
0
def test_Resource__local_update_IncompatibleKeys():
    r = Resource(mock.MagicMock())
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"_meta_data": "foo"})
    assert DPIKIO.value.message ==\
        "Response contains key '_meta_data' which is incompatible"\
        " with this API!!\n Response json: {'_meta_data': 'foo'}"
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"__MANGLENAME": "foo"})
    assert DPIKIO.value.message ==\
        "Device provided '__MANGLENAME' which is disallowed,"\
        " it mangles into a Python non-public attribute."
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"for": "foo"})
    assert DPIKIO.value.message ==\
        "Device provided 'for' which is disallowed because"\
        " it's a Python keyword."
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"%abcd": "foo"})
    assert DPIKIO.value.message ==\
        "Device provided '%abcd' which is disallowed because"\
        " it's not a valid Python 2.7 identifier."
예제 #27
0
 def test__check_generation_with_mismatch(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     r._meta_data['bigip']._meta_data['icr_session'].get.return_value =\
         MockResponse({u"generation": 0})
     r.generation = 1
     with pytest.raises(GenerationMismatch) as GMEIO:
         r.update(a=u"b")
     assert GMEIO.value.message ==\
         'The generation of the object on the BigIP (0)'\
         ' does not match the current object(1)'
예제 #28
0
 def test__meta_data_state(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     r._meta_data['bigip']._meta_data['icr_session'].get.return_value =\
         MockResponse({u"generation": 0})
     r._meta_data['bigip']._meta_data['icr_session'].put.return_value =\
         MockResponse({u"generation": 0})
     r.generation = 0
     pre_meta = r._meta_data.copy()
     r.update(a=u"b")
     assert pre_meta == r._meta_data
예제 #29
0
 def test_read_only_removal(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     r._meta_data['read_only_attributes'] = [u"READONLY"]
     attrs = {'put.return_value': MockResponse({u"generation": 0}),
              'get.return_value': MockResponse({u"generation": 0})}
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     r.generation = 0
     r.READONLY = True
     assert 'READONLY' in r.__dict__
     r.update(a=u"b")
     submitted = r._meta_data['bigip'].\
         _meta_data['icr_session'].put.call_args[1]['json']
     assert 'READONLY' not in submitted
예제 #30
0
 def test_Collection_removal(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     attrs = {
         'put.return_value': MockResponse({u"generation": 0}),
         'get.return_value': MockResponse({u"generation": 0})
     }
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     r.generation = 0
     r.contained = Collection(mock.MagicMock())
     assert 'contained' in r.__dict__
     r.update(a=u"b")
     submitted = r._meta_data['bigip'].\
         _meta_data['icr_session'].put.call_args[1]['json']
     assert 'contained' not in submitted
예제 #31
0
def test_Resource__local_update_IncompatibleKeys():
    r = Resource(mock.MagicMock())
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"_meta_data": "foo"})
    assert DPIKIO.value.message ==\
        "Response contains key '_meta_data' which is incompatible"\
        " with this API!!\n Response json: {'_meta_data': 'foo'}"
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"__MANGLENAME": "foo"})
    assert DPIKIO.value.message ==\
        "Device provided '__MANGLENAME' which is disallowed,"\
        " it mangles into a Python non-public attribute."
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"for": "foo"})
    assert DPIKIO.value.message ==\
        "Device provided 'for' which is disallowed because"\
        " it's a Python keyword."
    with pytest.raises(DeviceProvidesIncompatibleKey) as DPIKIO:
        r._local_update({"%abcd": "foo"})
    assert DPIKIO.value.message ==\
        "Device provided '%abcd' which is disallowed because"\
        " it's not a valid Python 2.7 identifier."
예제 #32
0
 def test_Collection_removal(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     attrs = {'put.return_value': MockResponse({u"generation": 0}),
              'get.return_value': MockResponse({u"generation": 0})}
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     r.generation = 0
     r.contained = Collection(mock.MagicMock())
     assert 'contained' in r.__dict__
     r.update(a=u"b")
     submitted = r._meta_data['bigip'].\
         _meta_data['icr_session'].put.call_args[1]['json']
     assert 'contained' not in submitted
예제 #33
0
 def test__check_generation_with_mismatch(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     r._meta_data['bigip']._meta_data['icr_session'].get.return_value =\
         MockResponse({u"generation": 0})
     r.generation = 1
     with pytest.raises(GenerationMismatch) as GMEIO:
         r.update(a=u"b")
     assert GMEIO.value.message ==\
         'The generation of the object on the BigIP (0)'\
         ' does not match the current object(1)'
예제 #34
0
 def test_successful(self):
     r = Resource(mock.MagicMock())
     MRO = MockResponse({
         u"kind": u"tm:",
         u"selfLink": u".../~Common~test_create"
     })
     r._meta_data['bigip']._meta_data['icr_session'].post.return_value = MRO
     r._meta_data['required_json_kind'] = u"tm:"
     r._meta_data['allowed_lazy_attributes'] = []
     r.create(partition="Common", name="test_create")
     assert r.kind == u"tm:"
     assert r.selfLink == u".../~Common~test_create"
예제 #35
0
 def test__meta_data_state(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     r._meta_data['bigip']._meta_data['icr_session'].get.return_value =\
         MockResponse({u"generation": 0})
     r._meta_data['bigip']._meta_data['icr_session'].put.return_value =\
         MockResponse({u"generation": 0})
     r.generation = 0
     pre_meta = r._meta_data.copy()
     r.update(a=u"b")
     assert pre_meta == r._meta_data
예제 #36
0
 def test_success(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     attrs = {'delete.return_value':
              MockResponse({u"generation": 0, u"status_code": 200}),
              'get.return_value':
              MockResponse({u"generation": 0, u"status_code": 200})}
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     r.generation = 0
     r.delete(force=False)
     assert r.__dict__ == {'deleted': True}
예제 #37
0
def test__activate_URI():
    r = Resource(mock.MagicMock())
    r._meta_data['allowed_lazy_attributes'] = []
    r._meta_data['attribute_registry'] = {u"tm:": u"SPAM"}
    r._meta_data['bigip']._meta_data = {'hostname': 'TESTDOMAIN'}
    TURI = 'https://localhost/mgmt/tm/'\
           'ltm/nat/~Common~testnat/?ver=11.5&a=b#FOO'
    assert r._meta_data['allowed_lazy_attributes'] == []
    r._activate_URI(TURI)
    assert r._meta_data['uri'] ==\
        'https://TESTDOMAIN/mgmt/tm/ltm/nat/~Common~testnat/'
    assert r._meta_data['creation_uri_qargs'] ==\
        {'a': ['b'], 'ver': ['11.5']}
    assert r._meta_data['creation_uri_frag'] == 'FOO'
    assert r._meta_data['allowed_lazy_attributes'] == [u"SPAM"]
예제 #38
0
 def test_success(self):
     r = Resource(mock.MagicMock())
     r._meta_data['allowed_lazy_attributes'] = []
     r._meta_data['uri'] = 'URI'
     attrs = {
         'delete.return_value':
         MockResponse({
             u"generation": 0,
             u"status_code": 200
         }),
         'get.return_value':
         MockResponse({
             u"generation": 0,
             u"status_code": 200
         })
     }
     mock_session = mock.MagicMock(**attrs)
     r._meta_data['bigip']._meta_data = {'icr_session': mock_session}
     r.generation = 0
     r.delete(force=False)
     assert r.__dict__ == {'deleted': True}
예제 #39
0
def test_Resource__check_keys_valid_rict():
    r = Resource(mock.MagicMock())
    res = r._check_keys({})
    assert res == {}
예제 #40
0
def test_Resource__check_keys_valid_rict():
    r = Resource(mock.MagicMock())
    res = r._check_keys({})
    assert res == {}