wavefront-sdk 3.7.1 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +43 -2
  3. data/.travis.yml +0 -1
  4. data/HISTORY.md +33 -0
  5. data/README.md +4 -3
  6. data/lib/wavefront-sdk/account.rb +104 -3
  7. data/lib/wavefront-sdk/api_mixins/user.rb +10 -0
  8. data/lib/wavefront-sdk/cloudintegration.rb +27 -0
  9. data/lib/wavefront-sdk/core/api_caller.rb +50 -7
  10. data/lib/wavefront-sdk/core/exception.rb +6 -0
  11. data/lib/wavefront-sdk/credentials.rb +28 -9
  12. data/lib/wavefront-sdk/defs/version.rb +1 -3
  13. data/lib/wavefront-sdk/paginator/base.rb +21 -15
  14. data/lib/wavefront-sdk/query.rb +0 -1
  15. data/lib/wavefront-sdk/role.rb +128 -0
  16. data/lib/wavefront-sdk/spy.rb +126 -0
  17. data/lib/wavefront-sdk/stdlib/array.rb +1 -1
  18. data/lib/wavefront-sdk/stdlib/time.rb +13 -0
  19. data/lib/wavefront-sdk/unstable/README.md +4 -0
  20. data/lib/wavefront-sdk/unstable/chart.rb +90 -0
  21. data/lib/wavefront-sdk/unstable/unstable.rb +9 -0
  22. data/lib/wavefront-sdk/user.rb +31 -0
  23. data/lib/wavefront-sdk/usergroup.rb +17 -16
  24. data/lib/wavefront-sdk/validators.rb +52 -7
  25. data/lib/wavefront-sdk/write.rb +9 -9
  26. data/spec/.rubocop.yml +41 -0
  27. data/spec/spec_helper.rb +4 -0
  28. data/spec/support/minitest_assertions.rb +4 -4
  29. data/spec/wavefront-sdk/account_spec.rb +107 -1
  30. data/spec/wavefront-sdk/cloudintegration_spec.rb +38 -0
  31. data/spec/wavefront-sdk/core/api_caller_spec.rb +43 -0
  32. data/spec/wavefront-sdk/credentials_spec.rb +3 -4
  33. data/spec/wavefront-sdk/metric_helper_spec.rb +1 -1
  34. data/spec/wavefront-sdk/role_spec.rb +96 -0
  35. data/spec/wavefront-sdk/spy_spec.rb +113 -0
  36. data/spec/wavefront-sdk/unstable/chart_spec.rb +39 -0
  37. data/spec/wavefront-sdk/user_spec.rb +8 -0
  38. data/spec/wavefront-sdk/usergroup_spec.rb +21 -11
  39. data/spec/wavefront-sdk/validators_spec.rb +31 -0
  40. data/wavefront-sdk.gemspec +8 -8
  41. metadata +32 -21
@@ -37,6 +37,36 @@ class WavefrontCloudIntegrationTest < WavefrontTestBase
37
37
  assert_raises(ArgumentError) { wf.disable }
38
38
  end
39
39
 
40
+ def test_create_aws_external_id
41
+ assert_posts('/api/v2/cloudintegration/awsExternalId', nil, :json) do
42
+ wf.create_aws_external_id
43
+ end
44
+ end
45
+
46
+ def test_delete_aws_external_id
47
+ assert_deletes("/api/v2/cloudintegration/awsExternalId/#{external_id}") do
48
+ wf.delete_aws_external_id(external_id)
49
+ end
50
+
51
+ assert_raises(Wavefront::Exception::InvalidAwsExternalId) do
52
+ wf.delete_aws_external_id(invalid_external_id)
53
+ end
54
+
55
+ assert_raises(ArgumentError) { wf.delete_aws_external_id }
56
+ end
57
+
58
+ def test_confirm_aws_external_id
59
+ assert_gets("/api/v2/cloudintegration/awsExternalId/#{external_id}") do
60
+ wf.confirm_aws_external_id(external_id)
61
+ end
62
+
63
+ assert_raises(Wavefront::Exception::InvalidAwsExternalId) do
64
+ wf.confirm_aws_external_id(invalid_external_id)
65
+ end
66
+
67
+ assert_raises(ArgumentError) { wf.confirm_aws_external_id }
68
+ end
69
+
40
70
  private
