Module: Kerbi::Mixins::CmBackendTesting

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

Instance Method Summary collapse

Instance Method Details

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



85
86
87
88
89
90
91
92
# File 'lib/mixins/cm_backend_testing.rb', line 85

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)
    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



78
79
80
81
82
83
# File 'lib/mixins/cm_backend_testing.rb', line 78

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
# File 'lib/mixins/cm_backend_testing.rb', line 22

def read_write_ready?
  namespace_exists? && resource_exists?
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



26
27
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
# File 'lib/mixins/cm_backend_testing.rb', line 26

def test_connection(options={})
  res_name = Kerbi::State::Consts::RESOURCE_NAME
  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/#{res_name} exists"
    }
  ]

  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]
    puts "\n---EXCEPTIONS---\n".colorize(:red).bold
    exceptions.each do |exc|
      puts "[#{exc[:test]}] #{exc[:exception]}".to_s.colorize(:red).bold
      puts exc[:exception].backtrace
      puts "\n\n"
    end
  end
end

#test_list_namespacesObject



69
70
71
# File 'lib/mixins/cm_backend_testing.rb', line 69

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

#test_target_ns_existsObject



73
74
75
# File 'lib/mixins/cm_backend_testing.rb', line 73

def test_target_ns_exists
  client.get_namespace namespace
end