Skip to content

Commit 3136a27

Browse files
authoredOct 25, 2019
Add PR validation workflow (#480)
* Basic test workflow * Add tests * Change let to const * Add workflow trigger on push * Broaden PR trigger
1 parent 0a87606 commit 3136a27

File tree

7 files changed

+5796
-1
lines changed

7 files changed

+5796
-1
lines changed
 

‎.eslintrc.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
"jest": true
6+
},
7+
"extends": [
8+
"standard"
9+
],
10+
"globals": {
11+
"Atomics": "readonly",
12+
"SharedArrayBuffer": "readonly"
13+
},
14+
"parserOptions": {
15+
"ecmaVersion": 2018,
16+
"sourceType": "script"
17+
},
18+
"rules": {
19+
"indent": [ "warn", 3 ],
20+
"semi": [ "error", "always" ],
21+
"space-before-function-paren": [ "error", {
22+
"anonymous": "never",
23+
"named": "never",
24+
"asyncArrow": "always"
25+
}],
26+
"prefer-const": "off"
27+
}
28+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: "verify-policy-samples"
2+
on: [pull_request, push]
3+
4+
jobs:
5+
test:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- run: npm ci
10+
- run: npm test

‎jsconfig.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"typeAcquisition": {
3+
"include": [ "jest" ]
4+
}
5+
}

‎package-lock.json

+5,629
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "azure-policy",
3+
"version": "1.0.0",
4+
"description": "{{description}}",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Azure/azure-policy.git"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/Azure/azure-policy/issues"
18+
},
19+
"homepage": "https://github.com/Azure/azure-policy#readme",
20+
"dependencies": {},
21+
"devDependencies": {
22+
"eslint": "^6.5.1",
23+
"eslint-config-standard": "^14.1.0",
24+
"eslint-plugin-import": "^2.18.2",
25+
"eslint-plugin-node": "^10.0.0",
26+
"eslint-plugin-promise": "^4.2.1",
27+
"eslint-plugin-standard": "^4.0.1",
28+
"jest": "^24.9.0"
29+
}
30+
}

‎samples/SQL/deploy-sql-server-threat-detection/azurepolicy.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"mode": "Indexed",
55
"description": "This policy ensures that Threat Detection is enabled on SQL Servers.",
66
"metadata": {
7-
"category": "SQL",
7+
"category": "SQL"
88
},
99
"parameters": {},
1010
"policyRule": {
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const fsasync = require('fs').promises;
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const samplesRootPath = path.join(__dirname, '..');
6+
7+
// These directories are excluded from samples tests
8+
const excludedDirectories = [
9+
path.join(samplesRootPath, '__tests__', '**').toUpperCase(),
10+
path.join(samplesRootPath, 'GuestConfiguration', 'package-samples', '**').toUpperCase(),
11+
path.join(samplesRootPath, 'KubernetesService', '**').toUpperCase()
12+
];
13+
14+
// Directories with these file type are excluded from samples tests
15+
const excludedFileExtensions = [
16+
'.REGO'
17+
];
18+
19+
const samplesDirectories = [];
20+
21+
function getFilePaths(rootDirPath) {
22+
const entryPaths = fs.readdirSync(rootDirPath).map(entry => path.join(rootDirPath, entry));
23+
const filePaths = entryPaths.filter(entryPath => fs.statSync(entryPath).isFile());
24+
const dirPaths = entryPaths.filter(entryPath => !filePaths.includes(entryPath));
25+
const dirFiles = dirPaths.reduce((prev, curr) => prev.concat(getFilePaths(curr)), []);
26+
return [...filePaths, ...dirFiles];
27+
}
28+
29+
// Get the full set of directory paths containing policy samples
30+
let allSamplesFiles = getFilePaths(samplesRootPath);
31+
let samplesDirectoriesByPath = {};
32+
allSamplesFiles.forEach(filePath => {
33+
let containingDirPath = path.dirname(filePath);
34+
35+
// Exclude some directories and files that are deemed 'special content'
36+
for (const excludedDir of excludedDirectories) {
37+
if ((excludedDir.endsWith('**') && containingDirPath.toUpperCase().startsWith(excludedDir.substr(0, excludedDir.length - 3))) || containingDirPath.toUpperCase() === excludedDir) {
38+
return;
39+
}
40+
}
41+
42+
if (excludedFileExtensions.includes(path.extname(filePath).toUpperCase())) {
43+
return;
44+
}
45+
46+
if (!samplesDirectoriesByPath[containingDirPath]) {
47+
samplesDirectoriesByPath[containingDirPath] = [filePath];
48+
} else {
49+
samplesDirectoriesByPath[containingDirPath].push(filePath);
50+
}
51+
});
52+
53+
for (const dirPath in samplesDirectoriesByPath) {
54+
samplesDirectories.push([dirPath, samplesDirectoriesByPath[dirPath]]);
55+
}
56+
57+
// Validates that each samples directory contains the expected files
58+
test.each(samplesDirectories)('Validate directory structure: %s', (dirPath, filePaths) => {
59+
const expectedPolicyDefinitionFiles = [
60+
path.join(dirPath, 'azurepolicy.json'),
61+
path.join(dirPath, 'azurepolicy.parameters.json'),
62+
path.join(dirPath, 'azurepolicy.rules.json'),
63+
path.join(dirPath, 'README.md')
64+
];
65+
66+
const expectedPolicySetFiles = [
67+
path.join(dirPath, 'azurepolicyset.json'),
68+
path.join(dirPath, 'azurepolicyset.parameters.json'),
69+
path.join(dirPath, 'azurepolicyset.definitions.json'),
70+
path.join(dirPath, 'README.md')
71+
];
72+
73+
// Special case samples dirs that only contain markdown
74+
if (filePaths.every(filePath => path.extname(filePath).toUpperCase() === '.MD')) {
75+
return;
76+
}
77+
78+
if (filePaths.some(filePath => path.basename(filePath, '.json').toUpperCase() === 'AZUREPOLICYSET')) {
79+
expect(filePaths).toEqual(expect.arrayContaining(expectedPolicySetFiles));
80+
} else {
81+
expect(filePaths).toEqual(expect.arrayContaining(expectedPolicyDefinitionFiles));
82+
}
83+
});
84+
85+
// Validates that all JSON files are valid JSON (not necessarily valid policy entities though)
86+
test.each(samplesDirectories.map(dirSet => dirSet[1]))('Validate JSON can be parsed: %s', async (filePath) => {
87+
if (path.extname(filePath).toUpperCase() !== '.JSON') {
88+
return;
89+
}
90+
91+
let fileContent = await fsasync.readFile(filePath);
92+
JSON.parse(fileContent);
93+
});

0 commit comments

Comments
 (0)
Please sign in to comment.