kubernetes-cli 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
1
+ # typed: false
2
+ require 'rspec/expectations'
3
+
4
+ class TestCommand
5
+ def initialize(cmd_s)
6
+ @cmd_s = cmd_s
7
+ @cmd = cmd_s.split(' ')
8
+ end
9
+
10
+ def includes?(args)
11
+ @cmd.each_cons(args.size).any? do |sub_cmd|
12
+ sub_cmd == args
13
+ end
14
+ end
15
+
16
+ def excludes?(args)
17
+ @cmd.each_cons(args.size).all? do |sub_cmd|
18
+ sub_cmd != args
19
+ end
20
+ end
21
+
22
+ def redirects_to?(path)
23
+ @cmd[-2..-1] == ['>', path]
24
+ end
25
+ end
26
+
27
+ module CommandHelpers
28
+ def self.handle_commands(commands)
29
+ @with_args ||= []
30
+ @without_args ||= []
31
+ @missing = []
32
+ @extra = []
33
+ @missing_redirect = false
34
+
35
+ commands.map! { |cmd_s| TestCommand.new(cmd_s) }
36
+
37
+ @matching_commands = commands.select do |cmd|
38
+ missing_for_cmd = []
39
+
40
+ @with_args.each do |with_args|
41
+ missing_for_cmd << with_args unless cmd.includes?(with_args)
42
+ end
43
+
44
+ @missing << missing_for_cmd unless missing_for_cmd.empty?
45
+ missing_for_cmd.empty?
46
+ end
47
+
48
+ @matching_commands.select! do |cmd|
49
+ extra_for_cmd = []
50
+
51
+ @without_args.each do |without_args|
52
+ extra_for_cmd << without_args unless cmd.excludes?(without_args)
53
+ end
54
+
55
+ @extra << extra_for_cmd unless extra_for_cmd.empty?
56
+ extra_for_cmd.empty?
57
+ end
58
+
59
+ if @redirect_path
60
+ @matching_commands.select! do |cmd|
61
+ cmd.redirects_to?(@redirect_path).tap do |does_redirect|
62
+ @missing_redirect = true unless does_redirect
63
+ end
64
+ end
65
+ end
66
+
67
+ !@matching_commands.empty?
68
+ end
69
+
70
+ def self.failure_message
71
+ message_parts = [].tap do |message_parts|
72
+ if @matching_commands.empty?
73
+ return 'No commands were executed.'
74
+ end
75
+
76
+ unless @missing.empty?
77
+ missing_for_all = @missing[0].intersection(*@missing)
78
+ missing_str = missing_for_all.map { |m| " #{m.join(' ')}" }.join("\n")
79
+ message_parts << "expected at least one command to contain args:\n#{missing_str}"
80
+ end
81
+
82
+ unless @extra.empty?
83
+ extra_for_any = @extra[0].union(*@extra)
84
+ extra_str = extra_for_any.map { |m| " #{m.join(' ')}" }.join("\n")
85
+ message_parts << "at least one viable command contained unexpected args:\n#{extra_str}"
86
+ end
87
+
88
+ if @missing_redirect
89
+ message_parts << "at least one viable command did not redirect to #{@redirect_path}"
90
+ end
91
+ end
92
+
93
+ message_parts.join("\n\n")
94
+ end
95
+ end
96
+
97
+ RSpec::Matchers.define :run_exec do |_expected|
98
+ match do |actual|
99
+ instance_eval do
100
+ CommandHelpers.handle_commands(actual.exec_commands)
101
+ end
102
+ end
103
+
104
+ # args should be an array of arrays
105
+ chain :with_args do |*with_args|
106
+ @with_args = with_args
107
+ end
108
+
109
+ chain :without_args do |*without_args|
110
+ @without_args = without_args
111
+ end
112
+
113
+ chain :and_redirect_to do |path|
114
+ @redirect_path = path
115
+ end
116
+
117
+ failure_message do
118
+ instance_eval do
119
+ CommandHelpers.failure_message
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,37 @@
1
+ # typed: ignore
2
+
3
+ require 'kubectl-rb'
4
+ require 'kubernetes-cli'
5
+ require 'stringio'
6
+
7
+ class FakeStatus
8
+ attr_reader :exitstatus
9
+
10
+ def initialize(exitstatus)
11
+ @exitstatus = exitstatus
12
+ end
13
+
14
+ def success?
15
+ exitstatus == 0
16
+ end
17
+ end
18
+
19
+ class TestCLI < KubernetesCLI
20
+ attr_reader :exec_commands, :system_commands
21
+
22
+ def initialize(kubeconfig_path, executable = KubectlRb.executable)
23
+ @exec_commands = []
24
+ @system_commands = []
25
+
26
+ super
27
+ end
28
+
29
+ def on_exec(&block)
30
+ @exec_callback = block
31
+ end
32
+
33
+ def exec(cmd)
34
+ @exec_commands << cmd
35
+ @exec_callback.call(cmd) if @exec_callback
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ apiVersion: v1
2
+ kind: ConfigMap
3
+ metadata:
4
+ namespace: test
5
+ name: test-config-external
6
+ data:
7
+ key: external value
@@ -0,0 +1,8 @@
1
+ apiVersion: v1
2
+ kind: ConfigMap
3
+ metadata:
4
+ namespace: test
5
+ name: test-config-external
6
+ data:
7
+ key: external value
8
+ nonExistent: foo
@@ -0,0 +1,22 @@
1
+ # typed: true
2
+ require 'yaml'
3
+
4
+ class TestObject
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ def to_resource
10
+ TestResource.new(@object)
11
+ end
12
+ end
13
+
14
+ class TestResource
15
+ def initialize(object)
16
+ @object = object
17
+ end
18
+
19
+ def to_yaml
20
+ YAML.dump(@object)
21
+ end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-10 00:00:00.000000000 Z
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kubectl-rb
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.1'
26
+ version: '0.2'
27
27
  description: Ruby wrapper around the Kubernetes CLI.
28
28
  email:
29
29
  - camertron@gmail.com
@@ -34,10 +34,19 @@ files:
34
34
  - CHANGELOG.md
35
35
  - Gemfile
36
36
  - LICENSE
37
+ - README.md
37
38
  - Rakefile
38
39
  - kubernetes-cli.gemspec
39
40
  - lib/kubernetes-cli.rb
40
41
  - lib/kubernetes-cli/version.rb
42
+ - rbi/kubernetes-cli.rbi
43
+ - spec/cli_spec.rb
44
+ - spec/spec_helper.rb
45
+ - spec/support/matchers.rb
46
+ - spec/support/test_cli.rb
47
+ - spec/support/test_config_map.yaml
48
+ - spec/support/test_config_map_bad.yaml
49
+ - spec/support/test_resource.rb
41
50
  homepage: http://github.com/getkuby/kubernetes-cli
42
51
  licenses:
43
52
  - MIT
@@ -57,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
66
  - !ruby/object:Gem::Version
58
67
  version: '0'
59
68
  requirements: []
60
- rubygems_version: 3.1.4
69
+ rubygems_version: 3.2.32
61
70
  signing_key:
62
71
  specification_version: 4
63
72
  summary: Ruby wrapper around the Kubernetes CLI.