wavefront-cli 8.2.0 → 8.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'pathname'
5
+ require 'spy'
6
+ require 'minitest/autorun'
7
+ require_relative '../../lib/wavefront-cli/event_store'
8
+
9
+ TEST_EVENT_STORE_DIR = Pathname.new(Dir.mktmpdir)
10
+
11
+ # Tests for event store class. This is tested well via the interface of the
12
+ # events CLI class.
13
+ #
14
+ class Test < MiniTest::Test
15
+ attr_reader :wf
16
+
17
+ include WavefrontCli::Constants
18
+
19
+ def before_setup
20
+ FileUtils.mkdir_p(TEST_EVENT_STORE_DIR)
21
+ end
22
+
23
+ def setup
24
+ @wf = WavefrontCli::EventStore.new({}, TEST_EVENT_STORE_DIR)
25
+ end
26
+
27
+ def teardown
28
+ FileUtils.rm_r(TEST_EVENT_STORE_DIR)
29
+ end
30
+
31
+ def test_state_file_needed?
32
+ wf1 = WavefrontCli::EventStore.new({}, TEST_EVENT_STORE_DIR)
33
+ assert wf1.state_file_needed?
34
+
35
+ wf2 = WavefrontCli::EventStore.new({ nostate: true }, TEST_EVENT_STORE_DIR)
36
+ refute wf2.state_file_needed?
37
+
38
+ wf3 = WavefrontCli::EventStore.new({ instant: true }, TEST_EVENT_STORE_DIR)
39
+ refute wf3.state_file_needed?
40
+
41
+ wf4 = WavefrontCli::EventStore.new({ start: Time.now - 20, end: Time.now },
42
+ TEST_EVENT_STORE_DIR)
43
+ refute wf4.state_file_needed?
44
+ end
45
+
46
+ def test_event_file
47
+ x = wf.event_file(id)
48
+ assert_instance_of(Pathname, x)
49
+ assert_equal(wf.dir, x.dirname)
50
+ assert_equal(id, x.basename.to_s)
51
+
52
+ assert_nil(wf.event_file('not_a_valid_id'))
53
+ end
54
+
55
+ def test_create_dir_ok
56
+ dir = TEST_EVENT_STORE_DIR + 'testdir'
57
+ refute dir.exist?
58
+ wf.create_dir(dir)
59
+ assert dir.exist?
60
+ dir.unlink
61
+ end
62
+
63
+ def test_list
64
+ setup_test_state_dir
65
+
66
+ x = wf.list
67
+ assert(x.all? { |e| e.is_a?(Pathname) })
68
+ assert_equal(4, x.size)
69
+
70
+ empty_test_state_dir
71
+ end
72
+
73
+ def test_list_empty_stack
74
+ out, err = capture_io { assert_raises(SystemExit) { wf.list } }
75
+ assert_empty(out)
76
+ assert_equal("No locally recorded events.\n", err)
77
+ end
78
+
79
+ def test_pop_event
80
+ setup_test_state_dir
81
+
82
+ assert (wf.dir + '1568133440530:ev3:0').exist?
83
+ assert_equal('1568133440530:ev3:0', wf.pop_event!)
84
+ refute (wf.dir + '1568133440530:ev3:0').exist?
85
+
86
+ empty_test_state_dir
87
+ end
88
+
89
+ def test_pop_event_named
90
+ setup_test_state_dir
91
+
92
+ assert (wf.dir + '1568133440515:ev1:1').exist?
93
+ assert_equal('1568133440515:ev1:1', wf.pop_event!('ev1'))
94
+ refute (wf.dir + '1568133440515:ev1:1').exist?
95
+
96
+ empty_test_state_dir
97
+ end
98
+
99
+ def test_event_specific
100
+ setup_test_state_dir
101
+
102
+ assert (wf.dir + '1568133440515:ev1:1').exist?
103
+ assert_equal('1568133440515:ev1:1', wf.event('1568133440515:ev1:1'))
104
+ assert (wf.dir + '1568133440515:ev1:1').exist?
105
+
106
+ empty_test_state_dir
107
+ end
108
+
109
+ def test_pop_event_empty_stack
110
+ out, err = capture_io { assert_raises(SystemExit) { wf.pop_event! } }
111
+ assert_empty(out)
112
+ assert_equal("No locally recorded events.\n", err)
113
+ end
114
+
115
+ def test_event_state_dir
116
+ ENV['WF_EVENT_STATE_DIR'] = nil
117
+ assert_equal(EVENT_STATE_DIR, wf.event_state_dir)
118
+
119
+ ENV['WF_EVENT_STATE_DIR'] = '/tmp/tester'
120
+ assert_equal(Pathname.new('/tmp/tester'), wf.event_state_dir)
121
+ ENV['WF_EVENT_STATE_DIR'] = nil
122
+
123
+ assert_equal(Pathname.new('/tmp/mydir'), wf.event_state_dir('/tmp/mydir'))
124
+ end
125
+
126
+ def test_create_dir_fail
127
+ spy = Spy.on(FileUtils, :mkdir_p).and_return(false)
128
+
129
+ assert_raises(WavefrontCli::Exception::SystemError) do
130
+ wf.create_dir(Pathname.new('/any/old/directory'))
131
+ end
132
+
133
+ assert spy.has_been_called?
134
+ spy.unhook
135
+ end
136
+
137
+ def test_event_file_data
138
+ wf = WavefrontCli::EventStore.new({ desc: 'test event' },
139
+ TEST_EVENT_STORE_DIR)
140
+ x = wf.event_file_data
141
+ assert_instance_of(String, x)
142
+ y = JSON.parse(x, symbolize_names: true)
143
+ assert_equal('test event', y[:description])
144
+ assert_equal(%i[hosts description severity tags], y.keys)
145
+ assert_nil(y[:tags])
146
+ end
147
+
148
+ def test_create
149
+ refute (wf.dir + id).exist?
150
+ out, err = capture_io { wf.create!(id) }
151
+ assert_match(/Event state recorded at .*1481553823153:testev:0./, out)
152
+ assert_empty(err)
153
+ assert (wf.dir + id).exist?
154
+ end
155
+
156
+ def test_create_with_nostate
157
+ wf1 = WavefrontCli::EventStore.new(nostate: true)
158
+ assert_nil wf1.create!(id)
159
+ end
160
+
161
+ private
162
+
163
+ def id
164
+ '1481553823153:testev:0'
165
+ end
166
+
167
+ def dummy_event_files
168
+ %w[1568133440510:ev1:0
169
+ 1568133440515:ev1:1
170
+ 1568133440520:ev2:0
171
+ 1568133440530:ev3:0]
172
+ end
173
+
174
+ def setup_test_state_dir
175
+ dummy_event_files.each do |f|
176
+ File.open(wf.dir + f, 'w') { |fh| fh.puts('dummy_data') }
177
+ end
178
+ end
179
+
180
+ def empty_test_state_dir
181
+ dummy_event_files.each do |f|
182
+ file = wf.dir + f
183
+ FileUtils.rm(file) if file.exist?
184
+ end
185
+ end
186
+ end
@@ -36,6 +36,19 @@ class ProxyEndToEndTest < EndToEndTest
36
36
  assert_abort_on_missing_creds("rename #{id} newname")