41
71
 
42
72
  def id
@@ -62,4 +92,12 @@ class WavefrontCloudIntegrationTest < WavefrontTestBase
62
92
  def api_class
63
93
  'cloudintegration'
64
94
  end
95
+
96
+ def external_id
97
+ 'HqOM4mru5svd3uFp'
98
+ end
99
+
100
+ def invalid_external_id
101
+ '__nonsense__'
102
+ end
65
103
  end
@@ -37,6 +37,49 @@ class WavefrontApiCallerTest < MiniTest::Test
37
37
  assert_requested(:get, uri, headers: headers)
38
38
  end
39
39
 
40
+ def test_get_flat_params
41
+ query = { rkey: %w[val1 val2 val3], ukey: 36 }
42
+ uri = "#{uri_base}/path?rkey=val1&rkey=val2&rkey=val3&ukey=36"
43
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
44
+ wf.get_flat_params('/path', query)
45
+ assert_requested(:get, uri, headers: headers)
46
+ end
47
+
48
+ def test_get_stream
49
+ uri = "#{uri_base}/path?key1=val1"
50
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
51
+ out, err = capture_io { wf.get_stream('/path', key1: 'val1') }
52
+ assert_requested(:get, uri, headers: headers)
53
+ assert_equal(out.strip, DUMMY_RESPONSE)
54
+ assert_empty(err)
55
+ end
56
+
57
+ def test_get_stream_array_params
58
+ uri = "#{uri_base}/path?key=val1&key=val2"
59
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
60
+ out, err = capture_io { wf.get_stream('/path', key: %w[val1 val2]) }
61
+ assert_requested(:get, uri, headers: headers)
62
+ assert_equal(out.strip, DUMMY_RESPONSE)
63
+ assert_empty(err)
64
+ end
65
+
66
+ def test_get_stream_timestamp
67
+ uri = "#{uri_base}/path?key1=val1"
68
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
69
+
70
+ out, err = capture_io do
71
+ wf.get_stream('/path',
72
+ { key1: 'val1' },
73
+ timestamp_chunks: true)
74
+ end
75
+
76
+ assert_requested(:get, uri, headers: headers)
77
+ out_lines = out.split("\n")
78
+ assert_match(/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d \+\d{4}$/, out_lines[0])
79
+ assert_equal(out_lines[1], DUMMY_RESPONSE)
80
+ assert_empty(err)
81
+ end
82
+
40
83
  def test_post
41
84
  uri = "#{uri_base}/path"
42
85
  obj = { key: 'value' }
@@ -153,10 +153,9 @@ class GibletsTest < MiniTest::Test
153
153
  end
154
154
 
155
155
  def test_load_from_file
156
- assert_equal({},
157
- wf.load_from_file(
158
- [Pathname.new('/no/file/1'), Pathname.new('/no/file/2')]
159
- ))
156
+ assert_raises(Wavefront::Exception::InvalidConfigFile) do
157
+ wf.load_from_file([Pathname.new('/nofile/1'), Pathname.new('/nofile/2')])
158
+ end
160
159
 
161
160
  assert_equal({ file: CONF1 }, wf.load_from_file([CONF1], 'noprofile'))
162
161
 
@@ -118,7 +118,7 @@ class WavefrontMetricHelperTest < MiniTest::Test
118
118
  assert_equal(1, out.select do |o|
119
119
  o[:value] == [[2, 10.0], [1, 11.0],
120
120
  [1, 12.0]]
121
- end .size)
121
+ end.size)
122
122
  assert_equal(1, out.select { |o| o[:tags] == WH_TAGS }.size)
123
123
  assert_equal(3, out.select { |o| o[:path] == 'test.dist1' }.size)
