Skip to content

Commit

Permalink
feat(cli:socket): verifying socket compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucharz committed Jan 28, 2018
1 parent 76aefea commit c3003f8
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@
"prepush": "npm run lint && npm run test:unit",
"prepublish": "npm run build",
"build": "npx babel src -d lib/ --copy-files --ignore *.test.js,*.test-e2e.js --source-maps",
"build:watch": "npm run build -w",
"build:watch": "npm run build -- -w",
"clean": "rm -rf lib/",
"unit": "mocha 'tests/unit/*.test.js' --require babel-register",
"unit:watch": "npm run unit -w",
"unit:watch": "npm run unit -- -w",
"test": "npm-run-all --parallel test:unit test:e2e:group:*",
"test:e2e:init": "npm run test:e2e-single tests/e2e/init.test-e2e.js",
"test:e2e:anonymous": "npm run test:e2e-single tests/e2e/anonymous.test-e2e.js",
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/commands/socket-submit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import format from 'chalk'
import { CompatibilityError } from '../utils/errors'
import { error, echo, echon } from '../utils/print-tools'

export default class SocketSubmitCmd {
Expand Down Expand Up @@ -44,7 +45,10 @@ export default class SocketSubmitCmd {
echo(`Type ${publishCommand} to make it available for everyone!`)
echo()
} catch (err) {
if (err.response && err.response.data) {
if (err instanceof CompatibilityError) {
error(4)(err.message)
echo()
} else if (err.response && err.response.data) {
error(4)(err.response.data.message)
echo()
} else {
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ class CompileError {
}
}

class CompatibilityError {
constructor (socketVersion, envVersion) {
this.message = `Socket major version (${socketVersion}) is not\
comaptible with this Syncano environment major version (${envVersion}).`
}
}

export {
CompatibilityError,
CompileError
}
3 changes: 3 additions & 0 deletions packages/cli/src/utils/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import walkUp from 'node-walkup'
import Promise from 'bluebird'

import logger from './debug'
import pjson from '../../package.json'
import getSettings from '../settings'
import genUniqueName from './unique-instance'
import Socket from './sockets'
Expand All @@ -26,6 +27,8 @@ export class Session {
this.userId = null
this.walkup = Promise.promisify(walkUp)

this.majorVersion = pjson.version.split('.')[0]

this.HOST = process.env.SYNCANO_HOST || 'api.syncano.io'
this.ENDPOINT_HOST = this.HOST === 'api.syncano.io' ? 'syncano.space' : 'syncano.link'
}
Expand Down
11 changes: 10 additions & 1 deletion packages/cli/src/utils/sockets/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Hosting from '../hosting'
import Registry from '../registry'
import { p, echo } from '../print-tools'
import { getTemplate } from '../templates'
import { CompileError } from '../errors'
import { CompileError, CompatibilityError } from '../errors'

const { debug } = logger('utils-sockets')

Expand Down Expand Up @@ -1284,6 +1284,7 @@ class Socket {

submit () {
debug('submit')
this.isCompatible()
const registry = new Registry()
return registry.submitSocket(this)
}
Expand Down Expand Up @@ -1369,6 +1370,14 @@ class Socket {
return checksums
}

isCompatible () {
const socketMajorVersion = this.spec.version.split('.')[0]
if (socketMajorVersion !== session.majorVersion) {
throw new CompatibilityError(socketMajorVersion, session.majorVersion)
}
return true
}

shouldBeUpdated () {
debug('shouldBeUpdated')
if (this.existLocally && this.existRemotely) {
Expand Down
21 changes: 19 additions & 2 deletions packages/cli/tests/e2e/registry.test-e2e.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* global describe it before after */
import replace from 'replace'
import path from 'path'
import {
nixt,
Expand Down Expand Up @@ -41,7 +42,7 @@ describe('[E2E] CLI Registry', function () {
.end(done)
})

it('can submit created socket to registry', function (done) {
it('can submit created socket to the registry', function (done) {
testNixt()
.run(`${cliLocation} submit ${createdSocketName}`)
.stdout(/to make it available for everyone/)
Expand All @@ -50,11 +51,27 @@ describe('[E2E] CLI Registry', function () {

it('can bump version of submited socket', function (done) {
testNixt()
.run(`${cliLocation} submit ${createdSocketName} -b major`)
.run(`${cliLocation} submit ${createdSocketName} -b minor`)
.stdout(/\(1\.0\.0\)\.\.\. Done/)
.end(done)
})

it('can submit socket with wrong version to the registry', function (done) {
testNixt()
.before(() => {
replace({
regex: 'version: 0.0.1',
replacement: 'version: 25.0.1',
paths: [path.join(testsLocation, testInstance, 'syncano', createdSocketName)],
recursive: true,
silent: true
})
})
.run(`${cliLocation} submit ${createdSocketName}`)
.stdout(/is not comaptible with this Syncano environment/)
.end(done)
})

it('can publish created socket', function (done) {
testNixt()
.run(`${cliLocation} publish ${createdSocketName}`)
Expand Down
4 changes: 4 additions & 0 deletions packages/lib-js-core/src/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pjson from '../package.json'
import Data from './data'
import Users from './users'
import Account from './account'
Expand All @@ -20,6 +21,9 @@ class Server {
const getConfig = className => Object.assign({className}, settings)
const config = getConfig()

this.version = pjson.version
this.majorVersion = pjson.version.split('.')[0]

this._class = new Class(config)
this.event = new Event(config)
this.endpoint = new Endpoint(config)
Expand Down
1 change: 1 addition & 0 deletions packages/lib-js-core/test/e2e/event.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global it describe before after */
import {expect} from 'chai'
import Server from '../../src'
import {getRandomString, createTestInstance, deleteTestInstance} from '../utils'
Expand Down
11 changes: 11 additions & 0 deletions packages/lib-js-core/test/e2e/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* global it describe */
import {expect} from 'chai'
import pjson from '../../package.json'
import Server from '../../src'

describe('Server', function () {
it('get major version', async () => {
const majorVersion = new Server().majorVersion
expect(majorVersion).to.be.equal(pjson.version.split('.')[0])
})
})

0 comments on commit c3003f8

Please sign in to comment.