Skip to content

Commit

Permalink
Merge pull request #204 from faucetsdn/revert-197-merge_refactor_1
Browse files Browse the repository at this point in the history
Revert "Refactor Chewie for Better SoC"
  • Loading branch information
gizmoguy authored Dec 9, 2019
2 parents 32ca7c8 + 13737fc commit 6927ffa
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 354 deletions.
4 changes: 0 additions & 4 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
[MASTER]
jobs=4

[MESSAGES CONTROL]
disable=no-absolute-import


[TYPECHECK]
generated-members=socket.*
302 changes: 192 additions & 110 deletions chewie/chewie.py

Large diffs are not rendered by default.

133 changes: 0 additions & 133 deletions chewie/managed_port.py

This file was deleted.

4 changes: 2 additions & 2 deletions chewie/radius_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, radius_secret, server_id, logger):
self.packet_id_to_request_authenticator = {}

def process_outbound(self, radius_output_bits):
"""Placeholder method extracted from Chewie._send_radius_messages()"""
"""Placeholder method extracted from Chewie.send_radius_messages()"""
radius_payload = radius_output_bits.message
src_mac = radius_output_bits.src_mac
username = radius_output_bits.identity
Expand Down Expand Up @@ -71,7 +71,7 @@ def build_event_radius_message_received(self, radius):
return EventRadiusMessageReceived(radius, state, radius.attributes.to_dict())

def process_outbound_mab_request(self, radius_output_bits):
"""Placeholder method extracted from Chewie._send_radius_messages()"""
"""Placeholder method extracted from Chewie.send_radius_messages()"""
src_mac = radius_output_bits.src_mac
port_id = radius_output_bits.port_mac
self.logger.info("Sending MAB to RADIUS: %s", src_mac)
Expand Down
5 changes: 0 additions & 5 deletions chewie/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utility Functions"""
import logging
from collections import namedtuple # pytype: disable=pyi-error
import random


def get_logger(logname):
Expand All @@ -21,10 +20,6 @@ def wrapped(self, *args, **kwargs):
return wrapped


def get_random_id(): # pylint: disable=missing-docstring
return random.randint(0, 200)


class MessageParseError(Exception):
"""Error for when parsing cannot be successfully completed."""
pass
Expand Down
27 changes: 8 additions & 19 deletions test/unit/test_chewie.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from chewie.state_machines.mab_state_machine import MacAuthenticationBypassStateMachine
from eventlet.queue import Queue

from chewie.managed_port import ManagedPort
from helpers import FakeTimerScheduler

FROM_SUPPLICANT = Queue()
Expand All @@ -31,7 +30,7 @@
def patch_things(func):
"""decorator to mock patch socket operations and random number generators"""

@patch('chewie.managed_port.get_random_id', get_random_id_helper)
@patch('chewie.chewie.get_random_id', get_random_id_helper)
@patch('chewie.chewie.EapSocket', FakeEapSocket)
@patch('chewie.chewie.RadiusSocket', FakeRadiusSocket)
@patch('chewie.chewie.MabSocket', FakeMabSocket)
Expand Down Expand Up @@ -297,6 +296,7 @@ def test_get_state_machine(self):
state_machine = self.chewie.get_state_machine('12:34:56:78:9a:bc',
# pylint: disable=invalid-name
'00:00:00:00:00:01')

self.assertEqual(len(self.chewie.state_machines), 1)

self.assertIs(state_machine, self.chewie.get_state_machine('12:34:56:78:9a:bc',
Expand All @@ -314,7 +314,6 @@ def test_get_state_machine(self):
# port 2 has 1 mac
self.assertEqual(len(self.chewie.state_machines['00:00:00:00:00:02']), 1)

# TODO Stop Test from touching internal get_state_machine_from_radius_packet
def test_get_state_machine_by_packet_id(self):
"""Tests Chewie.get_state_machine_by_packet_id()"""
self.chewie.radius_lifecycle.packet_id_to_mac[56] = {'src_mac': '12:34:56:78:9a:bc',
Expand All @@ -323,10 +322,10 @@ def test_get_state_machine_by_packet_id(self):
# pylint: disable=invalid-name
'00:00:00:00:00:01')

self.assertIs(self.chewie._get_state_machine_from_radius_packet_id(56),
self.assertIs(self.chewie.get_state_machine_from_radius_packet_id(56),
state_machine)
with self.assertRaises(KeyError):
self.chewie._get_state_machine_from_radius_packet_id(20)
self.chewie.get_state_machine_from_radius_packet_id(20)

@patch_things
@setup_generators(sup_replies_success, radius_replies_success)
Expand All @@ -344,6 +343,9 @@ def test_success_dot1x(self):
'00:00:00:00:00:01').state,
FullEAPStateMachine.SUCCESS2)




@patch_things
@setup_generators(sup_replies_success, radius_replies_success)
def test_chewie_identity_response_dot1x(self):
Expand Down Expand Up @@ -401,7 +403,7 @@ def test_port_status_changes(self):
# This will keep adding jobs forever.
self.assertEqual(len(self.fake_scheduler.jobs), 1)
self.assertEqual(self.fake_scheduler.jobs[0].function.__name__,
ManagedPort.send_preemptive_identity_request.__name__)
Chewie.send_preemptive_identity_request_if_no_active_on_port.__name__)


@patch_things
Expand Down Expand Up @@ -543,16 +545,3 @@ def test_mab_failure_auth(self):
self.chewie.get_state_machine('02:42:ac:17:00:6f',
'00:00:00:00:00:01').state,
MacAuthenticationBypassStateMachine.AAA_FAILURE)

@patch_things
@setup_generators(sup_replies_success, radius_replies_success)
def test_smoke_test_clients(self):
"""Test success api"""
FROM_SUPPLICANT.put_nowait(bytes.fromhex("0000000000010242ac17006f888e01010000"))

pool = eventlet.GreenPool()
pool.spawn(self.chewie.run)

eventlet.sleep(1)
self.assertIsNotNone(self.chewie.clients)
self.assertEqual(len(self.chewie.clients), 1)
53 changes: 23 additions & 30 deletions test/unit/test_chewie_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from chewie.event import EventMessageReceived
from chewie.utils import EapQueueMessage

# pylint: disable=protected-access

def return_if(expected, return_value):
"""allows us to do expect-this-return-that style mocking"""
Expand All @@ -22,10 +21,8 @@ def inner_function(*args):
return inner_function


FakeLogger = namedtuple('FakeLogger', ('name',)
) # pylint: disable=invalid-name
FakeEapMessage = namedtuple(
'FakeEapMessage', ('src_mac',)) # pylint: disable=invalid-name
FakeLogger = namedtuple('FakeLogger', ('name',)) # pylint: disable=invalid-name
FakeEapMessage = namedtuple('FakeEapMessage', ('src_mac',)) # pylint: disable=invalid-name


class ChewieWithMocksTestCase(unittest.TestCase):
Expand All @@ -44,16 +41,14 @@ def setUp(self):
def test_eap_packet_in_goes_to_new_state_machine(self, state_machine,
ethernet_parse): # pylint: disable=invalid-name
"""test EAP packet creates a new state machine and is sent on"""
self.chewie._eap_socket = Mock(
**{'receive.return_value': 'message from socket'})
self.chewie.eap_socket = Mock(**{'receive.return_value': 'message from socket'})
ethernet_parse.side_effect = return_if(
('message from socket',),
(FakeEapMessage('fake src mac'), 'fake dst mac')
)
self.chewie._receive_eap_messages()
self.chewie.receive_eap_messages()
state_machine().event.assert_called_with(
EventMessageReceived(FakeEapMessage(
'fake src mac'), 'fake dst mac')
EventMessageReceived(FakeEapMessage('fake src mac'), 'fake dst mac')
)

@patch("chewie.chewie.Chewie.running", Mock(side_effect=[True, False]))
Expand All @@ -62,35 +57,34 @@ def test_eap_packet_in_goes_to_new_state_machine(self, state_machine,
def test_eap_output_packet_gets_packed_and_sent(self,
ethernet_pack): # pylint: disable=invalid-name
"""test EAP packet creates a new state machine and is sent on"""
self.chewie._eap_socket = Mock()
self.chewie.eap_socket = Mock()
ethernet_pack.return_value = "packed ethernet"
self.chewie.eap_output_messages.put_nowait(
EapQueueMessage("output eap message", "src mac", "port mac"))
self.chewie._send_eap_messages()
self.chewie._eap_socket.send.assert_called_with("packed ethernet")
self.chewie.send_eap_messages()
self.chewie.eap_socket.send.assert_called_with("packed ethernet")

@patch("chewie.chewie.Chewie.running", Mock(side_effect=[True, False]))
@patch("chewie.chewie.MessageParser.radius_parse")
@patch("chewie.chewie.Chewie._get_state_machine_from_radius_packet_id")
@patch("chewie.chewie.Chewie.get_state_machine_from_radius_packet_id")
@patch("chewie.chewie.sleep", Mock())
def test_radius_packet_in_goes_to_state_machine(self, state_machine,
radius_parse): # pylint: disable=invalid-name
"""test radius packet goes to a state machine"""
# note that the state machine has to exist already - if not then we blow up
fake_radius = namedtuple('Radius', ('packet_id',))('fake packet id')
self.chewie._radius_socket = Mock(
**{'receive.return_value': 'message from socket'})
self.chewie.radius_socket = Mock(**{'receive.return_value': 'message from socket'})
self.chewie.radius_lifecycle = Mock(**{'build_event_radius_message_received.side_effect':
return_if(
(fake_radius,),
'fake event'
)})
return_if(
(fake_radius,),
'fake event'
)})
radius_parse.side_effect = return_if(
('message from socket', 'SECRET', self.chewie.radius_lifecycle),
fake_radius
)
# not checking args as we can't mock the callback
self.chewie._receive_radius_messages()
self.chewie.receive_radius_messages()
state_machine().event.assert_called_with(
'fake event'
)
Expand All @@ -99,14 +93,13 @@ def test_radius_packet_in_goes_to_state_machine(self, state_machine,
@patch("chewie.chewie.sleep", Mock())
def test_radius_output_packet_gets_packed_and_sent(self): # pylint: disable=invalid-name
"""test EAP packet creates a new state machine and is sent on"""
self.chewie._radius_socket = Mock()
self.chewie.radius_socket = Mock()

self.chewie.radius_output_messages.put_nowait(
'fake radius output bits')
self.chewie.radius_output_messages.put_nowait('fake radius output bits')
self.chewie.radius_lifecycle = Mock(**{'process_outbound.side_effect':
return_if(
('fake radius output bits',),
'packed radius'
)})
self.chewie._send_radius_messages()
self.chewie._radius_socket.send.assert_called_with("packed radius")
return_if(
('fake radius output bits',),
'packed radius'
)})
self.chewie.send_radius_messages()
self.chewie.radius_socket.send.assert_called_with("packed radius")
Loading

0 comments on commit 6927ffa

Please sign in to comment.