|
| 1 | +const AWS = require('aws-sdk-mock'); |
| 2 | +const fs = require('fs-promise'); |
| 3 | +const Plugin = require('./index'); |
| 4 | + |
| 5 | +const slsDir = '.serverless'; |
| 6 | +const templatePrefix = `${slsDir}/cloudformation-template-update-stack`; |
| 7 | +const exampleTemplate = `${templatePrefix}.json`; |
| 8 | +const exampleOrgTemplate = `${templatePrefix}.org.json`; |
| 9 | +const slsDefaults = { |
| 10 | + service: { |
| 11 | + service: 'foo', |
| 12 | + defaults: { |
| 13 | + stage: 'foo', |
| 14 | + region: 'eu-west-1', |
| 15 | + }, |
| 16 | + }, |
| 17 | + cli: { |
| 18 | + log: jest.fn(), |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +beforeAll(() => { |
| 23 | + if (!fs.existsSync(slsDir)) { |
| 24 | + fs.mkdirSync(slsDir); |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +describe('serverless-plugin-write-env-vars', () => { |
| 29 | + describe('constructor', () => { |
| 30 | + it('sets the correct defaults', () => { |
| 31 | + const plugin = new Plugin(slsDefaults, {}); |
| 32 | + |
| 33 | + expect(plugin.options.stage).toBe('foo'); |
| 34 | + expect(plugin.options.region).toBe('eu-west-1'); |
| 35 | + expect(plugin.options.diffTool).toBe('diff'); |
| 36 | + expect(plugin.options.localTemplate).toBe(`${templatePrefix}.json`); |
| 37 | + expect(plugin.options.orgTemplate).toBe(`${templatePrefix}.org.json`); |
| 38 | + }); |
| 39 | + |
| 40 | + it('registers the appropriate hooks', () => { |
| 41 | + const plugin = new Plugin(slsDefaults, {}); |
| 42 | + |
| 43 | + expect(typeof plugin.hooks['before:diff:diff']).toBe('function'); |
| 44 | + expect(typeof plugin.hooks['diff:diff']).toBe('function'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('downloadTemplate', () => { |
| 49 | + afterEach(() => AWS.restore('CloudFormation')); |
| 50 | + |
| 51 | + describe('with successful CloudFormation call', () => { |
| 52 | + beforeEach(() => |
| 53 | + AWS.mock('CloudFormation', 'getTemplate', { |
| 54 | + TemplateBody: '{"foo":"bar"}', |
| 55 | + }) |
| 56 | + ); |
| 57 | + |
| 58 | + it('downloads currently deployed template', () => { |
| 59 | + const plugin = new Plugin(slsDefaults, {}); |
| 60 | + |
| 61 | + return plugin.downloadTemplate() |
| 62 | + .then(() => |
| 63 | + fs.readFile(`${templatePrefix}.org.json`, { encoding: 'utf8' }) |
| 64 | + .then(data => expect(data).toMatchSnapshot()) |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('with unsuccessful CloudFormation call', () => { |
| 70 | + beforeEach(() => |
| 71 | + AWS.mock('CloudFormation', 'getTemplate', (Object, callback) => |
| 72 | + callback(new Error('Stack with id foo-foo does not exist'), null) |
| 73 | + ) |
| 74 | + ); |
| 75 | + |
| 76 | + it('could not download deployed template', () => { |
| 77 | + const plugin = new Plugin(slsDefaults, {}); |
| 78 | + |
| 79 | + return plugin.downloadTemplate() |
| 80 | + .catch(err => expect(err).toMatchSnapshot()); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('diff', () => { |
| 86 | + beforeEach(() => |
| 87 | + fs.writeFile(exampleTemplate, '{"foo":"foo"}') |
| 88 | + .then(() => fs.writeFile(exampleOrgTemplate, '{"foo":"bar"}')) |
| 89 | + ); |
| 90 | + |
| 91 | + describe('successfully triggers diff', () => { |
| 92 | + it('runs diff with defaults', () => { |
| 93 | + const plugin = new Plugin(slsDefaults, {}); |
| 94 | + |
| 95 | + return plugin.diff() |
| 96 | + .then(data => expect(data).toMatchSnapshot()); |
| 97 | + }); |
| 98 | + |
| 99 | + it('runs diff with custom diff tool', () => { |
| 100 | + const plugin = new Plugin(slsDefaults, { |
| 101 | + diffTool: 'diff', |
| 102 | + }); |
| 103 | + |
| 104 | + return plugin.diff() |
| 105 | + .then(data => expect(data).toMatchSnapshot()); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('successfully triggers diff', () => { |
| 110 | + const customTemplate = `${templatePrefix}-foo.json`; |
| 111 | + const customOrgTemplate = `${templatePrefix}-foo.org.json`; |
| 112 | + |
| 113 | + beforeEach(() => |
| 114 | + fs.writeFile(customTemplate, '{"foo":"foo"}') |
| 115 | + .then(() => fs.writeFile(customOrgTemplate, '{"foo":"custom"}')) |
| 116 | + ); |
| 117 | + |
| 118 | + it('runs diff with custom localTemplate template', () => { |
| 119 | + const plugin = new Plugin(slsDefaults, { |
| 120 | + localTemplate: customTemplate, |
| 121 | + }); |
| 122 | + |
| 123 | + return plugin.diff() |
| 124 | + .then(data => expect(data).toMatchSnapshot()); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('unsuccessfully triggers diff', () => { |
| 129 | + beforeEach(() => fs.unlink(exampleTemplate)); |
| 130 | + |
| 131 | + it('could not find locally compiled template', () => { |
| 132 | + const plugin = new Plugin(slsDefaults, {}); |
| 133 | + |
| 134 | + return plugin.diff() |
| 135 | + .catch(err => expect(err).toMatchSnapshot()); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments