-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
MockController
, MockKubeResourceMeta
- these will enable simpler testing here - it should also enable simpler testing of the `mustachetemplate` controller - this is also ground work for a PR to `mustachetemplate` to add a new script it its `bin/` that can render MTPs from files on disk, instead of only k8s resources in a live environment, to enable better testing of MTPs generally
- Loading branch information
1 parent
3321891
commit e40c9e1
Showing
5 changed files
with
128 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const MockKubeResourceMeta = require('../lib/MockKubeResourceMeta'); | ||
|
||
module.exports = class MockController { | ||
constructor(eventData, kubeData) { | ||
this.data = eventData; | ||
this.kubeResourceMeta = new MockKubeResourceMeta( | ||
this.data.object.apiVersion, | ||
this.data.object.kind, | ||
kubeData | ||
); | ||
this.kubeClass = { | ||
getKubeResourceMeta: (apiVersion, kind) => { | ||
return new MockKubeResourceMeta(apiVersion, kind, kubeData); | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module.exports = class MockKubeResourceMeta { | ||
constructor(apiVersion, kind, kubeData) { | ||
this._apiVersion = ((apiVersion === undefined || apiVersion === '') ? 'deploy.razee.io/v1alpha2' : apiVersion); | ||
this._kind = ((kind === undefined || kind === '') ? 'MustacheTemplate' : kind); | ||
this.kubeData = kubeData; | ||
} | ||
|
||
async request(reqOpt) { | ||
const ref = { | ||
apiVersion: reqOpt.uri.apiVersion, | ||
kind: reqOpt.uri.kind, | ||
name: reqOpt.uri.name, | ||
namespace: reqOpt.uri.namespace, | ||
labelSelector: reqOpt?.qs?.labelSelector | ||
}; | ||
|
||
const res = await this.kubeGetResource(ref); | ||
return res; | ||
} | ||
|
||
async get(name, namespace) { | ||
const ref = this.uri({ name, namespace }); | ||
return await this.kubeGetResource(ref); | ||
} | ||
|
||
uri(options) { | ||
return { ...options, apiVersion: this._apiVersion, kind: this._kind, }; | ||
} | ||
|
||
async kubeGetResource(ref) { | ||
const { | ||
name, | ||
labelSelector, | ||
namespace, | ||
kind, | ||
apiVersion | ||
} = ref; | ||
if (!this.kubeData[kind]) { | ||
return; | ||
} | ||
|
||
let fn = labelSelector ? 'filter' : 'find'; | ||
let lookup = this.kubeData[kind][fn](obj => { | ||
let match = true; | ||
match = (obj.apiVersion === apiVersion && match) ? true : false; | ||
match = (obj.kind === kind && match) ? true : false; | ||
match = ((obj.metadata.name === name || labelSelector !== undefined) && match) ? true : false; | ||
match = (obj.metadata.namespace === namespace && match) ? true : false; | ||
if (labelSelector) { | ||
const objLabels = obj.metadata.labels ?? {}; | ||
labelSelector.split(',').forEach(label => { | ||
let [key, value] = label.split('='); | ||
match = (objLabels[key] === value && match) ? true : false; | ||
}); | ||
} | ||
return match; | ||
}); | ||
return labelSelector ? { items: lookup } : lookup; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters