|
| 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 OpenWhiskCompileServiceBindings = require('../index'); |
| 10 | + |
| 11 | +describe('OpenWhiskCompileServiceBindings', () => { |
| 12 | + let serverless; |
| 13 | + let sandbox; |
| 14 | + let openwhiskCompileServiceBindings; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + sandbox = sinon.sandbox.create(); |
| 18 | + serverless = {classes: {Error}, service: {provider: {}, resources: {}, getAllFunctions: () => []}, getProvider: sandbox.spy()}; |
| 19 | + const options = { |
| 20 | + stage: 'dev', |
| 21 | + region: 'us-east-1', |
| 22 | + }; |
| 23 | + openwhiskCompileServiceBindings = new OpenWhiskCompileServiceBindings(serverless, options); |
| 24 | + serverless.service.service = 'serviceName'; |
| 25 | + serverless.service.provider = { |
| 26 | + namespace: 'testing', |
| 27 | + apihost: '', |
| 28 | + auth: '', |
| 29 | + }; |
| 30 | + |
| 31 | + serverless.cli = { consoleLog: () => {}, log: () => {} }; |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + sandbox.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('#parseServiceBindings()', () => { |
| 39 | + it('should return empty array when missing service bindings', () => { |
| 40 | + const action = 'fnName' |
| 41 | + expect(openwhiskCompileServiceBindings.parseServiceBindings(action, {})).to.deep.equal([]) |
| 42 | + expect(openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: []})).to.deep.equal([]) |
| 43 | + expect(openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: [{}]})).to.deep.equal([]) |
| 44 | + expect(openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: [{blah: {}}]})).to.deep.equal([]) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return array with single service binding property', () => { |
| 48 | + const action = 'fnName' |
| 49 | + const service = { name: 'my-service', instance: 'my-instance', key: 'mykey' } |
| 50 | + const response = { action: `serviceName_fnName`, name: 'my-service', instance: 'my-instance', key: 'mykey' } |
| 51 | + const result = openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: [{ service }]}) |
| 52 | + expect(result).to.deep.equal([response]) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should return array with multiple service binding properties', () => { |
| 56 | + const action = 'fnName' |
| 57 | + const service_a = { action: `serviceName_fnName`, name: 'my-service-a', instance: 'my-instance-a', key: 'mykey' } |
| 58 | + const service_b = { action: `serviceName_fnName`, name: 'my-service-b', instance: 'my-instance-b', key: 'mykey' } |
| 59 | + const service_c = { action: `serviceName_fnName`, name: 'my-service-c', instance: 'my-instance-c', key: 'mykey' } |
| 60 | + const services = [{ service: service_a }, { service: service_b }, { service: service_c } ] |
| 61 | + const result = openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: services}) |
| 62 | + expect(result).to.deep.equal([service_a, service_b, service_c]) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should throw an error if service binding is missing name', () => { |
| 66 | + const service = { instance: 'my-instance-a', key: 'mykey' } |
| 67 | + const action = 'fnName' |
| 68 | + const services = [{ service }] |
| 69 | + expect(() => openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: services})) |
| 70 | + .to.throw(Error, /service binding missing name parameter/); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should throw an error if multiple bindings for same service name', () => { |
| 74 | + const action = 'fnName' |
| 75 | + const service = { name: 'my-service', instance: 'my-instance-a', key: 'mykey' } |
| 76 | + const services = [{ service }, { service }] |
| 77 | + expect(() => openwhiskCompileServiceBindings.parseServiceBindings(action, {bind: services})) |
| 78 | + .to.throw(Error, /multiple bindings for same service not supported/); |
| 79 | + }); |
| 80 | + }) |
| 81 | + |
| 82 | + describe('#compileServiceBindings()', () => { |
| 83 | + it('should return service bindings for simple functions', () => { |
| 84 | + const fns = { |
| 85 | + a: { bind: [{ service: { name: 'service-name-a' } }] }, |
| 86 | + b: { bind: [{ service: { name: 'service-name-b', instance: 'instance-name' } }] }, |
| 87 | + c: { bind: [{ service: { name: 'service-name-a' } }, { service: { name: 'service-name-b' } }] }, |
| 88 | + d: { }, |
| 89 | + } |
| 90 | + |
| 91 | + const service = openwhiskCompileServiceBindings.serverless.service |
| 92 | + service.getAllFunctions = () => Object.keys(fns) |
| 93 | + service.getFunction = name => fns[name] |
| 94 | + |
| 95 | + const services = [ |
| 96 | + { action: 'serviceName_a', name: 'service-name-a' }, |
| 97 | + { action: 'serviceName_b', name: 'service-name-b', instance: 'instance-name' }, |
| 98 | + { action: 'serviceName_c', name: 'service-name-a' }, |
| 99 | + { action: 'serviceName_c', name: 'service-name-b' } |
| 100 | + ] |
| 101 | + return openwhiskCompileServiceBindings.compileServiceBindings().then(result => { |
| 102 | + expect(service.bindings).to.deep.equal(services) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should return service bindings for functions with explicit name', () => { |
| 107 | + const fns = { |
| 108 | + a: { name: 'some_name', bind: [{ service: { name: 'service-name-a' } }] } |
| 109 | + } |
| 110 | + |
| 111 | + const service = openwhiskCompileServiceBindings.serverless.service |
| 112 | + service.getAllFunctions = () => Object.keys(fns) |
| 113 | + service.getFunction = name => fns[name] |
| 114 | + |
| 115 | + const services = [ { action: 'some_name', name: 'service-name-a' } ] |
| 116 | + return openwhiskCompileServiceBindings.compileServiceBindings().then(result => { |
| 117 | + expect(service.bindings).to.deep.equal(services) |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + |
| 122 | + it('should return service bindings for packages', () => { |
| 123 | + const service = openwhiskCompileServiceBindings.serverless.service |
| 124 | + service.resources.packages = { |
| 125 | + a: { bind: [{ service: { name: 'service-name-a' } }] }, |
| 126 | + b: { bind: [{ service: { name: 'service-name-b', instance: 'instance-name' } }] }, |
| 127 | + c: { bind: [{ service: { name: 'service-name-a' } }, { service: { name: 'service-name-b' } }] }, |
| 128 | + d: { }, |
| 129 | + } |
| 130 | + |
| 131 | + const services = [ |
| 132 | + { action: 'serviceName_a', name: 'service-name-a' }, |
| 133 | + { action: 'serviceName_b', name: 'service-name-b', instance: 'instance-name' }, |
| 134 | + { action: 'serviceName_c', name: 'service-name-a' }, |
| 135 | + { action: 'serviceName_c', name: 'service-name-b' } |
| 136 | + ] |
| 137 | + |
| 138 | + return openwhiskCompileServiceBindings.compileServiceBindings().then(() => { |
| 139 | + expect(service.bindings).to.deep.equal(services); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should return service bindings for functions & packages', () => { |
| 144 | + const service = openwhiskCompileServiceBindings.serverless.service; |
| 145 | + service.resources.packages = { |
| 146 | + a: { bind: [{ service: { name: 'service-name-a' } }] } |
| 147 | + }; |
| 148 | + |
| 149 | + const fns = { |
| 150 | + b: { bind: [{ service: { name: 'service-name-b', instance: 'instance-name' } }] }, |
| 151 | + } |
| 152 | + |
| 153 | + service.getAllFunctions = () => Object.keys(fns) |
| 154 | + service.getFunction = name => fns[name] |
| 155 | + |
| 156 | + const services = [ |
| 157 | + { action: 'serviceName_a', name: 'service-name-a' }, |
| 158 | + { action: 'serviceName_b', name: 'service-name-b', instance: 'instance-name' } |
| 159 | + ] |
| 160 | + |
| 161 | + return openwhiskCompileServiceBindings.compileServiceBindings().then(() => { |
| 162 | + expect(service.bindings).to.deep.equal(services); |
| 163 | + }); |
| 164 | + |
| 165 | + }) |
| 166 | + }) |
| 167 | +}); |
0 commit comments