-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require "cluster" | ||
require "services" | ||
Services.mongo! | ||
|
||
# Goes through all clusters, checks if they are valid, | ||
# and prints the first ocn of any invalid cluster to a file. | ||
|
||
class ClusterValidator | ||
attr_reader :output_path # file name | ||
def initialize(buffer_max: 500) | ||
@buffer = [] | ||
@buffer_max = buffer_max | ||
# Make an output file in the right place | ||
ymd = Time.now.strftime("%Y-%m-%d") | ||
dir = Settings.local_report_path | ||
FileUtils.mkdir_p(dir) | ||
@output_path = "#{dir}/cluster_validator_#{ymd}.txt" | ||
end | ||
|
||
def run | ||
puts "Logging to #{output_path}" | ||
@outf = File.open(output_path, "w") | ||
# Go through each cluster and check if valid. | ||
comment "These are ocns of invalid clusters:" | ||
Cluster.each do |c| | ||
unless c.valid? | ||
@buffer << c.ocns.first | ||
end | ||
empty_buffer? | ||
end | ||
ensure | ||
# Empty bufferfer one last time and be done. | ||
empty_buffer? | ||
comment "Done" | ||
@outf.close | ||
end | ||
|
||
private | ||
|
||
# Make a comment in the outfile | ||
def comment(msg) | ||
@outf.puts "# " + msg | ||
end | ||
|
||
# So we don't write to file every iteration. | ||
def empty_buffer? | ||
if @buffer.size > @buffer_max | ||
puts "empty buffer..." | ||
@outf.puts(@buffer.join("\n")) | ||
@buffer = [] | ||
end | ||
end | ||
end | ||
|
||
if __FILE__ == $0 then | ||
ClusterValidator.new.run | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require_relative "../bin/cluster_validator" | ||
require "mongo_updater" | ||
|
||
RSpec.describe ClusterValidator do | ||
let(:cluster_validator) { described_class.new } | ||
# The output file will have 2 lines, header and footer, even if no body. | ||
# So if the output file has 2 lines it is "empty" for the purposes of these tests. | ||
let(:empty_file_line_count) { 2 } | ||
let(:one_invalid_cluster_line_count) { 3 } | ||
|
||
before(:each) do | ||
Cluster.collection.find.delete_many | ||
end | ||
|
||
def get_output_lines | ||
described_class.new.run | ||
File.read(cluster_validator.output_path).split("\n") | ||
end | ||
|
||
it "makes an outfile when it runs" do | ||
expect(File.exist?(cluster_validator.output_path)).to be false | ||
cluster_validator.run | ||
expect(File.exist?(cluster_validator.output_path)).to be true | ||
end | ||
|
||
it "makes an empty-ish outfile if there are no clusters" do | ||
# empty-ish meaning it'll only have the header and footer, which begin with "#". | ||
lines = get_output_lines | ||
expect(lines.count).to eq empty_file_line_count | ||
expect(lines[0]).to start_with("#") | ||
expect(lines[1]).to start_with("#") | ||
end | ||
|
||
it "does not count valid clusters" do | ||
# Start with building a valid cluster, and verify. | ||
ocn = 5 | ||
org = "umich" | ||
loc = "loc_1" | ||
cluster_tap_save( | ||
build(:holding, ocn: ocn, organization: org, local_id: loc), | ||
build(:ht_item, :spm, ocns: [ocn], rights: "pd", access: "allow"), | ||
build(:commitment, ocn: ocn, organization: org, local_id: loc) | ||
) | ||
# Verify we have one valid cluster. | ||
# Verify it does not count towards the report. | ||
expect(Cluster.count).to eq 1 | ||
expect(Cluster.first.valid?).to be true | ||
expect(get_output_lines.count).to eq empty_file_line_count | ||
# OK, verified. | ||
# Now try to mess up the cluster so it's not valid... | ||
# This should bypass the mongoid schema and let us do something "illegal" | ||
# and thus create an invalid cluster. | ||
MongoUpdater.update_embedded( | ||
clusterable: "commitments", | ||
matcher: {organization: "umich"}, | ||
updater: {local_shelving_type: "VALIDATE ME"} | ||
) | ||
expect(Cluster.count).to eq 1 | ||
expect(Cluster.first.valid?).to be false | ||
expect(get_output_lines.count).to eq one_invalid_cluster_line_count | ||
end | ||
end |