124
124
  end
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../spec_helper'
5
+ require_relative '../test_mixins/general'
6
+
7
+ # Unit tests for Wavefront::Role
8
+ #
9
+ class WavefrontRoleTest < WavefrontTestBase
10
+ include WavefrontTest::Create
11
+ include WavefrontTest::Delete
12
+ include WavefrontTest::Describe
13
+ include WavefrontTest::List
14
+ include WavefrontTest::Update
15
+
16
+ def test_add_assignees
17
+ assert_posts("/api/v2/role/#{id}/addAssignees", assignees.to_json) do
18
+ wf.add_assignees(id, assignees)
19
+ end
20
+
21
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
22
+ wf.add_assignees(invalid_id, assignees)
23
+ end
24
+ end
25
+
26
+ def test_remove_assignees
27
+ assert_posts("/api/v2/role/#{id}/removeAssignees", assignees.to_json) do
28
+ wf.remove_assignees(id, assignees)
29
+ end
30
+
31
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
32
+ wf.remove_assignees(invalid_id, assignees)
33
+ end
34
+ end
35
+
36
+ def test_grant
37
+ assert_posts("/api/v2/role/grant/#{permission}", roles.to_json) do
38
+ wf.grant(permission, roles)
39
+ end
40
+
41
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
42
+ wf.grant(permission, [invalid_id])
43
+ end
44
+
45
+ assert_raises(Wavefront::Exception::InvalidPermission) do
46
+ wf.grant('made_up_permission', roles)
47
+ end
48
+ end
49
+
50
+ def test_revoke
51
+ assert_posts("/api/v2/role/revoke/#{permission}", roles.to_json) do
52
+ wf.revoke(permission, roles)
53
+ end
54
+
55
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
56
+ wf.revoke(permission, [invalid_id])
57
+ end
58
+
59
+ assert_raises(Wavefront::Exception::InvalidPermission) do
60
+ wf.revoke('made_up_permission', roles)
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def api_class
67
+ :role
68
+ end
69
+
70
+ def id
71
+ 'f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672'
72
+ end
73
+
74
+ def roles
75
+ %w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672
76
+ 2659191e-aad4-4302-a94e-9667e1517127]
77
+ end
78
+
79
+ def assignees
80
+ roles.push('sa::test')
81
+ end
82
+
83
+ def invalid_id
84
+ '__BAD_ID__'
85
+ end
86
+
87
+ def payload
88
+ { name: 'test role',
89
+ permissions: %w[alerts_management events_management],
90
+ description: 'dummy role for unit tests' }
91
+ end
92
+
93
+ def permission
94
+ 'alerts_management'
95
+ end
96
+ end
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../spec_helper'
5
+ require_relative '../../lib/wavefront-sdk/spy'
6
+
7
+ # Unit tests for Spy class
8
+ #
9
+ class WavefrontSpyTest < MiniTest::Test
10
+ attr_reader :wf
11
+
12
+ def setup
13
+ @wf = Wavefront::Spy.new(CREDS)
14
+ end
15
+
16
+ def test_points
17
+ capture_io do
18
+ assert_gets('/api/spy/points?sampling=0.01') { wf.points }
19
+ assert_gets('/api/spy/points?sampling=0.05') { wf.points(0.05) }
20
+
21
+ assert_gets('/api/spy/points?sampling=0.05&metric=my_prefix') do
22
+ wf.points(0.05, prefix: 'my_prefix')
23
+ end
24
+
25
+ assert_gets('/api/spy/points?sampling=0.05&metric=my_prefix&host=h1') do
26
+ wf.points(0.05, prefix: 'my_prefix', host: 'h1')
27
+ end
28
+
29
+ assert_gets('/api/spy/points?sampling=0.02&metric=my_prefix&' \
30
+ 'pointTagKey=mytag') do
31
+ wf.points(0.02, prefix: 'my_prefix', tag_key: 'mytag')
32
+ end
33
+
34
+ assert_gets('/api/spy/points?sampling=0.02&metric=my_prefix&' \
35
+ 'pointTagKey=tag1&pointTagKey=tag2') do
36
+ wf.points(0.02, prefix: 'my_prefix', tag_key: %w[tag1 tag2])
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_histograms
42
+ capture_io do
43
+ assert_gets('/api/spy/histograms?sampling=0.01') { wf.histograms }
44
+ assert_gets('/api/spy/histograms?sampling=0.05') { wf.histograms(0.05) }
45
+
46
+ assert_gets('/api/spy/histograms?sampling=0.05&histogram=my_prefix') do
47
+ wf.histograms(0.05, prefix: 'my_prefix')
48
+ end
49
+
50
+ assert_gets(
51
+ '/api/spy/histograms?sampling=0.05&histogram=my_prefix&host=h1'
52
+ ) do
53
+ wf.histograms(0.05, prefix: 'my_prefix', host: 'h1')
54
+ end
55
+
56
+ assert_gets('/api/spy/histograms?sampling=0.02&histogram=my_prefix&' \
57
+ 'histogramTagKey=the_tag') do
58
+ wf.histograms(0.02, prefix: 'my_prefix', tag_key: 'the_tag')
59
+ end
60
+
61
+ assert_gets('/api/spy/histograms?sampling=0.02&histogram=my_prefix&' \
62
+ 'histogramTagKey=tag1&histogramTagKey=tag2') do
63
+ wf.histograms(0.02, prefix: 'my_prefix', tag_key: %w[tag1 tag2])
64
+ end
65
+ end
66
+ end
67
+
68
+ def test_spans
69
+ capture_io do
70
+ assert_gets('/api/spy/spans?sampling=0.01') { wf.spans }
71
+ assert_gets('/api/spy/spans?sampling=0.05') { wf.spans(0.05) }
72
+
73
+ assert_gets('/api/spy/spans?sampling=0.05&name=my_prefix') do
74
+ wf.spans(0.05, prefix: 'my_prefix')
75
+ end
76
+
77
+ assert_gets(
78
+ '/api/spy/spans?sampling=0.05&name=my_prefix&host=h1'
79
+ ) do
80
+ wf.spans(0.05, prefix: 'my_prefix', host: 'h1')
81
+ end
82
+
83
+ assert_gets('/api/spy/spans?sampling=0.02&name=my_prefix&' \
84
+ 'spanTagKey=the_tag') do
85
+ wf.spans(0.02, prefix: 'my_prefix', tag_key: 'the_tag')
86
+ end
87
+
88
+ assert_gets('/api/spy/spans?sampling=0.02&name=my_prefix&' \
89
+ 'spanTagKey=tag1&spanTagKey=tag2') do
90
+ wf.spans(0.02, prefix: 'my_prefix', tag_key: %w[tag1 tag2])
91
+ end
92
+ end
93
+ end
94
+
95
+ def test_ids
96
+ capture_io do
97
+ assert_gets('/api/spy/ids?sampling=0.01') { wf.ids }
98
+ assert_gets('/api/spy/ids?sampling=0.05') { wf.ids(0.05) }
99
+
100
+ assert_gets('/api/spy/ids?sampling=0.05&type=METRIC') do
101
+ wf.ids(0.05, type: 'METRIC')
102
+ end
103
+
104
+ assert_gets('/api/spy/ids?sampling=0.05&type=SPAN&name=my_prefix') do
105
+ wf.ids(0.05, type: 'SPAN', prefix: 'my_prefix')
106
+ end
107
+ end
108
+ end
109
+
110
+ def dummy_response
111
+ DUMMY_RESPONSE
112
+ end
113
+ end
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../../spec_helper'
5
+ require_relative '../../../lib/wavefront-sdk/unstable/chart'
6
+
7
+ # Unit tests for Chart class
8
+ #
9
+ class WavefrontChartTest < MiniTest::Test
10
+ attr_reader :wf
11
+
12
+ def setup
13
+ @wf = Wavefront::Unstable::Chart.new(CREDS)
14
+ end
15
+
16
+ def test_all_metrics
17
+ assert_gets('/chart/metrics/all?l=100&q=&trie=true') do
18
+ wf.metrics_under('')
19
+ end
20
+ end
21
+
22
+ def test_metrics_under
23
+ assert_gets('/chart/metrics/all?l=100&q=test.path&trie=true') do
24
+ wf.metrics_under('test.path')
25
+ end
26
+
27
+ assert_gets('/chart/metrics/all?l=10&q=test.path&trie=true') do
28
+ wf.metrics_under('test.path', nil, 10)
29
+ end
30
+
31
+ assert_gets('/chart/metrics/all?l=100&p=last.one&q=test.path&trie=true') do
32
+ wf.metrics_under('test.path', 'last.one')
33
+ end
34
+ end
35
+
36
+ def dummy_response
37
+ { metrics: ['test data'] }.to_json
38
+ end
39
+ end
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'logger'
4
5
  require_relative '../spec_helper'
