test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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) {
  39. return browser.wait(until.elementLocated(elem), TEST_TIMEOUT).then(function () {
  40. return browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT);
  41. });
  42. }
  43. function welcomePage(callback) {
  44. browser.get('https://' + app.fqdn).then(function () {
  45. return waitForElement(by.xpath('//*[text()="Cloudron LAMP App"]'));
  46. }).then(function () {
  47. return waitForElement(by.xpath('//h1[contains(text(), "7.2.10-0ubuntu0.18.04.1")]'));
  48. }).then(function () {
  49. callback();
  50. });
  51. }
  52. function uploadedFileExists(callback) {
  53. browser.get('https://' + app.fqdn + '/test.php').then(function () {
  54. return waitForElement(by.xpath('//*[text()="this works"]'));
  55. }).then(function () {
  56. return waitForElement(by.xpath('//*[text()="' + app.fqdn + '"]'));
  57. }).then(function () {
  58. callback();
  59. });
  60. }
  61. function checkPhpMyAdmin(callback) {
  62. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  63. if (error && !error.response) return callback(error); // network error
  64. if (result.statusCode !== 401) return callback('Expecting 401 error');
  65. superagent.get('https://' + app.fqdn + '/phpmyadmin')
  66. .auth(process.env.USERNAME, process.env.PASSWORD)
  67. .end(function (error, result) {
  68. if (error) return callback(error);
  69. if (result.text.indexOf(`${app.fqdn} / mysql | phpMyAdmin`) === -1) { // in the <title>
  70. console.log(result.text);
  71. return callback(new Error('could not detect phpmyadmin'));
  72. }
  73. callback();
  74. });
  75. });
  76. }
  77. function checkCron(callback) {
  78. this.timeout(60000 * 2);
  79. fs.writeFileSync('/tmp/crontab', '* * * * * echo -n "$MYSQL_HOST" > /app/data/public/cron\n', 'utf8');
  80. execSync('cloudron push /tmp/crontab /app/data/crontab');
  81. fs.unlinkSync('/tmp/crontab');
  82. execSync('cloudron restart --wait');
  83. console.log('Waiting for crontab to trigger');
  84. setTimeout(function () {
  85. superagent.get('https://' + app.fqdn + '/cron').end(function (error, result) {
  86. if (error && !error.response) return callback(error); // network error
  87. if (result.statusCode !== 200) return callback('Expecting 200, got ' + result.statusCode);
  88. if (result.text !== 'mysql') return callback('Unexpected text: ' + result.text);
  89. callback();
  90. });
  91. }, 60 * 1000); // give it a minute to run the crontab
  92. }
  93. xit('build app', function () {
  94. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  95. });
  96. it('install app', function () {
  97. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  98. });
  99. it('can get app information', function () {
  100. var inspect = JSON.parse(execSync('cloudron inspect'));
  101. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  102. expect(app).to.be.an('object');
  103. });
  104. it('can view welcome page', welcomePage);
  105. it('can upload file with sftp', function () {
  106. // remove from known hosts in case this test was run on other apps with the same domain already
  107. // if the tests fail here you want below in ~/.ssh/config
  108. // Host test.cloudron.xyz
  109. // StrictHostKeyChecking no
  110. // HashKnownHosts no
  111. console.log('If this test fails, see the comment above this log');
  112. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  113. 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));
  114. });
  115. it('can get uploaded file', uploadedFileExists);
  116. it('can access phpmyadmin', checkPhpMyAdmin);
  117. it('executes cron tasks', checkCron);
  118. it('backup app', function () {
  119. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  120. });
  121. it('restore app', function () {
  122. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  123. });
  124. it('can get uploaded file', uploadedFileExists);
  125. it('move to different location', function () {
  126. browser.manage().deleteAllCookies();
  127. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  128. var inspect = JSON.parse(execSync('cloudron inspect'));
  129. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  130. expect(app).to.be.an('object');
  131. });
  132. it('can get uploaded file', uploadedFileExists);
  133. it('can access phpmyadmin', checkPhpMyAdmin);
  134. // disable SFTP
  135. it('can disable sftp', function () {
  136. execSync('cloudron configure --wait -p SFTP_PORT=', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  137. });
  138. it('(nosftp) can view welcome page', welcomePage);
  139. it('(nosftp cannot upload file with sftp', function (done) {
  140. var client = new net.Socket();
  141. client.setTimeout(10000);
  142. client.connect(2222, app.fqdn, function() {
  143. client.destroy();
  144. done(new Error('Connected'));
  145. });
  146. client.on('timeout', function () { client.destroy(); done(); }); // the packet just got dropped (good)
  147. client.on('error', function (error) {
  148. client.destroy();
  149. done(new Error('Should have got timeout but got error:' + error.message));
  150. });
  151. });
  152. it('(nosftp) cannot access phpmyadmin', function (done) {
  153. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  154. if (error && !error.response) return done(error); // network error
  155. if (result.statusCode !== 404) return done('Expecting 404 error');
  156. done();
  157. });
  158. });
  159. it('uninstall app', function () {
  160. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  161. });
  162. // test update
  163. it('can install app', function () {
  164. execSync('cloudron install --new --wait --appstore-id lamp.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  165. var inspect = JSON.parse(execSync('cloudron inspect'));
  166. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  167. expect(app).to.be.an('object');
  168. });
  169. it('can upload file with sftp', function () {
  170. // remove from known hosts in case this test was run on other apps with the same domain already
  171. // if the tests fail here you want to set "HashKnownHosts no" in ~/.ssh/config
  172. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  173. 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));
  174. });
  175. it('can update', function () {
  176. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  177. });
  178. it('can get uploaded file', uploadedFileExists);
  179. it('can access phpmyadmin', checkPhpMyAdmin);
  180. it('uninstall app', function () {
  181. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  182. });
  183. });