Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Script that generate all permutations of user input for IVS and Livestream services #210

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feature(gen-test-payload): script that generate all permutations of u…
…ser input for ivs and livestream service
  • Loading branch information
spaniernathan committed Apr 19, 2021
commit cc36b0faa36b71c1ef6cbc48ae5842009486efd0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_STORE
node_modules
scripts/flow/*/.flowconfig
scripts/gen-test-payload/output/*.sh
*~
*.pyc
.grunt
Expand Down
135 changes: 135 additions & 0 deletions scripts/gen-test-payload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const fs = require('fs');
const ejs = require('ejs');

const servicesQuestions = [];
const servicesHelpers = {};

const questionFolder = `${__dirname}/../../provider-utils/`;
fs.readdirSync(questionFolder).forEach((file) => {
if (file.includes('questions')) {
servicesQuestions.push({
serviceType: file.split('-')[0],
content: require(`${questionFolder}${file}`),
});
}
});

const helpersFolder = './test-helpers/';
fs.readdirSync(helpersFolder).forEach((file) => {
if (file.includes('helpers')) {
servicesHelpers[file.split('-')[0]] = require(`${helpersFolder}${file}`);
}
});

class TreeNode {
constructor(value, key, depth) {
this.key = key;
this.value = value;
this.depth = depth;
this.parents = [];
this.childs = [];
}

addChild(newNode) {
this.childs = [...this.childs, newNode];
newNode.parents = [...newNode.parents, this];
return newNode;
}
}

class Tree {
constructor() {
this.maxDepth = 0;
this.paths = [];
}

buildTree(questions, helper) {
this.rootNode = new TreeNode('root', 'root', 0);
const firstQuestion = helper[questions.content.video.inputs[0].key];
const getNextNode = (currNode, currQuestion, depth) => {
depth++;
if (typeof currQuestion === 'undefined' || (!!currQuestion.next && !!currQuestion.options)) {
return;
}
if (!!currQuestion.type && currQuestion.type === 'list') {
Object.keys(currQuestion.options).forEach((optionKey) => {
if (!currQuestion.options[optionKey].next) {
currNode.addChild(new TreeNode(currQuestion.options[optionKey].value,
currQuestion.key, depth));
} else {
const nextNode = new TreeNode(currQuestion.options[optionKey].value,
currQuestion.key, depth);
currNode.addChild(nextNode);
getNextNode(nextNode, helper[currQuestion.options[optionKey].next],
depth);
}
});
} else if (!!currQuestion.type && currQuestion.type === 'confirm') {
// don't create node out of those because they don't hold payload
// they might hold one but we have no example for that
getNextNode(currNode, helper[currQuestion.yesNext], depth); // yes
getNextNode(currNode, helper[currQuestion.noNext], depth); // no
} else if (!!currQuestion.type && currQuestion.type === 'checkbox') {
// gen all permutations of checkboxes and call getNextNode for each of them
} else {
const nextNode = new TreeNode(helper[currQuestion.key].defaultValue,
currQuestion.key, depth);
currNode.addChild(nextNode);
getNextNode(nextNode, helper[currQuestion.next], depth);
}
};
const firstNode = new TreeNode(helper[questions.content.video.inputs[0].key].defaultValue,
questions.content.video.inputs[0].key, 1);
this.rootNode.addChild(firstNode);
getNextNode(firstNode, helper[firstQuestion.next], firstNode.depth);
return this;
}

buildPaths(node, path, helpers) {
if (node.childs.length === 0) {
path = [...path, { key: node.key, value: node.value }];
this.paths = [...this.paths, path];
return;
}
if (node.key !== 'root') {
path = [...path, { key: node.key, value: node.value }];
}
node.childs.forEach((c) => {
this.buildPaths(c, path, helpers);
});
}

static buildScript(questions, path, idx) {
ejs.renderFile('./template.ejs', {
payload: {
inputs: path,
serviceType: questions.serviceType,
provider: questions.content.video.provider,
},
}, (ejsErr, str) => {
if (ejsErr) {
console.error(ejsErr);
return;
}
fs.writeFile(`output/${questions.serviceType}-${idx}.sh`, str, (fsErr) => {
if (fsErr) {
console.error(fsErr);
}
});
});
}
}

// Entrypoint
servicesQuestions.forEach((question) => {
if (question.serviceType === 'livestream' || question.serviceType === 'ivs') {
console.info(`---Service ${question.serviceType}---`);
const tree = new Tree();
tree.buildTree(question, servicesHelpers[`${question.serviceType}`]);
tree.buildPaths(tree.rootNode, [], servicesHelpers[`${question.serviceType}`].content);
console.info('Number of permutations:', tree.paths.length);
tree.paths.forEach((val, idx) => {
tree.buildScript(question, val, idx);
});
}
});
Empty file.
13 changes: 13 additions & 0 deletions scripts/gen-test-payload/template.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e
IFS='|'

PERMUTATION="{
\"service\":\"video\",
\"serviceType\":\"<%- payload.serviceType %>\",
\"providerName\":\"<%- payload.provider %>\",
<% payload.inputs.forEach(({key, value}) => { %>
\"<%- key %>\":\"<%- value %>\",
<% }); %>
}"
amplify video add --payload $PERMUTATION
33 changes: 33 additions & 0 deletions scripts/gen-test-payload/test-helpers/ivs-helpers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"resourceName": {
"key": "ressourceName",
"next": "channelQuality",
"defaultValue": "mylivestream"
},
"channelQuality": {
"key": "channelQuality",
"type": "list",
"options": {
"STANDARD": {
"value": "STANDARD",
"next": "channelLatency"
},
"BASIC": {
"value": "BASIC",
"next": "channelLatency"
}
}
},
"channelLatency": {
"key": "channelLatency",
"type": "list",
"options": {
"LOW": {
"value": "LOW"
},
"NORMAL": {
"value": "NORMAL"
}
}
}
}
Loading