Skip to content

Commit

Permalink
Merge 14cc73c into 0f6a36f
Browse files Browse the repository at this point in the history
  • Loading branch information
jdowning committed Sep 15, 2020
2 parents 0f6a36f + 14cc73c commit 254a1c0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 57 deletions.
9 changes: 2 additions & 7 deletions commands/topics_tail.js
Expand Up @@ -5,7 +5,6 @@ const co = require('co')

const debug = require('../lib/debug')
const clusterConfig = require('../lib/shared').clusterConfig
const isPrivate = require('../lib/shared').isPrivate
const deprecated = require('../lib/shared').deprecated
const withCluster = require('../lib/clusters').withCluster

Expand All @@ -16,10 +15,6 @@ const MAX_LENGTH = 80
function * tail (context, heroku) {
const kafka = require('@heroku/no-kafka')
yield withCluster(heroku, context.app, context.args.CLUSTER, function * (addon) {
if (isPrivate(addon)) {
throw new Error('`kafka:topics:tail` is not available in Heroku Private Spaces')
}

let appConfig = yield heroku.get(`/apps/${context.app}/config-vars`)
let attachment = yield heroku.get(`/apps/${context.app}/addon-attachments/${addon.name}`,
{ headers: { 'accept-inclusion': 'config_vars' } })
Expand All @@ -40,7 +35,7 @@ function * tail (context, heroku) {
yield consumer.init()
} catch (e) {
debug(e)
throw new Error('Could not connect to kafka')
cli.exit(1, 'Could not connect to kafka')
}

var topicName = context.args.TOPIC
Expand All @@ -67,7 +62,7 @@ function * tail (context, heroku) {
})
} catch (e) {
debug(e)
reject(new Error('Could not subscribe to topic'))
cli.exit(1, 'Could not subscribe to topic')
}
})
})
Expand Down
5 changes: 0 additions & 5 deletions commands/topics_write.js
Expand Up @@ -5,7 +5,6 @@ const co = require('co')

const debug = require('../lib/debug')
const clusterConfig = require('../lib/shared').clusterConfig
const isPrivate = require('../lib/shared').isPrivate
const deprecated = require('../lib/shared').deprecated
const withCluster = require('../lib/clusters').withCluster

Expand All @@ -15,10 +14,6 @@ const IDLE_TIMEOUT = 1000
function * write (context, heroku) {
const kafka = require('@heroku/no-kafka')
yield withCluster(heroku, context.app, context.args.CLUSTER, function * (addon) {
if (isPrivate(addon)) {
cli.exit(1, '`kafka:topics:write` is not available in Heroku Private Spaces')
}

let appConfig = yield heroku.get(`/apps/${context.app}/config-vars`)
let attachment = yield heroku.get(`/apps/${context.app}/addon-attachments/${addon.name}`,
{ headers: { 'accept-inclusion': 'config_vars' } })
Expand Down
2 changes: 1 addition & 1 deletion lib/shared.js
Expand Up @@ -41,7 +41,7 @@ function clusterConfig (attachment, config) {
}

function isPrivate (addon) {
return addon.plan.name.indexOf('private-') !== -1
return addon.plan.name.indexOf('private-') !== -1 || addon.plan.name.indexOf('shield-') !== -1
}

function parseBool (boolStr) {
Expand Down
32 changes: 7 additions & 25 deletions test/commands/topics_tail_test.js
Expand Up @@ -8,6 +8,7 @@ const it = mocha.it
const beforeEach = mocha.beforeEach
const afterEach = mocha.afterEach
const proxyquire = require('proxyquire')
const expectExit = require('../expect_exit')

const cli = require('heroku-cli-util')
const nock = require('nock')
Expand Down Expand Up @@ -74,30 +75,15 @@ describe('kafka:topics:tail', () => {
tail.process = global.process
})

it('warns and exits with an error if used with a Private Spaces cluster', () => {
planName = 'heroku-kafka:beta-private-standard-2'
return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }})
.then(() => { throw new Error('expected error; got none') })
.catch((err) => {
expect(err.message).to.equal('`kafka:topics:tail` is not available in Heroku Private Spaces')
expect(cli.stdout).to.be.empty
expect(cli.stderr).to.be.empty
})
})

it('warns and exits with an error if it cannot connect', () => {
api.get('/apps/myapp/config-vars').reply(200, config)
api.get('/apps/myapp/addon-attachments/kafka-1')
.reply(200, { name: 'KAFKA', config_vars: stockConfigVars, app: { name: 'sushi' } })
consumer.init = () => { throw new Error('oh snap') }

return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }})
.then(() => { throw new Error('expected error; got none') })
.catch((err) => {
expect(err.message).to.equal(`Could not connect to kafka`)
expect(cli.stdout).to.be.empty
expect(cli.stderr).to.be.empty
})
return expectExit(1, cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }}))
.then(() => expect(cli.stdout).to.be.empty)
.then(() => expect(cli.stderr).to.equal(` ▸ Could not connect to kafka\n`))
})

