Commit 279cb235 authored by Antonin Coulibaly's avatar Antonin Coulibaly
Browse files

Add tests and continuous integration

parent 295c71a1
Loading
Loading
Loading
Loading

circle.yml

0 → 100644
+20 −0
Original line number Diff line number Diff line
dependencies:
    cache_directories:
      - "tests/dependencies/kinetic-java"
    pre:
          - sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0/bin/java
          - sudo update-alternatives --set javac /usr/lib/jvm/java-8-openjdk-amd64/bin/javac
          - echo 'export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64' >> ~/.circlerc
    post:
      - sudo apt-get install maven
      - make -C tests/functional build_cache

machine:
  node:
    version: 6.9

test:
  override:
    - npm run --silent lint
    - npm run --silent test
    - npm run --silent tests_functional
Original line number Diff line number Diff line
Subproject commit bb04c36537845e299788c0e4f4f12a0514fd9955
+35 −0
Original line number Diff line number Diff line
KINETIC_JAVA = ../dependencies/kinetic-java
SIMULATOR = $(KINETIC_JAVA)/bin/startSimulator.sh
SIMULATOR_POM = $(KINETIC_JAVA)/pom.xml
SIMULATOR_JAR = $(KINETIC_JAVA)/kinetic-simulator/target/kinetic-simulator-0.8.0.5-SNAPSHOT.jar
SIMULATOR_PID = kinetic-simulator.pid

default: test

.PHONY:
submodule_sync:
	git submodule sync
	git submodule update --init

$(SIMULATOR_POM): submodule_sync
$(SIMULATOR): submodule_sync

.PHONY:
build_cache: $(SIMULATOR_JAR)
$(SIMULATOR_JAR): $(SIMULATOR_POM)
	mvn clean package -f $(SIMULATOR_POM)

.PHONY:
start_simulator: $(SIMULATOR_PID)
$(SIMULATOR_PID): $(SIMULATOR) $(SIMULATOR_JAR)
	sh $< & echo "$$!" >$@
.PHONY:
stop_simulator: $(SIMULATOR_PID)
	kill $$(cat $<); rm $<

.PHONY:
run_tests:
	cd ../.. && \
	  mocha --compilers js:babel/register tests/functional/simulTest.js
.PHONY:
test: start_simulator run_tests stop_simulator
+175 −0
Original line number Diff line number Diff line
import assert from 'assert';
import crypto from 'crypto';
import net from 'net';
import util from 'util';

import winston from 'winston';

import kinetic from '../../index';

const HOST = '127.0.0.1';
const PORT = 8123;
const chunk = Buffer.from('CHUNK');
const key = Buffer.from('key');
const startKey = Buffer.from('key');
const endKey = Buffer.from('key5');
const maxRet = 100;

let sequence = 0;
let connectionID = 0;
let clusterVersion = 0;
const newVersion = Buffer.from('1');

const logger = new (winston.Logger)({
    transports: [new (winston.transports.Console)({ level: 'error' })]
});

const requestsArr = [
    ['put', 'PUT_RESPONSE'],
    ['get', 'GET_RESPONSE', {}, { key, }],
    ['put', 'PUT_RESPONSE', { newVersion, }],
    ['getVersion', 'GETVERSION_RESPONSE'],
    ['put', 'PUT_RESPONSE', { key: Buffer.from('key1') }],
    ['put', 'PUT_RESPONSE', { key: Buffer.from('key2') }],
    ['put', 'PUT_RESPONSE', { key: Buffer.from('key3') }],
    ['put', 'PUT_RESPONSE', { key: Buffer.from('key4') }],
    ['put', 'PUT_RESPONSE', { key: Buffer.from('key5') }],
    ['getRange', 'GETKEYRANGE_RESPONSE'],
    ['getNext', 'GETNEXT_RESPONSE',
     { key: Buffer.from('key1') }, { key: Buffer.from('key2') }],
    ['getPrevious', 'GETPREVIOUS_RESPONSE',
     { key: Buffer.from('key1') }, { key: Buffer.from('key') }],
    ['delete', 'DELETE_RESPONSE', { dbVersion: newVersion, }],
    ['noop', 'NOOP_RESPONSE'],
    ['flush', 'FLUSH_RESPONSE'],
    ['getLog', 'GETLOG_RESPONSE'],
    ['setClusterVersion', 'SETUP_RESPONSE'],
    ['setClusterVersionTo0', 'SETUP_RESPONSE'],
];

