def test_extract_none():
    """Test Loop Instance extract operational policy name."""
    loop = LoopInstance(template="template", name="test", details={})
    loop.details = {"operationalPolicies":[]}
    with pytest.raises(ValueError):
        policy_name = loop.extract_operational_policy_name(policy_type="Drools")
        assert policy_name == None
def test_undeploy_microservice_from_dcae(mock_send_message):
    """Test stop microservice."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    request = loop.undeploy_microservice_from_dcae()
    mock_send_message.assert_called_once_with(
        'PUT', 'Undeploy microservice from DCAE',
        (f"{loop.base_url()}/loop/undeploy/LOOP_test"))
Example #3
0
def test_Loop_customization():
    """Integration tests for Loop Instance."""
    requests.get("{}/reset".format(Clamp._base_url))
    Clamp(cert=('LICENSE', ''))
    svc = Service(name="service01")
    loop_template = Clamp.check_loop_template(service=svc)
    response = requests.post("{}/reset".format(Clamp._base_url))
    response.raise_for_status()
    loop = LoopInstance(template=loop_template, name="instance01", details={})
    loop.create()
    loop.update_microservice_policy()
    #add op policy FrequencyLimiter that already exists in clamp
    loop.add_operational_policy(
        policy_type="onap.policies.controlloop.guard.common.FrequencyLimiter",
        policy_version="1.0.0")
    #only frequency configuration is available in mock clamp
    loop.add_op_policy_config(loop.add_frequency_limiter, limit=1)
    submit = loop.act_on_loop_policy(loop.submit)
    assert submit
    stop = loop.act_on_loop_policy(loop.stop)
    assert stop
    restart = loop.act_on_loop_policy(loop.restart)
    assert restart
    deploy = loop.deploy_microservice_to_dcae()
    assert deploy
    loop.undeploy_microservice_from_dcae()
    new_details = loop._update_loop_details()
    assert new_details["components"]["DCAE"]["componentState"][
        "stateName"] == "MICROSERVICE_UNINSTALLED_SUCCESSFULLY"
def test_update_loop_details(mock_send_message_json):
    """Test Loop instance methode."""
    loop = LoopInstance(template="template", name="test", details={})
    mock_send_message_json.return_value = LOOP_DETAILS
    loop.details = loop._update_loop_details()
    mock_send_message_json.assert_called_once_with(
        'GET', 'Get loop details', (f"{loop.base_url()}/loop/LOOP_test"))
    assert loop.details == LOOP_DETAILS
def test_delete(mock_send_message):
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    request = loop.delete()
    mock_send_message.assert_called_once_with('PUT',
                                            'Delete loop instance',
                                            (f"{loop.base_url()}/loop/delete/{loop.name}"),
                                            cert=loop.cert,
                                            exception=ValueError)
def test_create_none(mock_send_message_json):
    """Test Loop instance creation."""
    instance = LoopInstance(template="template", name="test", details={})
    mock_send_message_json.return_value = {}
    with pytest.raises(ValueError):
        instance.create()
        mock_send_message_json.assert_called_once_with('POST', 'Create Loop Instance',
            (f"{instance.base_url()}/loop/create/LOOP_test?templateName=template"),
            cert=instance.cert)
def test_refresh_status(mock_send_message_json,mock_timer):
    """Test Loop instance methode."""
    loop = LoopInstance(template="template", name="test", details={})
    mock_send_message_json.return_value = LOOP_DETAILS
    loop.refresh_status()
    mock_send_message_json.assert_called_once_with('GET', 'Get loop status',
         (f"{loop.base_url()}/loop/getstatus/LOOP_test"),
         cert=loop.cert)
    assert loop.details == LOOP_DETAILS
def test_deploy_microservice_to_dcae(mock_send_message, mock_send_message_json,
                                     mock_timer):
    """Test stop microservice."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_send_message_json.return_value = SUBMITED
    state = loop.deploy_microservice_to_dcae()
    mock_send_message.assert_called_once_with(
        'PUT', 'Deploy microservice to DCAE',
        (f"{loop.base_url()}/loop/deploy/LOOP_test"))
    assert state
