Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate website status tests #1124

Merged
merged 5 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions .github/workflows/generate_chapters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@
# Updates timestamps, increments CSS, and then runs "npm run generate"
# on any pushes to main for markdown, templates or generate scripts
#
# Then it tests the website
#
# Then we open a Pull Request for any changes generated.
#
# We do it on a push, rather than on pull request, and only on main to
# avoid needing GitHub Personal Access Tokens and to avoid merge conflicts
#
name: Generate Chapters on Push
on:
workflow_dispatch:
push:
branches:
- main
paths:
- src/config/**
- src/**.py
- src/requirements.txt
- src/content/**
- src/templates/**
- src/tools/generate/**
- src/tools/**
- src/static/css/**
- src/static/js/**
- src/package.json
Expand All @@ -32,6 +38,7 @@ jobs:
with:
fetch-depth: 0
- name: Update CSS version for modified CSS files
if: github.event_name == 'push'
env:
COMMIT_SHA: ${{ github.event.commits[0].id }}
run: |
Expand All @@ -42,6 +49,7 @@ jobs:
git diff-tree --diff-filter=AM --no-commit-id --name-only -r $COMMIT_SHA src/static/css | grep "\.css" | sed 's!.*/!!' | sed "s/\(.*\)/s\/\1?v=[0-9][0-9]*\/\1?v=$(date +%Y%m%d%H%M%S)\//" | sed -f - -i src/templates/base.html
git diff
- name: Update JS version for modified CSS files
if: github.event_name == 'push'
env:
COMMIT_SHA: ${{ github.event.commits[0].id }}
run: |
Expand All @@ -52,6 +60,7 @@ jobs:
git diff-tree --diff-filter=AM --no-commit-id --name-only -r $COMMIT_SHA src/static/js | grep "\.js" | sed 's!.*/!!' | sed "s/\(.*\)/s\/\1?v=[0-9][0-9]*\/\1?v=$(date +%Y%m%d%H%M%S)\//" | sed -f - -i src/templates/base.html
git diff
- name: Update timestamp for modified files
if: github.event_name == 'push'
env:
COMMIT_SHA: ${{ github.event.commits[0].id }}
run: |
Expand All @@ -75,6 +84,27 @@ jobs:
npm install
echo "regenerating chapters"
npm run generate
- name: Set up Python 3.7
uses: actions/setup-python@v2
with:
python-version: '3.7'
- name: Install Requirements
run: |
python -m pip install --upgrade pip
cd src
pip install -r requirements.txt
- name: Run pytest
run: |
cd src
pytest
- name: Run website
run: |
cd src
python main.py background &
- name: Run tests
run: |
cd src
npm run tests
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v3
Expand All @@ -90,5 +120,4 @@ jobs:
[1]: https://github.com/peter-evans/create-pull-request
labels: generate chapters
- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
run: echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
18 changes: 18 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"homepage": "https://github.com/HTTPArchive/almanac.httparchive.org#readme",
"scripts": {
"test": "node ./tools/test",
"generate": "node ./tools/generate",
"ebooks": "node ./tools/generate/generate_ebook_pdfs",
"deploy": "echo \"Y\" | gcloud app deploy --project webalmanac --stop-previous-version"
Expand All @@ -25,6 +26,8 @@
"prettier": "^2.0.5",
"recursive-readdir": "^2.2.2",
"showdown": "^1.9.1",
"web-vitals": "^0.2.4"
"web-vitals": "^0.2.4",
"node-fetch": "^2.6.0",
"xml-js": "^1.6.11"
}
}
5 changes: 5 additions & 0 deletions src/tools/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let { test_status_codes } = require('./test_status_codes');

(async () => {
await test_status_codes();
})();
73 changes: 73 additions & 0 deletions src/tools/test/test_status_codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const fs = require("fs-extra");
const fetch = require("node-fetch");
const convert = require('xml-js');

const base_url = "http://127.0.0.1:8080";

let failures = 0;
let passes = 0;

const test_status_code = async (page,status,location) => {

if (location == undefined) {
location = null;
} else {
location = base_url + location;
}

try {

// Don't follow redirects
const options = {
redirect: 'manual'
}

const response = await fetch(base_url + page, options);

if (response.status === status && response.headers.get('location') === location) {
console.log('Success - expected:', status, 'got:',response.status, 'for page:', page);
passes++;
} else {
console.error('Failed - expected:', status, 'got:',response.status, 'for page:', page);
failures++;
}

} catch (error) {
console.error('Error - expected:', status, 'for page:', page);
console.log(error);
failures++;
}
};

const test_sitemap_pages = async (page,code) => {
const xml = await fs.readFile(`templates/sitemap.xml`, 'utf-8');
const sitemap = JSON.parse(convert.xml2json(xml,{compact: true}));
const urls = sitemap['urlset']['url'];
for ( var url in urls ) {
page = urls[url]['loc']['_text'];
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
page = page.replace('https://almanac.httparchive.org','');
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
await test_status_code(page,200);
}
}

const test_status_codes = async (page,code) => {

// Test success pages
await test_sitemap_pages();
await test_status_code('/sitemap.xml',200);

// Test Redirects
await test_status_code('/',302,'/en/2019/');
await test_status_code('/en/',302,'/en/2019/');

//Test 404
await test_status_code('/en/2019/random',404);

console.log('Passes:', passes, "Failures:", failures);
process.exitCode = failures;

}

module.exports = {
test_status_codes
};