Get tests working in this branch

This commit is contained in:
Greg Neagle
2016-12-31 14:18:30 -08:00
parent 1c7b6d5e2e
commit aa13257c5b
10 changed files with 45 additions and 45 deletions

View File

@@ -0,0 +1,57 @@
Munki Tests
===========
Testing key areas of Munki functionality is important as it can help us catch errors that tend to be hard to detect with code review.
The tests found here should focus on functions as a whole (preferably the larger, more complex ones). The general rule is to write tests for any function that is more than 200 lines in length. Once there is sufficient coverage for these functions, then additional functions can be targeted as desired.
Getting started
---------------
To run the test suite, install mock (ships with later versions of unittest, but alas is not on OS X yet.)
sudo easy_install mock
Then run the following command from the *root* of the project directory:
python -m unittest discover
This will run all the tests and show the results. If any tests fail, it will throw an exception with the name of the test which failed.
Writing tests
-------------
All unit tests are written using the unittest and mock frameworks. There are many tutorials and books written on how to write unittests, so if you'd like to learn more, *Python Testing Cookbook* by Greg L. Turnquist is a good resource and a good place to start.
The directory structure inside the `tests` directory models the structure of the `code` directory. If a function in munkicommon is being tested, be sure to place the test in the munkicommon directory within the tests directory structure.
For instance, here is the directory structure for all of the tests:
tests
├── README.md
└── code
└── client
└── munkilib
├── appleupdates_test.py
└── munkicommon
├── munkicommon_data_scaffolds.py
├── test_munkicommon_isapprunning.py
└── test_munkicommon_unicode.py
If you want to add tests for the `blockingApplicationsRunning` function, you would name it `test_blockingapplicationsrunning.py` and place it in `tests/code/client/munkilib/munkicommon`. The resulting file structure would look like this:
tests
├── README.md
└── code
└── client
└── munkilib
├── appleupdates_test.py
└── munkicommon
├── data_scaffolds.py
├── test_blockingapplicationsrunning.py
├── test_isapprunning.py
└── test_unicode.py
Make sure to always name the tests in a way that clearly describes what is being tested.
Note the test file must start with `test_` otherwise unittest discover will not find it. For examples of how to mock various types of data, take a look at existing tests and reference *Python Testing Cookbook* by Greg L. Turnquist. Also, feel free to ping @natewalck in #munki on the [MacAdmins Slack](https://macadmins.herokuapp.com/) with any unittest related questions.

View File

View File

View File

