From 8f9f793c32840ad1cb01b68bf87fca989816fef2 Mon Sep 17 00:00:00 2001 From: Emily Stark Date: Wed, 19 Feb 2014 11:25:22 -0800 Subject: [PATCH] Add test for 'meteor deploy --settings' --- tools/test-utils.js | 20 ++++++--- tools/tests/deploy-settings.js | 82 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 tools/tests/deploy-settings.js diff --git a/tools/test-utils.js b/tools/test-utils.js index a965a0f61e..0b6dfc2ae6 100644 --- a/tools/test-utils.js +++ b/tools/test-utils.js @@ -70,12 +70,22 @@ exports.cleanUpLegacyApp = function (sandbox, name, password) { }; // Creates an app and deploys it. Assumes the sandbox is already logged -// in. -exports.createAndDeployApp = function (sandbox) { - var name = randomAppName(); - sandbox.createApp(name, 'empty'); +// in. Returns the name of the deployed app. Options: +// - settingsFile: a path to a settings file to deploy with +// - appName: app name to use; will be generated randomly if not +// provided +// - templateApp: the name of the template app to use. defaults to 'empty' +exports.createAndDeployApp = function (sandbox, options) { + options = options || {}; + var name = options.appName || randomAppName(); + sandbox.createApp(name, options.templateApp || 'empty'); sandbox.cd(name); - var run = sandbox.run('deploy', name); + var runArgs = ['deploy', name]; + if (options.settingsFile) { + runArgs.push('--settings'); + runArgs.push(options.settingsFile); + } + var run = sandbox.run.apply(sandbox, runArgs); run.waitSecs(90); run.match('Now serving at ' + name + '.meteor.com'); run.waitSecs(10); diff --git a/tools/tests/deploy-settings.js b/tools/tests/deploy-settings.js new file mode 100644 index 0000000000..c5dd02c319 --- /dev/null +++ b/tools/tests/deploy-settings.js @@ -0,0 +1,82 @@ +var _ = require('underscore'); +var selftest = require('../selftest.js'); +var testUtils = require('../test-utils.js'); +var utils = require('../utils.js'); +var Sandbox = selftest.Sandbox; +var httpHelpers = require('../http-helpers.js'); + +// Poll the given app looking for the correct settings. Throws an error +// if the settings aren't found after a timeout. +var checkForSettings = function (appName, settings, timeoutSecs) { + var timer = setTimeout(function () { + throw new Error('Expected settings not found on app ', appName); + }, timeoutSecs * 1000); + while (true) { + var result = httpHelpers.request('http://' + appName + '.meteor.com'); + + // XXX This is brittle; the test will break if we start formatting the + // __meteor_runtime_config__ JS differently. Ideally we'd do something + // like point a phantom at the deployed app and actually evaluate + // Meteor.settings. + var configRegexp = /__meteor_runtime_config__ = (.+);<\/script>/; + var configMatch = result.body.match(configRegexp); + if (configMatch && configMatch[1]) { + var stringifiedConfig = configMatch[1].trim(); + var parsedConfig = JSON.parse(stringifiedConfig); + if (_.isEqual(parsedConfig.PUBLIC_SETTINGS, settings['public'])) { + clearTimeout(timer); + return; + } + } + } +}; + +selftest.define('deploy - with settings', ['net', 'slow'], function () { + var s = new Sandbox; + testUtils.login(s, 'test', 'testtest'); + var settings = { + 'public': { a: 'b' } + }; + s.write('settings.json', JSON.stringify(settings)); + + // Deploy an app with settings and check that the public settings + // appear in the HTTP response body. + var appName = testUtils.createAndDeployApp(s, { + // Use standard-app instead of empty because we actually want + // standard-app-packages (including webapp) so that we can send a + // HTTP request to the app and get a response. + templateApp: 'standard-app', + // The path is ../settings.json instead of settings.json because + // createAndDeployApp creates a new app directory and cd's into it. + settingsFile: '../settings.json' + }); + checkForSettings(appName, settings, 10); + + // Re-deploy without settings and check that the settings still + // appear. + s.cd('..'); + testUtils.createAndDeployApp(s, { + templateApp: 'standard-app', + appName: appName + }); + // It takes a few seconds for the app to actually update, and we don't + // want to get a false positive in the meantime (i.e., if the settings + // disappear, we don't want to send our request before the app has + // updated and conclude that the settings are still there). + utils.sleepMs(5000); + checkForSettings(appName, settings, 10); + + // Re-deploy with new settings and check that the settings get + // updated. + settings['public'].a = 'c'; + s.cd('..'); + s.write('settings.json', JSON.stringify(settings)); + testUtils.createAndDeployApp(s, { + templateApp: 'standard-app', + settingsFile: '../settings.json', + appName: appName + }); + checkForSettings(appName, settings, 10); + + testUtils.cleanUpApp(s, appName); +});