Browse Source

AMBARI-2695. Add security wizard unit tests. (Oleg Nechiporenko via yusaku)

Yusaku Sako 12 years ago
parent
commit
2810d1fde4

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

@@ -20,7 +20,9 @@ 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/admin/cluster_test');
+require('test/controllers/main/admin/security/add/addSecurity_controller_test');
 require('test/controllers/main/admin/security/add/step2_test');
+require('test/controllers/main/admin/security/add/step3_test');
 require('test/controllers/main/admin/security/add/step4_test');
 require('test/controllers/main/charts/heatmap_test');
 require('test/controllers/main/charts/heatmap_metrics/heatmap_metric_test');

+ 5 - 1
ambari-web/app/controllers/main/admin/security/add/addSecurity_controller.js

@@ -30,6 +30,10 @@ App.AddSecurityController = App.WizardController.extend({
     controllerName: 'addSecurityController'
   }),
 
+  installedServices: function() {
+    return App.Service.find().mapProperty('serviceName');
+  }.property(),
+
   /**
    * Loads all prior steps on refresh
    */
@@ -57,7 +61,7 @@ App.AddSecurityController = App.WizardController.extend({
   loadServices: function () {
     this.clearServices();
     var secureServices = require('data/secure_configs');
-    var installedServices = App.Service.find().mapProperty('serviceName');
+    var installedServices = this.get('installedServices');
     //General (only non service tab) tab is always displayed
     this.get('content.services').push(secureServices.findProperty('serviceName', 'GENERAL'));
     installedServices.forEach(function (_service) {

+ 21 - 15
ambari-web/app/controllers/main/admin/security/add/step3.js

@@ -44,20 +44,7 @@ App.MainAdminSecurityAddStep3Controller = Em.Controller.extend({
       'OOZIE_SERVER', 'NAGIOS_SERVER', 'HBASE_MASTER', 'HBASE_REGIONSERVER'];
     var securityUsers = [];
     if (!securityUsers || securityUsers.length < 1) { // Page could be refreshed in middle
-      if (App.testMode) {
-        securityUsers.pushObject({id: 'puppet var', name: 'hdfs_user', value: 'hdfs'});
-        securityUsers.pushObject({id: 'puppet var', name: 'mapred_user', value: 'mapred'});
-        securityUsers.pushObject({id: 'puppet var', name: 'hbase_user', value: 'hbase'});
-        securityUsers.pushObject({id: 'puppet var', name: 'hive_user', value: 'hive'});
-        securityUsers.pushObject({id: 'puppet var', name: 'smokeuser', value: 'ambari-qa'});
-        securityUsers.pushObject({id: 'puppet var', name: 'zk_user', value: 'zookeeper'});
-        securityUsers.pushObject({id: 'puppet var', name: 'oozie_user', value: 'oozie'});
-        securityUsers.pushObject({id: 'puppet var', name: 'nagios_user', value: 'nagios'});
-        securityUsers.pushObject({id: 'puppet var', name: 'user_group', value: 'hadoop'});
-      } else {
-        App.router.get('mainAdminSecurityController').setSecurityStatus();
-        securityUsers = App.router.get('mainAdminSecurityController').get('serviceUsers');
-      }
+      securityUsers = this.getSecurityUsers();
     }
     var isHbaseInstalled = App.Service.find().findProperty('serviceName', 'HBASE');
     var generalConfigs = configs.filterProperty('serviceName', 'GENERAL');
@@ -217,11 +204,30 @@ App.MainAdminSecurityAddStep3Controller = Em.Controller.extend({
     this.set('hostComponents', result);
   },
 
+  getSecurityUsers: function() {
+    var securityUsers = [];
+    if (App.testMode) {
+      securityUsers.pushObject({id: 'puppet var', name: 'hdfs_user', value: 'hdfs'});
+      securityUsers.pushObject({id: 'puppet var', name: 'mapred_user', value: 'mapred'});
+      securityUsers.pushObject({id: 'puppet var', name: 'hbase_user', value: 'hbase'});
+      securityUsers.pushObject({id: 'puppet var', name: 'hive_user', value: 'hive'});
+      securityUsers.pushObject({id: 'puppet var', name: 'smokeuser', value: 'ambari-qa'});
+      securityUsers.pushObject({id: 'puppet var', name: 'zk_user', value: 'zookeeper'});
+      securityUsers.pushObject({id: 'puppet var', name: 'oozie_user', value: 'oozie'});
+      securityUsers.pushObject({id: 'puppet var', name: 'nagios_user', value: 'nagios'});
+      securityUsers.pushObject({id: 'puppet var', name: 'user_group', value: 'hadoop'});
+    } else {
+      App.router.get('mainAdminSecurityController').setSecurityStatus();
+      securityUsers = App.router.get('mainAdminSecurityController').get('serviceUsers');
+    }
+    return securityUsers;
+  },
+
   changeDisplayName: function (name) {
     if (name === 'HiveServer2') {
       return 'Hive Metastore and HiveServer2';
     } else {
       return name;
     }
-  },
+  }
 });

+ 6 - 3
ambari-web/app/utils/string_utils.js

@@ -127,10 +127,13 @@ module.exports = {
 
   /**
    * Extracts filename from linux/unix path
-   * @param String: path
-   * @return {Sring}: filename
+   * @param path
+   * @return {string}: filename
    */
   getFileFromPath: function(path) {
-    return path.replace(/^.*[\/]/, '');
+    if (!path || typeof path !== 'string') {
+      return '';
+    }
+    return path.toString().replace(/^.*[\/]/, '');
   }
 }

+ 51 - 0
ambari-web/test/controllers/main/admin/security/add/addSecurity_controller_test.js

@@ -0,0 +1,51 @@
+/**
+ * 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/wizard');
+require('controllers/main/admin/security/add/addSecurity_controller');
+require('models/host_component');
+require('models/cluster');
+require('models/service');
+
+describe('App.AddSecurityController', function () {
+
+  var addSecurityController = App.AddSecurityController.create();
+
+  describe('#clearServices', function() {
+    addSecurityController.set('content.services', [{},{},{}]);
+    it('clear all services', function() {
+      addSecurityController.clearServices();
+      expect(addSecurityController.get('content.services.length')).to.equal(0);
+    });
+  });
+
+  describe('#loadServices', function() {
+
+    it('NAGIOS, HIVE and GENERAL (by default). FAKE not loaded', function() {
+      var ASC = App.AddSecurityController.extend({
+        installedServices: ['NAGIOS', 'HIVE', 'FAKE']
+      });
+      var c = ASC.create();
+      c.loadServices();
+      expect(c.get('content.services.length')).to.equal(3);
+    });
+  });
+
+});

+ 49 - 0
ambari-web/test/controllers/main/admin/security/add/step3_test.js

@@ -0,0 +1,49 @@
+/**
+ * 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('utils/string_utils');
+require('utils/helper');
+require('controllers/main/admin/security/add/step3');
+require('models/host_component');
+require('models/host');
+require('models/service');
+
+describe('App.MainAdminSecurityAddStep3Controller', function () {
+
+  var mainAdminSecurityAddStep3Controller = App.MainAdminSecurityAddStep3Controller.create();
+
+  describe('#getSecurityUsers', function() {
+    it('no hosts, just check users (testMode = true)', function() {
+      App.testMode = true;
+      expect(mainAdminSecurityAddStep3Controller.getSecurityUsers().length).to.equal(9);
+    });
+  });
+
+  describe('#changeDisplayName', function() {
+    it('HiveServer2', function() {
+      expect(mainAdminSecurityAddStep3Controller.changeDisplayName('HiveServer2')).to.equal('Hive Metastore and HiveServer2');
+    });
+    it('Not HiveServer2', function() {
+      expect(mainAdminSecurityAddStep3Controller.changeDisplayName('something')).to.equal('something');
+    });
+  });
+
+});

+ 27 - 0
ambari-web/test/utils/string_utils_test.js

@@ -82,4 +82,31 @@ describe('string_utils', function () {
       });
     });
   });
+
+  describe('#arrayToCSV', function() {
+    var test = [{a: 1, b:2, c:3}, {a: 1, b:2, c:3}, {a: 1, b:2, c:3}];
+    it('array of object to csv-string', function () {
+      expect(string_utils.arrayToCSV(test)).to.equal("1,2,3\n1,2,3\n1,2,3\n");
+    });
+  });
+
+  describe('#getFileFromPath', function() {
+    var tests = [
+      {t: undefined, e: ''},
+      {t: {}, e: ''},
+      {t: [], e: ''},
+      {t: '', e: ''},
+      {t: function(){}, e: ''},
+      {t: '/path/to/file.ext', e: 'file.ext'},
+      {t: 'file.ext', e: 'file.ext'},
+      {t: 'file', e: 'file'},
+      {t: '/path/to/file', e: 'file'}
+    ];
+    tests.forEach(function(test) {
+      it('Check ' + typeof test.t, function () {
+        expect(string_utils.getFileFromPath(test.t)).to.equal(test.e);
+      });
+    });
+  });
+
 });