serverspec-extra-types 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +6 -0
  5. data/.travis.yml +7 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +191 -0
  10. data/Rakefile +7 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/lib/serverspec_extra_types.rb +3 -0
  14. data/lib/serverspec_extra_types/helpers/properties.rb +11 -0
  15. data/lib/serverspec_extra_types/matchers.rb +34 -0
  16. data/lib/serverspec_extra_types/matchers/be_a_manager_node.rb +15 -0
  17. data/lib/serverspec_extra_types/matchers/be_a_worker_node.rb +14 -0
  18. data/lib/serverspec_extra_types/matchers/be_active.rb +11 -0
  19. data/lib/serverspec_extra_types/matchers/configure_queue.rb +8 -0
  20. data/lib/serverspec_extra_types/matchers/have_count.rb +11 -0
  21. data/lib/serverspec_extra_types/matchers/have_domain_name.rb +10 -0
  22. data/lib/serverspec_extra_types/matchers/have_engine_version.rb +11 -0
  23. data/lib/serverspec_extra_types/matchers/have_environment_variable.rb +21 -0
  24. data/lib/serverspec_extra_types/matchers/have_ha_mode.rb +5 -0
  25. data/lib/serverspec_extra_types/matchers/have_ha_nodes.rb +8 -0
  26. data/lib/serverspec_extra_types/matchers/have_ha_sync_mode.rb +5 -0
  27. data/lib/serverspec_extra_types/matchers/have_host.rb +8 -0
  28. data/lib/serverspec_extra_types/matchers/have_hostname.rb +6 -0
  29. data/lib/serverspec_extra_types/matchers/have_image.rb +8 -0
  30. data/lib/serverspec_extra_types/matchers/have_image_sha.rb +5 -0
  31. data/lib/serverspec_extra_types/matchers/have_label.rb +20 -0
  32. data/lib/serverspec_extra_types/matchers/have_mount.rb +11 -0
  33. data/lib/serverspec_extra_types/matchers/have_network.rb +8 -0
  34. data/lib/serverspec_extra_types/matchers/have_placement_constraint.rb +8 -0
  35. data/lib/serverspec_extra_types/matchers/have_replica_count.rb +8 -0
  36. data/lib/serverspec_extra_types/matchers/have_restart_limit.rb +8 -0
  37. data/lib/serverspec_extra_types/matchers/have_restart_policy.rb +8 -0
  38. data/lib/serverspec_extra_types/matchers/have_user.rb +10 -0
  39. data/lib/serverspec_extra_types/matchers/have_vhost.rb +5 -0
  40. data/lib/serverspec_extra_types/matchers/include_regex.rb +5 -0
  41. data/lib/serverspec_extra_types/matchers/map_port.rb +25 -0
  42. data/lib/serverspec_extra_types/matchers/mirror_all.rb +5 -0
  43. data/lib/serverspec_extra_types/matchers/publish_all_ports.rb +3 -0
  44. data/lib/serverspec_extra_types/matchers/read_from_queue.rb +8 -0
  45. data/lib/serverspec_extra_types/matchers/write_to_queue.rb +8 -0
  46. data/lib/serverspec_extra_types/types.rb +18 -0
  47. data/lib/serverspec_extra_types/types/api_base.rb +36 -0
  48. data/lib/serverspec_extra_types/types/consul_base.rb +40 -0
  49. data/lib/serverspec_extra_types/types/consul_node.rb +20 -0
  50. data/lib/serverspec_extra_types/types/consul_node_list.rb +32 -0
  51. data/lib/serverspec_extra_types/types/consul_service.rb +24 -0
  52. data/lib/serverspec_extra_types/types/consul_service_list.rb +32 -0
  53. data/lib/serverspec_extra_types/types/docker_container.rb +124 -0
  54. data/lib/serverspec_extra_types/types/docker_node.rb +41 -0
  55. data/lib/serverspec_extra_types/types/docker_service.rb +121 -0
  56. data/lib/serverspec_extra_types/types/rabbitmq_base.rb +38 -0
  57. data/lib/serverspec_extra_types/types/rabbitmq_node_list.rb +18 -0
  58. data/lib/serverspec_extra_types/types/rabbitmq_user_permission.rb +39 -0
  59. data/lib/serverspec_extra_types/types/rabbitmq_vhost_list.rb +22 -0
  60. data/lib/serverspec_extra_types/types/rabbitmq_vhost_policy.rb +57 -0
  61. data/lib/serverspec_extra_types/version.rb +3 -0
  62. data/properties.yml +23 -0
  63. data/serverspec-extra-types.gemspec +36 -0
  64. metadata +232 -0
