test_kubernetes.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from elyra.util.kubernetes import is_valid_kubernetes_resource_name
  17. def test_is_valid_kubernetes_resource_name_invalid_input():
  18. """
  19. Verify that is_valid_kubernetes_resource_name detects whether or
  20. not a given string is a valid Kubernetes resource name:
  21. - contains no more than 253 characters
  22. - contain only lowercase alphanumeric characters, '-' or '.'
  23. - start with an alphanumeric character
  24. - end with an alphanumeric character
  25. """
  26. # test length violations
  27. assert not is_valid_kubernetes_resource_name(name=None) # Too short
  28. assert not is_valid_kubernetes_resource_name(name="") # Too short
  29. assert not is_valid_kubernetes_resource_name(name="a" * 254) # Too long
  30. # test first character violations (not alphanum or lowercase)
  31. assert not is_valid_kubernetes_resource_name(name="-a")
  32. assert not is_valid_kubernetes_resource_name(name=".b")
  33. assert not is_valid_kubernetes_resource_name(name=" c")
  34. assert not is_valid_kubernetes_resource_name(name="Dave")
  35. # test last character violations (not alphanum or lowercase)
  36. assert not is_valid_kubernetes_resource_name(name="a-")
  37. assert not is_valid_kubernetes_resource_name(name="b.")
  38. assert not is_valid_kubernetes_resource_name(name="c ")
  39. assert not is_valid_kubernetes_resource_name(name="sw33T")
  40. # test middle characters violations
  41. assert not is_valid_kubernetes_resource_name(name="aBBa")
  42. assert not is_valid_kubernetes_resource_name(name="b b")
  43. def test_is_valid_kubernetes_resource_name_valid_input():
  44. """
  45. Verify that is_valid_kubernetes_resource_name detects whether or
  46. not a given string is a valid Kubernetes resource name:
  47. - contains no more than 253 characters
  48. - contain only lowercase alphanumeric characters, '-' or '.'
  49. - start with an alphanumeric character
  50. - end with an alphanumeric character
  51. """
  52. # test valid names
  53. assert is_valid_kubernetes_resource_name(name="l0l")
  54. assert is_valid_kubernetes_resource_name(name="l-l")
  55. assert is_valid_kubernetes_resource_name(name="l.l")
  56. assert is_valid_kubernetes_resource_name(name="4-you")
  57. assert is_valid_kubernetes_resource_name(name="you.2")