You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by yu...@apache.org on 2013/05/29 04:01:12 UTC

svn commit: r1487197 - in /incubator/ambari/trunk: CHANGES.txt ambari-web/app/assets/test/tests.js ambari-web/app/utils/date.js ambari-web/app/utils/helper.js ambari-web/test/utils/date_test.js ambari-web/test/utils/host_progress_popup_test.js

Author: yusaku
Date: Wed May 29 02:01:12 2013
New Revision: 1487197

URL: http://svn.apache.org/r1487197
Log:
AMBARI-2207. Add unit tests for Utils. (yusaku)

Added:
    incubator/ambari/trunk/ambari-web/test/utils/date_test.js
    incubator/ambari/trunk/ambari-web/test/utils/host_progress_popup_test.js
Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/ambari-web/app/assets/test/tests.js
    incubator/ambari/trunk/ambari-web/app/utils/date.js
    incubator/ambari/trunk/ambari-web/app/utils/helper.js

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1487197&r1=1487196&r2=1487197&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Wed May 29 02:01:12 2013
@@ -309,6 +309,8 @@ Trunk (unreleased changes):
 
  IMPROVEMENTS
 
+ AMBARI-2207. Add unit tests for Utils. (yusaku)
+
  AMBARI-2206. Add unit tests for wizard steps 2, 5, and 6. (yusaku)
 
  AMBARI-2194. Hadoop2 Installer: MapReduce2 shows only advanced section. 

Modified: incubator/ambari/trunk/ambari-web/app/assets/test/tests.js
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-web/app/assets/test/tests.js?rev=1487197&r1=1487196&r2=1487197&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-web/app/assets/test/tests.js (original)
+++ incubator/ambari/trunk/ambari-web/app/assets/test/tests.js Wed May 29 02:01:12 2013
@@ -36,9 +36,11 @@ require('test/main/dashboard_test');
 require('test/main/host/details_test');
 require('test/main/host_test');
 require('test/main/item_test');
+require('test/utils/config_test');
+require('test/utils/date_test');
 require('test/utils/form_field_test');
+require('test/utils/host_progress_popup_test');
 require('test/utils/misc_test');
 require('test/utils/validator_test');
-require('test/utils/config_test');
 require('test/utils/string_utils_test');
-require('test/views/common/chart/linear_time_test');
\ No newline at end of file
+require('test/views/common/chart/linear_time_test');