def test_restart_policy(mock_send_message, mock_refresh):
    """Test submit policies to policy engine."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    action = loop.act_on_loop_policy(loop.restart)
    mock_send_message.assert_called_once_with(
        'PUT', 'restart policy', (f"{loop.base_url()}/loop/restart/LOOP_test"))
    mock_refresh.assert_called_once()
    loop.details = SUBMITED_POLICY
    assert loop.details["components"]["POLICY"]["componentState"][
        "stateName"] == "SENT_AND_DEPLOYED"
def test_create(mock_send_message_json):
    """Test Loop instance creation."""
    instance = LoopInstance(template="template", name="test", details={})
    mock_send_message_json.return_value = LOOP_DETAILS
    instance.create()
    mock_send_message_json.assert_called_once_with('POST', 'Create Loop Instance',
         (f"{instance.base_url()}/loop/create/LOOP_test?templateName=template"),
         cert=instance.cert)
    assert instance.name == "LOOP_test"
    assert len(instance.details["microServicePolicies"]) > 0
def test_refresh_status_error(mock_send_message_json):
    """Test Loop instance methode."""
    loop = LoopInstance(template="template", name="test", details={})
    mock_send_message_json.return_value = {}
    with pytest.raises(ValueError):
        loop.refresh_status()
        mock_send_message_json.assert_called_once_with('GET', 'Get loop status',
            (f"{loop.base_url()}/loop/getstatus/LOOP_test"),
            cert=loop.cert)
        assert loop.details == {}
def test_update_microservice_policy(mock_send_message):
    """Test Loop Instance add TCA configuration."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_send_message.return_value = True
    loop.update_microservice_policy()
    mock_send_message.assert_called_once()
    method, description, url = mock_send_message.call_args[0]
    assert method == "POST"
    assert description == "ADD TCA config"
    assert url == (f"{loop.base_url()}/loop/updateMicroservicePolicy/{loop.name}")
Example #13
0
def test_Loop_creation():
    """Integration tests for Loop Instance."""
    requests.get("{}/reset".format(Clamp._base_url))
    Clamp()
    svc = Service(name="service01")
    loop_template = Clamp.check_loop_template(service=svc)
    response = requests.post("{}/reset".format(Clamp._base_url))
    response.raise_for_status()
    loop = LoopInstance(template=loop_template, name="instance01", details={})
    loop.create()
def test_add_frequency_policy_config(mock_send_message, mock_extract):
    """Test Loop Instance add op policy configuration."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_send_message.return_value = True
    loop.add_op_policy_config(loop.add_frequency_limiter)
    mock_send_message.assert_called_once()
    method, description, url = mock_send_message.call_args[0]
    assert method == "POST"
    assert description == "ADD operational policy config"
    assert url == (f"{loop.base_url()}/loop/updateOperationalPolicies/{loop.name}")
def test_add_two_policies_config(mock_freq, mock_min, mock_send_message):
    """Test Loop Instance add op policy configuration."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_min.return_value = '[{"test1":"test1"}]'
    mock_freq.return_value = '[{"test2":"test2"}]'
    loop.add_op_policy_config(loop.add_minmax_config)
    mock_min.assert_called_once()
    mock_send_message.assert_called_once()
    loop.add_op_policy_config(loop.add_frequency_limiter)
    mock_freq.assert_called_once()
    assert loop.operational_policies == '[{"test1":"test1"},{"test2":"test2"}]'
def test_stop_policy(mock_send_message, mock_refresh):
    """Test submit policies to policy engine."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    action = loop.act_on_loop_policy(loop.stop)
    mock_send_message.assert_called_once_with('PUT',
                                            'stop policy',
                                            (f"{loop.base_url()}/loop/stop/LOOP_test"),
                                            cert=loop.cert,
                                            exception=ValueError)
    mock_refresh.assert_called_once()
    loop.details = {"components":{"POLICY":{"componentState":{"stateName":"SENT"}}}}
    assert loop.details["components"]["POLICY"]["componentState"]["stateName"] == "SENT"
def test_add_op_policy_config_error(mock_send_message, mock_freq):
    """Test Loop Instance add op policy configuration."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_send_message.return_value = False
    #if u put a non cong function
    with pytest.raises(ValueError):
        loop.add_op_policy_config(loop.add_frequency_limiter)
        mock_send_message.assert_called_once()
        method, description, url = mock_send_message.call_args[0]
        assert method == "POST"
        assert description == "ADD operational policy config"
        assert url == (f"{loop.base_url()}/loop/updateOperationalPolicies/{loop.name}")
def test_add_operational_policy_key_parameter_error(mock_send_message_json):
    """Test adding an op policy - key doesn't exist."""
    loop = LoopInstance(template="template", name="test", details={})
    loop.details = {}
    with pytest.raises(ParameterError) as exc:
        mock_send_message_json.return_value = loop.details
        loop.add_operational_policy(policy_type="FrequencyLimiter",
                                    policy_version="not_correct")
        mock_send_message_json.assert_called_once_with(
            'PUT', 'Create Operational Policy',
            (f"{loop.base_url()}/loop/addOperationaPolicy/{loop.name}/policyModel/FrequencyLimiter/not_correct"
             ))
        assert exc.type is ParameterError
