Exemple #1
0
    def test_observation_failure_or_found(self):
        context = ExecutionContext()
        observation = jc.Observation()
        observation.add_object(_LETTER_DICT)

        failure_predicate = jc.ObservationErrorPredicate(
            jp.ExceptionMatchesPredicate(ValueError, regex='an error'))
        failure_result = failure_predicate(context, observation)
        self.assertFalse(failure_result)

        good_predicate = jc.ObservationValuePredicate(
            jp.PathPredicate('a', jp.STR_EQ('A')))

        builder = jc.ObservationVerifierBuilder('TestAddConstraint')
        verifier = (
            builder.EXPECT(failure_predicate).OR(good_predicate).build())

        expect = jc.ObservationVerifyResult(
            valid=True,
            observation=observation,
            bad_results=[failure_result],
            good_results=[good_predicate(context, observation)],
            failed_constraints=[])

        got = verifier(context, observation)
        self.assertEqual(expect, got)
    def delete_load_balancer(self):
        """Creates OperationContract for deleteLoadBalancer.

    To verify the operation, we just check that the AWS resources
    created by upsert_load_balancer are no longer visible on AWS.
    """
        region = self.bindings['TEST_AWS_REGION']
        payload = self.agent.type_to_payload(
            'deleteAmazonLoadBalancerDescription', {
                'credentials': self.bindings['SPINNAKER_AWS_ACCOUNT'],
                'regions': [region],
                'loadBalancerName': self.__use_lb_name
            })

        builder = aws.AwsPythonContractBuilder(self.aws_observer)
        (builder.new_clause_builder('Load Balancer Removed').call_method(
            self.elb_client.describe_load_balancers,
            LoadBalancerNames=[self.__use_lb_name]).EXPECT(
                ov_factory.error_list_contains(
                    jp.ExceptionMatchesPredicate((BotoCoreError, ClientError),
                                                 'LoadBalancerNotFound'))))

        return st.OperationContract(self.new_post_operation(
            title='delete_amazon_load_balancer', data=payload, path='ops'),
                                    contract=builder.build())
Exemple #3
0
    def test_observation_failure_ok(self):
        error_text = 'the error'
        context = ExecutionContext()

        observation = jc.Observation()
        error = ValueError(error_text)
        observation.add_error(error)

        ex_pred = jp.ExceptionMatchesPredicate(ValueError, error_text)
        ex_result = ex_pred(context, error)
        ex_observation_predicate_result = jc.ObservationPredicateResult(
            True, observation, jp.LIST_MATCHES([ex_pred]),
            jp.LIST_MATCHES([ex_pred])(context, [error]))

        expect_failure = jc.ObservationVerifyResult(
            valid=True,
            observation=observation,
            good_results=[ex_observation_predicate_result],
            bad_results=[],
            failed_constraints=[])

        builder = jc.ValueObservationVerifierBuilder(title='Test For Error')
        builder.EXPECT(jc.ObservationErrorPredicate(jp.LIST_MATCHES([ex_pred
                                                                     ])))
        verifier = builder.build()

        self.assertEqual(expect_failure, verifier(context, observation))
 def test_exception_types_differ(self):
     ex_predicate = jp.ExceptionMatchesPredicate(ValueError,
                                                 regex='the error')
     self.assertEqual(
         jp.PredicateResult(False,
                            comment='Expected ValueError, got KeyError.'),
         ex_predicate(ExecutionContext(), KeyError('the error')))