37
37
  end
38
38
 
39
+ def test_shutdown
40
+ assert_output("Requested shutdown of proxy '#{id}'.\n") do
41
+ assert_cmd_puts("shutdown #{id}", "/api/v2/proxy/#{id}",
42
+ { shutdown: true }.to_json)
43
+ end
44
+
45
+ assert_noop("shutdown #{id}",
46
+ "uri: PUT https://default.wavefront.com/api/v2/proxy/#{id}",
47
+ 'body: {"shutdown":true}')
48
+ assert_invalid_id("shutdown #{invalid_id}")
49
+ assert_abort_on_missing_creds("shutdown #{id}")
50
+ end
51
+
39
52
  private
40
53
 
41
54
  def id
@@ -41,20 +41,36 @@ class ServiceAccountEndToEndTest < EndToEndTest
41
41
  assert_abort_on_missing_creds("groups #{id}")
42
42
  end
43
43
 
44
- def test_permissions
44
+ def test_roles
45
45
  quietly do
46
- assert_cmd_gets("permissions #{id}", "/api/v2/#{api_path}/#{id}")
46
+ assert_cmd_gets("roles #{id}", "/api/v2/#{api_path}/#{id}")
47
47
  end
48
48
 
49
- assert_invalid_id("permissions #{invalid_id}")
50
- assert_usage('permissions')
49
+ assert_invalid_id("roles #{invalid_id}")
50
+ assert_usage('roles')
51
51
 
