bookie_accounting 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,15 +21,15 @@ module Bookie
21
21
  yield
22
22
  end
23
23
  end
24
-
24
+
25
25
  @locks = {}
26
-
26
+
27
27
  ##
28
28
  #Returns a lock by name
29
29
  def self.[](name)
30
- @locks[name.to_sym] ||= find_by_name(name.to_s) or raise "Unable to find lock '#{name}'"
30
+ @locks[name.to_sym] ||= find_by!(name: name.to_s)
31
31
  end
32
-
32
+
33
33
  validates_presence_of :name
34
34
  end
35
35
  end
@@ -10,17 +10,17 @@ module Bookie
10
10
  #A system type
11
11
  class SystemType < ActiveRecord::Base
12
12
  has_many :systems
13
-
13
+
14
14
  validates_presence_of :name, :memory_stat_type
15
15
 
16
16
  ##
17
17
  #Maps memory stat type symbols to their enumerated values in the database
18
18
  MEMORY_STAT_TYPE = {:unknown => 0, :avg => 1, :max => 2}
19
-
19
+
20
20
  ##
21
21
  #The inverse of MEMORY_STAT_TYPE
22
22
  MEMORY_STAT_TYPE_INVERSE = MEMORY_STAT_TYPE.invert
23
-
23
+
24
24
  ##
25
25
  #Finds a system type by name and memory stat type, creating it if it doesn't exist
26
26
  #
@@ -30,7 +30,7 @@ module Bookie
30
30
  def self.find_or_create!(name, memory_stat_type)
31
31
  sys_type = nil
32
32
  Lock[:system_types].synchronize do
33
- sys_type = SystemType.find_by_name(name)
33
+ sys_type = SystemType.find_by(name: name)
34
34
  if sys_type
35
35
  unless sys_type.memory_stat_type == memory_stat_type
36
36
  type_code = MEMORY_STAT_TYPE[memory_stat_type]
@@ -49,7 +49,7 @@ module Bookie
49
49
  end
50
50
  sys_type
51
51
  end
52
-
52
+
53
53
  ##
54
54
  #Returns the memory stat type as a symbol
55
55
  #
@@ -63,7 +63,7 @@ module Bookie
63
63
  raise "Unrecognized memory stat type code #{type_code}" unless type
64
64
  type
65
65
  end
66
-
66
+
67
67
  ##
68
68
  #Sets the memory stat type
69
69
  #
@@ -9,7 +9,7 @@ module Bookie
9
9
  #Model for a user
10
10
  class User < ActiveRecord::Base
11
11
  belongs_to :group
12
-
12
+
13
13
  def self.by_name(name)
14
14
  where('users.name = ?', name)
15
15
  end
@@ -19,11 +19,11 @@ module Bookie
19
19
  end
20
20
 
21
21
  def self.by_group_name(name)
22
- group = Group.find_by_name(name)
22
+ group = Group.find_by(name: name)
23
23
  return by_group(group) if group
24
24
  self.none
25
25
  end
26
-
26
+
27
27
  ##
28
28
  #Finds a user by name and group, creating it if it doesn't exist
29
29
  #
@@ -36,7 +36,7 @@ module Bookie
36
36
  unless user
37
37
  Lock[:users].synchronize do
38
38
  #Does the user already exist?
