wavefront-client 3.5.3 → 3.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -2
  3. data/README-cli.md +84 -60
  4. data/bin/wavefront +84 -69
  5. data/lib/wavefront/alerting.rb +14 -3
  6. data/lib/wavefront/batch_writer.rb +7 -0
  7. data/lib/wavefront/cli.rb +15 -17
  8. data/lib/wavefront/cli/alerts.rb +15 -10
  9. data/lib/wavefront/cli/batch_write.rb +12 -3
  10. data/lib/wavefront/cli/events.rb +19 -3
  11. data/lib/wavefront/cli/sources.rb +22 -12
  12. data/lib/wavefront/cli/ts.rb +9 -1
  13. data/lib/wavefront/cli/write.rb +7 -0
  14. data/lib/wavefront/client.rb +9 -5
  15. data/lib/wavefront/client/version.rb +1 -1
  16. data/lib/wavefront/constants.rb +20 -0
  17. data/lib/wavefront/events.rb +26 -4
  18. data/lib/wavefront/metadata.rb +23 -6
  19. data/lib/wavefront/opt_handler.rb +61 -0
  20. data/spec/cli_spec.rb +584 -0
  21. data/spec/spec_helper.rb +42 -0
  22. data/spec/wavefront/alerting_spec.rb +8 -9
  23. data/spec/wavefront/batch_writer_spec.rb +1 -1
  24. data/spec/wavefront/cli/alerts_spec.rb +5 -4
  25. data/spec/wavefront/cli/batch_write_spec.rb +4 -2
  26. data/spec/wavefront/cli/events_spec.rb +3 -2
  27. data/spec/wavefront/cli/sources_spec.rb +3 -2
  28. data/spec/wavefront/cli/write_spec.rb +4 -2
  29. data/spec/wavefront/cli_spec.rb +11 -43
  30. data/spec/wavefront/client_spec.rb +2 -2
  31. data/spec/wavefront/events_spec.rb +1 -1
  32. data/spec/wavefront/metadata_spec.rb +1 -1
  33. data/spec/wavefront/mixins_spec.rb +1 -1
  34. data/spec/wavefront/opt_handler_spec.rb +89 -0
  35. data/spec/wavefront/resources/conf.yaml +10 -0
  36. data/spec/wavefront/response_spec.rb +3 -3
  37. data/spec/wavefront/validators_spec.rb +1 -1
  38. data/spec/wavefront/writer_spec.rb +1 -1
  39. metadata +9 -3
  40. data/.ruby-version +0 -1
@@ -22,6 +22,7 @@ require 'wavefront/alerting'
22
22
  require 'wavefront/events'
23
23
  require 'wavefront/batch_writer'
24
24
  require 'wavefront/validators'
25
+ require 'wavefront/opt_handler'
25
26
  require 'wavefront/cli'
26
27
  require 'wavefront/cli/alerts'
27
28
  require 'wavefront/cli/events'
@@ -79,3 +80,44 @@ end
79
80
  def concat_url(*args)
80
81
  'https://' + args.join('/').squeeze('/')
81
82
  end
83
+
84
+ def raw(str)
85
+ #
86
+ # eval. I know. But some of the CLI tests dump raw Ruby hashes in the
87
+ # debug output. This parses them so you can check them. They'll be
88
+ # prefixed with 'POST' or 'GET'
89
+ #
90
+ eval(str.split[1..-1].join(' '))
91
+ end
92
+
93
+ def wf(args = '')
94
+ #
95
+ # Run the 'wavefront' CLI command, with arguments, and return a struct
96
+ # for easy access
97
+ #
98
+ ret = OpenStruct.new
99
+ env = {'RUBYLIB' => [LIB.to_s, ENV['RUBYLIB']].join(':') }
100
+
101
+ puts "testing #{WF} #{args}"
102
+ stdout, stderr, status = Open3.capture3(env, "#{WF} #{args}")
103
+
104
+ ret.status = status.exitstatus
105
+ ret.stdout_a = stdout.split("\n")
106
+ ret.stdout = stdout.strip
107
+ ret.stderr_a = stderr.split("\n")
108
+ ret.stderr = stderr.strip
109
+ ret
110
+ end
111
+
112
+ # A matcher that tells you whether you have a key=value setting in a query
113
+ # string. Call it with have_element([:key, value])
114
+ #
115
+ RSpec::Matchers.define :have_element do |expected|
116
+ match do |str|
117
+ str.sub(/^\S+ /, '').sub(/^.*\?/, '').split('&').
118
+ each_with_object([]) do |e, aggr|
119
+ k, v = e.split('=')
120
+ aggr.<< [k.to_sym, v]
121
+ end.include?([expected[0].to_sym, URI.escape(expected[1].to_s)])
122
+ end
123
+ end
@@ -1,4 +1,4 @@
1
- =begin
1
+ =begin
2
2
  Copyright 2015 Wavefront Inc.
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
  require 'pathname'
