mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
257 lines
8.5 KiB
JavaScript
257 lines
8.5 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Test: Serverless State Class
|
|
*/
|
|
|
|
let Serverless = require('../../../lib/Serverless.js'),
|
|
path = require('path'),
|
|
utils = require('../../../lib/utils/index'),
|
|
assert = require('chai').assert,
|
|
testUtils = require('../../test_utils'),
|
|
config = require('../../config');
|
|
|
|
let serverless;
|
|
let instance;
|
|
|
|
describe('Test Serverless State Class', function() {
|
|
|
|
before(function(done) {
|
|
this.timeout(0);
|
|
testUtils.createTestProject(config)
|
|
.then(projPath => {
|
|
|
|
process.chdir(projPath);
|
|
|
|
// Instantiate Serverless
|
|
serverless = new Serverless({
|
|
interactive: false,
|
|
projectPath: projPath
|
|
});
|
|
|
|
// Instantiate Class
|
|
instance = new serverless.classes.State(serverless);
|
|
|
|
done();
|
|
});
|
|
});
|
|
|
|
after(function(done) {
|
|
done();
|
|
});
|
|
|
|
describe('Tests', function() {
|
|
|
|
it('Load instance from file system', function(done) {
|
|
instance.load()
|
|
.then(function(instance) {
|
|
done();
|
|
})
|
|
.catch(e => {
|
|
done(e);
|
|
});
|
|
});
|
|
|
|
it('Get instance data, without private properties', function(done) {
|
|
let clone = instance.get();
|
|
assert.equal(true, typeof clone._config === 'undefined');
|
|
done();
|
|
});
|
|
|
|
it('Get populated instance data', function(done) {
|
|
let data = instance.getPopulated({ stage: config.stage, region: config.region });
|
|
assert.equal(true, JSON.stringify(data).indexOf('$${') == -1);
|
|
assert.equal(true, JSON.stringify(data).indexOf('${') == -1);
|
|
done();
|
|
});
|
|
|
|
it('Set instance data', function(done) {
|
|
let clone = instance.get();
|
|
clone.project.name = 'newProject';
|
|
instance.set(clone);
|
|
assert.equal(true, instance.project.name === 'newProject');
|
|
done();
|
|
});
|
|
|
|
it('Save instance to the file system', function(done) {
|
|
instance.save()
|
|
.then(function(instance) {
|
|
done();
|
|
})
|
|
.catch(e => {
|
|
done(e);
|
|
});
|
|
});
|
|
|
|
it('Get project', function(done) {
|
|
let project = instance.getProject();
|
|
assert.equal(true, project.name === 'newProject');
|
|
done();
|
|
});
|
|
|
|
it('Get meta', function(done) {
|
|
let meta = instance.getMeta();
|
|
assert.equal(true, typeof meta.variables !== 'undefined');
|
|
done();
|
|
});
|
|
|
|
it('Get resources (unpopulated)', function(done) {
|
|
let resources = instance.getResources();
|
|
assert.equal(true, JSON.stringify(resources).indexOf('${') !== -1);
|
|
done();
|
|
});
|
|
|
|
it('Get resources (populated)', function(done) {
|
|
let resources = instance.getResources({ populate: true, stage: config.stage, region: config.region });
|
|
assert.equal(true, JSON.stringify(resources).indexOf('$${') == -1);
|
|
assert.equal(true, JSON.stringify(resources).indexOf('${') == -1);
|
|
done();
|
|
});
|
|
|
|
it('Get stages', function(done) {
|
|
let stages = instance.getStages();
|
|
assert.equal(true, stages[0] === config.stage);
|
|
done();
|
|
});
|
|
|
|
it('Get regions', function(done) {
|
|
let regions = instance.getRegions(config.stage);
|
|
assert.equal(true, regions[0] === config.region);
|
|
done();
|
|
});
|
|
|
|
it('Get components w/o paths', function(done) {
|
|
let components = instance.getComponents();
|
|
assert.equal(true, components[0].name === 'nodejscomponent');
|
|
done();
|
|
});
|
|
|
|
it('Get components w paths', function(done) {
|
|
let components = instance.getComponents({ paths: ['nodejscomponent'] });
|
|
assert.equal(true, components[0].name === 'nodejscomponent');
|
|
done();
|
|
});
|
|
|
|
it('Get components by component', function(done) {
|
|
let components = instance.getComponents({ component: 'nodejscomponent' });
|
|
assert.equal(true, components[0].name === 'nodejscomponent');
|
|
done();
|
|
});
|
|
|
|
it('Get modules w/o paths', function(done) {
|
|
let modules = instance.getModules();
|
|
assert.equal(true, modules[0].name === 'module1');
|
|
done();
|
|
});
|
|
|
|
it('Get modules w paths', function(done) {
|
|
let modules = instance.getModules({ paths: ['nodejscomponent/module1'] });
|
|
assert.equal(true, modules[0].name === 'module1');
|
|
done();
|
|
});
|
|
|
|
it('Get modules by component and module', function(done) {
|
|
let modules = instance.getModules({ component: 'nodejscomponent', module: 'module1' });
|
|
assert.equal(true, modules[0].name === 'module1');
|
|
done();
|
|
});
|
|
|
|
it('Get modules by component', function(done) {
|
|
let modules = instance.getModules({ component: 'nodejscomponent' });
|
|
assert.equal(true, modules.length === 1);
|
|
done();
|
|
});
|
|
|
|
it('Get functions w/o paths', function(done) {
|
|
let functions = instance.getFunctions();
|
|
assert.equal(true, functions.length === 3);
|
|
done();
|
|
});
|
|
|
|
it('Get functions w paths', function(done) {
|
|
let functions = instance.getFunctions({ paths: ['nodejscomponent/module1/function1'] });
|
|
assert.equal(true, functions.length === 1);
|
|
done();
|
|
});
|
|
|
|
it('Get functions by component, module and function', function(done) {
|
|
let functions = instance.getFunctions({ component: 'nodejscomponent', module: 'module1', function: 'function1' });
|
|
assert.equal(true, functions.length === 1);
|
|
done();
|
|
});
|
|
|
|
it('Get endpoints w/o paths', function(done) {
|
|
let endpoints = instance.getEndpoints();
|
|
assert.equal(true, endpoints.length === 4);
|
|
done();
|
|
});
|
|
|
|
it('Get endpoints w paths', function(done) {
|
|
let endpoints = instance.getEndpoints({ paths: ['nodejscomponent/module1/function1@module1/function1~GET'] });
|
|
assert.equal(true, endpoints.length === 1);
|
|
done();
|
|
});
|
|
|
|
it('Get endpoints by component, module, function, path and method', function(done) {
|
|
let endpoints = instance.getEndpoints({ component: 'nodejscomponent', module: 'module1', function: 'function3', endpointPath: 'module1/function3', endpointMethod: 'POST' });
|
|
assert.equal(true, endpoints.length === 1);
|
|
done();
|
|
});
|
|
|
|
it('Get endpoints by component, module and function', function(done) {
|
|
let endpoints = instance.getEndpoints({ component: 'nodejscomponent', module: 'module1', function: 'function1' });
|
|
assert.equal(true, endpoints.length === 1);
|
|
done();
|
|
});
|
|
|
|
it('Get endpoints by component and method', function(done) {
|
|
let endpoints = instance.getEndpoints({ component: 'nodejscomponent', endpointMethod: 'GET' });
|
|
assert.equal(true, endpoints.length === 3);
|
|
done();
|
|
});
|
|
|
|
it('Validate stage exists', function(done) {
|
|
assert.equal(true, instance.validateStageExists(config.stage));
|
|
assert.equal(false, instance.validateStageExists('invalid'));
|
|
done();
|
|
});
|
|
|
|
it('Validate region exists', function(done) {
|
|
assert.equal(true, instance.validateRegionExists(config.stage, config.region));
|
|
assert.equal(false, instance.validateRegionExists(config.stage, 'invalid'));
|
|
done();
|
|
});
|
|
|
|
it('Set Assets', function(done) {
|
|
|
|
let project = new instance._S.classes.Project(instance._S);
|
|
project.name = 'testProject';
|
|
instance.setAsset(project);
|
|
|
|
let component = new instance._S.classes.Component(instance._S, { component: 'testComponent' });
|
|
component.name = 'testComponent';
|
|
instance.setAsset(component);
|
|
|
|
let module = new instance._S.classes.Module(instance._S, { component: component.name, module: 'testModule' });
|
|
module.name = 'testModule';
|
|
instance.setAsset(module);
|
|
|
|
let func = new instance._S.classes.Function(instance._S, { component: component.name, module: module.name, function: 'testFunction' });
|
|
func.name = 'testFunction';
|
|
instance.setAsset(func);
|
|
|
|
let endpoint = new instance._S.classes.Endpoint(instance._S, { component: component.name, module: module.name, function: func.name, endpointPath: 'test/function', endpointMethod: 'GET' });
|
|
endpoint.path = 'test/endpoint';
|
|
instance.setAsset(endpoint);
|
|
|
|
assert.equal(true, instance.project.name === 'testProject');
|
|
assert.equal(true, typeof instance.project.components[component.name] !== 'undefined');
|
|
assert.equal(true, typeof instance.project.components[component.name].modules[module.name] !== 'undefined');
|
|
assert.equal(true, typeof instance.project.components[component.name].modules[module.name].functions[func.name] !== 'undefined');
|
|
assert.equal(true, instance.project.components[component.name].modules[module.name].functions[func.name].endpoints.length > 0);
|
|
|
|
done();
|
|
});
|
|
});
|
|
});
|