Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #468 from vrtdev/feature/mongodb_database_provider
add gsub to replace invalid json values with a 1
- Loading branch information
Showing
5 changed files
with
87 additions
and
12 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
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
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,16 @@ | ||
module Puppet | ||
module Util | ||
module MongodbOutput | ||
def self.sanitize(data) | ||
# Dirty hack to remove JavaScript objects | ||
data.gsub!(%r{\w+\((\d+).+?\)}, '\1') # Remove extra parameters from 'Timestamp(1462971623, 1)' Objects | ||
data.gsub!(%r{\w+\((.+?)\)}, '\1') | ||
|
||
data.gsub!(%r{^Error\:.+}, '') | ||
data.gsub!(%r{^.*warning\:.+}, '') # remove warnings if sslAllowInvalidHostnames is true | ||
data.gsub!(%r{^.*The server certificate does not match the host name.+}, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x | ||
data | ||
end | ||
end | ||
end | ||
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
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,66 @@ | ||
require 'spec_helper' | ||
require 'puppet/util/mongodb_output' | ||
require 'json' | ||
|
||
describe Puppet::Util::MongodbOutput do | ||
let(:bson_data) do | ||
<<-EOT | ||
{ | ||
"setName": "rs_test", | ||
"ismaster": true, | ||
"secondary": false, | ||
"hosts": [ | ||
"mongo1:27017" | ||
], | ||
"primary": "mongo1:27017", | ||
"me": "mongo1:27017", | ||
"maxBsonObjectSize": 16777216, | ||
"maxMessageSizeBytes": 48000000, | ||
"hash": BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), | ||
"keyId": NumberLong(0), | ||
"clusterTime": Timestamp(1538381287, 1), | ||
"replicaSetId": ObjectId("5bb1d270137a581ebd3d61f2"), | ||
"slaveDelay": NumberLong(-1), | ||
"majorityWriteDate": ISODate("2018-10-01T08:08:01Z"), | ||
"lastHeartbeat": ISODate("2018-10-01T08:08:05.859Z"), | ||
"ok": 1 | ||
} | ||
EOT | ||
end | ||
|
||
let(:json_data) do | ||
<<-EOT | ||
{ | ||
"setName": "rs_test", | ||
"ismaster": true, | ||
"secondary": false, | ||
"hosts": [ | ||
"mongo1:27017" | ||
], | ||
"primary": "mongo1:27017", | ||
"me": "mongo1:27017", | ||
"maxBsonObjectSize": 16777216, | ||
"maxMessageSizeBytes": 48000000, | ||
"hash": 0, | ||
"keyId": 0, | ||
"clusterTime": 1538381287, | ||
"replicaSetId": "5bb1d270137a581ebd3d61f2", | ||
"slaveDelay": -1, | ||
"majorityWriteDate": "2018-10-01T08:08:01Z", | ||
"lastHeartbeat": "2018-10-01T08:08:05.859Z", | ||
"ok": 1 | ||
} | ||
EOT | ||
end | ||
|
||
describe '.sanitize' do | ||
it 'returns a valid json' do | ||
sanitized_json = described_class.sanitize(bson_data) | ||
expect { JSON.parse(sanitized_json) }.not_to raise_error | ||
end | ||
it 'replaces data types' do | ||
sanitized_json = described_class.sanitize(bson_data) | ||
expect(JSON.parse(sanitized_json)).to include(JSON.parse(json_data)) | ||
end | ||
end | ||
end |