test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env node
  2. 'use strict';
  3. require('chromedriver');
  4. var execSync = require('child_process').execSync,
  5. expect = require('expect.js'),
  6. fs = require('fs'),
  7. net = require('net'),
  8. path = require('path'),
  9. superagent = require('superagent'),
  10. util = require('util'),
  11. webdriver = require('selenium-webdriver');
  12. var by = webdriver.By,
  13. until = webdriver.until,
  14. Builder = require('selenium-webdriver').Builder;
  15. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  16. if (!process.env.USERNAME || !process.env.PASSWORD) {
  17. console.log('USERNAME and PASSWORD env vars need to be set');
  18. process.exit(1);
  19. }
  20. describe('Application life cycle test', function () {
  21. this.timeout(0);
  22. var server, browser = new Builder().forBrowser('chrome').build();
  23. before(function (done) {
  24. var seleniumJar= require('selenium-server-standalone-jar');
  25. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  26. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  27. server.start();
  28. done();
  29. });
  30. after(function (done) {
  31. browser.quit();
  32. server.stop();
  33. done();
  34. });
  35. var LOCATION = 'test';
  36. var TEST_TIMEOUT = 50000;
  37. var app;
  38. function waitForElement(elem, callback) {
  39. browser.wait(until.elementLocated(elem), TEST_TIMEOUT).then(function () {
  40. browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT).then(function () {
  41. callback();
  42. });
  43. });
  44. }
  45. function welcomePage(callback) {
  46. browser.get('https://' + app.fqdn);
  47. waitForElement(by.xpath('//*[text()="Cloudron LAMP App"]'), function () {
  48. waitForElement(by.xpath('//*[text()="PHP Version 7.0.30-0ubuntu0.16.04.1"]'), callback);
  49. });
  50. }
  51. function uploadedFileExists(callback) {
  52. browser.get('https://' + app.fqdn + '/test.php');
  53. waitForElement(by.xpath('//*[text()="this works"]'), function () {
  54. waitForElement(by.xpath('//*[text()="' + app.fqdn + '"]'), callback);
  55. });
  56. }
  57. function checkPhpMyAdmin(callback) {
  58. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  59. if (error && !error.response) return callback(error); // network error
  60. if (result.statusCode !== 401) return callback('Expecting 401 error');
  61. superagent.get('https://' + app.fqdn + '/phpmyadmin')
  62. .auth(process.env.USERNAME, process.env.PASSWORD)
  63. .end(function (error, result) {
  64. if (error) return callback(error);
  65. if (result.text.indexOf(`${app.fqdn} / mysql | phpMyAdmin`) === -1) { // in the <title>
  66. console.log(result.text);
  67. return callback(new Error('could not detect phpmyadmin'));
  68. }
  69. callback();
  70. });
  71. });
  72. }
  73. function checkCron(callback) {
  74. this.timeout(60000 * 2);
  75. fs.writeFileSync('/tmp/crontab', '* * * * * echo -n "$MYSQL_HOST" > /app/data/public/cron\n', 'utf8');
  76. execSync('cloudron push /tmp/crontab /app/data/crontab');
  77. fs.unlinkSync('/tmp/crontab');
  78. execSync('cloudron restart --wait');
  79. console.log('Waiting for crontab to trigger');
  80. setTimeout(function () {
  81. superagent.get('https://' + app.fqdn + '/cron').end(function (error, result) {
  82. if (error && !error.response) return callback(error); // network error
  83. if (result.statusCode !== 200) return callback('Expecting 200, got ' + result.statusCode);
  84. if (result.text !== 'mysql') return callback('Unexpected text: ' + result.text);
  85. callback();
  86. });
  87. }, 60 * 1000); // give it a minute to run the crontab
  88. }
  89. xit('build app', function () {
  90. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  91. });
  92. it('install app', function () {
  93. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  94. });
  95. it('can get app information', function () {
  96. var inspect = JSON.parse(execSync('cloudron inspect'));
  97. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  98. expect(app).to.be.an('object');
  99. });
  100. it('can view welcome page', welcomePage);
  101. it('can upload file with sftp', function () {
  102. // remove from known hosts in case this test was run on other apps with the same domain already
  103. // if the tests fail here you want below in ~/.ssh/config
  104. // Host test.cloudron.xyz
  105. // StrictHostKeyChecking no
  106. // HashKnownHosts no
  107. console.log('If this test fails, see the comment above this log');
  108. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  109. execSync(util.format('lftp sftp://%s:%s@%s:%s -e "set sftp:auto-confirm yes; cd public/; put test.php; bye"', process.env.USERNAME, process.env.PASSWORD, app.fqdn, app.portBindings.SFTP_PORT));
  110. });
  111. it('can get uploaded file', uploadedFileExists);
  112. it('can access phpmyadmin', checkPhpMyAdmin);
  113. it('executes cron tasks', checkCron);
  114. it('backup app', function () {
  115. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  116. });
  117. it('restore app', function () {
  118. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  119. });
  120. it('can get uploaded file', uploadedFileExists);
  121. it('move to different location', function () {
  122. browser.manage().deleteAllCookies();
  123. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  124. var inspect = JSON.parse(execSync('cloudron inspect'));
  125. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  126. expect(app).to.be.an('object');
  127. });
  128. it('can get uploaded file', uploadedFileExists);
  129. it('can access phpmyadmin', checkPhpMyAdmin);
  130. // disable SFTP
  131. it('can disable sftp', function () {
  132. execSync('cloudron configure --wait -p SFTP_PORT=', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  133. });
  134. it('(nosftp) can view welcome page', welcomePage);
  135. it('(nosftp cannot upload file with sftp', function (done) {
  136. var client = new net.Socket();
  137. client.setTimeout(10000);
  138. client.connect(2222, app.fqdn, function() {
  139. client.destroy();
  140. done(new Error('Connected'));
  141. });
  142. client.on('timeout', function () { client.destroy(); done(); }); // the packet just got dropped (good)
  143. client.on('error', function (error) {
  144. client.destroy();
  145. done(new Error('Should have got timeout but got error:' + error.message));
  146. });
  147. });
  148. it('(nosftp) cannot access phpmyadmin', function (done) {
  149. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  150. if (error && !error.response) return done(error); // network error
  151. if (result.statusCode !== 404) return done('Expecting 404 error');
  152. done();
  153. });
  154. });
  155. it('uninstall app', function () {
  156. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  157. });
  158. // test update
  159. it('can install app', function () {
  160. execSync('cloudron install --new --wait --appstore-id lamp.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  161. var inspect = JSON.parse(execSync('cloudron inspect'));
  162. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  163. expect(app).to.be.an('object');
  164. });
  165. it('can upload file with sftp', function () {
  166. // remove from known hosts in case this test was run on other apps with the same domain already
  167. // if the tests fail here you want to set "HashKnownHosts no" in ~/.ssh/config
  168. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  169. execSync(util.format('lftp sftp://%s:%s@%s:%s -e "set sftp:auto-confirm yes; cd public/; put test.php; bye"', process.env.USERNAME, process.env.PASSWORD, app.fqdn, app.portBindings.SFTP_PORT));
  170. });
  171. it('can update', function () {
  172. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  173. });
  174. it('can get uploaded file', uploadedFileExists);
  175. it('can access phpmyadmin', checkPhpMyAdmin);
  176. it('uninstall app', function () {
  177. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  178. });
  179. });