Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Use mock.patch to test out socket/exception paths for normal sidekick…
Browse files Browse the repository at this point in the history
… backend
  • Loading branch information
a5huynh committed Feb 12, 2016
1 parent 9c369f0 commit e0e20f3
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions tests/test_sidekick.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# @Author: ahuynh
# @Date: 2015-06-18 20:15:30
# @Last Modified by: ahuynh
# @Last Modified time: 2016-02-11 16:42:23
# @Last Modified time: 2016-02-11 17:27:50
import unittest

from sidekick import announce_services, check_name, find_matching_container
from sidekick import check_health, public_ports, parse_args

from tests import MockEtcd
from unittest.mock import patch


class TestSidekick( unittest.TestCase ):
Expand Down Expand Up @@ -38,14 +38,38 @@ def setUp( self ):
def test_announce_services( self ):
""" Test `announce_services` functionality """
services = find_matching_container( [self.container], self.args )
announce_services( services.items(), 'test', self.etcd_client, 0, 0, False )

# Test successful health check
with patch( 'sidekick.check_health', return_value=True ):
announce_services( services.items(), 'test', self.etcd_client, 0, 0, False )
self.assertEqual( len( self.etcd_client.written.keys() ), 2 )

# Test unsuccessful health check
with patch( 'sidekick.check_health', return_value=False ):
announce_services( services.items(), 'test', self.etcd_client, 0, 0, False )
self.assertEqual( len( self.etcd_client.deleted ), 2 )

# Test correct etcd exception handling
self.etcd_client = MockEtcd( raise_exception=True )
with patch( 'logging.error' ) as mock_method:
announce_services( services.items(), 'test', self.etcd_client, 0, 0, False )
self.assertEquals( str(mock_method.call_args[0][0]), 'Test Exception' )

def test_check_health( self ):
""" Test `check_health` functionality """
results = find_matching_container( [self.container], self.args )

for value in results.values():
# Unsuccessful socket connection
self.assertFalse( check_health( value ) )

# Successful socket connection
with patch( 'socket.socket.connect' ) as mock_method:
check_health( value )
args = mock_method.call_args[0][0]
self.assertEqual( args[0], value[ 'check_ip' ] )
self.assertEqual( args[1], value[ 'port' ] )

def test_check_name( self ):
""" Test `check_name` functionality """
self.assertTrue( check_name( self.container, 'test' ) )
Expand Down

0 comments on commit e0e20f3

Please sign in to comment.