Skip to content
This repository was archived by the owner on Sep 14, 2022. It is now read-only.

Commit f0c7e5d

Browse files
committed
Merge pull request #274 from apigee-127/load-test-cli
Expose swagger-test-templates load-testing API
2 parents fac9037 + edfae43 commit f0c7e5d

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

bin/swagger-project.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ app
7474
.option('-f, --test-module <module>', 'one of: ' + testmodules)
7575
.option('-t, --assertion-format <type>', 'one of: ' + assertiontypes)
7676
.option('-o, --force', 'allow overwriting of all existing test files matching those generated')
77+
.option('-l, --load-test [path]', 'generate load-tests for specified operations')
7778
.action(execute(project.generateTest));
7879

7980
app.parse(process.argv);

lib/commands/project/project.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ function testGenerate(directory, options, cb) {
399399
var testModule = options.testModule || TEST_MODULES[0];
400400
var assertionFormat = options.assertionFormat || TEST_ASSERTION_TYPES[0];
401401
var overwriteAll = options.force || false;
402+
var loadTesting = options.loadTest || false;
402403
directory = directory || process.cwd();
403404

404405
findProjectFile(directory, null, function(err, projPath) {
@@ -456,6 +457,17 @@ function testGenerate(directory, options, cb) {
456457
assertionFormat: assertionFormat
457458
};
458459

460+
// pass list of paths targeted for load testing
461+
if (loadTesting) {
462+
if ((typeof loadTesting) !== 'boolean' && fs.existsSync(path.join(directory, loadTesting))) {
463+
config.loadTest = parseJsonFile(directory, loadTesting).loadTargets;
464+
} else if (fs.existsSync(path.join(directory, 'load-config.json'))){
465+
config.loadTest = parseJsonFile(directory, 'load-config.json').loadTargets;
466+
} else {
467+
return cb(new Error('Config file not found. Please specify a load test config or add load-config.json file to your project directory.'));
468+
}
469+
}
470+
459471
var finalResult = template.testGen(result, config);
460472
var existingFiles = fs.readdirSync(path.join(directory, 'test/api/client'));
461473
var skipAll = false;
@@ -528,6 +540,10 @@ function testGenerate(directory, options, cb) {
528540
});
529541
}
530542

543+
function parseJsonFile(directory, filePath) {
544+
return JSON.parse(fs.readFileSync(path.join(directory, filePath)));
545+
}
546+
531547
function installDependencies(directory, message, cb) {
532548
spawn('npm', ['install'], directory, function(err) {
533549
if (err) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"serve-static": "^1.9.2",
3232
"swagger-converter": "^0.1.7",
3333
"swagger-editor": "^2.9.2",
34-
"swagger-test-templates": "^1.1.0",
34+
"swagger-test-templates": "^1.2.0",
3535
"swagger-tools": "^0.9.0"
3636
},
3737
"devDependencies": {

test/commands/project/project.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,62 @@ describe('project', function() {
515515
});
516516
});
517517
});
518+
519+
it ('should create load tests from myLoadTest.json', function(done) {
520+
var loadTargets = {
521+
"loadTargets": [
522+
{
523+
"pathName":"/hello",
524+
"operation": "get",
525+
"load": {
526+
"requests": 1000,
527+
"concurrent": 100
528+
}
529+
}
530+
]
531+
};
532+
533+
fs.writeFileSync(path.join(projPath, 'myLoadTest.json'), JSON.stringify(loadTargets));
534+
535+
var options = {loadTest: './myLoadTest.json', force: true};
536+
537+
project.generateTest(projPath, options, function(err) {
538+
fs.existsSync(path.resolve(projPath, 'test/api/client/test-test.js')).should.be.ok;
539+
fs.existsSync(path.resolve(projPath, 'test/api/client/hello-test.js')).should.be.ok;
540+
fs.readFile(path.resolve(projPath, 'test/api/client/hello-test.js'), {encoding: 'utf8'}, function(err, string) {
541+
string.search('arete').should.be.ok;
542+
done(err);
543+
});
544+
});
545+
});
546+
547+
it ('should create load tests from load-config.json', function(done) {
548+
var loadTargets = {
549+
"loadTargets": [
550+
{
551+
"pathName":"/hello",
552+
"operation": "get",
553+
"load": {
554+
"requests": 1000,
555+
"concurrent": 100
556+
}
557+
}
558+
]
559+
};
560+
561+
fs.writeFileSync(path.join(projPath, 'load-config.json'), JSON.stringify(loadTargets));
562+
563+
var options = {loadTest: true, force: true};
564+
565+
project.generateTest(projPath, options, function(err) {
566+
fs.existsSync(path.resolve(projPath, 'test/api/client/test-test.js')).should.be.ok;
567+
fs.existsSync(path.resolve(projPath, 'test/api/client/hello-test.js')).should.be.ok;
568+
fs.readFile(path.resolve(projPath, 'test/api/client/hello-test.js'), {encoding: 'utf8'}, function(err, string) {
569+
string.search('arete').should.be.ok;
570+
done(err);
571+
});
572+
});
573+
});
518574
});
519575

520576
});

0 commit comments

Comments
 (0)