Module: KubernetesHarness::HarnessFile

Defined in:
lib/k8s_harness/harness_file.rb

Overview

This module handles reading and validating .k8sharness files.

Class Method Summary collapse

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(options)
  # 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(options)
  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_pathObject



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!(options, key, message)
  rendered = render(options)
  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 message
  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!(options)
  exec_command!(options, :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!(options)
  exec_command!(options, :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!(options)
  exec_command!(options, :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(options)
  YAML.safe_load(File.read(harness_file_path(options)), 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(options)
  if !options.nil? && options.key?(:alternate_harnessfile)
    options[:alternate_harnessfile]
  else
    default_harness_file_path
  end
end

.render(options = {}) ⇒ Object

Raises:

  • (KeyError)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/k8s_harness/harness_file.rb', line 59

def self.render(options = {})
  fp = harness_file_path(options)
  raise "k8s-harness file not found at: #{fp}" unless File.exist? fp
  return convert_to_commands(options) if test_present?(options)

  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

Returns:

  • (Boolean)


38
39
40
# File 'lib/k8s_harness/harness_file.rb', line 38

def self.test_present?(options)
  harness_file(options).key? :test
end

.validate(options) ⇒ Object



71
72
73
# File 'lib/k8s_harness/harness_file.rb', line 71

def self.validate(options)
  puts YAML.dump(render(options.to_h))
end