From 08fb123f8b65dbe60cbcdda877dc9fa64c1725ad Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Fri, 16 Oct 2015 09:05:30 -0600 Subject: [PATCH] Improving JS unit test branch coverage This patch is testing untested branches. There is a bug in the Launch Instance model where it is supposed to remove null properties from the top level of the model. It is not doing that properly. So that bug is also fixed. This patch currently raises the branch coverage in openstack_dashboard from ~84% to ~89%. Closes-bug: 1506891 Change-Id: I7434126246b15209b62daeff75744e0a3b435494 --- .../launch-instance-model.service.js | 2 +- .../launch-instance-model.service.spec.js | 79 +++++++++++++++++++ openstack_dashboard/karma.conf.js | 2 +- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.js b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.js index c9f026b911..5f53a7467c 100644 --- a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.js +++ b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.js @@ -237,7 +237,7 @@ function createInstance() { var finalSpec = angular.copy(model.newInstanceSpec); - cleanNullProperties(); + cleanNullProperties(finalSpec); setFinalSpecBootsource(finalSpec); setFinalSpecFlavor(finalSpec); diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.spec.js b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.spec.js index 7bd4d9c39d..b90b7c7793 100644 --- a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.spec.js +++ b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-model.service.spec.js @@ -438,6 +438,85 @@ var finalSpec = model.createInstance(); expect(finalSpec.flavor_id).toBeUndefined(); }); + + it('should handle source type of "volume"', function() { + model.newInstanceSpec.source_type.type = 'volume'; + model.newInstanceSpec.source[0].id = 'imAnID'; + model.newInstanceSpec.vol_delete_on_terminate = 'yep'; + + var finalSpec = model.createInstance(); + expect(finalSpec.block_device_mapping.volTestName) + .toBe('imAnID:vol::yep'); + expect(finalSpec.source_id).toBe(''); + }); + + it('should handle source type of "snapshot"', function() { + model.newInstanceSpec.source_type.type = 'snapshot'; + model.newInstanceSpec.source[0].id = 'imAnID'; + + var finalSpec = model.createInstance(); + expect(finalSpec.source_id).toBe('imAnID'); + }); + + it('should handle source type of "volume_snapshot"', function() { + model.newInstanceSpec.source_type.type = 'volume_snapshot'; + model.newInstanceSpec.source[0].id = 'imAnID'; + model.newInstanceSpec.vol_delete_on_terminate = 'yep'; + + var finalSpec = model.createInstance(); + expect(finalSpec.block_device_mapping.volTestName) + .toBe('imAnID:snap::yep'); + expect(finalSpec.source_id).toBe(''); + }); + + it('should process source_id if unknown type', function() { + model.newInstanceSpec.source_type.type = 'unknown'; + model.newInstanceSpec.source[0].id = 'imAnID'; + + var finalSpec = model.createInstance(); + expect(finalSpec.source_id).toBe('imAnID'); + }); + + it('should not create block device mappings if not creating a volume', function() { + model.newInstanceSpec.source_type.type = 'image'; + model.newInstanceSpec.vol_create = false; + + var finalSpec = model.createInstance(); + expect(finalSpec.block_device_mapping_v2).toBeUndefined(); + }); + + it('sets a null key name & removes keypair if no key pair presented', function() { + model.newInstanceSpec.key_pair = []; + + var finalSpec = model.createInstance(); + expect(finalSpec.key_name).toBeNull(); + expect(finalSpec.key_pair).toBeUndefined(); + }); + + it('leaves the key name and removes keypair property if no key pair presented', function() { + model.newInstanceSpec.key_pair = []; + model.newInstanceSpec.key_name = 'Jerry'; + + var finalSpec = model.createInstance(); + expect(finalSpec.key_name).toBe('Jerry'); + expect(finalSpec.key_pair).toBeUndefined(); + }); + + it('stips null properties', function() { + model.newInstanceSpec.useless = null; + + var finalSpec = model.createInstance(); + expect(finalSpec.useless).toBeUndefined(); + }); + + it('provides null for device_name when falsy', function() { + model.newInstanceSpec.source_type.type = 'image'; + model.newInstanceSpec.vol_device_name = false; + model.newInstanceSpec.vol_create = true; + + var finalSpec = model.createInstance(); + expect(finalSpec.block_device_mapping_v2[0].device_name).toBeNull(); + }); }); }); }); diff --git a/openstack_dashboard/karma.conf.js b/openstack_dashboard/karma.conf.js index 3c9e6ba4bd..8e266d07fb 100644 --- a/openstack_dashboard/karma.conf.js +++ b/openstack_dashboard/karma.conf.js @@ -162,7 +162,7 @@ module.exports = function (config) { // Coverage threshold values. thresholdReporter: { statements: 90, // target 100 - branches: 84, // target 100 + branches: 89, // target 100 functions: 89, // target 100 lines: 90 // target 100 }