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



162
163
164
# File 'lib/stormforge/client.rb', line 162

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

#aquire_api_token(email, password) ⇒ Object



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

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
# File 'lib/stormforge/client.rb', line 25

def connection
  @connection ||= Faraday.new(url: @url_base, ssl: {
  verify: !@skip_ssl_verification}) do |faraday|
    faraday.basic_auth(email, token)

    faraday.headers["User-Agent"] = "stormforge-ruby (#{StormForge::VERSION})"
    faraday.headers["Content-Type"] = "application/json"

    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



79
80
81
82
83
# File 'lib/stormforge/client.rb', line 79

def create_or_update(test_case)
  handle_fixtures(test_case)

  handle_test_case(test_case)
end

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



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

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



135
136
137
138
139
# File 'lib/stormforge/client.rb', line 135

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

#fetch_test_run(id) ⇒ Object



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

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



65
66
67
# File 'lib/stormforge/client.rb', line 65

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)


185
186
187
188
189
190
# File 'lib/stormforge/client.rb', line 185

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/stormforge/client.rb', line 166

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stormforge/client.rb', line 85

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



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
127
128
129
130
131
132
133
# File 'lib/stormforge/client.rb', line 97

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



158
159
160
# File 'lib/stormforge/client.rb', line 158

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

#pingObject



69
70
71
# File 'lib/stormforge/client.rb', line 69

def ping
  connection.get("/")
end

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



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

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