diff --git a/package.json b/package.json index 5e383b5..0fd99cd 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "dependencies": { "amqplib": "^0.5.2", + "chai-as-promised": "^7.1.1", "newrelic": "^6.5.0", "raven": "^2.6.4", "shelljs": "^0.8.1" @@ -40,8 +41,8 @@ "prestart": "npm run build", "start": "NODE_PATH=dist ./wait-for-it.sh ${AMQP_HOST}:${AMQP_PORT} -- node dist/taskmaster.js", "dev": "NODE_PATH=dist nodemon dist/taskmaster.js", - "test": "docker build . -t codingblocks/judge-taskmaster && NODE_PATH=src mocha --timeout 10000 --exit --require ts-node/register test/**/*.ts", - "test.parallel": "NODE_PATH=src mocha-parallel-tests --timeout 10000 --exit --require ts-node/register test/**/*.ts", + "test": "docker build . -t codingblocks/judge-taskmaster && NODE_PATH=src mocha --timeout 100000 --exit --require ts-node/register test/**/*.ts", + "test.parallel": "NODE_PATH=src mocha-parallel-tests --timeout 100000 --exit --require ts-node/register test/**/*.ts", "cover": "nyc npm test" }, "engines": { diff --git a/src/tasks/index.ts b/src/tasks/index.ts index 7e1744c..886f0b3 100644 --- a/src/tasks/index.ts +++ b/src/tasks/index.ts @@ -29,7 +29,7 @@ export async function execute (job: Job) { } // Setup RUNBOX - await scenario.setup(currentJobDir, job) + await scenario.setup(currentJobDir, job) // Run worker await scenario.run(currentJobDir, job) diff --git a/test/project/project.nodejs.spec.ts b/test/project/project.nodejs.spec.ts new file mode 100644 index 0000000..8ad2ebf --- /dev/null +++ b/test/project/project.nodejs.spec.ts @@ -0,0 +1,92 @@ +import { execute } from '../../src/tasks/' +import { ProjectJob } from '../../src/tasks/jobs/project' + +const chai = require('chai'); +const chaiAsPromised = require('chai-as-promised'); +const {expect} = chai +chai.use(chaiAsPromised) + +describe('project-nodejs', () => { + it('should run nodejs project correctly', async () => { + const projectResult = await execute(new ProjectJob({ + id: 26, + source: 'https://minio.cb.lk/public/problem.zip', + problem: 'https://minio.cb.lk/public/solution.zip', + submissionDirs: 'src/*', + lang: 'nodejs', + timelimit: 20, + scenario: 'project' + })) + + expect(projectResult).to.have.keys( + 'id', + 'stderr', + 'stdout', + 'code', + 'time', + 'score' + ) + expect(projectResult.code).to.equal(0) + expect(projectResult.score).to.equal(100) + }) + + it('should return code = 25 when test directory is modified', async () => { + // changing submissionDirs to mock that :P + const projectResult = await execute(new ProjectJob({ + id: 23, + source: 'https://minio.cb.lk/public/problem.zip', + problem: 'https://minio.cb.lk/public/solution.zip', + submissionDirs: 'test/*', + lang: 'nodejs', + timelimit: 20, + scenario: 'project' + })) + expect(projectResult).to.have.keys( + 'id', + 'stderr', + 'stdout', + 'code', + 'time', + 'score' + ) + expect(projectResult.code).to.equal(25) + expect(projectResult.score).to.equal(0) + }) + + it('should throw error when url is invalid', async () => { + const job = new ProjectJob({ + id: 25, + source: 'https://www.invalidurl.com', + problem: 'https://www.invalidurl.com', + submissionDirs: 'src/*', + lang: 'nodejs', + timelimit: 20, + scenario: 'project' + }) + + await expect(execute(job)).to.be.rejected; + }) + + it('should return code = 1 when there is a build error', async () => { + // file downloaded from google.com will not be a nodejs project, hence build error + const projectResult = await execute(new ProjectJob({ + id: 25, + source: 'https://www.google.com', + problem: 'https://www.google.com', + submissionDirs: 'src/*', + lang: 'nodejs', + timelimit: 20, + scenario: 'project' + })) + expect(projectResult).to.have.keys( + 'id', + 'stderr', + 'stdout', + 'code', + 'time', + 'score' + ) + expect(projectResult.code).to.equal(1) + expect(projectResult.score).to.equal(0) + }) +}) \ No newline at end of file diff --git a/test/scenarios/projectScenario.spec.ts b/test/scenarios/projectScenario.spec.ts index 92a8fb3..228b53f 100644 --- a/test/scenarios/projectScenario.spec.ts +++ b/test/scenarios/projectScenario.spec.ts @@ -1,16 +1,32 @@ -import { expect } from 'chai' +import { assert } from 'chai' import { ProjectJob } from '../../src/tasks/jobs/project'; -import ProjectScenarion from '../../src/tasks/scenarios/project' +import config = require('../../config.js') +import ProjectScenario from '../../src/tasks/scenarios/project' +import {mkdir, rm} from "shelljs"; +import * as path from "path"; +import * as fs from 'fs' + describe('Project Scenario', () => { it('should setup', async () => { - // const job: ProjectJob = { - // id: 1, - // source: '', - // lang: 'nodejs', - // timelimit: 20, - // scenario: 'problem', - // problem: '' - // } + const job: ProjectJob = { + id: 1, + source: 'https://minio.cb.lk/public/problem.zip', + problem: 'https://minio.cb.lk/public/solution.zip', + submissionDirs: 'src/*', + lang: 'nodejs', + timelimit: 20, + scenario: 'problem' + } + + rm('-rf', config.RUNBOX.DIR) + mkdir('-p', config.RUNBOX.DIR) + const currentJobDir = path.join(config.RUNBOX.DIR, job.id.toString()) + mkdir('-p', currentJobDir) + + await (new ProjectScenario()).setup(currentJobDir, job) + + assert.isOk(fs.existsSync(path.join(currentJobDir, 'problem'))) + assert.isOk(fs.existsSync(path.join(currentJobDir, 'solution'))); }) })