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 vulcand backend
Browse files Browse the repository at this point in the history
  • Loading branch information
a5huynh committed Feb 12, 2016
1 parent 72823e2 commit 9c369f0
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions tests/backends/test_vulcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# @Author: ahuynh
# @Date: 2016-02-11 14:28:02
# @Last Modified by: ahuynh
# @Last Modified time: 2016-02-11 15:33:08
# @Last Modified time: 2016-02-11 17:27:07
import unittest

from sidekick import announce_services, find_matching_container, parse_args

from tests import MockEtcd
from unittest.mock import patch


class TestVulcandBackend( unittest.TestCase ):
Expand All @@ -23,18 +23,19 @@ def setUp( self ):
])

self.etcd_client = MockEtcd()

self.container = {
'Image': 'image:latest',
'Ports': [{
'PrivatePort': 9200,
'IP': '0.0.0.0',
'Type': 'tcp',
'PublicPort': 9200 }, {
'PublicPort': 9200
}, {
'PrivatePort': 9300,
'IP': '0.0.0.0',
'Type': 'tcp',
'PublicPort': 9300}],
'PublicPort': 9300
}],
'Created': 1427906382,
'Names': ['/test'],
'Status': 'Up 2 days'
Expand All @@ -43,4 +44,20 @@ def setUp( self ):
def test_vulcand_announce( self ):
""" Test `announce_services` functionality """
services = find_matching_container( [ self.container ], self.args )
announce_services( services.items(), 'test', self.etcd_client, 0, 0, True )

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

# Unsuccessful health check
self.etcd_client._reset()
with patch( 'sidekick.check_health', return_value=False ):
announce_services( services.items(), 'test', self.etcd_client, 0, 0, True )
self.assertEqual( len( self.etcd_client.deleted ), 4 )

# 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, True )
self.assertEquals( str(mock_method.call_args[0][0]), 'Test Exception' )

0 comments on commit 9c369f0

Please sign in to comment.