예제 #1
0
    def test_profile_rule_add_remove_add_rule_exists(self, m_client):
        """
        Test for profile_rule_add_remove when adding a Rule that already
        exists.

        Assert that the profile_update_rules function is not called and that
        the Rules object has not changed.
        """
        # Set up arguments to pass to method under test
        operation = 'add'
        name = 'profile1'
        position = None
        action = 'allow'
        direction = 'inbound'

        # Set up Mock objects
        rule_dict = {'action': 'allow'}
        rule = Rule(**rule_dict)
        m_Rules = Mock(spec=Rules,
                       id=name,
                       inbound_rules=[rule],
                       outbound_rules=[rule])
        m_Profile = Mock(spec=Profile, name=name, rules=m_Rules)
        m_client.get_profile.return_value = m_Profile

        # Call method under test
        profile_rule_add_remove(operation, name, position, action, direction)

        # Assert
        m_client.get_profile.assert_called_once_with(name)
        self.assertFalse(m_client.profile_update_rules.called)
        self.assertEqual(m_Rules.inbound_rules, [rule])
        self.assertEqual(m_Rules.outbound_rules, [rule])
예제 #2
0
    def test_profile_rule_add_remove_add_rule_exists(self, m_client):
        """
        Test for profile_rule_add_remove when adding a Rule that already
        exists.

        Assert that the profile_update_rules function is not called and that
        the Rules object has not changed.
        """
        # Set up arguments to pass to method under test
        operation = 'add'
        name = 'profile1'
        position = None
        action = 'allow'
        direction = 'inbound'

        # Set up Mock objects
        rule_dict = {
            'action': 'allow'
        }
        rule = Rule(**rule_dict)
        m_Rules = Mock(spec=Rules, id=name, inbound_rules=[rule],
                       outbound_rules=[rule])
        m_Profile = Mock(spec=Profile, name=name, rules=m_Rules)
        m_client.get_profile.return_value = m_Profile

        # Call method under test
        profile_rule_add_remove(operation, name, position, action, direction)

        # Assert
        m_client.get_profile.assert_called_once_with(name)
        self.assertFalse(m_client.profile_update_rules.called)
        self.assertEqual(m_Rules.inbound_rules, [rule])
        self.assertEqual(m_Rules.outbound_rules, [rule])
예제 #3
0
    def test_profile_rule_add_remove_add_rule_front_of_list(
            self, direction_arg, position_arg):
        """
        Test for profile_rule_add_remove function when adding a new Rule to the
        front of the inbound or outbound rules list.

        Test for both directions - inbound and outbound.

        Test for multiple positions less than 0
        (including positions out of range)
        """
        with patch('calico_ctl.profile.client', autospec=True) as m_client:
            # Setup arguments to pass to method under test
            operation = 'add'
            name = 'profile1'
            position = position_arg
            action = 'allow'
            direction = direction_arg

            # Set up Mock objects
            rule = Rule()
            m_Rules = Mock(spec=Rules,
                           id=name,
                           inbound_rules=[rule],
                           outbound_rules=[rule])
            m_Profile = Mock(spec=Profile, name=name, rules=m_Rules)
            m_client.get_profile.return_value = m_Profile

            # Set up new rule that function will create/add - compare in asserts
            rule_dict = {'action': 'allow', 'icmp_type': 5, 'icmp_code': 5}
            new_rule = Rule(**rule_dict)

            # Call method under test
            profile_rule_add_remove(operation,
                                    name,
                                    position,
                                    action,
                                    direction,
                                    icmp_type='5',
                                    icmp_code='5')

            # Assert
            m_client.get_profile.assert_called_once_with(name)
            m_client.profile_update_rules.assert_called_once_with(m_Profile)
            if direction_arg == 'inbound':
                self.assertEqual(m_Rules.inbound_rules, [new_rule, rule])
            else:
                self.assertEqual(m_Rules.outbound_rules, [new_rule, rule])