Exemple #5
0
    def delete_load_balancer(self):
        """Create OperationContract that delete the Load Balancer

        To verify the operation, we just check that the spinnaker load balancer
        for the given application was deleted.
        """

        payload = self.agent.make_json_payload_from_kwargs(
            job=[{
                "cloudProvider": "azure",
                "loadBalancerName": self.__full_lb_name,
                "credentials": self.ACCOUNT,
                "region": self.__rg_location,
                "appName": self.TEST_APP,
                "type": "deleteLoadBalancer",
                "user": "******",
            }],
            description="Test - Delete load balancer: {lb}".format(
                lb=self.__full_lb_name),
            application=self.TEST_APP,
        )

        builder = az.AzContractBuilder(self.az_observer)
        (builder.new_clause_builder("Load Balancer Deleted",
                                    retryable_for_secs=30).collect_resources(
                                        az_resource="network",
                                        command="application-gateway",
                                        args=[
                                            "list",
                                            "--resource-group",
                                            "{app}-{rg}".format(
                                                app=self.TEST_APP,
                                                rg=self.__rg_location),
                                        ],
                                    )
         # expected no lb
         .EXPECT(
             ov_factory.error_list_contains(
                 jp.ExceptionMatchesPredicate(
                     klass=st.CliAgentRunError,
                     regex=
                     r"(?:.* operation: Cannot find .*)|(?:.*\(.*could not be found.\).*)",
                 )))
         # or no target lb
         .OR(
             ov_factory.value_list_path_excludes(
                 "name", jp.STR_EQ(self.__full_lb_name))))

        return st.OperationContract(
            self.new_post_operation(
                title="delete_load_balancer",
                data=payload,
                path=("applications/{app}/tasks").format(app=self.TEST_APP),
                max_wait_secs=1800,
            ),
            contract=builder.build(),
        )
    def trigger_disable_and_destroy(self, sg_id='v000'):
        """Create OperationContract that manually trigger the disable and destroy pipeline

        To verify the operation, we just check that the spinnaker server group
        for the given load balancer was deleted.
        """

        pipeline_id = self.destroy_pipeline_id
        payload = self.agent.make_json_payload_from_kwargs(
            job=[{
                "dryRun": False,
                "type": "manual",
                "user": "******"
            }],
            description="Test - begin disable and destroy server group: {pl}".
            format(pl=pipeline_id),
            application=self.TEST_APP)

        builder = az.AzContractBuilder(self.az_observer)
        (builder.new_clause_builder("Has No Virtual Machine Scale Set",
                                    retryable_for_secs=30).collect_resources(
                                        az_resource='vmss',
                                        command='list',
                                        args=[
                                            '--resource-group',
                                            '{app}-{rg}'.format(
                                                app=self.TEST_APP,
                                                rg=self.__rg_location)
                                        ]).
         EXPECT(
             ov_factory.error_list_contains(
                 jp.ExceptionMatchesPredicate(
                     klass=st.CliAgentRunError,
                     regex=
                     r'(?:.* operation: Cannot find .*)|(?:.*\(.*could not be found.\).*)'
                 ))).OR(
                     ov_factory.value_list_excludes(
                         jp.DICT_MATCHES({
                             "name":
                             jp.STR_EQ('{lb}-{sg}'.format(
                                 lb=self.__full_lb_name, sg=sg_id))
                         }))))

        return st.OperationContract(
            self.new_post_operation(
                title='disable and destroy',
                data=payload,
                # TODO: cannot use v2 url: pipelines/v2/{app}/{pl}
                path='pipelines/{app}/{pl}'.format(app=self.TEST_APP,
                                                   pl=pipeline_id),
                max_wait_secs=3600),
            contract=builder.build())
  def delete_server_group(self):
    """Creates OperationContract for deleteServerGroup.

    To verify the operation, we just check that the AWS Auto Scaling Group
    is no longer visible on AWS (or is in the process of terminating).
    """
    bindings = self.bindings

    group_name = '{app}-{stack}-v000'.format(
        app=self.TEST_APP, stack=bindings['TEST_STACK'])

    payload = self.agent.make_json_payload_from_kwargs(
        job=[{
            'cloudProvider': 'aws',
            'type': 'destroyServerGroup',
            'serverGroupName': group_name,
            'asgName': group_name,
            'region': bindings['TEST_AWS_REGION'],
            'regions': [bindings['TEST_AWS_REGION']],
            'credentials': bindings['SPINNAKER_AWS_ACCOUNT'],
            'user': '******'
        }],
        application=self.TEST_APP,
        description='DestroyServerGroup: ' + group_name)

    builder = aws.AwsPythonContractBuilder(self.aws_observer)
    (builder.new_clause_builder('Auto Scaling Group Removed')
     .call_method(
         self.autoscaling_client.describe_auto_scaling_groups,
         AutoScalingGroupNames=[group_name])
     .EXPECT(
         ov_factory.error_list_contains(
             jp.ExceptionMatchesPredicate(
                   (BotoCoreError, ClientError), 'AutoScalingGroupNotFound')))
     .OR(
         ov_factory.value_list_path_contains(
             'AutoScalingGroups',
           jp.LIST_MATCHES([])))
     .OR(
         ov_factory.value_list_path_contains(
             'AutoScalingGroups',
             jp.LIST_MATCHES([
                 jp.DICT_MATCHES({'Status': jp.STR_SUBSTR('Delete'),
                                  'MaxSize': jp.NUM_EQ(0)})])))
     )

    return st.OperationContract(
        self.new_post_operation(
            title='delete_server_group', data=payload, path='tasks'),
        contract=builder.build())
    def delete_a_security_group(self):
        """Creates azContract for deleteServerGroup.

        To verify the operation, we just check that the spinnaker security group
        for the given application was deleted.
        """

        payload = self.agent.make_json_payload_from_kwargs(
            job=[{
                "Provider": "azure",
                "appName": self.bindings["TEST_APP"],
                "region": self.bindings["TEST_AZURE_RG_LOCATION"],
                "regions": [self.bindings["TEST_AZURE_RG_LOCATION"]],
                "credentials": self.bindings["SPINNAKER_AZURE_ACCOUNT"],
                "securityGroupName": self.TEST_SECURITY_GROUP,
                "cloudProvider": "azure",
                "type": "deleteSecurityGroup",
                "user": "******",
            }],
            application=self.bindings["TEST_APP"],
            description="Delete Security Group: : " + self.TEST_SECURITY_GROUP,
        )

        builder = az.AzContractBuilder(self.az_observer)
        (builder.new_clause_builder("Security Group Deleted",
                                    retryable_for_secs=30).
         collect_resources(
             az_resource="network",
             command="nsg",
             args=["list", "--resource-group", self.TEST_SECURITY_GROUP_RG],
         ).EXPECT(
             ov_factory.error_list_contains(
                 jp.ExceptionMatchesPredicate(
                     klass=st.CliAgentRunError,
                     regex=
                     "(?:.* operation: Cannot find .*)|(?:.*\(.*NotFound\).*)",
                 ))).OR(
                     ov_factory.value_list_path_excludes(
                         "name", jp.STR_EQ(self.TEST_SECURITY_GROUP))))

        return st.OperationContract(
            self.new_post_operation(
                title="delete_security_group",
                data=payload,
                path="applications/{app}/tasks".format(
                    app=self.bindings["TEST_APP"]),
            ),
            contract=builder.build(),
        )
  def delete_load_balancer(self, use_vpc):
    """Creates OperationContract for deleteLoadBalancer.

    To verify the operation, we just check that the AWS resources
    created by upsert_load_balancer are no longer visible on AWS.

    Args:
      use_vpc: [bool] if True delete the VPC load balancer, otherwise
         the non-VPC load balancer.
    """
    load_balancer_name = self.lb_name
    if not use_vpc:
      # This is the second load balancer, where we decorated the name in upsert.
      load_balancer_name += '-pub'

    payload = self.agent.make_json_payload_from_kwargs(
        job=[{
            'type': 'deleteLoadBalancer',
            'cloudProvider': 'aws',

            'credentials': self.bindings['SPINNAKER_AWS_ACCOUNT'],
            'regions': [self.bindings['TEST_AWS_REGION']],
            'loadBalancerName': load_balancer_name
        }],
        description='Delete Load Balancer: {0} in {1}:{2}'.format(
            load_balancer_name,
            self.bindings['SPINNAKER_AWS_ACCOUNT'],
            self.bindings['TEST_AWS_REGION']),
        application=self.TEST_APP)

    builder = aws.AwsPythonContractBuilder(self.aws_observer)
    (builder.new_clause_builder('Load Balancer Removed')
     .call_method(
         self.elb_client.describe_load_balancers,
         LoadBalancerNames=[load_balancer_name])
     .EXPECT(
         ov_factory.error_list_contains(
             jp.ExceptionMatchesPredicate(
                   (BotoCoreError, ClientError), 'LoadBalancerNotFound'))))

    title_decorator = '_with_vpc' if use_vpc else '_without_vpc'
    return st.OperationContract(
        self.new_post_operation(
            title='delete_load_balancer' + title_decorator,
            data=payload,
            path='tasks'),
        contract=builder.build())
    def create_a_security_group(self):
        """Creates AzContract for createServerGroup.

        To verify the operation, we just check that the spinnaker security group
        for the given application was created.
        This will create a Network Security Group in a Resource Group on your azure Subscription
        """
        rules = [{
            "access": "Allow",
            "destinationAddressPrefix": "*",
            "destinationPortRange": "80-80",
            "direction": "InBound",
            "endPort": 80,
            "name": self.TEST_SECURITY_GROUP_RULE_1,
            "priority": 100,
            "protocol": "tcp",
            "sourceAddressPrefix": "*",
            "sourcePortRange": "*",
            "startPort": 80,
        }]
        job = [{
            "provider":
            "azure",
            "application":
            self.bindings["TEST_APP"],
            "appName":
            self.bindings["TEST_APP"],
            "region":
            self.bindings["TEST_AZURE_RG_LOCATION"],
            "stack":
            self.bindings["TEST_STACK"],
            "description":
            "Test - create security group for {app}".format(
                app=self.bindings["TEST_APP"]),
            "detail":
            "",
            "credentials":
            self.bindings["SPINNAKER_AZURE_ACCOUNT"],
            "securityRules":
            rules,
            "name":
            self.TEST_SECURITY_GROUP,
            "securityGroupName":
            self.TEST_SECURITY_GROUP,
            "cloudProvider":
            "azure",
            "type":
            "upsertSecurityGroup",
            "user":
            "******",
        }]
        builder = az.AzContractBuilder(self.az_observer)
        (builder.new_clause_builder("Security Group Created",
                                    retryable_for_secs=30).collect_resources(
                                        az_resource="network",
                                        command="nsg",
                                        args=[
                                            "show",
                                            "--name",
                                            self.TEST_SECURITY_GROUP,
                                            "--resource-group",
                                            self.TEST_SECURITY_GROUP_RG,
                                        ],
                                    ).
         EXPECT(
             ov_factory.error_list_contains(
                 jp.ExceptionMatchesPredicate(
                     klass=st.CliAgentRunError,
                     regex=
                     "(?:.* operation: Cannot find .*)|(?:.*\(.*NotFound\).*)",
                 ))).OR(
                     ov_factory.value_list_contains(
                         # sec grp name matches expected
                         jp.DICT_MATCHES({
                             "name":
                             jp.STR_SUBSTR(self.TEST_SECURITY_GROUP),
                             "securityRules":
                             jp.LIST_MATCHES(
                                 [
                                     jp.DICT_MATCHES({
                                         "protocol":
                                         jp.STR_EQ("tcp"),
                                         "name":
                                         jp.STR_EQ(
                                             self.TEST_SECURITY_GROUP_RULE_1),
                                     })
                                 ],
                                 strict=True,
                             ),
                         }))))

        payload = self.agent.make_json_payload_from_kwargs(
            job=job,
            description=" Test - create security group for {app}".format(
                app=self.bindings["TEST_APP"]),
            application=self.bindings["TEST_APP"],
        )

        return st.OperationContract(
            self.new_post_operation(
                title="create_security_group",
                data=payload,
                path="applications/{app}/tasks".format(
                    app=self.bindings["TEST_APP"]),
            ),
            contract=builder.build(),
        )
 def test_exception_messages_differ(self):
     ex_predicate = jp.ExceptionMatchesPredicate(ValueError,
                                                 regex='the error')
     self.assertEqual(jp.PredicateResult(False, comment='Errors differ.'),
                      ex_predicate(ExecutionContext(), ValueError()))
 def test_exception_matches(self):
     ex_predicate = jp.ExceptionMatchesPredicate(ValueError,
                                                 regex='the error')
     self.assertEqual(
         jp.PredicateResult(True, comment='Error matches.'),
         ex_predicate(ExecutionContext(), ValueError('the error')))