test.js 8.6 KB

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