Browse Source

AMBARI-1987. Add unit tests for admin/cluster page and cluster loading. (yusaku)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/trunk@1469636 13f79535-47bb-0310-9956-ffa450edef68
Yusaku Sako 12 years ago
parent
commit
1a98388f30

+ 3 - 0
CHANGES.txt

@@ -269,6 +269,9 @@ Trunk (unreleased changes):
 
  IMPROVEMENTS
 
+ AMBARI-1987. Add unit tests for admin/cluster page and cluster loading.
+ (yusaku)
+
  AMBARI-1929. Make the default stack and version configurable via mvn build.
  (yusaku)
 

+ 2 - 0
ambari-web/app/assets/test/tests.js

@@ -18,8 +18,10 @@
 
 require('test/utils/ajax_test');
 require('test/controllers/global/background_operations_test');
+require('test/controllers/global/cluster_controller_test');
 require('test/controllers/main/app_contoller_test');
 require('test/controllers/main/charts/heatmap_metrics/heatmap_metric_test');
+require('test/controllers/main/admin/cluster_test');
 require('test/installer/step1_test');
 require('test/installer/step2_test');
 require('test/installer/step3_test');

+ 8 - 4
ambari-web/app/controllers/global/cluster_controller.js

@@ -30,11 +30,15 @@ App.ClusterController = Em.Controller.extend({
   updateLoadStatus:function (item) {
     var loadList = this.get('dataLoadList');
     var loaded = true;
-    var numLoaded= 0;
+    var numLoaded = 0;
+    var loadListLength = 0;
     loadList.set(item, true);
     for (var i in loadList) {
-      if (loadList.hasOwnProperty(i) && !loadList[i] && loaded) {
-        loaded = false;
+      if (loadList.hasOwnProperty(i)) {
+        loadListLength++;
+        if(!loadList[i] && loaded){
+          loaded = false;
+        }
       }
       // calculate the number of true
       if (loadList.hasOwnProperty(i) && loadList[i]){
@@ -42,7 +46,7 @@ App.ClusterController = Em.Controller.extend({
       }
     }
     this.set('isLoaded', loaded);
-    this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded/8*100)).toString() + '%');
+    this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
   },
 
   dataLoadList:Em.Object.create({

+ 50 - 0
ambari-web/test/controllers/global/cluster_controller_test.js

@@ -0,0 +1,50 @@
+/**
+ * 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 App = require('app');
+require('controllers/global/cluster_controller');
+require('utils/http_client');
+
+describe('App.clusterController', function () {
+
+  /**
+   * Test object
+   */
+  var controller = App.ClusterController.create();
+  describe('#updateLoadStatus()', function () {
+    it('all items are loaded', function(){
+      controller.set('dataLoadList', Em.Object.create({
+        'item1':true,
+        'item2':false
+      }));
+      controller.updateLoadStatus.call(controller, 'item2');
+      expect(controller.get('isLoaded')).to.equal(true);
+      expect(controller.get('clusterDataLoadedPercent')).to.equal('width:100%');
+    })
+    it('one item of two is loaded', function(){
+      controller.set('dataLoadList', Em.Object.create({
+        'item1':false,
+        'item2':false
+      }));
+      controller.updateLoadStatus.call(controller, 'item1');
+      expect(controller.get('isLoaded')).to.equal(false);
+      expect(controller.get('clusterDataLoadedPercent')).to.equal('width:50%');
+    })
+  })
+})

+ 72 - 0
ambari-web/test/controllers/main/admin/cluster_test.js

@@ -0,0 +1,72 @@
+/**
+ * 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 App = require('app');
+require('controllers/main/admin/cluster');
+
+
+describe('App.MainAdminClusterController', function () {
+
+  /**
+   * Predefined data
+   *
+   */
+  App.set('currentStackVersion', 'HDP-1.2.2');
+  App.set('defaultStackVersion', 'HDP-1.2.2');
+  /**
+   * Test object
+   */
+  var controller = App.MainAdminClusterController.create({
+    parseServicesInfo:function(){}
+  });
+  var data = {
+    "items" : [
+      {
+        "Versions" : {
+          "stack_version" : "1.3.0",
+          "min_upgrade_version" : "1.2.0"
+        }
+      },
+      {
+        "Versions" : {
+          "stack_version" : "1.2.2",
+          "min_upgrade_version" : "1.2.0"
+        }
+      },
+      {
+        "Versions" : {
+          "stack_version" : "1.2.0",
+          "min_upgrade_version" : "1.2.0"
+        }
+      }
+    ]
+  };
+
+  describe('#updateUpgradeVersionSuccessCallback()', function () {
+    it('upgrade version of stack should be "HDP-1.3.0"', function(){
+      controller.updateUpgradeVersionSuccessCallback.call(controller, data);
+      expect(controller.get('upgradeVersion')).to.equal('HDP-1.3.0');
+    })
+    it('if min upgrade version less then current then upgrade version equal current', function(){
+      data.items[0].Versions.min_upgrade_version = "1.2.3";
+      controller.updateUpgradeVersionSuccessCallback.call(controller, data);
+      expect(controller.get('upgradeVersion')).to.equal('HDP-1.2.2');
+    })
+  })
+})

+ 38 - 1
ambari-web/test/utils/validator_test.js

@@ -197,7 +197,7 @@ describe('validator', function () {
     it('"55454" - invalid Domain Name', function () {
       expect(validator.isDomainName('55454')).to.equal(false);
     })
-  }),
+  })
   describe('#isValidUserName(value)', function() {
     var tests = [
       {m:'"" - invalid',i:'',e:false},
@@ -216,5 +216,42 @@ describe('validator', function () {
       })
     });
   })
+  describe('#isValidUNIXUser(value)', function() {
+    var tests = [
+      {m:'"" - invalid',i:'',e:false},
+      {m:'"abc123" - valid',i:'abc123',e:true},
+      {m:'"1abc123" - invalid',i:'1abc123',e:false},
+      {m:'"abc123$" - invalid',i:'abc123$',e:false},
+      {m:'"~1abc123" - invalid',i: '~1abc123',e:false},
+      {m:'"abc12345679abc1234567890abc1234567890$" - invalid',i:'abc12345679abc1234567890abc1234567890$',e:false},
+      {m:'"1abc123$$" - invalid',i:'1abc123$$',e:false},
+      {m:'"a" - valid',i:'a',e:true},
+      {m:'"!" - invalid',i:'!',e:false},
+      {m:'"abc_" - valid',i:'abc_',e:true},
+      {m:'"_abc" - valid',i:'_abc',e:true},
+      {m:'"abc_abc" - valid',i:'_abc',e:true}
+    ];
+    tests.forEach(function(test) {
+      it(test.m + ' ', function () {
+        expect(validator.isValidUNIXUser(test.i)).to.equal(test.e);
+      })
+    });
+  })
+  describe('#isValidDir(value)', function() {
+    var tests = [
+      {m:'"dir" - invalid',i:'dir',e:false},
+      {m:'"/dir" - valid',i:'/dir',e:true},
+      {m:'"/dir1,dir2" - invalid',i:'/dir1,dir2',e:false},
+      {m:'"/dir1,/dir2" - valid',i:'/dir1,/dir2',e:true},
+      {m:'"/123" - valid',i:'/111',e:true},
+      {m:'"/abc" - valid',i:'/abc',e:true},
+      {m:'"/1a2b3c" - valid',i:'/1a2b3c',e:true}
+    ];
+    tests.forEach(function(test) {
+      it(test.m + ' ', function () {
+        expect(validator.isValidDir(test.i)).to.equal(test.e);
+      })
+    });
+  })
 
 })