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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/inspec/plugin/v1/plugin_types/resource.rb', line 46
def __register(name, obj) cl = Class.new(obj) do attr_reader :resource_exception_message
def initialize(backend, name, *args)
@resource_skipped = false
@resource_failed = false
@supports = Inspec::Resource.supports[name]
@__backend_runner__ = backend
@__resource_name__ = name
supported = true
supported = check_supports unless @supports.nil?
test_backend = defined?(Train::Transports::Mock::Connection) && backend.backend.class == Train::Transports::Mock::Connection
return unless supported || test_backend
begin
super(*args)
rescue Inspec::Exceptions::ResourceSkipped => e
skip_resource(e.message)
rescue Inspec::Exceptions::ResourceFailed => e
fail_resource(e.message)
rescue NoMethodError => e
raise unless test_backend && e.receiver.class == Train::Transports::Mock::Connection
skip_resource(e.message)
end
end
def self.desc(description = nil)
return @description if description.nil?
@description = description
end
def self.example(example = nil)
return @example if example.nil?
@example = example
end
def check_supports
status = inspec.platform.supported?(@supports)
fail_msg = "Resource #{@__resource_name__.capitalize} is not supported on platform #{inspec.platform.name}/#{inspec.platform.release}."
fail_resource(fail_msg) unless status
status
end
def skip_resource(message)
@resource_skipped = true
@resource_exception_message = message
end
def resource_skipped?
@resource_skipped
end
def fail_resource(message)
@resource_failed = true
@resource_exception_message = message
end
def resource_failed?
@resource_failed
end
def inspec
@__backend_runner__
end
end
if __resource_registry.key?(name)
Inspec::Log.warn("Overwriting resource #{name}. To reference a specific version of #{name} use the resource() method")
end
__resource_registry[name] = cl
end
|