test_validators.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  3. # Copyright 2013, Red Hat, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. import os
  17. import shutil
  18. import tempfile
  19. from unittest import TestCase
  20. from packstack.installer.validators import *
  21. from ..test_base import PackstackTestCaseMixin
  22. class ValidatorsTestCase(PackstackTestCaseMixin, TestCase):
  23. def setUp(self):
  24. # Creating a temp directory that can be used by tests
  25. self.tempdir = tempfile.mkdtemp()
  26. def tearDown(self):
  27. # remove the temp directory
  28. shutil.rmtree(self.tempdir)
  29. def test_validate_integer(self):
  30. """Test packstack.installer.validators.validate_integer."""
  31. validate_integer('1')
  32. self.assertRaises(ParamValidationError, validate_integer, 'test')
  33. def test_validate_regexp(self):
  34. """Test packstack.installer.validators.validate_regexp."""
  35. validate_regexp('Test_123', options=['\w'])
  36. self.assertRaises(ParamValidationError, validate_regexp,
  37. '!#$%', options=['\w'])
  38. def test_validate_port(self):
  39. """Test packstack.installer.validators.validate_port."""
  40. validate_port('666')
  41. self.assertRaises(ParamValidationError, validate_port, 'test')
  42. self.assertRaises(ParamValidationError, validate_port, '-3')
  43. def test_validate_not_empty(self):
  44. """Test packstack.installer.validators.validate_not_empty."""
  45. validate_not_empty('test')
  46. validate_not_empty(False)
  47. self.assertRaises(ParamValidationError, validate_not_empty, '')
  48. self.assertRaises(ParamValidationError, validate_not_empty, [])
  49. self.assertRaises(ParamValidationError, validate_not_empty, {})
  50. def test_validate_options(self):
  51. """Test packstack.installer.validators.validate_options."""
  52. validate_options('a', options=['a', 'b'])
  53. validate_options('b', options=['a', 'b'])
  54. self.assertRaises(ParamValidationError, validate_options,
  55. 'c', options=['a', 'b'])
  56. def test_validate_ip(self):
  57. """Test packstack.installer.validators.validate_ip."""
  58. validate_ip('127.0.0.1')
  59. validate_ip('::1')
  60. self.assertRaises(ParamValidationError, validate_ip, 'test')
  61. def test_validate_file(self):
  62. """Test packstack.installer.validators.validate_file."""
  63. dname = os.path.join(self.tempdir, '.test_validate_file')
  64. bad_name = os.path.join(self.tempdir, '.me_no/exists')
  65. os.mkdir(dname)
  66. validate_writeable_directory(dname)
  67. self.assertRaises(ParamValidationError, validate_writeable_directory, bad_name)
  68. def test_validate_writeable_directory(self):
  69. """Test packstack.installer.validators.validate_writeable_directory."""
  70. fname = os.path.join(self.tempdir, '.test_validate_writeable_directory')
  71. bad_name = os.path.join(self.tempdir, '.me_no_exists')
  72. with open(fname, 'w') as f:
  73. f.write('test')
  74. validate_file(fname)
  75. self.assertRaises(ParamValidationError, validate_file, bad_name)
  76. def test_validate_ping(self):
  77. """Test packstack.installer.validators.validate_ping."""
  78. # ping to broadcast fails
  79. self.assertRaises(ParamValidationError, validate_ping,
  80. '255.255.255.255')
  81. def test_validate_ssh(self):
  82. """Test packstack.installer.validators.validate_ssh."""
  83. # ssh to broadcast fails
  84. self.assertRaises(ParamValidationError, validate_ssh,
  85. '255.255.255.255')
  86. def test_validate_float(self):
  87. """Test packstack.installer.validators.validate_float."""
  88. validate_float('5.3')
  89. self.assertRaises(ParamValidationError, validate_float, 'test')