Class: Sysdig::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/sysdig/mock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



35
36
37
38
39
40
41
42
# File 'lib/sysdig/mock.rb', line 35

def initialize(options={})
  @url     = options[:url]    || "https://langley.engineyard.com"
  @logger  = options[:logger] || Logger.new(nil)

  username, password, token = *options.values_at(:username, :password, :token)

  @account_id = (username && password) ? Digest::SHA256.hexdigest("#{username}:#{password}") : token
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



33
34
35
# File 'lib/sysdig/mock.rb', line 33

def 
  @account_id
end

#loggerObject (readonly)

Returns the value of attribute logger.



33
34
35
# File 'lib/sysdig/mock.rb', line 33

def logger
  @logger
end

#urlObject (readonly)

Returns the value of attribute url.



33
34
35
# File 'lib/sysdig/mock.rb', line 33

def url
  @url
end

Class Method Details

.dataObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sysdig/mock.rb', line 8

def self.data
  @@data ||= Hash.new { |h,url|
    h[url] = {
      :alerts => {},
      :alert_notifications => {},
      :user_notifications => {
        "email" => {
          "enabled"    => false,
          "recipients" => []
        },
        "sns" => {
          "enabled" => false,
          "topics"  => []
        },
        "pagerDuty" => {
          "enabled"     => false,
          "integrated"  => false,
          "resolveOnOk" => false,
          "connectUrl"  => "https://connect.pagerduty.com/connect?vendor=x&callback=https://app.sysdigcloud.com/api/pagerDuty/callback/y/z"
        }
      }
    }
  }
end

.reset!Object



2
3
4
5
6
# File 'lib/sysdig/mock.rb', line 2

def self.reset!
  super

  @id = 0
end

Instance Method Details

#create_alert_notification(alert, state: "ACTIVE", resolved: false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sysdig/mock.rb', line 80

def create_alert_notification(alert, state: "ACTIVE", resolved: false)
  alert_notification_id = self.serial_id
  alert_data = self.data[:alerts].fetch(alert.identity)

  alert_notification = self.data[:alert_notifications][alert_notification_id] = {
    "id"        => alert_notification_id,
    "timestamp" => (Time.now.to_i - rand(60..300)) * 1_000_000,
    "timespan"  => alert_data["timespan"].to_i,
    "filter"    => alert_data["filter"],
    "condition" => alert_data["condition"],
    "state"     => state.upcase,
    "resolved"  => resolved,
    "alert"     => alert.identity.to_s,
    "entities"  => [
      "filter" => alert_data["filter"],
      "target" => {}, # @fixme the hell
      "metricValues" => {}, # @fixme breakdown of condition
    ]
  }

  self.alert_notifications.new(alert_notification)
end

#dataObject



71
72
73
# File 'lib/sysdig/mock.rb', line 71

def data
  self.class.data[self.]
end

#response(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sysdig/mock.rb', line 48

def response(options={})
  status  = options[:status] || 200
  body    = options[:body]
  headers = {
    "Content-Type" => "application/json; charset=utf-8",
  }.merge(options[:headers] || {})

  logger.debug "MOCKED RESPONSE: #{status}"
  logger.debug('response') { headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") }
  logger.debug caller[0]
  logger.debug('response.body') { body }

  Sysdig::Response.new(
    :status  => status,
    :headers => headers,
    :body    => body,
    :request => {
      :method  => :mocked,
      :url     => caller[1],
    }
  ).raise!
end

#serial_idObject



75
76
77
78
# File 'lib/sysdig/mock.rb', line 75

def serial_id
  @id ||= 0
  @id += 1
end

#url_for(*pieces) ⇒ Object



44
45
46
# File 'lib/sysdig/mock.rb', line 44

def url_for(*pieces)
  File.join(self.url, *pieces)
end