Module: Kerbi::Mixins::CmBackendTesting

Included in:
State::ConfigMapBackend
Defined in:
lib/mixins/cm_backend_testing.rb

Instance Method Summary collapse

Instance Method Details

#data_readable?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/mixins/cm_backend_testing.rb', line 86

def data_readable?
  entries.is_a?(Array)
end

#echo_init(msg, result, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/mixins/cm_backend_testing.rb', line 98

def echo_init(msg, result, options={})
  unless options[:quiet].present?
    outcome_str = result.present? ? "Already existed" : "Created"
    color = result.present? ? :green : :blue
    outcome_str = outcome_str.colorize(color)
    #noinspection RubyResolve
    puts "#{msg}: #{outcome_str}".bold
  end
end

#namespace_exists?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


6
7
8
9
10
11
12
# File 'lib/mixins/cm_backend_testing.rb', line 6

def namespace_exists?
  begin
    !!client("v1").get_namespace(namespace)
  rescue Kubeclient::ResourceNotFoundError
    false
  end
end

#puts_outcome(msg, result) ⇒ Object

noinspection RubyResolve



91
92
93
94
95
96
# File 'lib/mixins/cm_backend_testing.rb', line 91

def puts_outcome(msg, result)
  outcome_str = result.present? ? "Success" : "Failure"
  color = result.present? ? :green : :red
  outcome_str = outcome_str.colorize(color)
  puts "#{msg}: #{outcome_str}".bold
end

#read_write_ready?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/mixins/cm_backend_testing.rb', line 22

def read_write_ready?
  namespace_exists? && \
  resource_exists? && \
  data_readable?
end

#resource_exists?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/mixins/cm_backend_testing.rb', line 14

def resource_exists?
  begin
    !!resource
  rescue Kubeclient::ResourceNotFoundError
    false
  end
end

#test_connection(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mixins/cm_backend_testing.rb', line 28

def test_connection(options={})
  exceptions = []

  schema = [
    {
      method: :client,
      message: "1. Create Kubernetes client"
    },
    {
      method: :test_list_namespaces,
      message: "2. List cluster namespaces"
    },
    {
      method: :test_target_ns_exists,
      message: "3. Target namespace #{namespace} exists"
    },
    {
      method: :load_resource,
      message: "4. Resource #{namespace}/cm/#{cm_name} exists"
    },
    {
      method: :entries,
      message: "5. Data from resource is readable"
    }
  ]

  schema.each do |spec|
    begin
      self.send(spec[:method])
      puts_outcome(spec[:message], true)
    rescue StandardError => e
      puts_outcome(spec[:message], false)
      exceptions << { exception: e, test: spec[:message] }
    end
  end

  if exceptions.any? && options[:verbose]
    #noinspection RubyResolve
    puts "\n---EXCEPTIONS---\n".colorize(:red).bold
    exceptions.each do |exc|
      base = "[#{exc[:test]}] #{exc[:exception]}"
      #noinspection RubyResolve
      puts base.to_s.colorize(:red).bold
      puts exc[:exception].backtrace
      puts "\n\n"
    end
  end
end

#test_list_namespacesObject



77
78
79
80
# File 'lib/mixins/cm_backend_testing.rb', line 77

def test_list_namespaces
  #noinspection RubyResolve
  client("v1").get_namespaces.any?
end

#test_target_ns_existsObject



82
83
84
# File 'lib/mixins/cm_backend_testing.rb', line 82

def test_target_ns_exists
  client.get_namespace namespace
end