Class: StormForge::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stormforge/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stormforge/client.rb', line 8

def initialize(options={})
  @email = options[:user] || options[:email]
  @token = options[:token]

  @debug = options[:debug]
  @logger = options.fetch(:logger, logger)

  @skip_ssl_verification = options.fetch(:skip_ssl_verify, false)

  @url_base = if options[:dev_mode]
                "http://localhost:3000/api"
              else
                "https://api.stormforger.com"
              end
  @url_base = options[:url_base] if options[:url_base]
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



6
7
8
# File 'lib/stormforge/client.rb', line 6

def email
  @email
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/stormforge/client.rb', line 6

def token
  @token
end

#url_baseObject (readonly)

Returns the value of attribute url_base.



6
7
8
# File 'lib/stormforge/client.rb', line 6

def url_base
  @url_base
end

Instance Method Details

#abort_test_run(id) ⇒ Object



170
171
172
# File 'lib/stormforge/client.rb', line 170

def abort_test_run(id)
  connection.post(test_run_abort_endpoint(id)).body
end

#aquire_api_token(email, password) ⇒ Object



81
82
83
84
85
# File 'lib/stormforge/client.rb', line 81

def aquire_api_token(email, password)
  response = connection.get authentication_endpoint, email: email, password: password
  return unless response.status == 200
  response.body["authentication_token"]
end

#connectionObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stormforge/client.rb', line 25

def connection
  @connection ||= Faraday.new(url: @url_base, ssl: {
  verify: !@skip_ssl_verification}) do |faraday|
    faraday.headers["User-Agent"] = "stormforge-ruby (#{StormForge::VERSION})"
    faraday.headers["Content-Type"] = "application/json"
    if email && token
      faraday.headers["X-User-Email"] = email
      faraday.headers["X-User-Token"] = token
    end

    faraday.request :json
    faraday.request :multipart

    faraday.response :json, :content_type => /\bjson$/

    faraday.use Faraday::Response::Logger, logger
    faraday.use Faraday::Response::RaiseError

    faraday.adapter Faraday.default_adapter
  end
end

#create_or_update(test_case) ⇒ Object



87
88
89
90
91
# File 'lib/stormforge/client.rb', line 87

def create_or_update(test_case)
  handle_fixtures(test_case)

  handle_test_case(test_case)
end

#create_test_run(test_case_slug, description = "") ⇒ Object



149
150
151
152
153
154
# File 'lib/stormforge/client.rb', line 149

def create_test_run(test_case_slug, description="")
  connection.post(test_run_endpoint(nil), {
    test_case: test_case_slug,
    test_run: { description: description }
  }).body
end

#fetch_test_case(slug) ⇒ Object



143
144
145
146
147
# File 'lib/stormforge/client.rb', line 143

def fetch_test_case(slug)
  connection.get(test_case_endpoint(slug)).body
rescue Faraday::Error::ResourceNotFound
  nil
end

#fetch_test_run(id) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/stormforge/client.rb', line 156

def fetch_test_run(id)
  response = connection.get(test_run_endpoint(id)).body

  return unless response

  response["test_runs"][0]
rescue Faraday::Error::ResourceNotFound
  nil
end

#fetch_test_run_log(test_run_id, since, limit, include_start) ⇒ Object



67
68
69
# File 'lib/stormforge/client.rb', line 67

def fetch_test_run_log(test_run_id, since, limit, include_start)
  connection.get("#{test_run_endpoint(test_run_id)}/log?since=#{since}&limit=#{limit}&include_start=#{include_start}")
end

#fixture_exists?(md5_hash) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
# File 'lib/stormforge/client.rb', line 193

def fixture_exists?(md5_hash)
  endpoint = "#{fixture_endpoint}/by_md5/#{md5_hash}.json"
  connection.get(endpoint).status == 200
rescue Faraday::Error::ResourceNotFound
  false
end

#fixture_upload(fixture) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/stormforge/client.rb', line 174

def fixture_upload(fixture)
  logger.info "Uploading file #{fixture.name.to_s}..."

  payload = { fixture: {
    name: fixture.name.to_s,
    file: Faraday::UploadIO.new(fixture.source, "text/plain")
  }}

  connection.post do |request|
    request.url fixture_endpoint
    request.headers["Content-Type"] = "multipart/form-data"
    request.body = payload
  end

rescue Exception => e
  puts "Error while uploading '#{fixture.name.to_s}' ('#{fixture.source}')"
  puts e.message
end

#handle_fixtures(test_case) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/stormforge/client.rb', line 93

def handle_fixtures(test_case)
  return unless test_case.data_sources

  test_case.data_sources.file_fixture_sources.map do |fixture_name, fixture|
    logger.info "Fixture #{fixture_name} checking (#{fixture.md5_hash})"
    next if fixture_exists? fixture.md5_hash
    logger.info "Fixture #{fixture_name} uploading"

    fixture_upload(fixture)
  end
end

#handle_test_case(test_case) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/stormforge/client.rb', line 105

def handle_test_case(test_case)
  if existing_test_case = fetch_test_case(test_case.slug)
    test_case_id = existing_test_case["test_cases"][0]["id"]
  end

  payload = {
    test_case: {
      json_definition: test_case.as_json(root: false),
      dsl_version: test_case.dsl_version || :v1,
      slug: test_case.slug,
      title: test_case.title
    }
  }

  unless existing_test_case.present?
    logger.info "Test Case: Creating new test case"

    connection.post do |request|
      request.url test_case_endpoint
      request.headers["Content-Type"] = "application/json"
      request.body = payload.to_json
    end
  else
    logger.info "Test Case: Updating case #{test_case.slug}"

    payload[:id] = test_case_id

    connection.put do |request|
      request.url test_case_endpoint(test_case.slug)
      request.headers["Content-Type"] = "application/json"
      request.body = payload.to_json
    end
  end

rescue Faraday::Error::ClientError => e
  puts e.response
end

#list_test_runs(test_case_slug = nil) ⇒ Object



166
167
168
# File 'lib/stormforge/client.rb', line 166

def list_test_runs(test_case_slug=nil)
  connection.get(test_run_endpoint, test_case: test_case_slug).body
end

#ping(authenticated = false) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/stormforge/client.rb', line 71

def ping(authenticated=false)
  path = if authenticated
          "#{url_base}/authenticated_ping"
        else
          "#{url_base}/ping"
        end

  connection.get(path)
end

#test_run_log(test_run_id, since, limit = 10, stream = true, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stormforge/client.rb', line 47

def test_run_log(test_run_id, since, limit=10, stream=true, &block)
  include_start = true
  while (response = fetch_test_run_log(test_run_id, since, limit, include_start)).body["stats"].present? || stream
    include_start = false
    since = response.body["last"] || since

    sleep 1 if response.body["stats"].empty?

    response.body["stats"].each do |stats|
      yield stats
    end

    break if response.body["end_reached"]
  end
rescue Interrupt
  STDERR.puts "aborting..."
rescue Faraday::Error::ResourceNotFound
  raise Thor::Error, "Test Run #{test_run_id} not found!"
end