52
52
  assert_noop(
53
- "permissions #{id}",
53
+ "roles #{id}",
54
54
  "uri: GET https://default.wavefront.com/api/v2/#{api_path}/#{id}"
55
55
  )
56
56
 
57
- assert_abort_on_missing_creds("permissions #{id}")
57
+ assert_abort_on_missing_creds("roles #{id}")
58
+ end
59
+
60
+ def test_ingestionpolicy
61
+ quietly do
62
+ assert_cmd_gets("ingestionpolicy #{id}", "/api/v2/#{api_path}/#{id}")
63
+ end
64
+
65
+ assert_invalid_id("ingestionpolicy #{invalid_id}")
66
+ assert_usage('ingestionpolicy')
67
+
68
+ assert_noop(
69
+ "ingestionpolicy #{id}",
70
+ "uri: GET https://default.wavefront.com/api/v2/#{api_path}/#{id}"
71
+ )
72
+
73
+ assert_abort_on_missing_creds("ingestionpolicy #{id}")
58
74
  end
59
75
 
60
76
  def test_activate
@@ -85,7 +101,7 @@ class ServiceAccountEndToEndTest < EndToEndTest
85
101
  '/api/v2/account/serviceaccount',
86
102
  identifier: id,
87
103
  active: true,
88
- groups: [],
104
+ roles: [],
89
105
  tokens: [],
90
106
  userGroups: [])
91
107
  end
@@ -95,8 +111,8 @@ class ServiceAccountEndToEndTest < EndToEndTest
95
111
  'uri: POST https://default.wavefront.com/api/v2/account/serviceaccount',
96
112
  'body: ' + { identifier: id,
97
113
  active: true,
98
- groups: [],
99
114
  tokens: [],
115
+ roles: [],
100
116
  userGroups: [] }.to_json
101
117
  )
102
118
 
@@ -111,43 +127,56 @@ class ServiceAccountEndToEndTest < EndToEndTest
111
127
  identifier: id,
112
128
  description: 'words',
113
129
  active: false,
114
- groups: [],
130
+ roles: [],
115
131
  tokens: [],
116
132
  userGroups: [])
117
133
  end
118
134
  end
119
135
 
120
- def test_create_account_in_usergroups
136
+ def test_create_account_with_usergroups
121
137
  quietly do
122
138
  assert_cmd_posts("create -g #{usergroups[0]} -g #{usergroups[1]} #{id}",
123
139
  '/api/v2/account/serviceaccount',
124
140
  identifier: id,
125
141
  active: true,
126
- groups: [],
127
142
  tokens: [],
143
+ roles: [],
128
144
  userGroups: usergroups)
129
145
  end
130
146
  end
131
147
 
148
+ def test_create_account_with_roles
149
+ quietly do
150
+ assert_cmd_posts("create -r #{roles[0]} -r #{roles[1]} #{id}",
151
+ '/api/v2/account/serviceaccount',
152
+ identifier: id,
153
+ active: true,
154
+ tokens: [],
155
+ roles: roles,
156
+ userGroups: [])
157
+ end
158
+ end
159
+
132
160
  def test_create_account_with_tokens
133
161
  quietly do
134
162
  assert_cmd_posts("create -k #{tokens[0]} -k #{tokens[1]} #{id}",
135
163
  '/api/v2/account/serviceaccount',
136
164
  identifier: id,
137
165
  active: true,
138
- groups: [],
139
166
  tokens: tokens,
167
+ roles: [],
140
168
  userGroups: [])
141
169
  end
142
170
  end
143
171
 
144
- def test_create_account_with_permissions
172
+ def test_create_account_with_ingestion_policy
145
173
  quietly do
