Skip to content

Commit

Permalink
Merge pull request #125 from rabbah/docker
Browse files Browse the repository at this point in the history
Support action create/update with --docker.
  • Loading branch information
ddragosd authored Feb 20, 2020
2 parents 178bbcc + 20be232 commit 23f871c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
23 changes: 18 additions & 5 deletions src/commands/runtime/action/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ class ActionCreate extends RuntimeBaseCommand {
// sanity check must either be a sequence or a file but permit neither in case of an update
if (args.actionPath && flags.sequence) {
throw (new Error('Cannot specify sequence and a code artifact at the same time'))
} else if (!args.actionPath && !flags.sequence && !this.isUpdate()) {
throw (new Error('Must provide a code artifact or define a sequence'))
} else if (flags.docker && flags.sequence) {
throw (new Error('Cannot specify sequence and a container image at the same time'))
} else if (flags.docker && flags.kind) {
throw (new Error('Cannot specify a kind and a container image at the same time'))
} else if (!args.actionPath && !flags.sequence && !flags.docker && !this.isUpdate()) {
throw (new Error('Must provide a code artifact, container image, or a sequence'))
}

// can only specify main handler when also providing a file
Expand All @@ -51,8 +55,8 @@ class ActionCreate extends RuntimeBaseCommand {
exec = {}

if (args.actionPath.endsWith('.zip') || flags.binary) {
if (!flags.kind) {
throw (new Error('Invalid argument(s). creating an action from a .zip artifact requires specifying the action kind explicitly'))
if (!flags.kind && !flags.docker) {
throw (new Error('Invalid argument(s). creating an action from a zip/binary artifact requires specifying the action kind explicitly'))
}
exec.code = fs.readFileSync(args.actionPath).toString('base64')
} else {
Expand All @@ -65,7 +69,7 @@ class ActionCreate extends RuntimeBaseCommand {

if (flags.kind) {
exec.kind = flags.kind
} else {
} else if (!flags.docker) {
exec.kind = kindForFileExtension(args.actionPath)
}
} else {
Expand All @@ -80,6 +84,12 @@ class ActionCreate extends RuntimeBaseCommand {
}
}

if (flags.docker) {
exec = exec || {}
exec.kind = 'blackbox'
exec.image = flags.docker
}

if (flags.param) {
// each --param flag expects two values ( a key and a value ). Multiple --param flags can be passed
// For example : aio runtime:action:update --param name "foo" --param city "bar"
Expand Down Expand Up @@ -234,6 +244,9 @@ ActionCreate.flags = {
sequence: flags.string({
description: 'treat ACTION as comma separated sequence of actions to invoke' // help description for flag
}),
docker: flags.string({
description: '[Restricted Access] use provided Docker image (a path on DockerHub) to run the action' // help description for flag
}),
main: flags.string({
description: 'the name of the action entry point (function or fully-qualified method name when applicable)'
}),
Expand Down
70 changes: 68 additions & 2 deletions test/commands/runtime/action/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ describe('instance methods', () => {

test('creates an action with action name and --sequence flag', () => {
const name = 'hello'
ow.actions.client.options = { namespace: 'ns' }
const cmd = ow.mockResolved(owAction, '')
command.argv = [name, '--sequence', 'a,p/b,ns/p/c,/ns2/p/d,/ns3/e']
return command.run()
Expand All @@ -127,6 +126,47 @@ describe('instance methods', () => {
})
})

test('creates an action with action name and --docker flag', () => {
const name = 'hello'
const cmd = ow.mockResolved(owAction, '')
command.argv = [name, '--docker', 'some-image']
return command.run()
.then(() => {
expect(cmd).toHaveBeenCalledWith({
name,
action: {
name,
exec: {
kind: 'blackbox',
image: 'some-image'
}
}
})
expect(stdout.output).toMatch('')
})
})

test('creates an action with action name and action path and --docker flag', () => {
const name = 'hello'
const cmd = ow.mockResolved(owAction, '')
command.argv = [name, '/action/actionFile.js', '--docker', 'some-image']
return command.run()
.then(() => {
expect(cmd).toHaveBeenCalledWith({
name,
action: {
name,
exec: {
code: jsFile,
kind: 'blackbox',
image: 'some-image'
}
}
})
expect(stdout.output).toMatch('')
})
})

test('creates an action with action name and action path --json', () => {
const name = 'hello'
const cmd = ow.mockResolved(owAction, '')
Expand Down Expand Up @@ -542,14 +582,40 @@ describe('instance methods', () => {
})
})

test('creates an action with --docker and --sequence', () => {
return new Promise((resolve, reject) => {
ow.mockRejected(owAction, '')
command.argv = ['hello', '--docker', 'some-image', '--sequence', 'a,b,c']
return command.run()
.then(() => reject(new Error('does not throw error')))
.catch(() => {
expect(handleError).toHaveBeenLastCalledWith('failed to create the action', new Error('Cannot specify sequence and a container image at the same time'))
resolve()
})
})
})

test('creates an action with --docker and --kind', () => {
return new Promise((resolve, reject) => {
ow.mockRejected(owAction, '')
command.argv = ['hello', '/action/actionFile.js', '--kind', 'nodejs:8', '--docker', 'some-image']
return command.run()
.then(() => reject(new Error('does not throw error')))
.catch(() => {
expect(handleError).toHaveBeenLastCalledWith('failed to create the action', new Error('Cannot specify a kind and a container image at the same time'))
resolve()
})
})
})

test('tests for incorrect action create missing code and sequence', () => {
return new Promise((resolve, reject) => {
ow.mockRejected(owAction, '')
command.argv = ['hello']
return command.run()
.then(() => reject(new Error('does not throw error')))
.catch(() => {
expect(handleError).toHaveBeenLastCalledWith('failed to create the action', new Error('Must provide a code artifact or define a sequence'))
expect(handleError).toHaveBeenLastCalledWith('failed to create the action', new Error('Must provide a code artifact, container image, or a sequence'))
resolve()
})
})
Expand Down
3 changes: 1 addition & 2 deletions test/commands/runtime/action/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ describe('instance methods', () => {
test('updates an action with action name and --sequence flag', () => {
const name = 'hello'
const cmd = ow.mockResolved(owAction, '')
ow.actions.client.options = { namespace: 'ns' }
command.argv = [name, '--sequence', 'a,p/b,ns/p/c,/ns2/p/d,/ns3/e']
return command.run()
.then(() => {
Expand Down Expand Up @@ -761,7 +760,7 @@ describe('instance methods', () => {
return command.run()
.then(() => reject(new Error('does not throw error')))
.catch(() => {
expect(handleError).toHaveBeenLastCalledWith('failed to update the action', new Error('Invalid argument(s). creating an action from a .zip artifact requires specifying the action kind explicitly'))
expect(handleError).toHaveBeenLastCalledWith('failed to update the action', new Error('Invalid argument(s). creating an action from a zip/binary artifact requires specifying the action kind explicitly'))
resolve()
})
})
Expand Down

0 comments on commit 23f871c

Please sign in to comment.