Module: KubernetesHarness::HarnessFile
- Defined in:
- lib/k8s_harness/harness_file.rb
Overview
This module handles reading and validating .k8sharness files.
Class Method Summary collapse
- .convert_to_commands(options) ⇒ Object
- .default_harness_file_path ⇒ Object
- .exec_command!(options, key, message) ⇒ Object
- .execute_setup!(options) ⇒ Object
-
.execute_teardown!(options) ⇒ Object
TODO: Tests missing (but execute_setup! has a test and implementation is the same.).
-
.execute_tests!(options) ⇒ Object
TODO: Tests missing (but execute_setup! has a test and implementation is the same.).
- .harness_file(options) ⇒ Object
- .harness_file_path(options) ⇒ Object
- .render(options = {}) ⇒ Object
- .test_present?(options) ⇒ Boolean
- .validate(options) ⇒ Object
Class Method Details
.convert_to_commands(options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/k8s_harness/harness_file.rb', line 42 def self.convert_to_commands() # TODO: Currently, we are assuming that the steps provided in the .k8sharness # will always be invoked in a shell. # First, we shouldn't assume that the user will want to use `sh` for these commands. # Second, we should allow users to invoke code in the language of their preference to # maximize codebase homogeneity. rendered = harness_file() rendered.each_key do |key| if rendered[key].match?(/.(sh|bash|zsh)$/) rendered[key] = "sh #{rendered[key]}" else rendered[key] = "sh -c '#{Shellwords.escape(rendered[key])}'" \ unless rendered[key].match?(/^(sh|bash|zsh) -c/) end end end |
.default_harness_file_path ⇒ Object
75 76 77 |
# File 'lib/k8s_harness/harness_file.rb', line 75 def self.default_harness_file_path "#{Dir.pwd}/.k8sharness" end |
.exec_command!(options, key, message) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/k8s_harness/harness_file.rb', line 24 def self.exec_command!(, key, ) rendered = render() raise 'No tests found' if (key == :test) && !rendered.key?(:test) KubernetesHarness.logger.debug "Checking for: #{key}" return nil unless rendered.key? key KubernetesHarness.nice_logger.info command = KubernetesHarness::ShellCommand.new(rendered[key]) command.execute! KubernetesHarness.logger.error command.stderr unless command.stderr.empty? puts command.stdout end |
.execute_setup!(options) ⇒ Object
10 11 12 |
# File 'lib/k8s_harness/harness_file.rb', line 10 def self.execute_setup!() exec_command!(, :setup, 'Setting up your tests.') end |
.execute_teardown!(options) ⇒ Object
TODO: Tests missing (but execute_setup! has a test and implementation is the same.)
20 21 22 |
# File 'lib/k8s_harness/harness_file.rb', line 20 def self.execute_teardown!() exec_command!(, :teardown, 'Tearing down your test bench.') end |
.execute_tests!(options) ⇒ Object
TODO: Tests missing (but execute_setup! has a test and implementation is the same.)
15 16 17 |
# File 'lib/k8s_harness/harness_file.rb', line 15 def self.execute_tests!() exec_command!(, :test, 'Running your tests.') end |
.harness_file(options) ⇒ Object
87 88 89 |
# File 'lib/k8s_harness/harness_file.rb', line 87 def self.harness_file() YAML.safe_load(File.read(harness_file_path()), symbolize_names: true) end |
.harness_file_path(options) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/k8s_harness/harness_file.rb', line 79 def self.harness_file_path() if !.nil? && .key?(:alternate_harnessfile) [:alternate_harnessfile] else default_harness_file_path end end |
.render(options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/k8s_harness/harness_file.rb', line 59 def self.render( = {}) fp = harness_file_path() raise "k8s-harness file not found at: #{fp}" unless File.exist? fp return convert_to_commands() if test_present?() raise KeyError, " It appears that your test isn't defined in \#{fp}. Ensure that \\\n a key called 'test' is in \#{fp}. See .k8sharness.example for \\\n an example of what a valid .k8sharness looks like.\n MESSAGE\nend\n".strip |
.test_present?(options) ⇒ Boolean
38 39 40 |
# File 'lib/k8s_harness/harness_file.rb', line 38 def self.test_present?() harness_file().key? :test end |
.validate(options) ⇒ Object
71 72 73 |
# File 'lib/k8s_harness/harness_file.rb', line 71 def self.validate() puts YAML.dump(render(.to_h)) end |