146
- assert_cmd_posts("create -p #{permissions[0]} -p #{permissions[1]} #{id}",
174
+ assert_cmd_posts("create -p #{ingestion_policy} #{id}",
147
175
  '/api/v2/account/serviceaccount',
148
176
  identifier: id,
149
177
  active: true,
150
- groups: permissions,
178
+ ingestionPolicyId: ingestion_policy,
179
+ roles: [],
151
180
  tokens: [],
152
181
  userGroups: [])
153
182
  end
@@ -158,8 +187,8 @@ class ServiceAccountEndToEndTest < EndToEndTest
158
187
  "create -g abcdefg #{id}")
159
188
  end
160
189
 
161
- def test_create_invalid_permission
162
- assert_exits_with("'123456' is not a valid Wavefront permission.",
190
+ def test_create_invalid_ingestion_policy
191
+ assert_exits_with("'123456' is not a valid ingestion policy ID.",
163
192
  "create -p 123456 #{id}")
164
193
  end
165
194
 
@@ -358,8 +387,9 @@ class ServiceAccountEndToEndTest < EndToEndTest
358
387
  'service account'
359
388
  end
360
389
 
361
- def permissions
362
- %w[alerts_management events_management]
390
+ def roles
391
+ %w[07fc5cdd-0979-489e-8f70-325f39d15e55
392
+ 0a42adf6-e738-4c5d-9e53-fd10bd979a31]
363
393
  end
364
394
 
365
395
  def tokens
@@ -372,6 +402,10 @@ class ServiceAccountEndToEndTest < EndToEndTest
372
402
  abcdef12-1234-abcd-1234-abcdef012345]
373
403
  end
374
404
 
405
+ def ingestion_policy
406
+ 'test-policy-1607616352537'
407
+ end
408
+
375
409
  def import_fields
376
410
  %i[identifier description active tokens groups userGroups]
377
411
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |gem|
26
26
 
27
27
  gem.add_runtime_dependency 'docopt', '~> 0.6.0'
28
28
  gem.add_runtime_dependency 'inifile', '~> 3.0'
29
- gem.add_runtime_dependency 'wavefront-sdk', '~> 5.2'
29
+ gem.add_runtime_dependency 'wavefront-sdk', '~> 5.4', '>= 5.4.2'
30
30
 
31
31
  gem.add_development_dependency 'minitest', '~> 5.14'
32
32
  gem.add_development_dependency 'rake', '~> 13.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavefront-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.2.0
4
+ version: 8.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Fisher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-06 00:00:00.000000000 Z
11
+ date: 2021-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt
@@ -44,14 +44,20 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.2'
47
+ version: '5.4'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 5.4.2
48
51
  type: :runtime
49
52
  prerelease: false
50
53
  version_requirements: !ruby/object:Gem::Requirement
51
54
  requirements:
52
55
  - - "~>"
53
56
  - !ruby/object:Gem::Version
54
- version: '5.2'
57
+ version: '5.4'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 5.4.2
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: minitest
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -226,6 +232,7 @@ files:
226
232
  - lib/wavefront-cli/display/webhook.rb
227
233
  - lib/wavefront-cli/display/write.rb
228
234
  - lib/wavefront-cli/event.rb
235
+ - lib/wavefront-cli/event_store.rb
229
236
  - lib/wavefront-cli/exception.rb
230
237
  - lib/wavefront-cli/exception_handler.rb
231
238
  - lib/wavefront-cli/externallink.rb
@@ -316,6 +323,7 @@ files:
316
323
  - spec/wavefront-cli/display/printer/long_spec.rb
317
324
  - spec/wavefront-cli/display/printer/terse_spec.rb
318
325
  - spec/wavefront-cli/event_spec.rb
326
+ - spec/wavefront-cli/event_store_spec.rb
319
327
  - spec/wavefront-cli/externallink_spec.rb
320
328
  - spec/wavefront-cli/integration_spec.rb
321
329
  - spec/wavefront-cli/maintenancewindow_spec.rb
@@ -455,6 +463,7 @@ test_files:
455
463
  - spec/wavefront-cli/display/printer/long_spec.rb
456
464
  - spec/wavefront-cli/display/printer/terse_spec.rb
457
465
  - spec/wavefront-cli/event_spec.rb
466
+ - spec/wavefront-cli/event_store_spec.rb
458
467
  - spec/wavefront-cli/externallink_spec.rb
459
468
  - spec/wavefront-cli/integration_spec.rb
460
469
  - spec/wavefront-cli/maintenancewindow_spec.rb