it('warns and exits with an error if it cannot subscribe', () => {
Expand All @@ -106,13 +92,9 @@ describe('kafka:topics:tail', () => {
.reply(200, { name: 'KAFKA', config_vars: stockConfigVars, app: { name: 'sushi' } })
consumer.subscribe = () => { throw new Error('oh snap') }

return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }})
.then(() => { throw new Error('expected error; got none') })
.catch((err) => {
expect(err.message).to.equal('Could not subscribe to topic')
expect(cli.stdout).to.be.empty
expect(cli.stderr).to.be.empty
})
return expectExit(1, cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }, flags: {}}))
.then(() => expect(cli.stdout).to.be.empty)
.then(() => expect(cli.stderr).to.equal(` ▸ Could not subscribe to topic\n`))
})

it('tails a topic and prints the results', () => {
Expand Down
7 changes: 0 additions & 7 deletions test/commands/topics_write_test.js
Expand Up @@ -76,13 +76,6 @@ describe('kafka:topics:write', () => {
api.done()
})

it('warns and exits with an error if used with a Private Spaces cluster', () => {
planName = 'heroku-kafka:beta-private-standard-2'
return expectExit(1, cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }}))
.then(() => expect(cli.stdout).to.be.empty)
.then(() => expect(cli.stderr).to.equal(' ▸ `kafka:topics:write` is not available in Heroku Private Spaces\n'))
})

it('warns and exits with an error if it cannot connect', () => {
api.get('/apps/myapp/config-vars').reply(200, config)
api.get('/apps/myapp/addon-attachments/kafka-1')
Expand Down
30 changes: 18 additions & 12 deletions test/lib/shared_test.js
Expand Up @@ -53,18 +53,24 @@ describe('parseDuration', function () {

describe('isPrivate', function () {
let cases = [
[ { plan: { name: 'beta-standard-0' } }, false ],
[ { plan: { name: 'beta-standard-1' } }, false ],
[ { plan: { name: 'beta-standard-2' } }, false ],
[ { plan: { name: 'beta-extended-0' } }, false ],
[ { plan: { name: 'beta-extended-1' } }, false ],
[ { plan: { name: 'beta-extended-2' } }, false ],
[ { plan: { name: 'beta-private-standard-0' } }, true ],
[ { plan: { name: 'beta-private-standard-1' } }, true ],
[ { plan: { name: 'beta-private-standard-2' } }, true ],
[ { plan: { name: 'beta-private-extended-0' } }, true ],
[ { plan: { name: 'beta-private-extended-1' } }, true ],
[ { plan: { name: 'beta-private-extended-2' } }, true ]
[ { plan: { name: 'standard-0' } }, false ],
[ { plan: { name: 'standard-1' } }, false ],
[ { plan: { name: 'standard-2' } }, false ],
[ { plan: { name: 'extended-0' } }, false ],
[ { plan: { name: 'extended-1' } }, false ],
[ { plan: { name: 'extended-2' } }, false ],
[ { plan: { name: 'private-standard-0' } }, true ],
[ { plan: { name: 'private-standard-1' } }, true ],
[ { plan: { name: 'private-standard-2' } }, true ],
[ { plan: { name: 'private-extended-0' } }, true ],
[ { plan: { name: 'private-extended-1' } }, true ],
[ { plan: { name: 'private-extended-2' } }, true ],
[ { plan: { name: 'shield-standard-0' } }, true ],
[ { plan: { name: 'shield-standard-1' } }, true ],
[ { plan: { name: 'shield-standard-2' } }, true ],
[ { plan: { name: 'shield-extended-0' } }, true ],
[ { plan: { name: 'shield-extended-1' } }, true ],
[ { plan: { name: 'shield-extended-2' } }, true ]
]
cases.forEach(function (testcase) {
let addon = testcase[0]
Expand Down

0 comments on commit 254a1c0

Please sign in to comment.