5
6
  require_relative '../test_mixins/general'
6
7
 
@@ -13,6 +14,13 @@ class WavefrontUserTest < WavefrontTestBase
13
14
  include WavefrontTest::Describe
14
15
  include WavefrontTest::Update
15
16
 
17
+ # Override the parent constructor so we can suppress all the 'deprecated'
18
+ # log messages
19
+ #
20
+ def setup
21
+ @wf = Wavefront::User.new(CREDS, logger: Logger.new('/dev/null'))
22
+ end
23
+
16
24
  def test_list
17
25
  assert_gets('/api/v2/user') { wf.list }
18
26
  end
@@ -7,7 +7,8 @@ require_relative '../test_mixins/general'
7
7
  # Unit tests for WavefrontUserGroup
8
8
  #
9
9
  class WavefrontUserGroupTest < WavefrontTestBase
10
- attr_reader :users, :groups, :permission, :invalid_groups, :invalid_users
10
+ attr_reader :users, :groups, :permission, :invalid_groups, :invalid_users,
11
+ :roles, :invalid_roles
11
12
 
12
13
  include WavefrontTest::Create
13
14
  include WavefrontTest::Delete
@@ -39,22 +40,28 @@ class WavefrontUserGroupTest < WavefrontTestBase
39
40
  assert_invalid_id { wf.remove_users_from_group(invalid_id, users) }