39
- user = Bookie::Database::User.find_by_name_and_group_id(name, group.id)
39
+ user = Bookie::Database::User.find_by(name: name, group_id: group.id)
40
40
  user ||= Bookie::Database::User.create!(
41
41
  :name => name,
42
42
  :group => group
@@ -46,7 +46,7 @@ module Bookie
46
46
  end
47
47
  user
48
48
  end
49
-
49
+
50
50
  validates_presence_of :group, :name
51
51
  end
52
52
 
@@ -10,21 +10,23 @@ module Bookie
10
10
  @io = STDOUT
11
11
  end
12
12
  end
13
-
13
+
14
14
  def do_print_summary(field_values)
15
15
  Formatter::SUMMARY_FIELD_LABELS.zip(field_values) do |label, value|
16
16
  @io.printf("%-30.30s%s\n", "#{label}:", value)
17
17
  end
18
18
  end
19
-
19
+
20
20
  def do_print_jobs(jobs)
21
- #To consider: optimize by moving out of the function?
22
- format_string = "%-15.15s %-15.15s %-20.20s %-20.20s %-26.26s %-26.26s %-30.30s %-30.30s %-20.20s %-20.20s %-11.11s\n"
21
+ #TODO: optimize by moving out of the function?
22
+ format_string = "%-15.15s %-15.15s %-20.20s %-20.20s %-26.26s %-26.26s %-30.30s %-30.30s %-20.20s %-20.20s %-11.11s"
23
23
  heading = sprintf(format_string, *Formatter::DETAILS_FIELD_LABELS)
24
- @io.write heading
25
- @io.puts '-' * (heading.length - 1)
24
+ @io.puts heading.rstrip
25
+ @io.puts '-' * (heading.length)
26
26
  fields_for_each_job(jobs) do |fields|
27
- @io.printf(format_string, *fields)
27
+ line = sprintf(format_string, *fields)
28
+ line.rstrip!
29
+ @io.puts line
28
30
  end
29
31
  end
30
32
  end
@@ -1,4 +1,4 @@
1
1
  module Bookie
2
2
  #The library version
3
- VERSION = "2.0.0"
3
+ VERSION = "2.0.1"
4
4
  end
data/spec/config_spec.rb CHANGED
@@ -5,33 +5,33 @@ require 'active_record'
5
5
  describe Bookie::Config do
6
6
  it "loads correct data" do
7
7
  config = Bookie::Config.new('snapshot/test_config.json')
8
-
9
- config.db_type.should eql "sqlite3"
10
- config.server.should eql "localhost"
11
- config.port.should eql 8080
12
- config.database.should eql ":memory:"
13
- config.username.should eql "blm768"
14
- config.password.should eql "test"
15
- config.excluded_users.should eql Set.new(["root"])
16
- config.hostname.should eql "localhost"
17
- config.cores.should eql 8
18
- config.memory.should eql 8000000
19
- config.system_type.should eql 'torque_cluster'
8
+
9
+ expect(config.db_type).to eql "sqlite3"
10
+ expect(config.server).to eql "localhost"
11
+ expect(config.port).to eql 8080
12
+ expect(config.database).to eql ":memory:"
13
+ expect(config.username).to eql "blm768"
14
+ expect(config.password).to eql "test"
15
+ expect(config.excluded_users).to eql Set.new(["root"])
16
+ expect(config.hostname).to eql "localhost"
17
+ expect(config.cores).to eql 8
18
+ expect(config.memory).to eql 8000000
19
+ expect(config.system_type).to eql 'torque_cluster'
20
20
  end
21
-
21
+
22
22
  it "correctly verifies types" do
23
23
  config = Bookie::Config.new('snapshot/default.json')
24
24
  config.verify_type("abc", "test", String)
25
25
  expect { config.verify_type("abc", "test", Fixnum) }.to raise_error(TypeError, 'Invalid data type String for JSON field "test": Fixnum expected')
26
26
  end
27
-
27
+
28
28
  it "sets correct defaults" do
29
29
  dconfig = Bookie::Config.new('snapshot/default.json')
30
-
31
- dconfig.port.should eql nil
32
- dconfig.excluded_users.should eql Set.new([])
30
+
31
+ expect(dconfig.port).to eql nil
32
+ expect(dconfig.excluded_users).to eql Set.new([])
33
33
  end
34
-
34
+
35
35
  it 'correctly handles missing fields' do
36
36
  fields = JSON.parse(File.read('snapshot/default.json'))
37
37
  fields.keys.each do |key|
@@ -44,12 +44,12 @@ describe Bookie::Config do
44
44
  #This should not raise an error.
45
45
  Bookie::Config.new('snapshot/default.json')
46
46
  end
47
-
47
+
48
48
  it "configures and connects to the database" do
49
49
  config = Bookie::Config.new('snapshot/test_config.json')
50
50
  ActiveRecord::Base.expects(:establish_connection)
51
51
  config.connect
52
- ActiveRecord::Base.time_zone_aware_attributes.should eql true
53
- ActiveRecord::Base.default_timezone.should eql :utc
52
+ expect(ActiveRecord::Base.time_zone_aware_attributes).to eql true
53
+ expect(ActiveRecord::Base.default_timezone).to eql :utc
54
54
  end
55
55
  end
@@ -6,31 +6,30 @@ describe Bookie::Database::Group do
6
6
  Bookie::Database::Group.expects(:"create!")
7
7
  Bookie::Database::Group.find_or_create!('non_root')
8
8
  end
9
-
9
+
10
10
  it "returns the cached group if one exists" do
11
- group = Bookie::Database::Group.find_by_name('root')
11
+ group = Bookie::Database::Group.find_by(name: 'root')
12
12
  known_groups = {'root' => group}
13
- Bookie::Database::Group.find_or_create!('root', known_groups).should equal group
13
+ expect(Bookie::Database::Group.find_or_create!('root', known_groups)).to equal group
14
14
  end
15
-
15
+
16
16
  it "queries the database when this group is not cached" do
17
- group = Bookie::Database::Group.find_by_name('root')
17
+ group = Bookie::Database::Group.find_by(name: 'root')
18
18
  known_groups = {}
19
- Bookie::Database::Group.expects(:find_by_name).returns(group).twice
19
+ Bookie::Database::Group.expects(:find_by).returns(group).twice
20
20
  Bookie::Database::Group.expects(:"create!").never
21
- Bookie::Database::Group.find_or_create!('root', known_groups).should eql group
22
- Bookie::Database::Group.find_or_create!('root', nil).should eql group
23
- known_groups.should include 'root'
21
+ expect(Bookie::Database::Group.find_or_create!('root', known_groups)).to eql group
22
+ expect(Bookie::Database::Group.find_or_create!('root', nil)).to eql group
23
+ expect(known_groups).to include 'root'
24
24
  end
25
25
  end
26
-
26
+
27
27
  it "validates the name field" do
28
28
  group = Bookie::Database::Group.new(:name => nil)
29
- group.valid?.should eql false
29
+ expect(group.valid?).to eql false
30
30
  group.name = ''
31
- group.valid?.should eql false
31
+ expect(group.valid?).to eql false
32
32
  group.name = 'test'
33
- group.valid?.should eql true
33
+ expect(group.valid?).to eql true
34
34
  end
35
35
  end
36
-
@@ -16,15 +16,15 @@ end
16
16
  describe Bookie::Database::Job do
17
17
  it "correctly sets end times" do
18
18
  Job.find_each do |job|
19
- job.end_time.should eql job.start_time + job.wall_time
20
- job.end_time.should eql job.read_attribute(:end_time)
19
+ expect(job.end_time).to eql job.start_time + job.wall_time
20
+ expect(job.end_time).to eql job.read_attribute(:end_time)
21
21
  end
22
22
 
23
23
  #Test the update hook.
24
24
  job = Job.first
25
25
  job.start_time -= 1
26
26
  job.save!
27
- job.end_time.should eql job.read_attribute(:end_time)
27
+ expect(job.end_time).to eql job.read_attribute(:end_time)
28
28
  end
29
29
 
30
30
  describe "#end_time=" do
@@ -38,106 +38,106 @@ describe Bookie::Database::Job do
38
38
  expect(job.end_time).to eql(old_end_time - 1)
39
39
  end
40
40
  end
41
-
41
+
42
42
  it "correctly filters by user" do
43
43
  user = User.by_name('test').order(:id).first
44
44
  jobs = Job.by_user(user).to_a
45
45
  jobs.each do |job|
46
- job.user.should eql user
46
+ expect(job.user).to eql user
47
47
  end
48
- jobs.length.should eql 10
48
+ expect(jobs.length).to eql 10
49
49
  end
50
-
50
+
51
51
  it "correctly filters by user name" do
52
52
  jobs = Job.by_user_name('root').to_a
53
- jobs.length.should eql 10
54
- jobs[0].user.name.should eql "root"
53
+ expect(jobs.length).to eql 10
54
+ expect(jobs[0].user.name).to eql "root"
55
55
  jobs = Job.by_user_name('test').order(:end_time).to_a
56
- jobs.length.should eql 20
56
+ expect(jobs.length).to eql 20
57
57
  jobs.each do |job|
58
- job.user.name.should eql 'test'
58
+ expect(job.user.name).to eql 'test'
59
59
  end
60
- jobs[0].user_id.should_not eql jobs[-1].user_id
60
+ expect(jobs[0].user_id).to_not eql jobs[-1].user_id
61
61
  jobs = Job.by_user_name('user').to_a
62
- jobs.length.should eql 0
62
+ expect(jobs.length).to eql 0
63
63
  end
64
64
 
65
65
  it "correctly filters by group name" do
66
66
  jobs = Job.by_group_name("root").to_a
67
- jobs.length.should eql 10
67
+ expect(jobs.length).to eql 10
68
68
  jobs.each do |job|
69
- job.user.group.name.should eql "root"
69
+ expect(job.user.group.name).to eql "root"
70
70
  end
71
71
  jobs = Job.by_group_name("admin").order(:start_time).to_a
72
- jobs.length.should eql 20
73
- jobs[0].user.name.should_not eql jobs[1].user.name
72
+ expect(jobs.length).to eql 20
73
+ expect(jobs[0].user.name).to_not eql jobs[1].user.name
74
74
  jobs = Job.by_group_name("test").to_a
75
- jobs.length.should eql 0
75
+ expect(jobs.length).to eql 0
76
76
  end
77
-
77
+
78
78
  it "correctly filters by system" do
79
79
  sys = System.first
80
80
  jobs = Job.by_system(sys)
81
- jobs.length.should eql 10
81
+ expect(jobs.length).to eql 10
82
82
  jobs.each do |job|
83
- job.system.should eql sys
83
+ expect(job.system).to eql sys
84
84
  end
85
85
  end
86
-
86
+
87
87
  it "correctly filters by system name" do
88
88
  jobs = Job.by_system_name('test1')
89
- jobs.length.should eql 20
89
+ expect(jobs.length).to eql 20
90
90
  jobs = Job.by_system_name('test2')
91
- jobs.length.should eql 10
91
+ expect(jobs.length).to eql 10
92
92
  jobs = Job.by_system_name('test3')
93
- jobs.length.should eql 10
93
+ expect(jobs.length).to eql 10
94
94
  jobs = Job.by_system_name('test4')
95
- jobs.length.should eql 0
95
+ expect(jobs.length).to eql 0
96
96
  end
97
-
97
+
98
98
  it "correctly filters by system type" do
99
- sys_type = SystemType.find_by_name('Standalone')
99
+ sys_type = SystemType.find_by(name: 'Standalone')
100
100
  jobs = Job.by_system_type(sys_type)
101
- jobs.length.should eql 20
102
- sys_type = SystemType.find_by_name('TORQUE cluster')
101
+ expect(jobs.length).to eql 20
102
+ sys_type = SystemType.find_by(name: 'TORQUE cluster')
103
103
  jobs = Job.by_system_type(sys_type)
104
- jobs.length.should eql 20
104
+ expect(jobs.length).to eql 20
105
105
  end
106
106
 
107
107
  it "correctly filters by command name" do
108
108
  jobs = Job.by_command_name('vi')
109
- jobs.length.should eql 20
109
+ expect(jobs.length).to eql 20
110
110
  jobs = Job.by_command_name('emacs')
111
- jobs.length.should eql 20
111
+ expect(jobs.length).to eql 20
112
112
  end
113
-
113
+
114
114
  describe "#by_time_range" do
115
115
  let(:base_start) { base_time + 1.hours }
116
116
  let(:base_end) { base_start + 2.hours }
117
117
 
118
118
  it "filters by inclusive time range" do
119
119
  jobs = Job.by_time_range(base_start ... base_end + 1)
120
- jobs.count.should eql 3
121
- jobs = Job.by_time_range(base_start + 1 ... base_end)
122
- jobs.count.should eql 2
120
+ expect(jobs.count).to eql 3
121
+ jobs = Job.by_time_range(base_start + 1 ... base_end)
122
+ expect(jobs.count).to eql 2
123
123
  jobs = Job.by_time_range(base_start ... base_start)
124
- jobs.length.should eql 0
124
+ expect(jobs.length).to eql 0
125
125
  end
126
126
 
127
127
  it "filters by exclusive time range" do
128
128
  jobs = Job.by_time_range(base_start + 1 .. base_end)
129
- jobs.count.should eql 3
129
+ expect(jobs.count).to eql 3
130
130
  jobs = Job.by_time_range(base_start .. base_end - 1)
131
- jobs.count.should eql 2
131
+ expect(jobs.count).to eql 2
132
132
  jobs = Job.by_time_range(base_start .. base_start)
133
- jobs.count.should eql 1
133
+ expect(jobs.count).to eql 1
134
134
  end
135
-
135
+
136
136
  context "with an empty range" do
137
137
  it "finds no jobs" do
138
138
  (-1 .. 0).each do |offset|
139
139
  jobs = Job.by_time_range(base_start ... base_start + offset)
140
- jobs.count.should eql 0
140
+ expect(jobs.count).to eql 0
141
141
  end
142
142
  end
143
143
  end
@@ -176,7 +176,7 @@ describe Bookie::Database::Job do
176
176
  jobs = Job.overlapping_edges(base_start + 1 ... base_end)
177
177
  expect(jobs.count).to eql 1
178
178
  expect(jobs.first.start_time).to eql base_time
179
-
179
+
180
180
  #Overlapping end
181
181
  jobs = Job.overlapping_edges(base_start ... base_end - 1)
182
182
  expect(jobs.count).to eql 1
@@ -188,9 +188,9 @@ describe Bookie::Database::Job do
188
188
  expect(jobs.first.start_time).to eql base_time
189
189
 
190
190
  #Two jobs overlapping the endpoints
191
- jobs = Job.overlapping_edges(base_end - 1 ... base_end + 1)
191
+ jobs = Job.overlapping_edges(base_end - 1 ... base_end + 1)
192
192
  expect(jobs.count).to eql 2
193
-
193
+
194
194
  #Not overlapping any endpoints
195
195
  jobs = Job.overlapping_edges(base_start ... base_end)
196
196
  expect(jobs.count).to eql 0
@@ -218,7 +218,7 @@ describe Bookie::Database::Job do
218
218
  end
219
219
  end
220
220
  end
221
-
221
+
222
222
  describe "#summary" do
223
223
  let(:count) { Job.count }
224
224
  let(:summary) { create_summaries(Job, base_time) }
@@ -251,29 +251,29 @@ describe Bookie::Database::Job do
251
251
  :successful => num_clipped_jobs / 2 + 1,
252
252
  })
