|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const chaiAsPromised = require('chai-as-promised'); |
| 5 | + |
| 6 | +require('chai').use(chaiAsPromised); |
| 7 | + |
| 8 | +const sinon = require('sinon'); |
| 9 | +const Ruby = require('../ruby'); |
| 10 | +const JSZip = require('jszip'); |
| 11 | +const fs = require('fs-extra'); |
| 12 | + |
| 13 | +describe('Ruby', () => { |
| 14 | + let serverless; |
| 15 | + let ruby; |
| 16 | + let sandbox; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + sandbox = sinon.sandbox.create(); |
| 20 | + serverless = {classes: {Error}, service: {}, getProvider: sandbox.spy()}; |
| 21 | + serverless.service.provider = { name: 'openwhisk' }; |
| 22 | + ruby = new Ruby(serverless); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + sandbox.restore(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('#match()', () => { |
| 30 | + it('should match with explicit runtime', () => { |
| 31 | + serverless.service.provider.runtime = 'python'; |
| 32 | + expect(ruby.match({runtime: 'ruby', handler: 'file.func'})).to.equal(true) |
| 33 | + }); |
| 34 | + |
| 35 | + it('should match with provider runtime', () => { |
| 36 | + serverless.service.provider.runtime = 'ruby'; |
| 37 | + expect(ruby.match({handler: 'file.func'})).to.equal(true) |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not match when wrong explicit runtime', () => { |
| 41 | + expect(ruby.match({runtime: 'python', handler: 'file.func'})).to.equal(false) |
| 42 | + }); |
| 43 | + |
| 44 | + it('should not match when wrong provider runtime', () => { |
| 45 | + serverless.service.provider.runtime = 'python'; |
| 46 | + expect(ruby.match({handler: 'file.func'})).to.equal(false) |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not match when missing handler', () => { |
| 50 | + expect(ruby.match({})).to.equal(false) |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('#exec()', () => { |
| 55 | + it('should return ruby exec definition', () => { |
| 56 | + const fileContents = 'some file contents'; |
| 57 | + const handler = 'handler.some_func'; |
| 58 | + |
| 59 | + const exec = { main: 'some_func', kind: 'ruby:default', code: new Buffer(fileContents) }; |
| 60 | + sandbox.stub(ruby, 'generateActionPackage', (functionObj) => { |
| 61 | + expect(functionObj.handler).to.equal(handler); |
| 62 | + return Promise.resolve(new Buffer(fileContents)); |
| 63 | + }); |
| 64 | + return expect(ruby.exec({ handler, runtime: 'ruby'})) |
| 65 | + .to.eventually.deep.equal(exec); |
| 66 | + }) |
| 67 | + |
| 68 | + it('should support using custom image', () => { |
| 69 | + const fileContents = 'some file contents'; |
| 70 | + const handler = 'handler.some_func'; |
| 71 | + |
| 72 | + const exec = { main: 'some_func', image: 'blah', kind: 'blackbox', code: new Buffer(fileContents) }; |
| 73 | + sandbox.stub(ruby, 'generateActionPackage', (functionObj) => { |
| 74 | + expect(functionObj.handler).to.equal(handler); |
| 75 | + return Promise.resolve(new Buffer(fileContents)); |
| 76 | + }); |
| 77 | + return expect(ruby.exec({ handler, image: 'blah', runtime: 'ruby:7.1' })) |
| 78 | + .to.eventually.deep.equal(exec); |
| 79 | + }) |
| 80 | + }); |
| 81 | + |
| 82 | + describe('#generateActionPackage()', () => { |
| 83 | + it('should throw error for missing handler file', () => { |
| 84 | + expect(() => ruby.generateActionPackage({handler: 'does_not_exist.main'})) |
| 85 | + .to.throw(Error, 'Function handler (does_not_exist.rb) does not exist.'); |
| 86 | + }) |
| 87 | + |
| 88 | + it('should read service artifact and add main.rb for handler', () => { |
| 89 | + ruby.serverless.service.package = {artifact: '/path/to/zip_file.zip'}; |
| 90 | + ruby.isValidFile = () => true |
| 91 | + const zip = new JSZip(); |
| 92 | + const source = 'def main(args)\nname = args["name"] || "stranger"\ngreeting = "Hello #{name}!"\n{ "greeting" => greeting }\nend' |
| 93 | + zip.file("handler.rb", source); |
| 94 | + |
| 95 | + return zip.generateAsync({type:"nodebuffer"}).then(zipped => { |
| 96 | + sandbox.stub(fs, 'readFile', (path, cb) => { |
| 97 | + expect(path).to.equal('/path/to/zip_file.zip'); |
| 98 | + cb(null, zipped); |
| 99 | + }); |
| 100 | + return ruby.generateActionPackage({handler: 'handler.main'}).then(data => { |
| 101 | + return JSZip.loadAsync(new Buffer(data, 'base64')).then(zip => { |
| 102 | + expect(zip.file("handler.rb")).to.be.equal(null) |
| 103 | + return zip.file("main.rb").async("string").then(main => { |
| 104 | + expect(main).to.be.equal(source) |
| 105 | + }) |
| 106 | + }) |
| 107 | + }) |
| 108 | + }); |
| 109 | + }) |
| 110 | + |
| 111 | + it('should handle service artifact for individual function handler', () => { |
| 112 | + const functionObj = {handler: 'handler.main', package: { artifact: '/path/to/zip_file.zip'}} |
| 113 | + ruby.serverless.service.package = {individually: true}; |
| 114 | + ruby.isValidFile = () => true |
| 115 | + |
| 116 | + const zip = new JSZip(); |
| 117 | + const source = 'def main(args)\nname = args["name"] || "stranger"\ngreeting = "Hello #{name}!"\n{ "greeting" => greeting }\nend' |
| 118 | + zip.file("handler.rb", source); |
| 119 | + |
| 120 | + return zip.generateAsync({type:"nodebuffer"}).then(zipped => { |
| 121 | + sandbox.stub(fs, 'readFile', (path, cb) => { |
| 122 | + expect(path).to.equal('/path/to/zip_file.zip'); |
| 123 | + cb(null, zipped); |
| 124 | + }); |
| 125 | + return ruby.generateActionPackage(functionObj).then(data => { |
| 126 | + return JSZip.loadAsync(new Buffer(data, 'base64')).then(zip => { |
| 127 | + expect(zip.file("handler.rb")).to.be.equal(null) |
| 128 | + return zip.file("main.rb").async("string").then(main => { |
| 129 | + expect(main).to.be.equal(source) |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | + }); |
| 134 | + }); |
| 135 | + }) |
| 136 | +}); |
0 commit comments