40
41
  end
41
42
 
42
- def test_grant
43
- assert_posts("/api/v2/usergroup/grant/#{permission}",
44
- groups.to_json) do
45
- wf.grant(permission, groups)
43
+ def test_add_roles_to_group
44
+ assert_posts("/api/v2/usergroup/#{id}/addRoles", roles.to_json) do
45
+ wf.add_roles_to_group(id, roles)
46
46
  end
47
47
 
48
- assert_invalid_id { wf.grant(permission, invalid_groups) }
48
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
49
+ wf.add_roles_to_group(id, invalid_roles)
50
+ end
51
+
52
+ assert_invalid_id { wf.add_roles_to_group(invalid_id, roles) }
49
53
  end
50
54
 
51
- def test_revoke
52
- assert_posts("/api/v2/usergroup/revoke/#{permission}",
53
- groups.to_json) do
54
- wf.revoke(permission, groups)
55
+ def test_remove_roles_from_group
56
+ assert_posts("/api/v2/usergroup/#{id}/removeRoles", roles.to_json) do
57
+ wf.remove_roles_from_group(id, roles)
58
+ end
59
+
60
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
61
+ wf.remove_roles_from_group(id, invalid_roles)
55
62
  end
56
63
 
57
- assert_invalid_id { wf.revoke(permission, invalid_groups) }
64
+ assert_invalid_id { wf.remove_roles_from_group(invalid_id, roles) }
58
65
  end
59
66
 
60
67
  def setup_fixtures
@@ -63,7 +70,10 @@ class WavefrontUserGroupTest < WavefrontTestBase
63
70
  @groups = %w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672
64
71
  2659191e-aad4-4302-a94e-9667e1517127]
65
72
  @users = %w[someone@somewhere.com other@elsewhere.net]
73
+ @roles = %w[abcdef14-91a0-4ca9-8a2a-7d47f4db4672
74
+ fedcba1e-aad4-4302-a94e-9667e1517127]
66
75
  @invalid_users = ['bad' * 500, '']
76
+ @invalid_roles = ['some nonsense']
67
77
  end
68
78
 
69
79
  private