253
253
  end
254
-
254
+
255
255
  it "correctly handles summaries of empty sets" do
256
- summary[:empty].should eql({
256
+ expect(summary[:empty]).to eql({
257
257
  :num_jobs => 0,
258
258
  :cpu_time => 0,
259
259
  :memory_time => 0,
260
260
  :successful => 0,
261
261
  })
262
262
  end
263
-
263
+
264
264
  it "correctly handles inverted ranges" do
265
- Job.summary(Time.now() ... Time.now() - 1).should eql summary[:empty]
266
- Job.summary(Time.now() .. Time.now() - 1).should eql summary[:empty]
265
+ expect(Job.summary(Time.now() ... Time.now() - 1)).to eql summary[:empty]
266
+ expect(Job.summary(Time.now() .. Time.now() - 1)).to eql summary[:empty]
267
267
  end
268
268
 
269
269
  it "distinguishes between inclusive and exclusive ranges" do
270
270
  sum = Job.summary(base_time ... base_time + 3600)
271
- sum[:num_jobs].should eql 1
271
+ expect(sum[:num_jobs]).to eql 1
272
272
  sum = Job.summary(base_time .. base_time + 3600)
273
- sum[:num_jobs].should eql 2
273
+ expect(sum[:num_jobs]).to eql 2
274
274
  end
275
275
  end
276
-
276
+
277
277
  it "validates fields" do
278
278
  fields = {
279
279
  :user => User.first,
@@ -285,24 +285,23 @@ describe Bookie::Database::Job do
285
285
  :memory => 10000,
286
286
  :exit_code => 0
287
287
  }
288
-
288
+
289
289
  job = Job.new(fields)
290
- job.valid?.should eql true
291
-
290
+ expect(job.valid?).to eql true
291
+
292
292
  fields.each_key do |field|
293
293
  job = Job.new(fields)
294
294
  job.method("#{field}=".intern).call(nil)
295
- job.valid?.should eql false
295
+ expect(job.valid?).to eql false
296
296
  end
297
-
297
+
298
298
  [:cpu_time, :wall_time, :memory].each do |field|
299
299
  job = Job.new(fields)
300
300
  m = job.method("#{field}=".intern)
301
301
  m.call(-1)
302
- job.valid?.should eql false
302
+ expect(job.valid?).to eql false
303
303
  m.call(0)
304
- job.valid?.should eql true
304
+ expect(job.valid?).to eql true
305
305
  end
306
306
  end
307
307
  end
308
-