19
19
 
20
20
  describe Wavefront::Alerting do
@@ -55,7 +55,7 @@ describe Wavefront::Alerting do
55
55
  end
56
56
 
57
57
  it 'makes API request with specified path' do
58
- path = '/api/new_alerts'
58
+ path = '/api/new_alerts'
59
59
  expect(RestClient).to receive(:get).with("https://#{File.join(Wavefront::Alerting::DEFAULT_HOST, path, "all?t=#{TEST_TOKEN}")}")
60
60
  @wave.all({ :path => path })
61
61
  end
@@ -73,7 +73,7 @@ describe Wavefront::Alerting do
73
73
  end
74
74
 
75
75
  it 'makes API request with both appended private tags and shared tags' do
76
- private_tag = 'first'
76
+ private_tag = 'first'
77
77
  shared_tag = 'second'
78
78
  expect(RestClient).to receive(:get).with("https://#{File.join(Wavefront::Alerting::DEFAULT_HOST, Wavefront::Alerting::DEFAULT_PATH, "all?t=#{TEST_TOKEN}&customerTag=second&userTag=first")}")
79
79
  @wave.all({ :private_tags => private_tag, :shared_tags => shared_tag })
@@ -82,30 +82,29 @@ describe Wavefront::Alerting do
82
82
 
83
83
  describe '#active' do
84
84
  it 'requests all active alerts' do
85
- expect(@wave).to receive(:get_alerts).with("active", {})
85
+ expect(@wave).to receive(:get_alerts).with("active", {})
86
86
  @wave.active
87
87
  end
88
88
  end
89
89
 
90
90
  describe '#snoozed' do
91
91
  it 'requests all snoozed alerts' do
92
- expect(@wave).to receive(:get_alerts).with("snoozed", {})
92
+ expect(@wave).to receive(:get_alerts).with("snoozed", {})
93
93
  @wave.snoozed
94
94
  end
95
95
  end
96
96
 
97
97
  describe '#invalid' do
98
98
  it 'requests all invalid alerts' do
99
- expect(@wave).to receive(:get_alerts).with("invalid", {})
99
+ expect(@wave).to receive(:get_alerts).with("invalid", {})
100
100
  @wave.invalid
101
101
  end
102
102
  end
103
103
 
104
104
  describe '#affected_by_maintenance' do
105
105
  it 'requests all affected_by_maintenance alerts' do
106
- expect(@wave).to receive(:get_alerts).with("affected_by_maintenance", {})
106
+ expect(@wave).to receive(:get_alerts).with("affected_by_maintenance", {})
107
107
  @wave.affected_by_maintenance
108
108
  end
109
109
  end
110
-
111
110
  end
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
  require 'socket'
19
19
 
20
20
  opts = {}
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require_relative '../../spec_helper'
2
2
  require 'pathname'
3
3
  require 'json'
4
4
  require 'erb'
@@ -9,7 +9,8 @@ states = %w(active affected_by_maintenance all invalid snoozed)
9
9
  formats = %w(ruby json human)
10
10
 
11
11
  opts = {
12
- token: TEST_TOKEN,
12
+ token: TEST_TOKEN,
13
+ endpoint: TEST_HOST,
13
14
  }
14
15
 
15
16
  describe Wavefront::Cli::Alerts do
@@ -35,7 +36,7 @@ describe Wavefront::Cli::Alerts do
35
36
  k = Wavefront::Cli::Alerts.new(opts, ['all'])
36
37
 
37
38
  states.each do |state|
38
- expect(k.valid_state?(wfa, state.to_sym)).to be(true)
39
+ expect(k.valid_state?(wfa, state)).to be(true)
39
40
  end
40
41
  end
41
42
  end
@@ -94,7 +95,7 @@ describe Wavefront::Cli::Alerts do
94
95
  it 'reconstructs human output' do
95
96
  out = ERB.new(IO.read(Pathname.new(__FILE__).dirname +
96
97
  'resources' + 'alert.human.erb')).result
97
- expect(k.humanize(JSON.parse(src)).join("\n") + "\n").to eq(out)
98
+ expect(k.humanize(JSON.parse(src)).join("\n")).to eq(out)
98
99
  end
99
100
  end
100
101
 
@@ -1,7 +1,9 @@
1
1
  require 'pathname'
2
- require 'spec_helper'
2
+ require_relative '../../spec_helper'
3
3
 
4
- opts = {}
4
+ opts = {
5
+ proxy: 'wavefront.localnet',
6
+ }
5
7
 
6
8
  describe '#run' do
7
9
  end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require_relative '../../spec_helper'
2
2
  require 'pathname'
3
3
  require 'json'
4
4
 
@@ -8,7 +8,8 @@ states = %w(active affected_by_maintenance all invalid snoozed)
8
8
  formats = %w(ruby json human)
9
9
 
10
10
  opts = {
11
- token: TEST_TOKEN,
11
+ token: TEST_TOKEN,
12
+ endpoint: TEST_HOST,
12
13
  }
13
14
 
14
15
  describe Wavefront::Cli::Alerts do
@@ -2,11 +2,12 @@
2
2
  # Due to dependency requirements, webmock does not work with Ruby
3
3
  # 1.9.3. For as long as we have to support that, it's off the table.
4
4
  #
5
- require 'spec_helper'
5
+ require_relative '../../spec_helper'
6
6
  #require 'webmock/rspec'
7
7
 
8
8
  opts = {
9
- token: TEST_TOKEN,
9
+ token: TEST_TOKEN,
10
+ endpoint: TEST_HOST,
10
11
  }
11
12
 
12
13
  describe Wavefront::Cli::Sources do
@@ -1,7 +1,9 @@
1
- require 'spec_helper'
1
+ require_relative '../../spec_helper'
2
2
  require 'socket'
3
3
 
4
- opts = {}
4
+ opts = {
5
+ proxy: 'wavefront.localnet',
6
+ }
5
7
  args = 'write'
6
8
  k = Wavefront::Cli::Write.new(opts, args)
7
9
 
@@ -14,55 +14,23 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
18
- require 'pathname'
19
- require 'tempfile'
17
+ require_relative '../spec_helper'
20
18
 
21
- # A sample config file with two profiles.
22
- #
23
- pf_src = %Q(
24
- [default]
25
- token = 12345678-abcd-1234-abcd-123456789012
26
- endpoint = metrics.wavefront.com
27
- format = human
28
- proxy = wavefront.localnet
29
-
30
- [other]
31
- token = abcdefab-0123-abcd-0123-abcdefabcdef
32
- endpoint = test.wavefront.com
33
- format = human
34
- )
19
+ # Just test initialization
35
20
 
36
21
  describe Wavefront::Cli do
37
-
38
- profile = Tempfile.new('wf_test_profile')
39
- cf = profile.path
40
- profile.write(pf_src)
41
- profile.close
42
-
43
- it 'does not complain when there is no config file' do
44
- k = Wavefront::Cli.new({config: '/no/file', profile: 'default'}, false)
45
- expect(k.load_profile).to be_kind_of(NilClass)
46
- end
47
-
48
- it 'loads the specified profile' do
49
- k = Wavefront::Cli.new({config: cf, profile: 'other'}, false)
50
- pf = k.load_profile
51
- expect(pf).not_to include('proxy')
52
- expect(pf[:token]).to eq('abcdefab-0123-abcd-0123-abcdefabcdef')
22
+ it 'raises an error if no token is set' do
23
+ wf = Wavefront::Cli.new({}, nil)
24
+ expect{wf.validate_opts}.to raise_exception(RuntimeError)
53
25
  end
54
26
 
55
- it 'loads the default when no profile is specified' do
56
- k = Wavefront::Cli.new({config: cf}, false)
57
- pf = k.load_profile
58
- expect(pf[:proxy]).to eq('wavefront.localnet')
59
- expect(pf[:token]).to eq('12345678-abcd-1234-abcd-123456789012')
27
+ it 'raises an error if no endpoint is set' do
28
+ wf = Wavefront::Cli.new({token: 'abcdef' }, nil)
29
+ expect{wf.validate_opts}.to raise_exception(RuntimeError)
60
30
  end
61
31
 
62
- it 'prefers config file values to command-line options' do
63
- k = Wavefront::Cli.new({config: cf, format: 'graphite'}, false)
64
- pf = k.load_profile
65
- expect(pf[:format]).to eq('human')
66
- expect(pf[:token]).to eq('12345678-abcd-1234-abcd-123456789012')
32
+ it 'does not raise an error if an endpoint and token are set' do
33
+ wf = Wavefront::Cli.new({token: 'abcdef', endpoint: 'wavefront' }, nil)
34
+ expect{wf.validate_opts}.to_not raise_exception
67
35
  end
68
36
  end
@@ -1,4 +1,4 @@
1
- =begin
1
+ =begin
2
2
  Copyright 2015 Wavefront Inc.
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
  require 'pathname'
19
19
 
20
20
  describe Wavefront::Client do
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
 
19
19
  describe Wavefront::Events do
20
20
 
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
  require 'pathname'
19
19
 
20
20
  describe Wavefront::Metadata do
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
  require 'wavefront/mixins'
19
19
 
20
20
  HOST = 'i-12345678'
@@ -0,0 +1,89 @@
1
+ =begin
2
+ Copyright 2015 Wavefront Inc.
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+
15
+ =end
16
+
17
+ require_relative '../spec_helper'
18
+ require 'pathname'
19
+ require 'socket'
20
+
21
+ # DEFAULT_OPTS come from the real constants.rb file
22
+
23
+ describe Wavefront::OptHandler do
24
+ config_file = Pathname.new(__FILE__).dirname + 'resources' + 'conf.yaml'
25
+
26
+ it 'uses defaults if nothing else is supplied' do
27
+ opts = Wavefront::OptHandler.new(Pathname.new('/nofile'), {}).opts
28
+
29
+ expect(opts.class).to be(Hash)
30
+ expect(opts[:endpoint]).to eq('metrics.wavefront.com')
31
+ expect(opts[:host]).to eq(Socket.gethostname)
32
+ expect(opts[:sourceformat]).to eq(:human)
33
+ end
34
+
35
+ it 'ensures options override defaults' do
36
+ cli_opts = {
37
+ endpoint: 'myendpoint.wavefront.com',
38
+ sourceformat: 'ruby',
39
+ }
40
+
41
+ opts = Wavefront::OptHandler.new(Pathname.new('/nofile'), cli_opts).opts
42
+
43
+ expect(opts.class).to be(Hash)
44
+ expect(opts[:endpoint]).to eq('myendpoint.wavefront.com')
45
+ expect(opts[:host]).to eq(Socket.gethostname)
46
+ expect(opts[:sourceformat]).to eq('ruby')
47
+ end
48
+
49
+ it 'ensures default config file values override defaults' do
50
+ opts = Wavefront::OptHandler.new(config_file, {}).opts
51
+
52
+ expect(opts.class).to be(Hash)
53
+ expect(opts[:endpoint]).to eq('default.wavefront.com')
54
+ expect(opts[:host]).to eq(Socket.gethostname)
55
+ expect(opts[:sourceformat]).to eq('raw')
56
+ expect(opts[:format]).to eq(:raw)
57
+ end
58
+
59
+ it 'ensures alternate stanza config file values override defaults' do
60
+ opts = Wavefront::OptHandler.new(config_file, {profile: 'other'}).opts
61
+
62
+ expect(opts.class).to be(Hash)
63
+ expect(opts[:endpoint]).to eq('other.wavefront.com')
64
+ expect(opts[:host]).to eq(Socket.gethostname)
65
+ expect(opts[:sourceformat]).to eq(:human)
66
+ expect(opts[:format]).to eq(:raw)
67
+ end
68
+
69
+ it 'ensures command line options override defaults and config files' do
70
+ cli_opts = {
71
+ endpoint: 'cli.wavefront.com',
72
+ sourceformat: 'ruby',
73
+ }
74
+
75
+ opts = Wavefront::OptHandler.new(config_file, cli_opts).opts
76
+
77
+ expect(opts.class).to be(Hash)
78
+ expect(opts[:endpoint]).to eq('cli.wavefront.com')
79
+ expect(opts[:host]).to eq(Socket.gethostname)
80
+ expect(opts[:sourceformat]).to eq('ruby')
81
+ expect(opts[:format]).to eq(:raw)
82
+ end
83
+
84
+ it 'issues a warning if there is no config file' do
85
+ expect{Wavefront::OptHandler.new(Pathname.new('/nofile'), {})}.
86
+ to match_stdout("'/nofile' not found. Taking options from command-line.")
87
+ end
88
+
89
+ end
@@ -0,0 +1,10 @@
1
+ [default]
2
+ token = 12345678-abcd-1234-abcd-123456789012
3
+ endpoint = default.wavefront.com
4
+ proxy = wavefront.localnet
5
+ sourceformat = raw
6
+
7
+ [other]
8
+ token = abcdefab-0123-abcd-0123-abcdefabcdef
9
+ endpoint = other.wavefront.com
10
+ proxy = otherwf.localnet
@@ -1,4 +1,4 @@
1
- =begin
1
+ =begin
2
2
  Copyright 2015 Wavefront Inc.
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
 
15
15
  =end
16
16
 
17
- require 'spec_helper'
17
+ require_relative '../spec_helper'
18
18
  require 'pathname'
19
19
  require 'json'
20
20
 
@@ -72,7 +72,7 @@ describe Wavefront::Response::Highcharts do
72
72
  it 'returns something that resembles highcharts output' do
73
73
  example_response = File.read(Pathname.new(__FILE__).parent.parent.join('example_response.json'))
74
74
  response = Wavefront::Response::Highcharts.new(example_response)
75
-
75
+
76
76
  expect(response.highcharts[0]['data'].size).to eq(30)
77
77
  JSON.parse(response.to_json).each { |m| expect(m.keys.size).to eq(2) }
78
78
  end