From 4c4c948b05788a665011f05c57a3c230fc3a63e5 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 20 Jan 2015 13:24:42 +0100 Subject: [PATCH] Some basic unit tests for the tuskar.boxes js module Change-Id: I6b15ec7d818ad7d314954674641983a6c5ff8033 --- .../static/tuskar_boxes/js/tuskar.boxes.js | 122 +++++++++--------- .../static/tuskar_boxes/tests/tuskar.boxes.js | 55 +++++++- .../templates/tuskar_boxes/qunit.html | 52 ++++++++ 3 files changed, 166 insertions(+), 63 deletions(-) diff --git a/tuskar_boxes/static/tuskar_boxes/js/tuskar.boxes.js b/tuskar_boxes/static/tuskar_boxes/js/tuskar.boxes.js index 5b8fc52..037fe6e 100644 --- a/tuskar_boxes/static/tuskar_boxes/js/tuskar.boxes.js +++ b/tuskar_boxes/static/tuskar_boxes/js/tuskar.boxes.js @@ -3,67 +3,67 @@ tuskar.boxes = (function () { var module = {}; + + module.get_role_counts = function ($flavor) { + var roles = {}; + $flavor.find('div.boxes-drop-roles div.boxes-role').each(function () { + var $this = $(this); + var name = $this.data('name'); + var count = +$this.find('input.number-picker').val(); + roles[name] = count; + }); + return roles; + }; + + module.update_maximums = function ($flavor, roles, nodes) { + var used = 0; + $.each(roles, function (key, value) { used += value; }); + $flavor.find('div.boxes-drop-roles div.boxes-role').each(function () { + var $this = $(this); + var role = $this.data('name'); + var $picker = $this.find('input.number-picker'); + $picker.attr('max', Math.max(0, nodes - used + roles[role])); + }); + }; + + module.update_nodes = function ($flavor, roles) { + var role_names = Object.getOwnPropertyNames(roles); + var count = 0; + var role = 0; + $flavor.find('div.boxes-nodes div.boxes-node').each(function () { + var $this = $(this); + $this.removeClass('boxes-role-controller boxes-role-compute boxes-role-cinder-storage boxes-role-swift-storage boxes-role-none'); + while (count >= roles[role_names[role]]) { + role += 1; + count = 0; + } + if (!role_names[role]) { + $(this).html('free'); + $(this).addClass('boxes-role-none'); + } else { + $this.addClass('boxes-role-' + role_names[role]).html(' '); + } + count += 1; + }); + var free_nodes = $flavor.find('div.boxes-nodes div.boxes-role-none').length; + $flavor.find('span.free-nodes').text(free_nodes); + }; + + module.update_boxes = function () { + $('div.boxes-flavor').each(function () { + var $flavor = $(this); + var role_counts = module.get_role_counts($flavor); + var nodes_count = $flavor.find('div.boxes-nodes div.boxes-node').length; + module.update_nodes($flavor, role_counts); + module.update_maximums($flavor, role_counts, nodes_count); + }); + }; + module.init = function () { if ($('div.boxes-available-roles').length === 0) { // Only activate on a page that has the right classes. return; } - - function get_role_counts($flavor) { - var roles = {}; - $flavor.find('div.boxes-drop-roles div.boxes-role').each(function () { - var $this = $(this); - var name = $this.data('name'); - var count = +$this.find('input.number-picker').val(); - roles[name] = count; - }); - return roles; - } - - function update_maximums($flavor, roles) { - var nodes = $flavor.find('div.boxes-nodes div.boxes-node').length; - var used = 0; - $.each(roles, function (key, value) { used += value; }); - $flavor.find('div.boxes-drop-roles div.boxes-role').each(function () { - var $this = $(this); - var role = $this.data('name'); - var $picker = $this.find('input.number-picker'); - $picker.attr('max', Math.max(0, nodes - used + roles[role])); - }); - } - - function update_nodes($flavor, roles) { - var role_names = Object.getOwnPropertyNames(roles); - var count = 0; - var role = 0; - $flavor.find('div.boxes-nodes div.boxes-node').each(function () { - var $this = $(this); - $this.removeClass('boxes-role-controller boxes-role-compute boxes-role-cinder-storage boxes-role-swift-storage boxes-role-none'); - while (count >= roles[role_names[role]]) { - role += 1; - count = 0; - } - if (!role_names[role]) { - $(this).html('free'); - $(this).addClass('boxes-role-none'); - } else { - $this.addClass('boxes-role-' + role_names[role]).html(' '); - } - count += 1; - }); - var free_nodes = $flavor.find('div.boxes-nodes div.boxes-role-none').length; - $flavor.find('span.free-nodes').text(free_nodes); - } - - function update_boxes() { - $('div.boxes-flavor').each(function () { - var $flavor = $(this); - var roles = get_role_counts($flavor); - update_nodes($flavor, roles); - update_maximums($flavor, roles); - }); - } - $('div.boxes-role').draggable({ revert: 'invalid', helper: 'clone', @@ -82,7 +82,7 @@ tuskar.boxes = (function () { ui.draggable.find('input.boxes-flavor' ).val($(this).closest('.boxes-flavor').data('flavor')); $count.trigger('change'); - window.setTimeout(update_boxes, 0); + window.setTimeout(module.update_boxes, 0); } }); $('div.boxes-available-roles').droppable({ @@ -94,12 +94,12 @@ tuskar.boxes = (function () { ui.draggable.appendTo(this); ui.draggable.find('input.boxes-flavor').val(''); ui.draggable.find('input.number-picker').trigger('change').val(0); - window.setTimeout(update_boxes, 0); + window.setTimeout(module.update_boxes, 0); } }); - update_boxes(); - $('input.number-picker').change(update_boxes); + module.update_boxes(); + $('input.number-picker').change(module.update_boxes); $('.boxes-roles-menu li a').click(function () { var name = $(this).data('role'); @@ -111,7 +111,7 @@ tuskar.boxes = (function () { if (+$count.val() < 1) { $count.val(1); } $flavor.val($drop.closest('.boxes-flavor').data('flavor')); $count.trigger('change'); - window.setTimeout(update_boxes, 0); + window.setTimeout(module.update_boxes, 0); }); }; diff --git a/tuskar_boxes/static/tuskar_boxes/tests/tuskar.boxes.js b/tuskar_boxes/static/tuskar_boxes/tests/tuskar.boxes.js index 48f838d..8400731 100644 --- a/tuskar_boxes/static/tuskar_boxes/tests/tuskar.boxes.js +++ b/tuskar_boxes/static/tuskar_boxes/tests/tuskar.boxes.js @@ -3,7 +3,58 @@ horizon.addInitFunction(function () { module("Tuskar boxes (tuskar.boxes.js)"); - test("Passing test", function () { - equal(1, 1); + test("get_role_counts", function () { + var $flavor = $($('div.boxes-flavor')[0]); + var role_counts = tuskar.boxes.get_role_counts($flavor); + + deepEqual(role_counts, { + "cinder-storage": 0, + "compute": 1, + "controller": 1, + "swift-storage": 0 + }); + }); + + test("update_maximums", function () { + var $flavor = $($('div.boxes-flavor')[0]); + var role_counts = { + "cinder-storage": 0, + "compute": 2, + "controller": 1, + "swift-storage": 0 + }; + + tuskar.boxes.update_maximums($flavor, role_counts, 4); + equal($('div.boxes-role-cinder-storage input.number-picker').attr('max'), 1); + equal($('div.boxes-role-compute input.number-picker').attr('max'), 3); + equal($('div.boxes-role-controller input.number-picker').attr('max'), 2); + equal($('div.boxes-role-swift-storage input.number-picker').attr('max'), 1); + tuskar.boxes.update_maximums($flavor, role_counts, 2); + equal($('div.boxes-role-cinder-storage input.number-picker').attr('max'), 0); + equal($('div.boxes-role-compute input.number-picker').attr('max'), 1); + equal($('div.boxes-role-controller input.number-picker').attr('max'), 0); + equal($('div.boxes-role-swift-storage input.number-picker').attr('max'), 0); + }); + + test("update_nodes", function() { + var $flavor = $($('div.boxes-flavor')[0]); + var role_counts = { + "cinder-storage": 0, + "compute": 2, + "controller": 1, + "swift-storage": 0 + }; + + equal($('div.boxes-nodes div.boxes-role-').length, 2); + equal($('div.boxes-nodes div.boxes-role-cinder-storage').length, 0); + equal($('div.boxes-nodes div.boxes-role-compute').length, 1); + equal($('div.boxes-nodes div.boxes-role-controller').length, 1); + equal($('div.boxes-nodes div.boxes-role-swift-storage').length, 0); + tuskar.boxes.update_nodes($flavor, role_counts); + equal($('div.boxes-nodes div.boxes-role-none').length, 1); + equal($('div.boxes-nodes div.boxes-role-cinder-storage').length, 0); + equal($('div.boxes-nodes div.boxes-role-compute').length, 2); + equal($('div.boxes-nodes div.boxes-role-controller').length, 1); + equal($('div.boxes-nodes div.boxes-role-swift-storage').length, 0); }); }); diff --git a/tuskar_boxes/templates/tuskar_boxes/qunit.html b/tuskar_boxes/templates/tuskar_boxes/qunit.html index 426e4ff..6a5b4b6 100644 --- a/tuskar_boxes/templates/tuskar_boxes/qunit.html +++ b/tuskar_boxes/templates/tuskar_boxes/qunit.html @@ -22,5 +22,57 @@

    + +
    + +
    +
    +
    Flavor: baremetal x86_64, 1 CPU, 4096MB RAM, 30GB Disk
    +
    Free Nodes: / 4
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    Controller
    +
    +
    +
    +
    +
    +
    +
    Swift-Storage
    +
    +
    +
    +
    +
    +
    +
    Compute
    +
    +
    +
    +
    +
    +
    +
    Cinder-Storage
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    +
    +