diff --git a/app/services/automate_import_json_serializer_service.rb b/app/services/automate_import_json_serializer_service.rb index 250fe929616..eed51ee4d67 100644 --- a/app/services/automate_import_json_serializer_service.rb +++ b/app/services/automate_import_json_serializer_service.rb @@ -1,11 +1,12 @@ class AutomateImportJsonSerializerService def serialize(import_file_upload) - temp_file = Tempfile.new(['automate_temporary_zip', '.zip']) - temp_file.binmode - temp_file.write(import_file_upload.binary_blob.binary) - ae_import = MiqAeImport.new("*", "zip_file" => temp_file.path) - - temp_file.unlink + ae_import = nil + Tempfile.open(['automate_temporary_zip', '.zip']) do |temp_file| + temp_file.binmode + temp_file.write(import_file_upload.binary_blob.binary) + temp_file.close + ae_import = MiqAeImport.new("*", "zip_file" => temp_file.path) + end domains = ae_import.domain_entries("*") diff --git a/spec/services/automate_import_json_serializer_service_spec.rb b/spec/services/automate_import_json_serializer_service_spec.rb index 5e998202f58..2ee437d974f 100644 --- a/spec/services/automate_import_json_serializer_service_spec.rb +++ b/spec/services/automate_import_json_serializer_service_spec.rb @@ -59,7 +59,8 @@ allow(import_file_upload).to receive(:binary_blob).and_return(binary_blob) allow(binary_blob).to receive(:binary).and_return('a bunch of junk') allow(tempfile).to receive(:binmode) - allow(Tempfile).to receive(:new).with(['automate_temporary_zip', '.zip']).and_return(tempfile) + allow(tempfile).to receive(:close) + allow(Tempfile).to receive(:open).with(['automate_temporary_zip', '.zip']).and_yield(tempfile) allow(MiqAeImport).to receive(:new).with('*', 'zip_file' => tempfile.path).and_return(miq_ae_yaml_import_zipfs) allow(miq_ae_yaml_import_zipfs).to receive(:domain_entries).with('*').and_return( ['Customer/test1.yml', 'ManageIQ/test2.yml'] @@ -95,8 +96,11 @@ tempfile.unlink end - it 'sets the tempfile to binmode' do - expect(tempfile).to receive(:binmode) + it "sets the tempfile to binmode, writes to it, and then closes it" do + expect(tempfile).to receive(:binmode).ordered + expect(tempfile).to receive(:write).with("a bunch of junk").ordered + expect(tempfile).to receive(:close).ordered + automate_import_json_serializer_service.serialize(import_file_upload) end