def test_not_submited_policy(mock_send_message, mock_refresh):
    """Test submit policies to policy engine."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_refresh.return_value = NOT_SUBMITED_POLICY
    action = loop.act_on_loop_policy(loop.submit)
    mock_send_message.assert_called_once_with('PUT',
                                            'submit policy',
                                            (f"{loop.base_url()}/loop/submit/LOOP_test"),
                                            cert=loop.cert,
                                            exception=ValueError)
    mock_refresh.assert_called_once()
    loop.details = NOT_SUBMITED_POLICY
    assert loop.details["components"]["POLICY"]["componentState"]["stateName"] == "SENT"
def test_extract_operational_policy_name():
    """Test Loop Instance extract operational policy name."""
    loop = LoopInstance(template="template", name="test", details={})
    loop.details = {
        "operationalPolicies": [{
            "name": "test",
            "policyModel": {
                "policyAcronym": "Drools"
            }
        }]
    }
    policy_name = loop.extract_operational_policy_name(policy_type="Drools")
    assert policy_name == 'test'
def test_remove_operational_policy(mock_send_message_json):
    """Test remove an op policy."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_send_message_json.return_value = {
        "name": "LOOP_test",
        "operationalPolicies": [],
        "microServicePolicies": [{
            "name": "MICROSERVICE_test"
        }]
    }
    loop.remove_operational_policy(policy_type="FrequencyLimiter",
                                   policy_version="1.0.0")
    mock_send_message_json.assert_called_once_with(
        'PUT', 'Remove Operational Policy',
        (f"{loop.base_url()}/loop/removeOperationaPolicy/{loop.name}/policyModel/FrequencyLimiter/1.0.0"
         ))
    assert len(loop.details["operationalPolicies"]) == 0
    def instantiate_loop(self):
        """Instantiate the control loop."""
        loop = LoopInstance(template=self.template,
                            name=self.loop_name,
                            details={})
        details = loop.create()
        if details:
            self._logger.info("Loop instance %s successfully created !!",
                              self.loop_name)
        else:
            self._logger.error(
                "An error occured while creating the loop instance")

        self.add_policies(loop=loop)
        self.configure_policies(loop=loop)
        self.deploy(loop=loop)

        loop.details = loop._update_loop_details()
        return loop
def test_add_operational_policy(mock_send_message_json):
    """Test adding an op policy."""
    loop = LoopInstance(template="template", name="test", details={})
    loop.details = {
        "name" : "LOOP_test",
        "operationalPolicies" : None,
        "microServicePolicies" : [
            {
                "name" : "MICROSERVICE_test"
            }
        ]
    }
    mock_send_message_json.return_value = LOOP_DETAILS
    loop.add_operational_policy(policy_type="FrequencyLimiter", policy_version="1.0.0")
    mock_send_message_json.assert_called_once_with('PUT', 'Create Operational Policy',
        (f"{loop.base_url()}/loop/addOperationaPolicy/{loop.name}/policyModel/FrequencyLimiter/1.0.0"),
        cert=loop.cert)
    assert loop.name == "LOOP_test"
    assert len(loop.details["operationalPolicies"]) > 0
def test_add_operational_policy_condition_parameter_error(
        mock_send_message_json):
    """Test adding an op policy - response cintains more policies."""

    key = "operationalPolicies"

    response_policies = ["one"]  # N policies
    current_policies = ["one", "two"]  # N+1 policies

    details = {key: current_policies}
    response = {key: response_policies}

    loop = LoopInstance(template="template", name="test", details=details)

    assert len(response_policies) < len(current_policies)  # raising condition
    with pytest.raises(ParameterError) as exc:
        mock_send_message_json.return_value = response
        loop.add_operational_policy(policy_type="FrequencyLimiter",
                                    policy_version="not_correct")
    assert exc.type is ParameterError
def test_not_add_operational_policy(mock_send_message_json):
    """Test adding an op policy."""
    loop = LoopInstance(template="template", name="test", details={})
    loop.details = {
        "name" : "LOOP_test",
        "operationalPolicies" : [],
        "microServicePolicies" : [
            {
                "name" : "MICROSERVICE_test"
            }
        ]
    }
    with pytest.raises(ValueError):
        mock_send_message_json.return_value = loop.details
        #mistaken policy version
        loop.add_operational_policy(policy_type="FrequencyLimiter", policy_version="not_correct")
        mock_send_message_json.assert_called_once_with('PUT', 'Create Operational Policy',
            (f"{loop.base_url()}/loop/addOperationaPolicy/{loop.name}/policyModel/FrequencyLimiter/not_correct"),
            cert=loop.cert)
        assert len(loop.details["operationalPolicies"]) == 0
def test_cl_initialization():
    """Class initialization test."""
    loop = LoopInstance(template="template", name="LOOP_name", details={})
    assert isinstance(loop, LoopInstance)
def test_validate_details():
    """Test Loop instance details validation."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    loop.details = {"test":"test"}
    valid = loop.validate_details()
    assert  not valid
def test_update_microservice_policy_none(mock_send_message):
    """Test Loop Instance add TCA configuration."""
    loop = LoopInstance(template="template", name="test", details=LOOP_DETAILS)
    mock_send_message.return_value = False
    loop.update_microservice_policy()
    mock_send_message.assert_called_once()
def test_details(mock_update):
    """Test loop instace details gette."""
    loop = LoopInstance(template="template", name="LOOP_name", details={})
    mock_update.return_value = {"name" : "test"}
    details = loop.details
    assert details == {}