Permalink
Browse files

CLC-823 Separate recording of vcr data from prettification; introduce…

… timeshifter to rails startup
  • Loading branch information...
1 parent 2f30b04 commit 9ae2f3a21c6e4d46320cf7e881d076fd262e440f Chris Tweney committed Dec 18, 2012
View
@@ -33,3 +33,6 @@ app/assets/stylesheets/application.css
# Ignore the coverage directory (for checking in, otherwise useful)
/coverage
+
+# Ignore processed VCR files
+/fixtures/vcr_cassettes/*.json
@@ -0,0 +1,45 @@
+Rails.application.config.after_initialize do
+
+ # Go through fixtures/pretty_vcr_recordings/*.json and substitute current datetime values where we find tokens. Save
+ # the processed files in fixtures/vcr_cassettes for use by FakeableProxy.
+
+ # midnight on the current day
+ today = Date.today.to_time_in_current_zone.to_datetime
+
+ substitutions = {
+ ":::TWO_DAYS_AGO_PST:::" => today.advance(:days => -1, :minutes => -1).rfc3339,
+ ":::TODAY_AT_TEA_TIME:::" => today.advance(:hours => 15, :minutes => 47, :seconds => 13).rfc3339,
+ ":::SIX_DAYS_HENCE:::" => today.advance(:days => 6).rfc3339,
+ ":::EIGHT_DAYS_HENCE:::" => today.advance(:days => 8).rfc3339,
+ }
+
+ Rails.logger.info "Timeshifter: Today = #{today}; epoch = #{today.to_i}"
+
+ processed_dir = Rails.root.join("fixtures", "vcr_cassettes")
+
+ Dir.glob("#{Rails.root}/fixtures/pretty_vcr_recordings/*.json").each do |filename|
+ Rails.logger.info "Timeshifter: Processing #{filename}"
+ begin
+ input_file = File.open filename
+ content = input_file.read
+ output_file = File.open(processed_dir.join(File.basename(input_file)), "w")
+
+ # substitute tokens with formatted date values
+ substitutions.each { |k, v| content.gsub!(k, v) }
+
+ # convert debug_json back to string representation
+ json = JSON.parse(content)
+ json["http_interactions"].each do |interaction|
+ interaction["response"]["body"]["string"] = MultiJson.dump(interaction["response"]["body"]["debug_json"])
+ end
+
+ Rails.logger.info "Timeshifter: Output file = #{output_file.path}"
+ output_file.write(JSON.pretty_generate(json))
+ rescue JSON::ParserError
+ Rails.logger.info "No valid JSON to parse in #{filename}"
+ ensure
+ output_file.close
+ end
+ end
+
+end

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,243 @@
+{
+ "http_interactions": [
+ {
+ "request": {
+ "method": "get",
+ "uri": "https://sakai-dev.berkeley.edu/sakai-hybrid/sites?categorized=true",
+ "body": {
+ "encoding": "US-ASCII",
+ "string": ""
+ },
+ "headers": {
+ "x-sakai-token": [
+
+ ],
+ "Authorization": [
+ ""
+ ]
+ }
+ },
+ "response": {
+ "status": {
+ "code": 200,
+ "message": null
+ },
+ "headers": {
+ "date": [
+ "Mon, 26 Nov 2012 22:15:14 GMT"
+ ],
+ "set-cookie": [
+ "JSESSIONID=5c6145e4-935c-4238-af10-d4369bf80d7d.sakai-dev-01; Path=/; Secure; HttpOnly"
+ ],
+ "x-ua-compatible": [
+ "IE=EmulateIE8"
+ ],
+ "content-type": [
+ "application/json;charset=UTF-8"
+ ],
+ "content-length": [
+ "2597"
+ ],
+ "vary": [
+ "User-Agent"
+ ]
+ },
+ "body": {
+ "encoding": "US-ASCII",
+ "string": "{\"principal\":\"300939\",\"display\":4,\"categories\":[{\"category\":\"All sites\",\"sites\":[{\"title\":\"My Workspace\",\"id\":\"~11586ee9-00cd-485a-84e8-f99d252a7843\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/~11586ee9-00cd-485a-84e8-f99d252a7843\",\"description\":\"<p>MyWorkspace Site<\\/p>\"},{\"title\":\"AFRICAM R1A LEC 001 Fa09\",\"id\":\"58875fd1-6bf3-4658-8aee-a8330e01421e\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/58875fd1-6bf3-4658-8aee-a8330e01421e\"},{\"title\":\"Digital Library Project\",\"id\":\"29d475ae-a1c1-493f-b721-fcfeebdb038d\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/29d475ae-a1c1-493f-b721-fcfeebdb038d\"},{\"title\":\"HISTORY 7A LEC 001 Fa09\",\"id\":\"163664d0-d0dd-40ba-80fb-ba95a3e10555\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/163664d0-d0dd-40ba-80fb-ba95a3e10555\",\"description\":\"<br/>\"},{\"title\":\"MATH 53\",\"id\":\"697e4e1c-da87-4524-a299-ec37c970f8fb\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/697e4e1c-da87-4524-a299-ec37c970f8fb\",\"shortDescription\":\"Multivariable Calculus\"},{\"title\":\"PHYSICS 7B\",\"id\":\"cddb1acd-3e57-4f6a-9e66-a27a9a59b55f\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/cddb1acd-3e57-4f6a-9e66-a27a9a59b55f\",\"description\":\"<p>\\r\\n\\t<br/>\\r\\n\\tgood grief!<\\/p>\",\"shortDescription\":\"Physics for Scientists and Engineers\"}]},{\"category\":\"Fall 2012\",\"sites\":[{\"title\":\"MATH 53\",\"id\":\"697e4e1c-da87-4524-a299-ec37c970f8fb\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/697e4e1c-da87-4524-a299-ec37c970f8fb\",\"shortDescription\":\"Multivariable Calculus\"},{\"title\":\"PHYSICS 7B\",\"id\":\"cddb1acd-3e57-4f6a-9e66-a27a9a59b55f\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/cddb1acd-3e57-4f6a-9e66-a27a9a59b55f\",\"description\":\"<p>\\r\\n\\t<br/>\\r\\n\\tgood grief!<\\/p>\",\"shortDescription\":\"Physics for Scientists and Engineers\"}]},{\"category\":\"Fall 2009\",\"sites\":[{\"title\":\"AFRICAM R1A LEC 001 Fa09\",\"id\":\"58875fd1-6bf3-4658-8aee-a8330e01421e\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/58875fd1-6bf3-4658-8aee-a8330e01421e\"},{\"title\":\"HISTORY 7A LEC 001 Fa09\",\"id\":\"163664d0-d0dd-40ba-80fb-ba95a3e10555\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/163664d0-d0dd-40ba-80fb-ba95a3e10555\",\"description\":\"<br/>\"}]},{\"category\":\"Projects\",\"sites\":[{\"title\":\"Digital Library Project\",\"id\":\"29d475ae-a1c1-493f-b721-fcfeebdb038d\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/29d475ae-a1c1-493f-b721-fcfeebdb038d\"}]},{\"category\":\"Other\",\"sites\":[{\"title\":\"My Workspace\",\"id\":\"~11586ee9-00cd-485a-84e8-f99d252a7843\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/~11586ee9-00cd-485a-84e8-f99d252a7843\",\"description\":\"<p>MyWorkspace Site<\\/p>\"}]}]}",
+ "debug_json": {
+ "principal": "300939",
+ "display": 4,
+ "categories": [
+ {
+ "category": "All sites",
+ "sites": [
+ {
+ "title": "My Workspace",
+ "id": "~11586ee9-00cd-485a-84e8-f99d252a7843",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/~11586ee9-00cd-485a-84e8-f99d252a7843",
+ "description": "<p>MyWorkspace Site</p>"
+ },
+ {
+ "title": "AFRICAM R1A LEC 001 Fa09",
+ "id": "58875fd1-6bf3-4658-8aee-a8330e01421e",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/58875fd1-6bf3-4658-8aee-a8330e01421e"
+ },
+ {
+ "title": "Digital Library Project",
+ "id": "29d475ae-a1c1-493f-b721-fcfeebdb038d",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/29d475ae-a1c1-493f-b721-fcfeebdb038d"
+ },
+ {
+ "title": "HISTORY 7A LEC 001 Fa09",
+ "id": "163664d0-d0dd-40ba-80fb-ba95a3e10555",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/163664d0-d0dd-40ba-80fb-ba95a3e10555",
+ "description": "<br/>"
+ },
+ {
+ "title": "MATH 53",
+ "id": "697e4e1c-da87-4524-a299-ec37c970f8fb",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/697e4e1c-da87-4524-a299-ec37c970f8fb",
+ "shortDescription": "Multivariable Calculus"
+ },
+ {
+ "title": "PHYSICS 7B",
+ "id": "cddb1acd-3e57-4f6a-9e66-a27a9a59b55f",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/cddb1acd-3e57-4f6a-9e66-a27a9a59b55f",
+ "description": "<p>\r\n\t<br/>\r\n\tgood grief!</p>",
+ "shortDescription": "Physics for Scientists and Engineers"
+ }
+ ]
+ },
+ {
+ "category": "Fall 2012",
+ "sites": [
+ {
+ "title": "MATH 53",
+ "id": "697e4e1c-da87-4524-a299-ec37c970f8fb",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/697e4e1c-da87-4524-a299-ec37c970f8fb",
+ "shortDescription": "Multivariable Calculus"
+ },
+ {
+ "title": "PHYSICS 7B",
+ "id": "cddb1acd-3e57-4f6a-9e66-a27a9a59b55f",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/cddb1acd-3e57-4f6a-9e66-a27a9a59b55f",
+ "description": "<p>\r\n\t<br/>\r\n\tgood grief!</p>",
+ "shortDescription": "Physics for Scientists and Engineers"
+ }
+ ]
+ },
+ {
+ "category": "Fall 2009",
+ "sites": [
+ {
+ "title": "AFRICAM R1A LEC 001 Fa09",
+ "id": "58875fd1-6bf3-4658-8aee-a8330e01421e",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/58875fd1-6bf3-4658-8aee-a8330e01421e"
+ },
+ {
+ "title": "HISTORY 7A LEC 001 Fa09",
+ "id": "163664d0-d0dd-40ba-80fb-ba95a3e10555",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/163664d0-d0dd-40ba-80fb-ba95a3e10555",
+ "description": "<br/>"
+ }
+ ]
+ },
+ {
+ "category": "Projects",
+ "sites": [
+ {
+ "title": "Digital Library Project",
+ "id": "29d475ae-a1c1-493f-b721-fcfeebdb038d",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/29d475ae-a1c1-493f-b721-fcfeebdb038d"
+ }
+ ]
+ },
+ {
+ "category": "Other",
+ "sites": [
+ {
+ "title": "My Workspace",
+ "id": "~11586ee9-00cd-485a-84e8-f99d252a7843",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/~11586ee9-00cd-485a-84e8-f99d252a7843",
+ "description": "<p>MyWorkspace Site</p>"
+ }
+ ]
+ }
+ ]
+ }
+ },
+ "http_version": null
+ },
+ "recorded_at": "Mon, 26 Nov 2012 22:15:14 GMT"
+ },
+ {
+ "request": {
+ "method": "get",
+ "uri": "https://sakai-dev.berkeley.edu/sakai-hybrid/sites?unread=true",
+ "body": {
+ "encoding": "US-ASCII",
+ "string": ""
+ },
+ "headers": {
+ "x-sakai-token": null,
+ "Authorization": ""
+ }
+ },
+ "response": {
+ "status": {
+ "code": 200,
+ "message": null
+ },
+ "headers": {
+ "date": [
+ "Mon, 26 Nov 2012 22:15:14 GMT"
+ ],
+ "set-cookie": [
+ "JSESSIONID=e4c9306a-bbb7-4fc0-a7a6-de234ab33d56.sakai-dev-01; Path=/; Secure; HttpOnly"
+ ],
+ "x-ua-compatible": [
+ "IE=EmulateIE8"
+ ],
+ "content-type": [
+ "application/json;charset=UTF-8"
+ ],
+ "content-length": [
+ "1233"
+ ],
+ "vary": [
+ "User-Agent"
+ ]
+ },
+ "body": {
+ "encoding": "US-ASCII",
+ "string": "{\"principal\":\"300939\",\"display\":4,\"sites\":[{\"title\":\"My Workspace\",\"id\":\"~11586ee9-00cd-485a-84e8-f99d252a7843\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/~11586ee9-00cd-485a-84e8-f99d252a7843\",\"description\":\"<p>MyWorkspace Site<\\/p>\"},{\"title\":\"AFRICAM R1A LEC 001 Fa09\",\"id\":\"58875fd1-6bf3-4658-8aee-a8330e01421e\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/58875fd1-6bf3-4658-8aee-a8330e01421e\"},{\"title\":\"Digital Library Project\",\"id\":\"29d475ae-a1c1-493f-b721-fcfeebdb038d\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/29d475ae-a1c1-493f-b721-fcfeebdb038d\"},{\"title\":\"HISTORY 7A LEC 001 Fa09\",\"id\":\"163664d0-d0dd-40ba-80fb-ba95a3e10555\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/163664d0-d0dd-40ba-80fb-ba95a3e10555\",\"description\":\"<br/>\"},{\"title\":\"MATH 53\",\"id\":\"697e4e1c-da87-4524-a299-ec37c970f8fb\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/697e4e1c-da87-4524-a299-ec37c970f8fb\",\"shortDescription\":\"Multivariable Calculus\"},{\"title\":\"PHYSICS 7B\",\"id\":\"cddb1acd-3e57-4f6a-9e66-a27a9a59b55f\",\"url\":\"https://sakai-dev.berkeley.edu/portal/site/cddb1acd-3e57-4f6a-9e66-a27a9a59b55f\",\"description\":\"<p>\\r\\n\\t<br/>\\r\\n\\tgood grief!<\\/p>\",\"shortDescription\":\"Physics for Scientists and Engineers\"}]}",
+ "debug_json": {
+ "principal": "300939",
+ "display": 4,
+ "sites": [
+ {
+ "title": "My Workspace",
+ "id": "~11586ee9-00cd-485a-84e8-f99d252a7843",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/~11586ee9-00cd-485a-84e8-f99d252a7843",
+ "description": "<p>MyWorkspace Site</p>"
+ },
+ {
+ "title": "AFRICAM R1A LEC 001 Fa09",
+ "id": "58875fd1-6bf3-4658-8aee-a8330e01421e",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/58875fd1-6bf3-4658-8aee-a8330e01421e"
+ },
+ {
+ "title": "Digital Library Project",
+ "id": "29d475ae-a1c1-493f-b721-fcfeebdb038d",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/29d475ae-a1c1-493f-b721-fcfeebdb038d"
+ },
+ {
+ "title": "HISTORY 7A LEC 001 Fa09",
+ "id": "163664d0-d0dd-40ba-80fb-ba95a3e10555",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/163664d0-d0dd-40ba-80fb-ba95a3e10555",
+ "description": "<br/>"
+ },
+ {
+ "title": "MATH 53",
+ "id": "697e4e1c-da87-4524-a299-ec37c970f8fb",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/697e4e1c-da87-4524-a299-ec37c970f8fb",
+ "shortDescription": "Multivariable Calculus"
+ },
+ {
+ "title": "PHYSICS 7B",
+ "id": "cddb1acd-3e57-4f6a-9e66-a27a9a59b55f",
+ "url": "https://sakai-dev.berkeley.edu/portal/site/cddb1acd-3e57-4f6a-9e66-a27a9a59b55f",
+ "description": "<p>\r\n\t<br/>\r\n\tgood grief!</p>",
+ "shortDescription": "Physics for Scientists and Engineers"
+ }
+ ]
+ }
+ },
+ "http_version": null
+ },
+ "recorded_at": "Mon, 26 Nov 2012 22:15:14 GMT"
+ }
+ ],
+ "recorded_with": "VCR 2.3.0"
+}
No changes.
@@ -1,6 +1,6 @@
module FakeableProxy
VCR.configure do |c|
- c.cassette_library_dir = 'fixtures/fakeable_proxy_data'
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.hook_into :webmock, :faraday
c.allow_http_connections_when_no_cassette = true
c.debug_logger = File.open(Rails.root.join("log", "vcr-debug.log"), 'w')
@@ -33,6 +33,10 @@ def self.wrap_request(proxy_id, force_fake = nil, &proc_block)
private
def self.record_new_responses(proxy_id, proc_block)
+ Rails.logger.warn "FakeableProxy Recording new response for #{proxy_id}"
+ VCR.configure do |c|
+ c.cassette_library_dir = 'fixtures/raw_vcr_recordings'
+ end
VCR.use_cassette(proxy_id, options=default_cassette_options({:record => :new_episodes}), &block=proc_block)
end
View
@@ -2,7 +2,7 @@ require 'json'
namespace :vcr do
- desc "Records new fixtures with ENV=testext & pretty-print json result files"
+ desc "Records new fixtures with ENV=testext"
task :record do
ENV["RAILS_ENV"] = "testext"
ENV["freshen_vcr"] = "true"
@@ -12,25 +12,41 @@ namespace :vcr do
rescue RuntimeError => e
#Don't let spec failures stop json prettifying.
end
+ end
- Dir.glob("#{Rails.root}/fixtures/fakeable_proxy_data/*.json").each do |file|
- json_string = File.read(file)
- json = JSON.parse(json_string)
- json["http_interactions"].each do |interaction|
- body_string = interaction["response"]["body"]["string"]
- if body_string.length >= 2
- interaction["response"]["body"]["debug_json"] = JSON.parse(body_string)
+ desc "Pretties up the json result files"
+ task :prettify do
+ processed_dir = Rails.root.join("fixtures", "pretty_vcr_recordings")
+ Dir.glob("#{Rails.root}/fixtures/raw_vcr_recordings/*.json").each do |filename|
+ Rails.logger.info "Prettifying #{filename}"
+ begin
+ input_file = File.open filename
+ content = input_file.read
+ output_file = File.open(processed_dir.join(File.basename(input_file)), "w")
+
+ # convert debug_json back to string representation
+ json = JSON.parse(content)
+ json["http_interactions"].each do |interaction|
+ original_string = interaction["response"]["body"]["string"]
+ if original_string.length >= 2
+ interaction["response"]["body"]["debug_json"] = JSON.parse(original_string)
+ end
end
+
+ Rails.logger.info "Pretty Output file = #{output_file.path}"
+ output_file.write(JSON.pretty_generate(json))
+ rescue JSON::ParserError
+ Rails.logger.info "Got a JSON parse error prettiyfing #{filename}"
+ ensure
+ output_file.close
end
- pretty_json = JSON.pretty_generate(json)
- File.open(file, 'w') { |f| f.write(pretty_json)}
end
end
- desc "Dumps out the requests that have been recorded in /fixtures/fakeable_proxy/"
+ desc "Dumps out the requests that have been recorded in /fixtures/raw_vcr_recordings/"
task :list do
recordings_hash = {}
- Dir.glob("#{Rails.root}/fixtures/fakeable_proxy_data/*.json").each do |file|
+ Dir.glob("#{Rails.root}/fixtures/raw_vcr_recordings/*.json").each do |file|
json_string = File.read(file)
json = JSON.parse(json_string)
json["http_interactions"].each do |interaction|

0 comments on commit 9ae2f3a

Please sign in to comment.