function requestsLauncher(request, client, optionsA) {
    let pdu;
    let tag;

    const options = optionsA || {};

    switch (request) {
    case 'noop':
        pdu = new kinetic.NoOpPDU(sequence, connectionID, clusterVersion);
        break;
    case 'put':
        tag = crypto
            .createHmac('sha1', 'asdfasdf').update(chunk).digest();
        pdu = new kinetic.PutPDU(
            sequence, connectionID, clusterVersion, options.key || key,
            chunk.length, tag, options);
        break;
    case 'get':
        pdu = new kinetic.GetPDU(
            sequence, connectionID, clusterVersion, key);
        break;
    case 'getVersion':
        pdu = new kinetic.GetVersionPDU(
            sequence, connectionID, clusterVersion, key);
        break;
    case 'getRange':
        pdu = new kinetic.GetKeyRangePDU(sequence, connectionID, clusterVersion,
                                         startKey, endKey, maxRet, options);
        break;
    case 'delete':
        pdu = new kinetic.DeletePDU(
            sequence, connectionID,
            clusterVersion, key, options);
        break;
    case 'flush':
        pdu = new kinetic.FlushPDU(sequence, connectionID, clusterVersion);
        break;
    case 'getLog':
        pdu = new kinetic.GetLogPDU(
            sequence, connectionID, clusterVersion, [0, 1, 2, 4, 5, 6]);
        break;
    case 'setClusterVersion':
        pdu = new kinetic.SetClusterVersionPDU(
            sequence, connectionID, clusterVersion, 1234);
        clusterVersion = 1234;
        break;
    case 'setClusterVersionTo0':
        pdu = new kinetic.SetClusterVersionPDU(
            sequence, connectionID, clusterVersion, 0);
        clusterVersion = 0;
        break;
    case 'getNext':
        pdu = new kinetic.GetNextPDU(
            sequence, connectionID, clusterVersion, options.key || key);
        break;
    case 'getPrevious':
        pdu = new kinetic.GetPreviousPDU(
            sequence, connectionID, clusterVersion, options.key || key);
        break;
    default:
        break;
    }

    client.write(pdu.read());
    if (request === 'put') {
        client.write(chunk);
    }

    sequence++;
}

function checkTest(request, requestResponse, options, optRes, done) {
    const client = net.connect(PORT, HOST);

    client.on('data', function heandleData(data) {
        let pdu;
        let chunkResponse;

        try {
            pdu = new kinetic.PDU(data);
        } catch (e) {
            return done(e);
        }
        logger.info(util.inspect(pdu, {showHidden: false, depth: null}));

        if (pdu.getMessageType() === null ||
            kinetic.getOpName(pdu.getMessageType()) !== requestResponse) {
            connectionID = pdu.getConnectionId();
            clusterVersion = pdu.getClusterVersion();
            requestsLauncher(request, client, options, done);
        } else {
            logger.info(util.inspect(pdu.getProtobuf(),
                {showHidden: false, depth: null}));

            assert.deepEqual(pdu.getStatusCode(),
                kinetic.errors.SUCCESS);
            assert.deepEqual(
                pdu.getMessageType(), kinetic.ops[requestResponse]);

            if (request === 'get'
                || request === 'getNext'
                || request === 'getPrevious') {
                chunkResponse =
                          data.slice(data.length - pdu.getChunkSize());
                assert.deepEqual(chunkResponse, chunk);
                assert.deepEqual(pdu.getKey(), optRes.key);
            }
            client.end();

            done();
        }
    });
}

function checkIntegrity(requestArr) {
    const request = requestArr[0];
    const response = requestArr[1];
    const options = requestArr[2];
    const optRes = requestArr[3];
    describe(`Assess ${request} and its response ${response}`, () => {
        it(`Chunk and ${request} protobufMessage should be preserved`,
           (done) => { checkTest(request, response, options, optRes, done); });
    });
}

requestsArr.forEach(checkIntegrity);

tests/unit/kinetic.js

0 → 100644
+1460 −0

File added.

Preview size limit exceeded, changes collapsed.