Modified: incubator/ambari/trunk/ambari-web/app/utils/date.js
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-web/app/utils/date.js?rev=1487197&r1=1487196&r2=1487197&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-web/app/utils/date.js (original)
+++ incubator/ambari/trunk/ambari-web/app/utils/date.js Wed May 29 02:01:12 2013
@@ -38,24 +38,6 @@ module.exports = {
     return days[date.getDay()] + ', ' + months[date.getMonth()] + ' ' + this.dateFormatZeroFirst(date.getDate()) + ', ' + date.getFullYear() + ' ' + this.dateFormatZeroFirst(date.getHours()) + ':' + this.dateFormatZeroFirst(date.getMinutes());
   },
   /**
-   * Convert date-string 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES' to timestamp
-   * @param date_string
-   * @return {String}
-   */
-  dateUnformat: function(date_string) {
-    var date = date_string.substring(4);
-    var month = date.substring(1, 4);
-    var day = date.substring(5, 7);
-    var year = date.substring(9, 13);
-    var hours = date.substring(14, 16);
-    var minutes = date.substring(17, 19);
-
-    var months = this.dateMonths;
-    month = months.indexOf(month) + 1;
-    if (month < 10) month = '0' + month;
-    return year + month + day + hours + minutes;
-  },
-  /**
    * Convert timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR'
    * @param timestamp
    * @return {*}
@@ -71,60 +53,6 @@ module.exports = {
     return date.toDateString();
   },
   /**
-   * Convert date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR' to the timestamp
-   * @param date_string
-   * @return {Number}
-   */
-  dateUnformatShort: function(date_string) {
-    var date = new Date(date_string);
-    return date.getTime();
-  },
-  /**
-   * Convert time in mseconds to 'HOURS:MINUTES:SECONDS'
-   * @param ms_interval
-   * @return string formatted date
-   */
-  dateFormatInterval:function (ms_interval) {
-    if (!validator.isValidInt(ms_interval)) return ms_interval;
-    var hours = Math.floor(ms_interval / (60 * 60000));
-    var divisor_for_minutes = ms_interval % (60 * 60000);
-    var minutes = Math.floor(divisor_for_minutes / 60000);
-    var divisor_for_seconds = divisor_for_minutes % 60000;
-    var seconds = (divisor_for_seconds / 1000).toFixed(2);
-
-    return (hours < 10 ? '0' : '') + hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
-  },
-  /**
-   * Convert 'HOURS:MINUTES:SECONDS' to time in mseconds
-   * @param formattedDate date string
-   * @return time in mseconds
-   */
-  dateUnformatInterval: function(formattedDate) {
-    var formattedDateArray = formattedDate.split(' ');
-
-    if (Object.prototype.toString.call( formattedDateArray ) === '[object Array]' && formattedDateArray.length == 2) {
-      var oneMinMs = 60000;
-      var oneHourMs = 3600000;
-      var oneDayMs = 86400000;
-
-      if (formattedDateArray['1'] == 'ms') {
-        return formattedDateArray['0'];
-      } else if (formattedDateArray['1'] == 'secs') {
-        return formattedDateArray['0'] * 1000;
-      } else if (formattedDateArray['1'] == 'mins') {
-        return formattedDateArray['0'] * oneMinMs;
-      } else if (formattedDateArray['1'] == 'hours') {
-        return formattedDateArray['0'] * oneHourMs;
-      } else if (formattedDateArray['1'] == 'days') {
-        return formattedDateArray['0'] * oneDayMs;
-      } else {
-        console.warn('function dateUnformatInterval: Undefined format');
-      }
-    } else {
-      console.warn('function dateUnformatInterval: formattedDateArray');
-    }
-  },
-  /**
    * Convert time in mseconds to
    * 30 ms = 30 ms
    * 300 ms = 300 ms
@@ -141,6 +69,7 @@ module.exports = {
    */
   timingFormat:function (time) {
     var intTime  = parseInt(time);
+    if (!intTime) return null;
     var timeStr = intTime.toString();
     var lengthOfNumber = timeStr.length;
     var oneMinMs = 60000;

Modified: incubator/ambari/trunk/ambari-web/app/utils/helper.js
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-web/app/utils/helper.js?rev=1487197&r1=1487196&r2=1487197&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-web/app/utils/helper.js (original)
+++ incubator/ambari/trunk/ambari-web/app/utils/helper.js Wed May 29 02:01:12 2013
@@ -203,6 +203,8 @@ Number.prototype.long2ip = function () {
  * @param {String} testUrl  URL to be used if app is not in test mode (!App.testMode)
  * @return {String} Formatted URL
  */
+App = require('app');
+
 App.formatUrl = function (urlTemplate, substitutes, testUrl) {
   var formatted = urlTemplate;
   if (urlTemplate) {
@@ -361,12 +363,4 @@ App.format = {
   taskStatus:function (_taskStatus) {
     return _taskStatus.toLowerCase();
   }
-};
-
-Array.prototype.removeAll = function(array){
-  var temp = array;
-  for(var i = 0 ; i < array.length ; i++ ){
-    temp = temp.without(array[i]);
-  }
-  return temp;
-};
+};
\ No newline at end of file

Added: incubator/ambari/trunk/ambari-web/test/utils/date_test.js
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-web/test/utils/date_test.js?rev=1487197&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-web/test/utils/date_test.js (added)
+++ incubator/ambari/trunk/ambari-web/test/utils/date_test.js Wed May 29 02:01:12 2013
@@ -0,0 +1,114 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var Ember = require('ember');
+var App = require('app');
+
+var validator = require('utils/validator');
+var date = require('utils/date');
+
+require('views/common/modal_popup');
+require('controllers/global/background_operations_controller');
+require('utils/helper');
+require('utils/host_progress_popup');
+
+describe('date', function () {
+
+  var correct_tests = [
+    {t: 1349752195000, e: 'Tue, Oct 09, 2012 07:09', e2: 'Tue Oct 09 2012'},
+    {t: 1367752195000, e: 'Sun, May 05, 2013 15:09', e2: 'Sun May 05 2013'},
+    {t: 1369952195000, e: 'Fri, May 31, 2013 02:16', e2: 'Fri May 31 2013'},
+    {t: 1369052195000, e: 'Mon, May 20, 2013 16:16', e2: 'Mon May 20 2013'},
+    {t: 1369792195000, e: 'Wed, May 29, 2013 05:49', e2: 'Wed May 29 2013'},
+    {t: 1389752195000, e: 'Wed, Jan 15, 2014 05:16', e2: 'Wed Jan 15 2014'}
+  ];
+
+  var incorrect_tests = [
+    {t: null},
+    {t: ''},
+    {t: false},
+    {t: []},
+    {t: {}},
+    {t: undefined},
+    {t: function(){}}
+  ];
+
+  describe('#dateFormat', function() {
+    it('Correct timestamps', function(){
+      correct_tests.forEach(function(test) {
+        expect(date.dateFormat(test.t)).to.equal(test.e);
+      });
+    });
+    it('Incorrect timestamps', function() {
+      incorrect_tests.forEach(function(test) {
+        expect(date.dateFormat(test.t)).to.equal(test.t);
+      });
+    });
+  });
+
+  describe('#dateFormatShort', function() {
+    it('Correct timestamps', function(){
+      correct_tests.forEach(function(test) {
+        expect(date.dateFormatShort(test.t)).to.equal(test.e2);
+      });
+    });
+    it('Today timestamp', function() {
+      var now = new Date();
+      var then = new Date(now.getFullYear(),now.getMonth(),now.getDate(),0,0,0);
+      expect(date.dateFormatShort(then.getTime() + 10*3600*1000)).to.equal('Today 10:00:00');
+    });
+    it('Incorrect timestamps', function() {
+      incorrect_tests.forEach(function(test) {
+        expect(date.dateFormatShort(test.t)).to.equal(test.t);
+      });
+    });
+  });
+
+  describe('#timingFormat', function() {
+    var tests = [
+      {i: '30', e:'30 ms'},
+      {i: '300', e:'300 ms'},
+      {i: '999', e:'999 ms'},
+      {i: '1000', e:'1.00 secs'},
+      {i: '3000', e:'3.00 secs'},
+      {i: '35000', e:'35.00 secs'},
+      {i: '350000', e:'350.00 secs'},
+      {i: '999999', e:'1000.00 secs'},
+      {i: '1000000', e:'16.67 mins'},
+      {i: '3500000', e:'58.33 mins'},
+      {i: '35000000', e:'9.72 hours'},
+      {i: '350000000', e:'4.05 days'},
+      {i: '3500000000', e:'40.51 days'},
+      {i: '35000000000', e:'405.09 days'}
+    ];
+
+    it('Correct data', function(){
+      tests.forEach(function(test) {
+        expect(date.timingFormat(test.i)).to.equal(test.e);
+      });
+    });
+
+    it('Incorrect data', function(){
+      incorrect_tests.forEach(function(test) {
+        expect(date.timingFormat(test.t)).to.equal(null);
+      });
+    });
+
+  });
+
+});
\ No newline at end of file

Added: incubator/ambari/trunk/ambari-web/test/utils/host_progress_popup_test.js
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-web/test/utils/host_progress_popup_test.js?rev=1487197&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-web/test/utils/host_progress_popup_test.js (added)
+++ incubator/ambari/trunk/ambari-web/test/utils/host_progress_popup_test.js Wed May 29 02:01:12 2013
@@ -0,0 +1,268 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var Ember = require('ember');
+var App = require('app');
+
+require('views/common/modal_popup');
+require('controllers/global/background_operations_controller');
+require('utils/helper');
+require('utils/host_progress_popup');
+
+describe('App.HostPopup', function () {
+
+  var services = [
+    {
+      displayName: "Start service WebHCat",
+      hosts: [
+        {
+          logTasks: [
+            {
+              Tasks: {
+                command: "START",
+                host_name: "ip-10-12-123-90.ec2.internal",
+                role: "WEBHCAT_SERVER",
+                status: "QUEUED"
+              },
+              href: "http://ec2-54-224-233-43.compute-1.amazonaws.com:8080/api/v1/clusters/mycluster/requests/23/tasks/94"
+            }
+          ],
+          name: "ip-10-12-123-90.ec2.internal",
+          publicName: "ip-10-12-123-90.ec2.internal",
+          serviceName: "Start service WebHCat"
+        }
+      ],
+      isRunning: false
+    },
+    {
+      displayName: "Start service Hive/HCat",
+      hosts: [
+        {
+          logTasks: [
+            {
+              Tasks: {
+                command: "INSTALL",
+                host_name: "ip-10-12-123-90.ec2.internal",
+                status: "COMPLETED"
+              },
+              href: "http://ec2-54-224-233-43.compute-1.amazonaws.com:8080/api/v1/clusters/mycluster/requests/15/tasks/76"
+            }
+          ],
+          name: "ip-10-12-123-90.ec2.internal",
+          publicName: "ip-10-12-123-90.ec2.internal",
+          serviceName: "Start service Hive/HCat"
+        },
+        {
+          logTasks: [
+            {
+              Tasks: {
+                command: "START",
+                host_name: "ip-10-33-7-23.ec2.internal",
+                status: "COMPLETED"
+              },
+              href: "http://ec2-54-224-233-43.compute-1.amazonaws.com:8080/api/v1/clusters/mycluster/requests/15/tasks/78"
+            },
+            {
+              Tasks: {
+                command: "START",
+                host_name: "ip-10-33-7-23.ec2.internal",
+                status: "COMPLETED"
+              },
+              href: "http://ec2-54-224-233-43.compute-1.amazonaws.com:8080/api/v1/clusters/mycluster/requests/15/tasks/79"
+            }
+          ],
+          name: "ip-10-33-7-23.ec2.internal",
+          publicName: "ip-10-33-7-23.ec2.internal",
+          serviceName: "Start service Hive/HCat"
+        }
+      ],
+      isRunning: false
+    }
+  ];
+
+  var bgController = App.BackgroundOperationsController.create();
+  bgController.set('services', services);
+
+  describe('#initPopup', function() {
+    App.HostPopup.initPopup("", bgController, true);
+    it('services loaded', function() {
+      expect(App.HostPopup.get('inputData').length).to.equal(services.length);
+    });
+  });
+
+  var test_tasks = [
+    {
+      t: [
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 2
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 3
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 1
+          }
+        }
+      ],
+      m: 'All COMPLETED',
+      r: 'SUCCESS',
+      p: 100,
+      ids: [1,2,3]
+    },
+    {
+      t: [
+        {
+          Tasks: {
+            status: 'FAILED',
+            id: 2
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 1
+          }
+        }
+        ,
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 3
+          }
+        }
+      ],
+      m: 'One FAILED',
+      r: 'FAILED',
+      p: 100,
+      ids: [1,2,3]
+    },
+    {
+      t: [
+        {
+          Tasks: {
+            status: 'ABORTED',
+            id: 1
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 2
+          }
+        }
+      ],
+      m: 'One ABORTED',
+      r: 'CANCELLED',
+      p: 100,
+      ids: [1,2]
+    },
+    {
+      t: [
+        {
+          Tasks: {
+            status: 'TIMEDOUT',
+            id: 3
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 1
+          }
+        }
+      ],
+      m: 'One TIMEDOUT',
+      r: 'TIMEDOUT',
+      p: 100,
+      ids: [1,3]
+    },
+    {
+      t: [
+        {
+          Tasks: {
+            status: 'IN_PROGRESS',
+            id: 1
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 2
+          }
+        }
+      ],
+      m: 'One IN_PROGRESS',
+      r: 'IN_PROGRESS',
+      p: 68,
+      ids: [1,2]
+    },
+    {
+      t: [
+        {
+          Tasks: {
+            status: 'QUEUED',
+            id: 2
+          }
+        },
+        {
+          Tasks: {
+            status: 'COMPLETED',
+            id: 3
+          }
+        }
+      ],
+      m: 'Something else',
+      r: 'PENDING',
+      p: 55,
+      ids: [2,3]
+    }
+  ];
+
+  describe('#getStatus', function() {
+    test_tasks.forEach(function(test_task) {
+      it(test_task.m, function() {
+        expect(App.HostPopup.getStatus(test_task.t)[0]).to.equal(test_task.r);
+      });
+    });
+  });
+
+  describe('#getProgress', function() {
+    test_tasks.forEach(function(test_task) {
+      it(test_task.m, function() {
+        expect(App.HostPopup.getProgress(test_task.t)).to.equal(test_task.p);
+      });
+    });
+  });
+
+  describe('#sortTasksById', function() {
+    test_tasks.forEach(function(test_task) {
+      it(test_task.m, function() {
+        expect(App.HostPopup.sortTasksById(test_task.t).mapProperty('Tasks.id')).to.eql(test_task.ids);
+      });
+    });
+  });
+
+});