Skip to content

Commit

Permalink
feat: make LocalRenderer importable
Browse files Browse the repository at this point in the history
- `bin/renderlocal` now imports `test/lib/LocalRenderer`
  - this allows other node projects that include MTPs to be able to use
    the `LocalRenderer` in their own tests
- also addresses PR feedback:
  - items previously added to `src` are now in `test/lib`
  - `razeedeploy-core` PR was updated to remove logic defaulting
    namespaces. that logic has been moved here in
    `test/lib/IOUtils.js`'s `kubeDataFromYamlFiles` method and a warning
    is emitted when it defaults
  • Loading branch information
charlesthomas committed Jun 28, 2022
1 parent 9c47c9e commit 6ad352a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
28 changes: 5 additions & 23 deletions bin/renderlocal
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
const nopt = require('nopt');
const path = require('path');

const { FetchEnvs, MockController } = require('@razee/razeedeploy-core');
const IOUtils = require('../src/IOUtils');
const LocalMustacheTemplateController = require('../src/LocalMustacheTemplateController');
const IOUtils = require('../test/lib/IOUtils');
const LocalRenderer = require('../test/lib/LocalRenderer');

const args = nopt(
{
Expand All @@ -27,29 +26,12 @@ if (args.env === undefined || args.env.length < 1) {
}

async function renderLocal() {
const mtp = await IOUtils.readYamlFile(args.mtp);
const kubeData = await IOUtils.kubeDataFromYamlFiles(...args.env);

const eventData = {
type: 'ADDED',
object: mtp[0]
}

const fetchEnvs = new FetchEnvs(new MockController(eventData, kubeData));
const view = await fetchEnvs.get('spec');

const mtpController = new LocalMustacheTemplateController({eventData: eventData, kubeData: kubeData});
let templates = mtpController.concatTemplates();
await mtpController.processTemplate(templates, view);

if (templates.length > 1) {
throw Error("can only handle one template!");
}
const data = await new LocalRenderer(args.mtp, ...args.env).render();

if (args.out === undefined) {
await IOUtils.printYaml(templates[0]);
await IOUtils.printYaml(data);
} else {
await IOUtils.writeYamlFile(templates[0], args.out)
await IOUtils.writeYamlFile(data, args.out)
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { EventHandler, KubeClass } = require('@razee/kubernetes-util');

const ControllerString = 'MustacheTemplate';
const Controller = require(`./${ControllerString}Controller`);
const LocalRenderer = require('../test/lib/LocalRenderer');
const log = require('./logger').createLogger(ControllerString);

async function createNewEventHandler(kc) {
Expand Down Expand Up @@ -71,5 +72,6 @@ async function run() {

module.exports = {
run,
LocalRenderer,
MustacheTemplateController: Controller
};
11 changes: 8 additions & 3 deletions src/IOUtils.js → test/lib/IOUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs');
const {promises: fs} = require('fs');
const yaml = require('js-yaml');

async function kubeDataFromYamlFiles(...filePaths) {
Expand All @@ -11,6 +11,11 @@ async function kubeDataFromYamlFiles(...filePaths) {
kubeData[kind] = [];
}
kubeData[kind].push(element);
if (element.metadata.namespace === undefined) {
const msg = `assuming "default" namespace missing from ${element.kind}/${element.metadata.name} in ${filePath}`;
console.warn(msg);
element.metadata.namespace = 'default';
}
}
}
return kubeData;
Expand All @@ -25,15 +30,15 @@ async function printYaml(asObj) {
}

async function readFile(filePath) {
return await fs.promises.readFile(filePath);
return await fs.readFile(filePath);
}

async function readYamlFile(filePath) {
return await yamlToObj(await readFile(filePath));
}

async function writeFile(contents, filePath) {
fs.writeFile(filePath, contents, (err) => { console.error(err); });
await fs.writeFile(filePath, contents);
}

async function writeYamlFile(asObj, filePath) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { MockKubeResourceMeta } = require('@razee/razeedeploy-core');
const MustacheTemplateController = require('../src/MustacheTemplateController');
const MustacheTemplateController = require('../../src/MustacheTemplateController');

module.exports = class LocalMustacheTemplateController extends MustacheTemplateController {
constructor(params) {
Expand Down
34 changes: 34 additions & 0 deletions test/lib/LocalRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const IOUtils = require('./IOUtils');
const LocalMustacheTemplateController = require('./LocalMustacheTemplateController');

const { FetchEnvs, MockController } = require('@razee/razeedeploy-core');

module.exports = class LocalRenderer {
constructor(mtpPath, ...envPaths) {
this.mtpPath = mtpPath;
this.envPaths = envPaths;
}

async render() {
const mtp = await IOUtils.readYamlFile(this.mtpPath);
const kubeData = await IOUtils.kubeDataFromYamlFiles(...this.envPaths);

const eventData = {
type: 'ADDED',
object: mtp[0]
}

const fetchEnvs = new FetchEnvs(new MockController(eventData, kubeData));
const view = await fetchEnvs.get('spec');

const mtpController = new LocalMustacheTemplateController({eventData: eventData, kubeData: kubeData});
let templates = mtpController.concatTemplates();
templates = await mtpController.processTemplate(templates, view);

if (templates.length > 1) {
throw Error("can only handle one template!");
}

return templates[0];
}
}
2 changes: 1 addition & 1 deletion test/mustache-template-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('chai').assert;
var Controller = require('../src/LocalMustacheTemplateController');
var Controller = require('../test/lib/LocalMustacheTemplateController');

describe('#processTemplates', async function () {
const eventData = {
Expand Down

0 comments on commit 6ad352a

Please sign in to comment.