test_boto_secgroup.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. # import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing Libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.case import TestCase
  7. # Import Salt Libs
  8. import salt.states.boto_secgroup as boto_secgroup
  9. from salt.utils.odict import OrderedDict
  10. class Boto_SecgroupTestCase(TestCase, LoaderModuleMockMixin):
  11. '''
  12. TestCase for salt.states.boto_secgroup module
  13. '''
  14. def setup_loader_modules(self):
  15. return {boto_secgroup: {}}
  16. def test__get_rule_changes_no_rules_no_change(self):
  17. '''
  18. tests a condition with no rules in present or desired group
  19. '''
  20. present_rules = []
  21. desired_rules = []
  22. self.assertEqual(boto_secgroup._get_rule_changes(desired_rules, present_rules), ([], []))
  23. def test__get_rule_changes_create_rules(self):
  24. '''
  25. tests a condition where a rule must be created
  26. '''
  27. present_rules = [OrderedDict([('ip_protocol', 'tcp'), ('from_port', 22), ('to_port', 22), ('cidr_ip', '0.0.0.0/0')])]
  28. desired_rules = [OrderedDict([('ip_protocol', 'tcp'), ('from_port', 22), ('to_port', 22), ('cidr_ip', '0.0.0.0/0')]),
  29. OrderedDict([('ip_protocol', 'tcp'), ('from_port', 80), ('to_port', 80), ('cidr_ip', '0.0.0.0/0')])]
  30. # can also use: rules_to_create = [rule for rule in desired_rules if rule not in present_rules]
  31. rules_to_create = [OrderedDict([('ip_protocol', 'tcp'), ('from_port', 80), ('to_port', 80), ('cidr_ip', '0.0.0.0/0')])]
  32. self.assertEqual(boto_secgroup._get_rule_changes(desired_rules, present_rules), ([], rules_to_create))
  33. def test__get_rule_changes_delete_rules(self):
  34. '''
  35. tests a condition where a rule must be deleted
  36. '''
  37. present_rules = [OrderedDict([('ip_protocol', 'tcp'), ('from_port', 22), ('to_port', 22), ('cidr_ip', '0.0.0.0/0')]),
  38. OrderedDict([('ip_protocol', 'tcp'), ('from_port', 80), ('to_port', 80), ('cidr_ip', '0.0.0.0/0')])]
  39. desired_rules = [OrderedDict([('ip_protocol', 'tcp'), ('from_port', 22), ('to_port', 22), ('cidr_ip', '0.0.0.0/0')])]
  40. # can also use: rules_to_delete = [rule for rule in present_rules if rule not in desired_rules]
  41. rules_to_delete = [OrderedDict([('ip_protocol', 'tcp'), ('from_port', 80), ('to_port', 80), ('cidr_ip', '0.0.0.0/0')])]
  42. self.assertEqual(boto_secgroup._get_rule_changes(desired_rules, present_rules), (rules_to_delete, []))