Put Windows tests in their own class

This commit is contained in:
twangboy 2019-07-03 12:45:33 -06:00
parent 6cc8d9604a
commit 200e230fd9
No known key found for this signature in database
GPG Key ID: 93FF3BDEB278C9EB

View File

@ -1,9 +1,7 @@
# coding: utf-8
# Python libs
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import, print_function, unicode_literals
import os
import shutil
import tempfile
@ -43,6 +41,7 @@ def create(path, content=None):
@skipIf(not watchdog.HAS_WATCHDOG, 'watchdog is not available')
@skipIf(salt.utils.platform.is_windows(), 'Skip these tests on Windows')
class IWatchdogBeaconTestCase(TestCase, LoaderModuleMockMixin):
'''
Test case for salt.beacons.watchdog
@ -92,18 +91,11 @@ class IWatchdogBeaconTestCase(TestCase, LoaderModuleMockMixin):
ret = check_events(config)
# Windows only returns a single item for each event
# Other OS'es return 2 apparently
if salt.utils.platform.is_windows():
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'modified')
else:
self.assertEqual(len(ret), 2)
self.assertEqual(ret[0]['path'], os.path.dirname(path))
self.assertEqual(ret[0]['change'], 'modified')
self.assertEqual(ret[1]['path'], path)
self.assertEqual(ret[1]['change'], 'modified')
self.assertEqual(len(ret), 2)
self.assertEqual(ret[0]['path'], os.path.dirname(path))
self.assertEqual(ret[0]['change'], 'modified')
self.assertEqual(ret[1]['path'], path)
self.assertEqual(ret[1]['change'], 'modified')
def test_file_deleted(self):
path = os.path.join(self.tmpdir, 'tmpfile')
@ -144,18 +136,11 @@ class IWatchdogBeaconTestCase(TestCase, LoaderModuleMockMixin):
create(path)
ret = check_events(config)
# Windows only returns a single item for each event
# Other OS'es return 2 apparently
if salt.utils.platform.is_windows():
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
else:
self.assertEqual(len(ret), 2)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
self.assertEqual(ret[1]['path'], self.tmpdir)
self.assertEqual(ret[1]['change'], 'modified')
self.assertEqual(len(ret), 2)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
self.assertEqual(ret[1]['path'], self.tmpdir)
self.assertEqual(ret[1]['change'], 'modified')
def test_trigger_all_possible_events(self):
path = os.path.join(self.tmpdir, 'tmpfile')
@ -178,49 +163,165 @@ class IWatchdogBeaconTestCase(TestCase, LoaderModuleMockMixin):
ret = check_events(config)
# Windows only returns a single item for each event
# Other OS'es return 2 apparently
if salt.utils.platform.is_windows():
self.assertEqual(len(ret), 4)
self.assertEqual(len(ret), 8)
# create
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
# create
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
self.assertEqual(ret[1]['path'], self.tmpdir)
self.assertEqual(ret[1]['change'], 'modified')
# modify
self.assertEqual(ret[1]['path'], path)
self.assertEqual(ret[1]['change'], 'modified')
# modify
self.assertEqual(ret[2]['path'], path)
self.assertEqual(ret[2]['change'], 'modified')
self.assertEqual(ret[3]['path'], path)
self.assertEqual(ret[3]['change'], 'modified')
# move
self.assertEqual(ret[2]['path'], path)
self.assertEqual(ret[2]['change'], 'moved')
# move
self.assertEqual(ret[4]['path'], path)
self.assertEqual(ret[4]['change'], 'moved')
self.assertEqual(ret[5]['path'], self.tmpdir)
self.assertEqual(ret[5]['change'], 'modified')
# delete
self.assertEqual(ret[3]['path'], moved)
self.assertEqual(ret[3]['change'], 'deleted')
else:
self.assertEqual(len(ret), 8)
# delete
self.assertEqual(ret[6]['path'], moved)
self.assertEqual(ret[6]['change'], 'deleted')
self.assertEqual(ret[7]['path'], self.tmpdir)
self.assertEqual(ret[7]['change'], 'modified')
# create
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
self.assertEqual(ret[1]['path'], self.tmpdir)
self.assertEqual(ret[1]['change'], 'modified')
@skipIf(not watchdog.HAS_WATCHDOG, 'watchdog is not available')
@skipIf(not salt.utils.platform.is_windows(), 'Not a Windows system')
class IWatchdogBeaconWindowsTestCase(TestCase, LoaderModuleMockMixin):
'''
Test case for salt.beacons.watchdog on Windows
'''
# modify
self.assertEqual(ret[2]['path'], path)
self.assertEqual(ret[2]['change'], 'modified')
self.assertEqual(ret[3]['path'], path)
self.assertEqual(ret[3]['change'], 'modified')
def setup_loader_modules(self):
return {watchdog: {}}
# move
self.assertEqual(ret[4]['path'], path)
self.assertEqual(ret[4]['change'], 'moved')
self.assertEqual(ret[5]['path'], self.tmpdir)
self.assertEqual(ret[5]['change'], 'modified')
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
# delete
self.assertEqual(ret[6]['path'], moved)
self.assertEqual(ret[6]['change'], 'deleted')
self.assertEqual(ret[7]['path'], self.tmpdir)
self.assertEqual(ret[7]['change'], 'modified')
def tearDown(self):
watchdog.close({})
shutil.rmtree(self.tmpdir, ignore_errors=True)
def assertValid(self, config):
ret = watchdog.validate(config)
self.assertEqual(ret, (True, 'Valid beacon configuration'))
def test_empty_config(self):
config = [{}]
ret = watchdog.beacon(config)
self.assertEqual(ret, [])
def test_file_create(self):
path = os.path.join(self.tmpdir, 'tmpfile')
config = [{'directories': {self.tmpdir: {'mask': ['create']}}}]
self.assertValid(config)
self.assertEqual(watchdog.beacon(config), [])
create(path)
ret = check_events(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
def test_file_modified(self):
path = os.path.join(self.tmpdir, 'tmpfile')
config = [{'directories': {self.tmpdir: {'mask': ['modify']}}}]
self.assertValid(config)
self.assertEqual(watchdog.beacon(config), [])
create(path, 'some content')
ret = check_events(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'modified')
def test_file_deleted(self):
path = os.path.join(self.tmpdir, 'tmpfile')
create(path)
config = [{'directories': {self.tmpdir: {'mask': ['delete']}}}]
self.assertValid(config)
self.assertEqual(watchdog.beacon(config), [])
os.remove(path)
ret = check_events(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'deleted')
def test_file_moved(self):
path = os.path.join(self.tmpdir, 'tmpfile')
create(path)
config = [{'directories': {self.tmpdir: {'mask': ['move']}}}]
self.assertValid(config)
self.assertEqual(watchdog.beacon(config), [])
os.rename(path, path + '_moved')
ret = check_events(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'moved')
def test_file_create_in_directory(self):
config = [{'directories': {self.tmpdir: {'mask': ['create', 'modify']}}}]
self.assertValid(config)
self.assertEqual(watchdog.beacon(config), [])
path = os.path.join(self.tmpdir, 'tmpfile')
create(path)
ret = check_events(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
def test_trigger_all_possible_events(self):
path = os.path.join(self.tmpdir, 'tmpfile')
moved = path + '_moved'
config = [{'directories': {
self.tmpdir: {},
}}]
self.assertValid(config)
self.assertEqual(watchdog.beacon(config), [])
# create
create(path)
# modify
create(path, 'modified content')
# move
os.rename(path, moved)
# delete
os.remove(moved)
ret = check_events(config)
self.assertEqual(len(ret), 4)
# create
self.assertEqual(ret[0]['path'], path)
self.assertEqual(ret[0]['change'], 'created')
# modify
self.assertEqual(ret[1]['path'], path)
self.assertEqual(ret[1]['change'], 'modified')
# move
self.assertEqual(ret[2]['path'], path)
self.assertEqual(ret[2]['change'], 'moved')
# delete
self.assertEqual(ret[3]['path'], moved)
self.assertEqual(ret[3]['change'], 'deleted')