예제 #4
0
    def test_profile_rule_add_remove_add_rule_front_of_list(
            self, direction_arg, position_arg):
        """
        Test for profile_rule_add_remove function when adding a new Rule to the
        front of the inbound or outbound rules list.

        Test for both directions - inbound and outbound.

        Test for multiple positions less than 0
        (including positions out of range)
        """
        with patch('calico_ctl.profile.client', autospec=True) as m_client:
            # Setup arguments to pass to method under test
            operation = 'add'
            name = 'profile1'
            position = position_arg
            action = 'allow'
            direction = direction_arg

            # Set up Mock objects
            rule = Rule()
            m_Rules = Mock(spec=Rules, id=name, inbound_rules=[rule],
                       outbound_rules=[rule])
            m_Profile = Mock(spec=Profile, name=name, rules=m_Rules)
            m_client.get_profile.return_value = m_Profile

            # Set up new rule that function will create/add - compare in asserts
            rule_dict = {'action': 'allow', 'icmp_type':5, 'icmp_code':5}
            new_rule = Rule(**rule_dict)

            # Call method under test
            profile_rule_add_remove(operation, name, position, action, direction,
                                    icmp_type='5', icmp_code='5')

            # Assert
            m_client.get_profile.assert_called_once_with(name)
            m_client.profile_update_rules.assert_called_once_with(m_Profile)
            if direction_arg == 'inbound':
                self.assertEqual(m_Rules.inbound_rules, [new_rule, rule])
            else:
                self.assertEqual(m_Rules.outbound_rules, [new_rule, rule])
예제 #5
0
    def test_profile_rule_add_remove_remove_inbound(self, direction_arg,
                                                    position_arg):
        """
        Test for profile_rule_add_remove function when removing a Rule from a
        Profile.

        -  Test both direction - inbound and outbound.
        -  Test positions None and 1  (out of bounds tested elsewhere).
        """
        with patch('calico_ctl.profile.client', autospec=True) as m_client:
            # Setup arguments to pass to method under test
            operation = 'remove'
            name = 'profile1'
            position = position_arg
            action = 'allow'
            direction = direction_arg

            # Set up Mock objects
            rule_dict = {'action': 'allow'}
            rule = Rule(**rule_dict)
            m_Rules = Mock(spec=Rules,
                           id=name,
                           inbound_rules=[rule],
                           outbound_rules=[rule])
            m_Profile = Mock(spec=Profile, name=name, rules=m_Rules)
            m_client.get_profile.return_value = m_Profile

            # Call method under test
            profile_rule_add_remove(operation, name, position, action,
                                    direction)

            # Assert
            m_client.get_profile.assert_called_once_with(name)
            m_client.profile_update_rules.assert_called_once_with(m_Profile)
            if direction_arg == 'inbound':
                self.assertEqual(m_Rules.inbound_rules, [])
                self.assertEqual(m_Rules.outbound_rules, [rule])
            else:
                self.assertEqual(m_Rules.outbound_rules, [])
                self.assertEqual(m_Rules.inbound_rules, [rule])
예제 #6
0
    def test_profile_rule_add_remove_remove_inbound(self, direction_arg,
                                                    position_arg):
        """
        Test for profile_rule_add_remove function when removing a Rule from a
        Profile.

        -  Test both direction - inbound and outbound.
        -  Test positions None and 1  (out of bounds tested elsewhere).
        """
        with patch('calico_ctl.profile.client', autospec=True) as m_client:
            # Setup arguments to pass to method under test
            operation = 'remove'
            name = 'profile1'
            position = position_arg
            action = 'allow'
            direction = direction_arg

            # Set up Mock objects
            rule_dict = {'action': 'allow'}
            rule = Rule(**rule_dict)
            m_Rules = Mock(spec=Rules, id=name, inbound_rules=[rule],
                           outbound_rules=[rule])
            m_Profile = Mock(spec=Profile, name=name, rules=m_Rules)
            m_client.get_profile.return_value = m_Profile

            # Call method under test
            profile_rule_add_remove(operation, name, position, action, direction)

            # Assert
            m_client.get_profile.assert_called_once_with(name)
            m_client.profile_update_rules.assert_called_once_with(m_Profile)
            if direction_arg == 'inbound':
                self.assertEqual(m_Rules.inbound_rules, [])
                self.assertEqual(m_Rules.outbound_rules, [rule])
            else:
                self.assertEqual(m_Rules.outbound_rules, [])
                self.assertEqual(m_Rules.inbound_rules, [rule])