Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
phuongdh committed Feb 8, 2019
1 parent 582d3e6 commit c42f4d5
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 28 deletions.
30 changes: 2 additions & 28 deletions lib/active_encode/engine_adapters/elastic_transcoder_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,6 @@ def cancel(id)
build_encode(get_job_details(id)) if response.successful?
end

# TODO: decide to keep here to move somewhere else
def remove_output!(id)
track = output.find { |o| o[:id] == id }
raise "Unknown track: `#{id}'" if track.nil?
s3_object = FileLocator::S3File.new(track[:url]).object
if s3_object.key =~ /\.m3u8$/
delete_segments(s3_object)
else
s3_object.delete
end
end

def delete_segments(obj)
raise "Invalid segmented video object" unless obj.key =~ %r(quality-.+/.+\.m3u8$)
bucket = obj.bucket
prefix = obj.key.sub(/\.m3u8$/,'')
next_token = nil
loop do
response = s3client.list_objects_v2(bucket: obj.bucket_name, prefix: prefix, continuation_token: next_token)
response.contents.collect(&:key).each { |key| bucket.object(key).delete }
next_token = response.continuation_token
break if next_token.nil?
end
end

private

# Needs region and credentials setup per http://docs.aws.amazon.com/sdkforruby/api/Aws/ElasticTranscoder/Client.html
Expand Down Expand Up @@ -129,8 +104,7 @@ def convert_state(job)
end

def convert_current_operations(_job)
current_ops = []
current_ops
[]
end

def convert_percent_complete(job)
Expand Down Expand Up @@ -177,7 +151,7 @@ def check_s3_bucket input_url, source_bucket
s3_key = File.join(SecureRandom.uuid,s3_object.key)
# logger.info("Copying to `#{source_bucket}/#{input_url}'")
target = Aws::S3::Object.new(bucket_name: source_bucket, key: input_url)
target.copy_from(s3_object, multipart_copy: s3_object.size > 15.megabytes)
target.copy_from(s3_object, multipart_copy: s3_object.size > 15728640) # 15.megabytes
s3_key
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/elastic_transcoder_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,27 @@
end
end
end

describe "#check_s3_bucket" do
context "when file exists in masterfile_bucket" do
let(:input_url) { "s3://bucket1/file.mp4" }
let(:source_bucket) { "bucket1" }

it "just returns the key" do
# TODO: move these bucket helpers out to a service class so we don't have to test private methods
expect(described_class.new.send(:check_s3_bucket, input_url, source_bucket)).to eq "file.mp4"
end
end

context "when file is in another bucket" do
let(:input_url) { "s3://bucket1/file.mp4" }
let(:source_bucket) { "bucket2" }

it "copies to masterfile_bucket" do
# TODO: move these bucket helpers out to a service class so we don't have to test private methods
allow(SecureRandom).to receive(:uuid).and_return("randomstring")
expect(described_class.new.send(:check_s3_bucket, input_url, source_bucket)).to eq "randomstring/file.mp4"
end
end
end
end
128 changes: 128 additions & 0 deletions spec/units/file_locator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright 2011-2018, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# --- END LICENSE_HEADER BLOCK ---
require 'aws-sdk'
require 'rails_helper'

describe FileLocator, type: :service do
before do
Aws.config[:stub_responses] = true
end

describe 'S3File Class' do
let(:bucket) { "mybucket" }
let(:key) { "mykey.mp4" }
let(:s3file) { FileLocator::S3File.new("s3://#{bucket}/#{key}") }

it "should be able to initialize from an S3 URI" do
expect(s3file.bucket).to eq bucket
expect(s3file.key).to eq key
end

it "should return an S3 Object" do
s3_object = s3file.object
expect(s3_object).to be_an Aws::S3::Object
expect(s3_object.bucket_name).to eq bucket
expect(s3_object.key).to eq key
end
end

describe "Local file" do
let(:path) { "/path/to/file.mp4" }
let(:source) { "file://#{path}" }
let(:locator) { FileLocator.new(source) }

it "should return the correct uri" do
expect(locator.uri).to eq Addressable::URI.parse(source)
end

it "should return the correct location" do
expect(locator.location).to eq path
end

it "should tell if file exists" do
allow(File).to receive(:exist?).with(path) { true }
expect(locator.exist?).to be_truthy
end

context "return file" do
let(:file) { double(File) }
before :each do
allow(File).to receive(:open).and_return file
end

it "should return reader" do
expect(locator.reader).to eq file
end

it "should return attachment" do
expect(locator.attachment).to eq file
end
end
end

describe "s3 file" do
let(:bucket) { "mybucket" }
let(:key) { "mykey.mp4" }
let(:source) { "s3://#{bucket}/#{key}" }
let(:locator) { FileLocator.new(source) }

it "should return the correct uri" do
expect(locator.uri).to eq Addressable::URI.parse(source)
end

it "should return the correct location" do
expect(locator.location).to start_with "https://#{bucket}.s3.us-stubbed-1.amazonaws.com/#{key}"
end

it "should tell if file exists" do
expect(locator.exist?).to be_truthy
end

it "should return reader" do
expect(locator.reader).to be_a StringIO
end

it "should return attachment" do
expect(locator.attachment).to eq Addressable::URI.parse(source)
end
end

describe "Other file" do
let(:path) { "/path/to/file.mp4" }
let(:source) { "bogus://#{path}" }
let(:locator) { FileLocator.new(source) }

it "should return the correct uri" do
expect(locator.uri).to eq Addressable::URI.parse(source)
end

it "should return the correct location" do
expect(locator.location).to eq source
end

it "should tell if file exists" do
expect(locator.exist?).to be_falsy
end

it "should return reader" do
io = double(IO)
allow(Kernel).to receive(:open).and_return io
expect(locator.reader).to eq io
end

it "should return attachment" do
expect(locator.attachment).to eq locator.location
end
end
end

0 comments on commit c42f4d5

Please sign in to comment.