@@ -0,0 +1,158 @@
def getRunningProcessesMock():
return [
'/sbin/launchd',
'/Applications/Firefox.app/Contents/MacOS/firefox',
'/usr/sbin/syslogd',
'/usr/libexec/UserEventAgent',
'/usr/libexec/kextd',
'/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/Support/fseventsd',
'/usr/libexec/thermald',
'/usr/libexec/configd',
'/System/Library/CoreServices/appleeventsd',
'/System/Library/CoreServices/powerd.bundle/powerd',
'/usr/libexec/airportd',
'/usr/libexec/warmd',
'/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support/mds',
'/System/Library/CoreServices/iconservicesd',
'/System/Library/CoreServices/iconservicesagent',
'/usr/libexec/diskarbitrationd',
'/usr/libexec/coreduetd',
'/usr/libexec/wdhelper',
'/System/Library/PrivateFrameworks/WirelessDiagnostics.framework/Support/awdd',
'/usr/libexec/opendirectoryd',
'/usr/sbin/wirelessproxd',
'/usr/libexec/discoveryd',
'/System/Library/PrivateFrameworks/ApplePushService.framework/apsd',
'/System/Library/CoreServices/launchservicesd',
'/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/Resources/usbmuxd',
'/usr/sbin/securityd',
'/usr/sbin/blued',
'/Library/Application Support/VMware Tools/vmware-tools-daemon',
'/usr/libexec/stackshot',
'/System/Library/PrivateFrameworks/GenerationalStorage.framework/Versions/A/Support/revisiond',
'/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow',
'/System/Library/CoreServices/logind',
'/usr/sbin/KernelEventAgent',
'/usr/libexec/hidd',
'/usr/libexec/taskgated',
'/usr/sbin/notifyd',
'/usr/sbin/distnoted',
'/System/Library/CoreServices/coreservicesd',
'/System/Library/Frameworks/Security.framework/Versions/A/XPCServices/authd.xpc/Contents/MacOS/authd',
'/usr/sbin/cfprefsd',
'/usr/libexec/diagnosticd',
'/System/Library/Frameworks/PCSC.framework/Versions/A/XPCServices/com.apple.ctkpcscd.xpc/Contents/MacOS/com.apple.ctkpcscd',
'/System/Library/Frameworks/CryptoTokenKit.framework/ctkd',
'/usr/libexec/secinitd',
'/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Resources/WindowServer',
'/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mds_stores',
'/usr/libexec/discoveryd_helper',
'/usr/libexec/networkd',
'/usr/libexec/networkd_privileged',
'/usr/libexec/nsurlsessiond',
'/usr/libexec/nehelper',
'/usr/libexec/usbd',
'/usr/sbin/ntpd',
'/System/Library/CryptoTokenKit/com.apple.ifdreader.slotd/Contents/MacOS/com.apple.ifdreader',
'/System/Library/Frameworks/CoreMediaIO.framework/Resources/VDC.plugin/Contents/Resources/VDCAssistant',
'/usr/sbin/netbiosd',
'/System/Library/CoreServices/Software Update.app/Contents/Resources/softwareupdated',
'/System/Library/PrivateFrameworks/SoftwareUpdate.framework/Resources/suhelperd',
'/usr/libexec/watchdogd',
'/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/CVMServer',
'/System/Library/PrivateFrameworks/AmbientDisplay.framework/Versions/A/XPCServices/com.apple.AmbientDisplayAgent.xpc/Contents/MacOS/com.apple.AmbientDisplayAgent',
'/System/Library/Frameworks/Security.framework/Versions/A/XPCServices/com.apple.CodeSigningHelper.xpc/Contents/MacOS/com.apple.CodeSigningHelper',
'/usr/libexec/nsurlstoraged',
'/System/Library/PrivateFrameworks/PerformanceAnalysis.framework/XPCServices/com.apple.PerformanceAnalysis.animationperfd.xpc/Contents/MacOS/com.apple.PerformanceAnalysis.animationperfd',
'/System/Library/PrivateFrameworks/TCC.framework/Resources/tccd',
'/System/Library/PrivateFrameworks/AccountPolicy.framework/XPCServices/com.apple.AccountPolicyHelper.xpc/Contents/MacOS/com.apple.AccountPolicyHelper',
'/usr/libexec/systemstatsd',
'/usr/libexec/securityd_service',
'/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal',
'/System/Library/CoreServices/Dock.app/Contents/MacOS/Dock',
'/System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/SystemUIServer',
'/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder',
'/usr/sbin/pboard',
'/usr/sbin/coreaudiod',
'/System/Library/Frameworks/CoreAudio.framework/Versions/A/XPCServices/com.apple.audio.DriverHelper.xpc/Contents/MacOS/com.apple.audio.DriverHelper',
'/System/Library/Frameworks/ApplicationServices.framework/Frameworks/ATS.framework/Support/fontd',
'/System/Library/CoreServices/Spotlight.app/Contents/MacOS/Spotlight',
'/System/Library/PrivateFrameworks/CacheDelete.framework/deleted',
'/System/Library/PrivateFrameworks/CloudKitDaemon.framework/Support/cloudd',
'/System/Library/PrivateFrameworks/CloudDocsDaemon.framework/Versions/A/Support/bird',
'/usr/sbin/usernoted',
'/usr/libexec/locationd',
'/System/Library/PrivateFrameworks/CalendarAgent.framework/Executables/CalendarAgent',
'/usr/sbin/filecoordinationd',
'/System/Library/CoreServices/Dock.app/Contents/XPCServices/com.apple.dock.extra.xpc/Contents/MacOS/com.apple.dock.extra',
'/System/Library/Frameworks/Accounts.framework/Versions/A/Support/accountsd',
'/System/Library/CoreServices/backupd.bundle/Contents/Resources/TMCacheDelete',
'/usr/libexec/sharingd',
'/usr/libexec/pkd',
'/System/Library/PrivateFrameworks/IDS.framework/identityservicesd.app/Contents/MacOS/identityservicesd',
'/System/Library/PrivateFrameworks/ParsecUI.framework/Versions/A/Support/SpotlightNetHelper.app/Contents/MacOS/SpotlightNetHelper',
'/System/Library/PrivateFrameworks/SyncedDefaults.framework/Support/syncdefaultsd',
'/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Resources/storeaccountd',
'/System/Library/PrivateFrameworks/MessagesKit.framework/Resources/soagent.app/Contents/MacOS/soagent',
'/System/Library/PrivateFrameworks/CalendarAgent.framework/Versions/A/XPCServices/CalNCService.xpc/Contents/MacOS/CalNCService',
'/usr/libexec/secd',
'/System/Library/PrivateFrameworks/CallHistory.framework/Support/CallHistoryPluginHelper',
'/System/Library/PrivateFrameworks/CallHistory.framework/Support/CallHistorySyncHelper',
'/System/Library/PrivateFrameworks/IMCore.framework/imagent.app/Contents/MacOS/imagent',
'/System/Library/PrivateFrameworks/IMDPersistence.framework/XPCServices/IMDPersistenceAgent.xpc/Contents/MacOS/IMDPersistenceAgent',
'/System/Library/PrivateFrameworks/TelephonyUtilities.framework/callservicesd',
'/usr/libexec/fmfd',
'/System/Library/PrivateFrameworks/AOSKit.framework/Versions/A/XPCServices/com.apple.iCloudHelper.xpc/Contents/MacOS/com.apple.iCloudHelper',
'/System/Library/CoreServices/CoreServicesUIAgent.app/Contents/MacOS/CoreServicesUIAgent',
'/usr/libexec/spindump_agent',
'/System/Library/CoreServices/SocialPushAgent.app/Contents/MacOS/SocialPushAgent',
'/System/Library/CoreServices/Keychain Circle Notification.app/Contents/MacOS/Keychain Circle Notification',
'/System/Library/CoreServices/NotificationCenter.app/Contents/MacOS/NotificationCenter',
'/System/Library/CoreServices/AppleIDAuthAgent',
'/System/Library/PrivateFrameworks/AskPermission.framework/Versions/A/Resources/askpermissiond',
'/System/Library/PrivateFrameworks/HelpData.framework/Versions/A/Resources/helpd',
'/System/Library/CoreServices/diagnostics_agent',
'/Applications/iTunes.app/Contents/MacOS/iTunesHelper.app/Contents/MacOS/iTunesHelper',
'/usr/libexec/amfid',
'/System/Library/CoreServices/mapspushd',
'/System/Library/CoreServices/CrashReporterSupportHelper',
'/System/Library/Frameworks/Security.framework/Versions/A/Resources/CloudKeychainProxy.bundle/Contents/MacOS/CloudKeychainProxy',
'/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdflagwriter',
'/usr/libexec/SafariNotificationAgent',
'/System/Library/CoreServices/NotificationCenter.app/Contents/XPCServices/com.apple.notificationcenterui.WeatherSummary.xpc/Contents/MacOS/com.apple.notificationcenterui.WeatherSummary',
'/System/Library/CoreServices/SubmitDiagInfo',
'/System/Library/CoreServices/pbs',
'/System/Library/Services/AppleSpell.service/Contents/MacOS/AppleSpell',
'/System/Library/Frameworks/InputMethodKit.framework/Versions/A/XPCServices/com.apple.InputMethodKit.UserDictionary.xpc/Contents/MacOS/com.apple.InputMethodKit.UserDictionary',
'/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Resources/storelegacy',
'/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Resources/storeassetd',
'/System/Library/PrivateFrameworks/CommerceKit.framework/Resources/LaterAgent.app/Contents/MacOS/LaterAgent',
'/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Resources/storedownloadd',
'/usr/libexec/sandboxd',
'/System/Library/PrivateFrameworks/CoreSymbolication.framework/coresymbolicationd',
'/System/Library/PrivateFrameworks/DiskImages.framework/Resources/diskimages-helper',
'/System/Library/PrivateFrameworks/DiskImages.framework/Resources/hdiejectd',
'/System/Library/PrivateFrameworks/PackageKit.framework/Resources/installd',
'/usr/libexec/USBAgent',
'/System/Library/CoreServices/cloudphotosd.app/Contents/MacOS/cloudphotosd',
'/System/Library/PrivateFrameworks/CloudServices.framework/Resources/EscrowSecurityAlert.app/Contents/MacOS/EscrowSecurityAlert',
'/usr/sbin/aslmanager',
'/System/Library/PrivateFrameworks/Noticeboard.framework/Versions/A/Resources/nbagent.app/Contents/MacOS/nbagent',
'/usr/sbin/ocspd',
'/usr/libexec/periodic-wrapper',
'/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker',
'/System/Library/PrivateFrameworks/CloudPhotoServices.framework/Versions/A/Frameworks/CloudPhotoServicesConfiguration.framework/Versions/A/XPCServices/com.apple.CloudPhotosConfiguration.xpc/Contents/MacOS/com.apple.CloudPhotosConfiguration',
'/System/Library/PrivateFrameworks/PhotoLibraryPrivate.framework/Versions/A/Support/photolibraryd',
'/Applications/Safari.app/Contents/MacOS/Safari',
'/System/Library/StagedFrameworks/Safari/WebKit.framework/Versions/A/XPCServices/com.apple.WebKit.Networking.xpc/Contents/MacOS/com.apple.WebKit.Networking',
'/System/Library/PreferencePanes/DateAndTime.prefPane/Contents/Resources/TimeZone.prefPane/Contents/Resources/timezoned.app/Contents/MacOS/timezoned',
'/System/Library/StagedFrameworks/Safari/Safari.framework/Versions/A/XPCServices/com.apple.Safari.SearchHelper.xpc/Contents/MacOS/com.apple.Safari.SearchHelper',
'/System/Library/StagedFrameworks/Safari/WebKit.framework/Versions/A/XPCServices/com.apple.WebKit.WebContent.xpc/Contents/MacOS/com.apple.WebKit.WebContent',
'/System/Library/Frameworks/QTKit.framework/Versions/A/XPCServices/com.apple.qtkitserver.xpc/Contents/MacOS/com.apple.qtkitserver',
'/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker32',
'/System/Library/CoreServices/Software Update.app/Contents/Resources/softwareupdate_download_service',
'/System/Library/CoreServices/AssetCacheLocatorService',
'/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/XPCServices/com.apple.CommerceKit.TransactionService.xpc/Contents/MacOS/com.apple.CommerceKit.TransactionService',
'/usr/sbin/spindump',
'/bin/ps'
]

