diff --git a/cli.js b/cli.js index e773e59..cf58233 100755 --- a/cli.js +++ b/cli.js @@ -119,7 +119,7 @@ if(program.ls){ list.all(); } else{ - ps.prs(program.ls); + list.prs(program.ls); } } } diff --git a/lib/list.js b/lib/list.js index b5d21f4..07ae8b1 100644 --- a/lib/list.js +++ b/lib/list.js @@ -17,7 +17,7 @@ module.exports = { }, execution : (keyMap)=>{ var name = keyMap.name; - var cmd = "docker service ls -f 'name="+name+"'|grep -w '"+name+"'"; + var cmd = "docker service ls -f 'name="+name+"'"; chp.exec(cmd,(e, stdout, stderr)=> { if(stdout){ console.log(stdout); diff --git a/test/test-list.js b/test/test-list.js new file mode 100644 index 0000000..b26ffd9 --- /dev/null +++ b/test/test-list.js @@ -0,0 +1,53 @@ +'use strict'; + +var should = require('should'); +var chp = require('child_process'); +var list = require('../lib/list'); + +describe('List Command', function() { + describe('Service filtering', function() { + it('should generate correct docker command for specific service', function(done) { + // Mock child_process.exec to capture the command + var originalExec = chp.exec; + var capturedCommand = ''; + + chp.exec = function(cmd, callback) { + capturedCommand = cmd; + // Simulate successful execution + callback(null, 'ID NAME MODE REPLICAS IMAGE\n', ''); + }; + + // Test the prs function + list.prs('webapp'); + + // Restore original exec + chp.exec = originalExec; + + // Verify the command is correct + capturedCommand.should.equal("docker service ls -f 'name=webapp'"); + done(); + }); + + it('should generate correct docker command for all services', function(done) { + // Mock child_process.exec to capture the command + var originalExec = chp.exec; + var capturedCommand = ''; + + chp.exec = function(cmd, callback) { + capturedCommand = cmd; + // Simulate successful execution + callback(null, 'ID NAME MODE REPLICAS IMAGE\n', ''); + }; + + // Test the all function + list.all(); + + // Restore original exec + chp.exec = originalExec; + + // Verify the command is correct + capturedCommand.should.equal("docker service ls"); + done(); + }); + }); +}); \ No newline at end of file