@@ -0,0 +1,14 @@
1
+ RSpec::Matchers.define :be_a_worker_node do
2
+ match do |actual|
3
+ actual.role == 'worker'
4
+ end
5
+ failure_message do |actual|
6
+ "expected '#{actual.role}' to be worker"
7
+ end
8
+ description do
9
+ 'be a worker node'
10
+ end
11
+ end
12
+ RSpec::Matchers.alias_matcher :be_worker, :be_a_worker_node
13
+ RSpec::Matchers.alias_matcher :be_a_worker, :be_a_worker_node
14
+ RSpec::Matchers.alias_matcher :be_worker_node, :be_a_worker_node
@@ -0,0 +1,11 @@
1
+ RSpec::Matchers.define :be_active do
2
+ match do |actual|
3
+ actual.active?
4
+ end
5
+ failure_message do |actual|
6
+ "expected '#{actual.availability}' to be active"
7
+ end
8
+ description do
9
+ 'be active'
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :configure_queue do |vhost, queue|
2
+ match do |actual|
3
+ actual.configure_queue? vhost, queue
4
+ end
5
+ description do
6
+ "be able configure #{queue} on the #{vhost} vhost"
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ RSpec::Matchers.define :have_count do |count|
2
+ match do |actual|
3
+ actual.length == count
4
+ end
5
+ description do
6
+ "have #{count}"
7
+ end
8
+ failure_message do |actual|
9
+ "expected count to be #{count} was #{actual.length}"
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ RSpec::Matchers.define :have_domainname do |domain|
2
+ match do |actual|
3
+ actual.has_domainname? domain
4
+ end
5
+ failure_message do |actual|
6
+ "expected domain name to be #{domain}, was #{actual.domain_name}"
7
+ end
8
+ end
9
+
10
+ RSpec::Matchers.alias_matcher :have_domain_name, :have_domainname
@@ -0,0 +1,11 @@
1
+ RSpec::Matchers.define :have_engine_version do |version|
2
+ match do |actual|
3
+ actual.has_engine_version? version
4
+ end
5
+ failure_message do |actual|
6
+ "expected version #{actual.engine_version} to be #{version}"
7
+ end
8
+ description do
9
+ "be running docker version #{version}"
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ RSpec::Matchers.define :have_environment_variable do |regex|
2
+ chain :with_value do | value|
3
+ @value = value
4
+ end
5
+ match do |actual|
6
+ actual.has_environment_variable? regex, @value
7
+ end
8
+ description do
9
+ message = "have an environment variable #{regex}"
10
+ message << %( with a value of "#{@value}") if @value
11
+ message
12
+ end
13
+ failure_message do |actual|
14
+ if !@value
15
+ "expected #{actual.environment_variables} to contain #{regex}"
16
+ else
17
+ "expected #{regex} to have value #{@value}, it was #{actual.environment_variable(regex)}"
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_ha_mode do |mode|
2
+ match do |actual|
3
+ actual.has_ha_mode? mode
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_ha_nodes do |count|
2
+ match do |actual|
3
+ actual.has_ha_nodes? count
4
+ end
5
+ description do
6
+ "have #{count} ha nodes"
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_ha_sync_mode do |mode|
2
+ match do |actual|
3
+ actual.has_ha_sync_mode? mode
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_host do |host|
2
+ match do |actual|
3
+ actual.has_host? host
4
+ end
5
+ failure_message do |actual|
6
+ "expected #{actual.hosts} to contain #{host}"
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :have_hostname do |hostname|
2
+ match do |actual|
3
+ actual.has_hostname? hostname
4
+ end
5
+ end
6
+ RSpec::Matchers.alias_matcher :have_host_name, :have_hostname
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_image do |image|
2
+ match do |actual|
3
+ actual.has_image?(image)
4
+ end
5
+ failure_message do |actual|
6
+ "expected image to be #{image} was #{actual.image}"
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_image_sha do |sha|
2
+ match do |actual|
3
+ actual['Image'] == sha
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ RSpec::Matchers.define :have_label do |label|
2
+ match do |actual|
3
+ actual.labels.key? label
4
+ end
5
+ failure_message do |actual|
6
+ "expected #{actual.labels} to have key #{label}"
7
+ end
8
+ end
9
+
10
+ RSpec::Matchers.define :have_label_with_value do |label, value|
11
+ match do |actual|
12
+ actual.labels[label] == value
13
+ end
14
+ failure_message do |actual|
15
+ "expected #{actual.labels[label]} to be #{value}"
16
+ end
17
+ description do
18
+ "have label #{label} with value #{value}"
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ RSpec::Matchers.define :have_mount do |source, target, type = 'bind'|
2
+ match do |actual|
3
+ actual.has_mount?(source, target, type)
4
+ end
5
+ description do
6
+ "mount #{source} on host to #{target} in container as a #{type} mount"
7
+ end
8
+ failure_message do |actual|
9
+ "expected #{actual.mounts} to contain {'Target' => #{target}, 'Destination' => #{source} , 'Type' => #{type}}"
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_network do |name|
2
+ match do |actual|
3
+ actual.has_network? name
4
+ end
5
+ failure_message do |actual|
6
+ "expected #{actual.networks} to include #{name}"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_placement_constraint do |constraint|
2
+ match do |actual|
3
+ actual.has_placement_constraint? constraint
4
+ end
5
+ failure_message do |actual|
6
+ "expected #{actual.placement_constraint} to include #{constraint}"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_replica_count do |count|
2
+ match do |actual|
3
+ actual.replicas == count
4
+ end
5
+ description do
6
+ "have #{count} replica#{count != 1 ? 's' : ''}"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_restart_limit do |count|
2
+ match do |actual|
3
+ actual.has_restart_limit? count
4
+ end
5
+ failure_message do |actual|
6
+ "expected restart limit to be #{count}, was #{actual.restart_limit}"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :have_restart_policy do |policy|
2
+ match do |actual|
3
+ actual.has_restart_policy?(policy)
4
+ end
5
+ failure_message do |actual|
6
+ "expected restart policy to be #{policy}, was #{actual.restart_policy}"
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ RSpec::Matchers.define :have_user do |user|
2
+ match do |actual|
3
+ actual.has_user? user
4
+ end
5
+ failure_message do |actual|
6
+ "expected user to be #{user} was #{actual.user}"
7
+ end
8
+ end
9
+
10
+ RSpec::Matchers.alias_matcher :run_as_user, :have_user
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_vhost do |vhost|
2
+ match do |actual|
3
+ actual.has_vhost? vhost
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :include_regex do |regex|
2
+ match do |actual|
3
+ actual.include_regex? regex
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ RSpec::Matchers.define :map_port do |host, container|
2
+
3
+ match do |actual|
4
+ @protocol = 'tcp' unless @protocol
5
+ if @mode
6
+ actual.map_port? host, container, @protocol, @mode
7
+ else
8
+ actual.map_port? host, container, @protocol
9
+ end
10
+ end
11
+ description do
12
+ msg = "map container port #{container} to host port #{host}"
13
+ msg << %( using the "#{@protocol}" protocol) if @protocol
14
+ msg << %( in "#{@mode}" mode) if @mode
15
+ msg
16
+ end
17
+ failure_message do |actual|
18
+ @protocol = 'tcp' unless @protocol
19
+ "expected #{actual.port_map} to contain {\"#{container}/#{@protocol}\"=>[{\"HostPort\"=>\"#{host}\"}] }"
20
+ end
21
+
22
+ chain :using_protocol, :protocol
23
+ chain :with_mode, :mode
24
+
25
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :mirror_all do
2
+ match do |actual|
3
+ actual.mirror_all?
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ RSpec::Matchers.define :publish_all_ports do
2
+ match { |actual| actual.publishes_all_ports? }
3
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :read_from_queue do |vhost, queue|
2
+ match do |actual|
3
+ actual.read_from_queue?(vhost, queue)
4
+ end
5
+ description do
6
+ "be able to read from #{queue} on the #{vhost} vhost"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :write_to_queue do |vhost, queue|
2
+ match do |actual|
3
+ actual.write_to_queue? vhost, queue
4
+ end
5
+ description do
6
+ "be able to write to #{queue} on the #{vhost} vhost"
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ require 'serverspec_extra_types/types/docker_container'
2
+ require 'serverspec/helper/type'
3
+
4
+ module Serverspec
5
+ module Helper
6
+ module Type
7
+ types = %w[docker_service rabbitmq_vhost_policy rabbitmq_node_list rabbitmq_vhost_list
8
+ rabbitmq_user_permission consul_service consul_service_list consul_node consul_node_list]
9
+
10
+ types.each do |type|
11
+ require "serverspec_extra_types/types/#{type}"
12
+ define_method type do |*_args|
13
+ eval "Serverspec::Type::#{type.to_camel_case}.new(*_args)"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ require 'serverspec'
2
+ require 'serverspec/type/base'
3
+ require 'multi_json'
4
+ require 'serverspec_extra_types/helpers/properties'
5
+
6
+ module Serverspec::Type
7
+ class ApiBase < Base
8
+ def initialize(name = nil, options = {})
9
+ super(name, options)
10
+ end
11
+
12
+ def [](key)
13
+ value = inspection
14
+ key.split('.').each do |k|
15
+ is_index = k.start_with?('[') && k.end_with?(']')
16
+ value = value[is_index ? k[1..-2].to_i : k]
17
+ end
18
+ value
19
+ end
20
+
21
+ def url
22
+ @url_base
23
+ end
24
+
25
+ def inspection
26
+ @inspection ||= ::MultiJson.load(get_inspection.stdout)
27
+ end
28
+
29
+ private
30
+
31
+ def get_inspection
32
+ command = "curl -s #{url}"
33
+ @get_inspection ||= @runner.run_command(command)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ require 'serverspec'
2
+ require 'serverspec/type/base'
3
+ require 'multi_json'
4
+ require 'serverspec_extra_types/helpers/properties'
5
+ require 'serverspec_extra_types/types/api_base'
6
+
7
+ module Serverspec::Type
8
+ class ConsulBase < ApiBase
9
+ def initialize(name = nil, acl_token = nil, options = {})
10
+ super(name, options)
11
+ @token = acl_token
12
+ @url_base = property[:variables][:consul_url] || 'http://localhost:8500'
13
+ end
14
+
15
+ def [](key)
16
+ value = inspection
17
+ key.split('.').each do |k|
18
+ is_index = k.start_with?('[') && k.end_with?(']')
19
+ value = value[is_index ? k[1..-2].to_i : k]
20
+ end
21
+ value
22
+ end
23
+
24
+ def url
25
+ @url_base
26
+ end
27
+
28
+ def inspection
29
+ @inspection ||= ::MultiJson.load(get_inspection.stdout)[0]
30
+ end
31
+
32
+ private
33
+
34
+ def get_inspection
35
+ headers = @token ? "--header 'X-Consul-Token: #{@token}'" : ''
36
+ command = "curl -s #{headers} #{url}"
37
+ @get_inspection ||= @runner.run_command(command)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ require 'serverspec_extra_types/types/consul_base'
2
+
3
+
4
+ # TODO: List and singular
5
+ module Serverspec::Type
6
+ class ConsulNode < ConsulBase
7
+
8
+ def url
9
+ "#{@url_base}/v1/catalog/node/#{@name}"
10
+ end
11
+
12
+
13
+
14
+ def inspection
15
+ @inspection ||= ::MultiJson.load(get_inspection.stdout) #Need a find
16
+ end
17
+
18
+
19
+ end
20
+ end