View File

@@ -0,0 +1,116 @@
#!/usr/bin/python
# encoding: utf-8
"""
test_display_unicode.py
Unit tests for display.display_* functions.
"""
# Copyright 2014-2016 Greg Neagle.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import munkilib.display as display
import unittest
MSG_UNI = u'Günther\'s favorite thing is %s'
MSG_STR = 'Günther\'s favorite thing is %s'
ARG_UNI = u'Günther'
ARG_STR = 'Günther'
def log(msg, logname=''):
"""Redefine the logging function so our tests don't write
a bunch of garbage to Munki's logs"""
pass
display.munkilog.log = log
class TestDisplayInfoUnicodeOutput(unittest.TestCase):
"""Test display_info with text that may or may not be proper
Unicode."""
def test_display_info_with_unicode_msg(self):
display.display_info(MSG_UNI)
def test_display_info_with_str_msg(self):
display.display_info(MSG_STR)
def test_display_info_with_unicode_msg_unicode_arg(self):
display.display_info(MSG_UNI, ARG_UNI)
def test_display_info_with_unicode_msg_str_arg(self):
display.display_info(MSG_UNI, ARG_STR)
def test_display_info_with_str_msg_unicode_arg(self):
display.display_info(MSG_STR, ARG_UNI)
def test_display_info_with_str_msg_str_arg(self):
display.display_info(MSG_STR, ARG_STR)
class TestDisplayWarningUnicodeOutput(unittest.TestCase):
"""Test display_warning with text that may or may not be proper
Unicode."""
def test_display_warning_with_unicode_msg(self):
display.display_warning(MSG_UNI)
def test_display_warning_with_str_msg(self):
display.display_warning(MSG_STR)
def test_display_warning_with_unicode_msg_unicode_arg(self):
display.display_warning(MSG_UNI, ARG_UNI)
def test_display_warning_with_unicode_msg_str_arg(self):
display.display_warning(MSG_UNI, ARG_STR)
def test_display_warning_with_str_msg_unicode_arg(self):
display.display_warning(MSG_STR, ARG_UNI)
def test_display_warning_with_str_msg_str_arg(self):
display.display_warning(MSG_STR, ARG_STR)
class TestDisplayErrorUnicodeOutput(unittest.TestCase):
"""Test display_error with text that may or may not be proper
Unicode."""
def test_display_error_with_unicode_msg(self):
display.display_error(MSG_UNI)
def test_display_error_with_str_msg(self):
display.display_error(MSG_STR)
def test_display_error_with_unicode_msg_unicode_arg(self):
display.display_error(MSG_UNI, ARG_UNI)
def test_display_error_with_unicode_msg_str_arg(self):
display.display_error(MSG_UNI, ARG_STR)
def test_display_error_with_str_msg_unicode_arg(self):
display.display_error(MSG_STR, ARG_UNI)
def test_display_error_with_str_msg_str_arg(self):
display.display_error(MSG_STR, ARG_STR)
def main():
unittest.main(buffer=True)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,115 @@
#!/usr/bin/python
# encoding: utf-8
"""
test_isapprunning.py
Unit tests for processes.isAppRunning.
"""
# Copyright 2016-present Nate Walck.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from munkilib import processes
import unittest
from data_scaffolds import getRunningProcessesMock
try:
from mock import patch
except ImportError:
import sys
print >>sys.stderr, "mock module is required. run: easy_install mock"
raise
class TestIsAppRunning(unittest.TestCase):
"""Test munkicommonisAppRunning for each match catch."""
def setUp(self):
return
def tearDown(self):
self.processes = []
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_with_exact_path_match(self, ps_mock):
print("Testing isAppRunning with exact path match...")
self.assertEqual(
processes.isAppRunning('/Applications/Firefox.app/Contents/MacOS/firefox'),
True
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_with_exact_path_no_match(self, ps_mock):
print("Testing isAppRunning with exact path no matches...")
self.assertEqual(
processes.isAppRunning('/usr/local/bin/bonzi'),
False
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_by_filename_match(self, ps_mock):
print("Testing isAppRunning with file name match...")
self.assertEqual(
processes.isAppRunning('Firefox.app'),
True
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_by_filename_no_match(self, ps_mock):
print("Testing isAppRunning with file name no matches...")
self.assertEqual(
processes.isAppRunning('BonziBUDDY.app'),
False
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_by_executable_name_match(self, ps_mock):
print("Testing isAppRunning with executable name match...")
self.assertEqual(
processes.isAppRunning('firefox'),
True
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_by_executable_name_no_match(self, ps_mock):
print("Testing isAppRunning with executable name no matches...")
self.assertEqual(
processes.isAppRunning('bonzi'),
False
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_name_with_dot_app_match(self, ps_mock):
print("Testing isAppRunning with name plus .app match...")
self.assertEqual(
processes.isAppRunning('Firefox'),
True
)
@patch('munkilib.processes.getRunningProcesses', return_value=getRunningProcessesMock())
def test_app_name_with_dot_app_no_match(self, ps_mock):
print("Testing isAppRunning with name plus .app match...")
self.assertEqual(
processes.isAppRunning('BonziBUDDY'),
False
)
def main():
unittest.main(buffer=True)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,21 @@
#!/usr/bin/python env
import subprocess
import sys
print("Checking code against pep8...")
ps = subprocess.Popen(['git', 'diff', 'HEAD^'], stdout=subprocess.PIPE)
tests = subprocess.Popen(['flake8', '--diff', '--ignore=E501'], stdin=ps.stdout, stdout=subprocess.PIPE)
out, err = tests.communicate()
if out:
print(out)
print("Time to clean the lint...")
sys.exit(1)
elif err:
print("An error occured!")
print(err)
sys.exit(1)
else:
print("No lint errors, yay!")
sys.exit(0)