Skip to content

Commit

Permalink
New tests, 100% codecov and lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Himavanth committed May 29, 2020
1 parent 2df208a commit 15c132e
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 28 deletions.
11 changes: 8 additions & 3 deletions src/commands/runtime/deploy/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ async function createProjectJSON (entities, projectname, ow, fileDirectory) {
inputs: returninputsfromKeyValue(getTrigger.parameters),
annotations: returninputsfromKeyValue(getTrigger.annotations)
}
if(getTrigger.feed)
project['packages'][packageName]['triggers'][trigger]['feed'] = getTrigger.feed
for (const annotation of getTrigger.annotations) {
if (annotation.key === 'feed') {
project['packages'][packageName]['triggers'][trigger]['feed'] = annotation.value
break
}
}
}

for (const rule of entities.rules) {
Expand Down Expand Up @@ -186,7 +190,8 @@ function returninputsfromKeyValue (inputs) {
const inputObj = {}
for (const input of inputs) {
// whisk managed annotation not exported to yaml file
if (input.key !== 'whisk-managed') {
// and feed annotation becomes feed property in createProjectJSON
if (input.key !== 'whisk-managed' && input.key !== 'feed') {
inputObj[input.key] = input.value
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/commands/runtime/deploy/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class DeployReport extends RuntimeBaseCommand {
if (packages[key]['triggers']) {
Object.keys(packages[key]['triggers']).forEach((trigger) => {
const objTrigger = { name: trigger }
if (packages[key]['triggers'][trigger]['feed']) {
objTrigger.feed = packages[key]['triggers'][trigger]['feed']
}
const packageInputs = packages[key]['triggers'][trigger]['inputs'] || {}
let deploymentInputs = {}
if (trigger in deploymentTriggers) {
Expand All @@ -64,8 +67,6 @@ class DeployReport extends RuntimeBaseCommand {
let allInputs = returnUnion(packageInputs, deploymentInputs)
allInputs = processInputs(allInputs, {})
objTrigger.Inputs = allInputs
if(packages[key]['triggers'][trigger]['feed'])
objTrigger.feed = packages[key]['triggers'][trigger]['feed']
triggers.push(objTrigger)
})
}
Expand Down
10 changes: 5 additions & 5 deletions src/commands/runtime/trigger/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class TriggerCreate extends RuntimeBaseCommand {
} else if (flags['annotation-file']) {
annotationParams = createKeyValueArrayFromFile(flags['annotation-file'])
}
if(flags.feed) {
annotationParams.push({key: 'feed', value: flags.feed})
if (flags.feed) {
annotationParams.push({ key: 'feed', value: flags.feed })
}

// triggerParams.parameters is expected to be passed as an array of key value pairs
Expand All @@ -56,10 +56,10 @@ class TriggerCreate extends RuntimeBaseCommand {

const ow = await this.wsk()
await ow.triggers.create(options)
if(flags.feed) {
if (flags.feed) {
try {
await ow.feeds.create({name: flags.feed, trigger: args.triggerName, params: createKeyValueObjectFromFlag(flags.param)})
}catch (err) {
await ow.feeds.create({ name: flags.feed, trigger: args.triggerName, params: createKeyValueObjectFromFlag(flags.param) })
} catch (err) {
await ow.triggers.delete(options)
this.handleError('failed to create the feed, deleted trigger', err)
}
Expand Down
9 changes: 5 additions & 4 deletions src/commands/runtime/trigger/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ class TriggerDelete extends RuntimeBaseCommand {
const ow = await this.wsk()
const obj = { namespace, name }
const retTrigger = await ow.triggers.get(obj)
if(retTrigger.annotations)
for(const annotation of retTrigger.annotations) {
if(annotation.key == 'feed') {
await ow.feeds.delete({name: annotation.value, trigger: name})
if (retTrigger.annotations) {
for (const annotation of retTrigger.annotations) {
if (annotation.key === 'feed') {
await ow.feeds.delete({ name: annotation.value, trigger: name })
}
}
}

await ow.triggers.delete(obj)
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/runtime/trigger/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class TriggerUpdate extends RuntimeBaseCommand {

const ow = await this.wsk()
await ow.triggers.update(options)
if(flags.feed) {
await ow.feeds.update({name: flags.feed, trigger: args.triggerName, params: createKeyValueObjectFrom(flags.param)})
if (flags.feed) {
await ow.feeds.update({ name: flags.feed, trigger: args.triggerName, params: createKeyValueObjectFromFlag(flags.param) })
}
} catch (err) {
this.handleError('failed to update the trigger', err)
Expand Down
22 changes: 12 additions & 10 deletions src/runtime-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ function processPackage (packages, deploymentPackages, deploymentTriggers, param
}
if (packages[key]['triggers'][triggerName]['feed']) {
objTrigger.trigger.annotations = objTrigger.trigger.annotations || []
objTrigger.trigger.annotations.push({key:'feed', value: packages[key]['triggers'][triggerName]['feed']})
objTrigger.trigger.annotations.push({ key: 'feed', value: packages[key]['triggers'][triggerName]['feed'] })
objTrigger.trigger.feed = packages[key]['triggers'][triggerName]['feed']
}
ruleTrigger.push(triggerName)
Expand Down Expand Up @@ -869,11 +869,11 @@ async function deployPackage (entities, ow, logger) {
logger(`Info: Deploying trigger [${trigger.name}]...`)
await ow.triggers.update(trigger)
logger(`Info: trigger [${trigger.name}] has been successfully deployed.\n`)
if(trigger.trigger.feed) {
try{
if (trigger.trigger.feed) {
try {
// ow.feeds.update on a non-existent feed throws 502 (Bad Gateway) --> "Response Missing Error Message."
await ow.feeds.create({name: trigger.trigger.feed, trigger: trigger.name, params: createKeyValueObjectFromArray(trigger.trigger.parameters)})
}catch (err) {
await ow.feeds.create({ name: trigger.trigger.feed, trigger: trigger.name, params: createKeyValueObjectFromArray(trigger.trigger.parameters) })
} catch (err) {
await ow.triggers.delete({ name: trigger.name })
logger(`Info: feed for trigger [${trigger.name}] could not be deployed.\n`)
logger(`Info: Deleted trigger`)
Expand All @@ -897,11 +897,12 @@ async function undeployPackage (entities, ow, logger) {
}
for (const trigger of entities.triggers) {
logger(`Info: Undeploying trigger [${trigger.name}]...`)
let retTrigger = await ow.triggers.get({ name: trigger.name})
if(retTrigger.annotations)
for(const annotation of retTrigger.annotations) {
if(annotation.key == 'feed') {
await ow.feeds.delete({name: annotation.value, trigger: trigger.name})
const retTrigger = await ow.triggers.get({ name: trigger.name })
if (retTrigger.annotations) {
for (const annotation of retTrigger.annotations) {
if (annotation.key === 'feed') {
await ow.feeds.delete({ name: annotation.value, trigger: trigger.name })
}
}
}
await ow.triggers.delete({ name: trigger.name })
Expand Down Expand Up @@ -1133,6 +1134,7 @@ module.exports = {
createKeyValueArrayFromObject,
createKeyValueArrayFromFile,
createKeyValueArrayFromFlag,
createKeyValueObjectFromArray,
createKeyValueObjectFromFlag,
createKeyValueObjectFromFile,
parsePathPattern,
Expand Down
38 changes: 38 additions & 0 deletions test/__fixtures__/deploy/export_yaml_feed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
project:
name: proj
packages:
testSeq:
version: 0.0.2
namespace: ns
annotations: {}
actions:
helloAction1:
version: 0.0.9
namespace: 53444_51981/testSeq
annotations:
exec: 'nodejs:10'
inputs:
key1: val1
key2: val1
function: testSeq/helloAction1.js
runtime: 'nodejs:10'
main: split
limits:
timeout: 60000
memorySize: 256
logSize: 10
triggers:
meetPerson:
namespace: '53444_51981'
inputs:
name: Sam
place: ''
children: 0
annotations: {}
feed: /whisk.system/alarms/alarm
sequences: {}
rules:
meetPersonRule:
action: three
annotations: {}
trigger: meetPerson
13 changes: 13 additions & 0 deletions test/__fixtures__/deploy/manifest_dep_Triggers_feeds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements; and to You under the Apache License, Version 2.0.

# Example: processing data in a sequence
packages:
demo_package:
version: 1.0
license: Apache-2.0
triggers:
meetPerson:
feed: /whisk.system/alarms/alarm
inputs:
cron: '* * * * *'
15 changes: 15 additions & 0 deletions test/__fixtures__/deploy/report_triggersWithFeed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "",
"Inputs": {}
}
{
"name": "demo_package",
"Inputs": {}
}
{
"name": "meetPerson",
"feed": "/whisk.system/alarms/alarm",
"Inputs": {
"cron": "* * * * *"
}
}
74 changes: 72 additions & 2 deletions test/commands/runtime/deploy/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,26 @@ describe('instance methods', () => {
trigger: { name: 'meetPerson2', path: 'ns' },
version: '0.0.2'
}]

const triggerWithFeed = [{
annotations: [{
key: 'whisk-managed',
value: {
file: 'manifest.yaml',
projectDeps: [],
projectHash: 'wxyz',
projectName: 'proj'
}
},
{
key: 'feed',
value: '/whisk.system/alarms/alarm'
}],
name: 'meetPerson',
namespace: 'ns',
publish: false,
updated: 1555527174943,
version: '0.0.1'
}]
const triggerNoProjectName = [
{
annotations: [{
Expand Down Expand Up @@ -295,7 +314,43 @@ describe('instance methods', () => {
},
version: '0.0.5'
}

const triggerGetWithFeed = {
annotations: [{
key: 'whisk-managed',
value: {
file: 'manifest.yaml',
projectDeps: [],
projectHash: 'wxyz',
projectName: 'proj'
}
}, {
key: 'feed',
value: '/whisk.system/alarms/alarm'
}],
limits: {},
name: 'meetPerson',
namespace: '53444_51981',
parameters: [{
key: 'name', value: 'Sam'
},
{ key: 'place',
value: ''
},
{ key: 'children',
value: 0
}],
publish: false,
rules: {
'53444_51981/meetPersonRule': {
action: {
name: 'three',
path: '53444_51981/testSeq'
},
status: 'active'
}
},
version: '0.0.5'
}
const ruleslist = [{
action: { name: 'three', path: 'ns/testSeq' },
annotations: [{
Expand Down Expand Up @@ -407,6 +462,21 @@ describe('instance methods', () => {
})
})

test('write to manifest file when trigger has a feed', () => {
ow.mockResolved(owTriggerList, triggerWithFeed)
ow.mockResolved('triggers.get', triggerGetWithFeed)
ow.mockResolved(owRulesList, ruleslist)
fs.writeFileSync = jest.fn()
command.argv = ['--projectname', 'proj', '-m', '/deploy/manifest.yaml']
const yaml = fixtureFile('deploy/export_yaml_feed.yaml')
return command.run()
.then(() => {
expect(fs.writeFileSync).toHaveBeenCalledWith('/deploy/manifest.yaml', yaml)
expect(fs.writeFileSync).toHaveBeenCalledWith('/deploy/testSeq/helloAction1.js', 'code')
expect(fs.writeFileSync).toHaveBeenCalledWith('/deploy/testSeq/helloAction.js', 'code')
})
})

test('write action to js file', () => {
ow.mockResolved(owTriggerList, triggerlist)
ow.mockResolved('triggers.get', triggerGet)
Expand Down
24 changes: 24 additions & 0 deletions test/commands/runtime/deploy/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const owPackage = 'packages.update'
const owAction = 'actions.update'
const owAPI = 'routes.create'
const owTriggers = 'triggers.update'
const owFeeds = 'feeds.create'
const owRules = 'rules.update'

test('exports', async () => {
Expand Down Expand Up @@ -65,6 +66,7 @@ describe('instance methods', () => {
'deploy/parameters.json': fixtureFile('deploy/parameters.json'),
'deploy/apis_not_implemented.yml': fixtureFile('deploy/apis_not_implemented.yml'),
'deploy/sequences_implemented.yml': fixtureFile('deploy/sequences_implemented.yml'),
'deploy/manifest_dep_Triggers_feeds.yaml': fixtureFile('deploy/manifest_dep_Triggers_feeds.yaml'),
'deploy/manifest_triggersRules.yaml': fixtureFile('deploy/manifest_triggersRules.yaml'),
'deploy/manifest_triggersRules_IncorrectAction.yaml': fixtureFile('deploy/manifest_triggersRules_IncorrectAction.yaml'),
'deploy/manifest_triggersRules_noInputs.yaml': fixtureFile('deploy/manifest_triggersRules_noInputs.yaml'),
Expand Down Expand Up @@ -629,6 +631,28 @@ describe('instance methods', () => {
})
})

test('deploys trigger with feed in manifest file', () => {
const cmd = ow.mockResolved(owFeeds, '')
command.argv = ['-m', '/deploy/manifest_dep_Triggers_feeds.yaml']
return command.run()
.then(() => {
expect(cmd).toHaveBeenCalled()
expect(stdout.output).toMatch('')
})
})

test('deploys trigger with feed in manifest file - error', () => {
const cmd = ow.mockRejected(owFeeds, new Error('an error'))
const cmdDelete = ow.mockResolved('triggers.delete', '')
command.argv = ['-m', '/deploy/manifest_dep_Triggers_feeds.yaml']
return command.run()
.then(() => {
expect(cmd).toHaveBeenCalled()
expect(cmdDelete).toHaveBeenCalled()
expect(stdout.output).toMatch('')
})
})

test('deploys trigger without inputs in manifest file', () => {
const cmd = ow.mockResolved(owRules, '')
command.argv = ['-m', '/deploy/manifest_triggersRules_noInputs.yaml']
Expand Down
9 changes: 9 additions & 0 deletions test/commands/runtime/deploy/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('instance methods', () => {
handleError = jest.spyOn(command, 'handleError')
const json = {
'deploy/manifest_triggersRules.yaml': fixtureFile('deploy/manifest_triggersRules.yaml'),
'deploy/manifest_dep_Triggers_feeds.yaml': fixtureFile('deploy/manifest_dep_Triggers_feeds.yaml'),
'deploy/manifest.yaml': fixtureFile('deploy/manifest.yaml'),
'deploy/manifest_report.yaml': fixtureFile('deploy/manifest_report.yaml'),
'deploy/deployment_wrongpackage.yaml': fixtureFile('deploy/deployment_wrongpackage.yaml'),
Expand Down Expand Up @@ -129,6 +130,14 @@ describe('instance methods', () => {
})
})

test('print triggers from manifest file when deployment file has trigger with feed', () => {
command.argv = ['-m', '/deploy/manifest_dep_Triggers_feeds.yaml']
return command.run()
.then(() => {
expect(stdout.output).toMatchFixture('deploy/report_triggersWithFeed.txt')
})
})

test('project name different in manifest and deployment file', () => {
return new Promise((resolve, reject) => {
command.argv = ['-m', '/deploy/deployment_syncMissingAction.yaml', '-d', '/deploy/deployment-triggerError.yaml']
Expand Down
Loading

0 comments on commit 15c132e

Please sign in to comment.