Forráskód Böngészése

AMBARI-5682 UI unit tests for Add Security controller. (atkach)

atkach 11 éve
szülő
commit
06e2aeb537

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

@@ -33,6 +33,7 @@ require('test/controllers/main/admin/security_test');
 require('test/controllers/main/admin/security/disable_test');
 require('test/controllers/main/admin/security/security_progress_controller_test');
 require('test/controllers/main/admin/security/add/addSecurity_controller_test');
+require('test/controllers/main/admin/security/add/step1_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');

+ 54 - 44
ambari-web/app/controllers/main/admin/security/add/addSecurity_controller.js

@@ -31,10 +31,22 @@ App.AddSecurityController = App.WizardController.extend({
     controllerName: 'addSecurityController'
   }),
 
-  installedServices: function() {
+  /**
+   * installed services on cluster
+   */
+  installedServices: function () {
     return App.Service.find().mapProperty('serviceName');
   }.property(),
 
+  /**
+   * services with security configurations
+   */
+  secureServices: function () {
+    return (App.get('isHadoop2Stack')) ?
+      $.extend(true, [], require('data/HDP2/secure_configs')) :
+      $.extend(true, [], require('data/secure_configs'));
+  }.property('App.isHadoop2Stack'),
+
   /**
    * Loads all prior steps on refresh
    */
@@ -47,29 +59,17 @@ App.AddSecurityController = App.WizardController.extend({
         this.loadServiceConfigs();
       case '1':
         this.loadServices();
-        this.loadNnHastatus();
-    }
-  },
-
-  clearServices: function () {
-    if (this.get('content.services')) {
-      this.get('content.services').clear();
+        this.loadNnHaStatus();
     }
   },
-
   /**
-   * Loads all installed services
+   * Load installed services, which match secure services, to content
    */
   loadServices: function () {
-    this.clearServices();
-    var secureServices;
-    if(App.get('isHadoop2Stack')) {
-      secureServices = $.extend(true, [], require('data/HDP2/secure_configs'));
-    } else {
-      secureServices = $.extend(true, [], require('data/secure_configs'));
-    }
-
+    var secureServices = this.get('secureServices');
     var installedServices = this.get('installedServices');
+
+    this.get('content.services').clear();
     //General (only non service tab) tab is always displayed
     this.get('content.services').push(secureServices.findProperty('serviceName', 'GENERAL'));
     installedServices.forEach(function (_service) {
@@ -78,46 +78,35 @@ App.AddSecurityController = App.WizardController.extend({
         this.get('content.services').push(secureService);
       }
     }, this);
-
   },
-
-  loadNnHastatus: function() {
-    var isNnHa = App.db.getIsNameNodeHa();
-    this.set('content.isNnHa', isNnHa);
+  /**
+   * identify whether NameNode in high availability mode
+   */
+  loadNnHaStatus: function () {
+    this.set('content.isNnHa', App.db.getIsNameNodeHa());
   },
 
+  /**
+   * save service config properties to localStorage
+   * @param stepController
+   */
   saveServiceConfigProperties: function (stepController) {
     var serviceConfigProperties = [];
     stepController.get('stepConfigs').forEach(function (_content) {
       _content.get('configs').forEach(function (_configProperties) {
-        _configProperties.set('value', App.config.trimProperty(_configProperties,true));
-        var overrides = _configProperties.get('overrides');
-        var overridesArray = [];
-        if(overrides!=null){
-          overrides.forEach(function(override){
-            var overrideEntry = {
-              value: override.get('value'),
-              hosts: []
-            };
-            override.get('selectedHostOptions').forEach(function(host){
-              overrideEntry.hosts.push(host);
-            });
-            overridesArray.push(overrideEntry);
-          });
-        }
-        overridesArray = (overridesArray.length) ? overridesArray : null;
+        _configProperties.set('value', App.config.trimProperty(_configProperties, true));
         var configProperty = {
           id: _configProperties.get('id'),
           name: _configProperties.get('name'),
           value: _configProperties.get('value'),
           defaultValue: _configProperties.get('defaultValue'),
           serviceName: _configProperties.get('serviceName'),
-          domain:  _configProperties.get('domain'),
+          domain: _configProperties.get('domain'),
           filename: _configProperties.get('filename'),
           unit: _configProperties.get('unit'),
           components: _configProperties.get('components'),
           component: _configProperties.get('component'),
-          overrides: overridesArray
+          overrides: this.getConfigOverrides(_configProperties)
         };
         serviceConfigProperties.push(configProperty);
       }, this);
@@ -127,12 +116,33 @@ App.AddSecurityController = App.WizardController.extend({
   },
 
   /**
-   * Loads all service config properties
+   * get overrides of config
+   * @param _configProperties
+   * @return {Array}
    */
+  getConfigOverrides: function (_configProperties) {
+    var overrides = _configProperties.get('overrides');
+    var overridesArray = [];
+    if (Array.isArray(overrides)) {
+      overrides.forEach(function (override) {
+        var overrideEntry = {
+          value: override.get('value'),
+          hosts: []
+        };
+        override.get('selectedHostOptions').forEach(function (host) {
+          overrideEntry.hosts.push(host);
+        });
+        overridesArray.push(overrideEntry);
+      });
+    }
+    return (overridesArray.length > 0) ? overridesArray : null;
+  },
 
+  /**
+   * Load service config properties from localStorage
+   */
   loadServiceConfigs: function () {
-    var serviceConfigProperties = App.db.getSecureConfigProperties();
-    this.set('content.serviceConfigProperties', serviceConfigProperties);
+    this.set('content.serviceConfigProperties', App.db.getSecureConfigProperties());
   }
 });
 

+ 6 - 2
ambari-web/app/controllers/main/admin/security/add/step1.js

@@ -20,8 +20,12 @@ var App = require('app');
 App.MainAdminSecurityAddStep1Controller = Em.Controller.extend({
 
   name: 'mainAdminSecurityAddStep1Controller',
+  /**
+   * identify whether ATS(Application Timeline Server) is installed
+   * @return {Boolean}
+   */
   isATSInstalled: function() {
-    return this.get('content.services').findProperty('serviceName', 'YARN') &&
-      App.Service.find('YARN').get('hostComponents').someProperty('componentName', 'APP_TIMELINE_SERVER')
+    return this.get('content.services').someProperty('serviceName', 'YARN') &&
+      App.Service.find('YARN').get('hostComponents').someProperty('componentName', 'APP_TIMELINE_SERVER');
   }
 });

+ 258 - 15
ambari-web/test/controllers/main/admin/security/add/addSecurity_controller_test.js

@@ -18,36 +18,279 @@
 
 
 var App = require('app');
-require('models/host');
 require('mixins/common/localStorage');
 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();
+  var controller = App.AddSecurityController.create({
+    currentStep: null
+  });
+
+  describe('#installedServices', function () {
+
+    afterEach(function () {
+      App.Service.find.restore();
+    });
+
+    it('No installed services', function () {
+      sinon.stub(App.Service, 'find', function () {
+        return [];
+      });
+      expect(controller.get('installedServices')).to.eql([]);
+    });
+    it('One service installed', function () {
+      sinon.stub(App.Service, 'find', function () {
+        return [Em.Object.create({serviceName: 'HDFS'})];
+      });
+      Em.propertyDidChange(controller, 'installedServices');
+      expect(controller.get('installedServices')).to.eql(['HDFS']);
+    });
+  });
+
+  describe('#secureServices', function () {
+
+    afterEach(function () {
+      App.get.restore();
+    });
 
-  describe('#clearServices', function() {
-    addSecurityController.set('content.services', [{},{},{}]);
-    it('clear all services', function() {
-      addSecurityController.clearServices();
-      expect(addSecurityController.get('content.services.length')).to.equal(0);
+    it('App.isHadoop2Stack = false', function () {
+      var result = [
+        "GENERAL",
+        "HDFS",
+        "MAPREDUCE",
+        "HIVE",
+        "WEBHCAT",
+        "HBASE",
+        "ZOOKEEPER",
+        "OOZIE",
+        "NAGIOS"
+      ];
+      sinon.stub(App, 'get', function () {
+        return false;
+      });
+      expect(controller.get('secureServices').mapProperty('serviceName')).to.eql(result);
+    });
+    it('App.isHadoop2Stack = true', function () {
+      var result = [
+        "GENERAL",
+        "HDFS",
+        "MAPREDUCE2",
+        "YARN",
+        "HIVE",
+        "WEBHCAT",
+        "HBASE",
+        "ZOOKEEPER",
+        "OOZIE",
+        "NAGIOS",
+        "STORM",
+        "FALCON"
+      ];
+      sinon.stub(App, 'get', function () {
+        return true;
+      });
+      Em.propertyDidChange(App, 'isHadoop2Stack');
+      expect(controller.get('secureServices').mapProperty('serviceName')).to.eql(result);
     });
   });
 
-  describe('#loadServices', function() {
+  describe('#loadAllPriorSteps()', function () {
 
-    it('NAGIOS, HIVE and GENERAL (by default). FAKE not loaded', function() {
-      var ASC = App.AddSecurityController.extend({
-        installedServices: ['NAGIOS', 'HIVE', 'FAKE']
+    beforeEach(function () {
+      sinon.stub(controller, 'loadServiceConfigs', Em.K);
+      sinon.stub(controller, 'loadServices', Em.K);
+      sinon.stub(controller, 'loadNnHaStatus', Em.K);
+    });
+    afterEach(function () {
+      controller.loadServiceConfigs.restore();
+      controller.loadServices.restore();
+      controller.loadNnHaStatus.restore();
+    });
+
+    var commonSteps = ['4', '3', '2'];
+    commonSteps.forEach(function (step) {
+      it('Current step - ' + step, function () {
+        controller.set('currentStep', step);
+        controller.loadAllPriorSteps();
+        expect(controller.loadServiceConfigs.calledOnce).to.be.true;
+        expect(controller.loadServices.calledOnce).to.be.true;
+        expect(controller.loadNnHaStatus.calledOnce).to.be.true;
       });
-      var c = ASC.create();
-      c.loadServices();
-      expect(c.get('content.services.length')).to.equal(3);
+    });
+    it('Current step - 1', function () {
+      controller.set('currentStep', '1');
+      controller.loadAllPriorSteps();
+      expect(controller.loadServiceConfigs.called).to.be.false;
+      expect(controller.loadServices.calledOnce).to.be.true;
+      expect(controller.loadNnHaStatus.calledOnce).to.be.true;
+    });
+  });
+
+  describe('#loadServices()', function () {
+    it('No installed services', function () {
+      controller.reopen({
+        installedServices: [],
+        secureServices: [
+          {serviceName: 'GENERAL'}
+        ]
+      });
+      controller.loadServices();
+      expect(controller.get('content.services').mapProperty('serviceName')).to.eql(['GENERAL']);
+    });
+    it('Installed service does not match the secure one', function () {
+      controller.set('installedServices', ["HDFS"]);
+      controller.loadServices();
+      expect(controller.get('content.services').mapProperty('serviceName')).to.eql(['GENERAL']);
+    });
+    it('Installed service matches the secure one', function () {
+      controller.set('secureServices', [
+        {serviceName: 'GENERAL'},
+        {serviceName: 'HDFS'}
+      ]);
+      controller.loadServices();
+      expect(controller.get('content.services').mapProperty('serviceName')).to.eql(['GENERAL', 'HDFS']);
     });
   });
 
+  describe('#loadNnHaStatus()', function () {
+    afterEach(function () {
+      App.db.getIsNameNodeHa.restore();
+    });
+    it('NameNode HA is off', function () {
+      sinon.stub(App.db, 'getIsNameNodeHa', function () {
+        return false;
+      });
+      controller.loadNnHaStatus();
+      expect(controller.get('content.isNnHa')).to.be.false;
+    });
+    it('NameNode HA is on', function () {
+      sinon.stub(App.db, 'getIsNameNodeHa', function () {
+        return true;
+      });
+      controller.loadNnHaStatus();
+      expect(controller.get('content.isNnHa')).to.be.true;
+    });
+  });
+
+  describe('#loadServiceConfigs()', function () {
+    afterEach(function () {
+      App.db.getSecureConfigProperties.restore();
+    });
+    it('SecureConfigProperties is empty', function () {
+      sinon.stub(App.db, 'getSecureConfigProperties', function () {
+        return [];
+      });
+      controller.loadServiceConfigs();
+      expect(controller.get('content.serviceConfigProperties')).to.eql([]);
+    });
+    it('SecureConfigProperties has one config', function () {
+      sinon.stub(App.db, 'getSecureConfigProperties', function () {
+        return [{}];
+      });
+      controller.loadServiceConfigs();
+      expect(controller.get('content.serviceConfigProperties')).to.eql([{}]);
+    });
+  });
+
+  describe('#getConfigOverrides()', function () {
+    var testCases = [
+      {
+        title: 'overrides is null',
+        configProperty: Em.Object.create({overrides: null}),
+        result: null
+      },
+      {
+        title: 'overrides is empty',
+        configProperty: Em.Object.create({overrides: []}),
+        result: null
+      },
+      {
+        title: 'overrides has one override',
+        configProperty: Em.Object.create({
+          overrides: [
+            Em.Object.create({
+              value: 'value1',
+              selectedHostOptions: []
+            })
+          ]
+        }),
+        result: [{
+          value: 'value1',
+          hosts: []
+        }]
+      },
+      {
+        title: 'overrides has one override with hosts',
+        configProperty: Em.Object.create({
+          overrides: [
+            Em.Object.create({
+              value: 'value1',
+              selectedHostOptions: ['host1']
+            })
+          ]
+        }),
+        result: [{
+          value: 'value1',
+          hosts: ['host1']
+        }]
+      }
+    ];
+
+    testCases.forEach(function(test){
+      it(test.title, function () {
+        expect(controller.getConfigOverrides(test.configProperty)).to.eql(test.result);
+      });
+    });
+  });
+
+  describe('#saveServiceConfigProperties()', function () {
+    var testCases = [
+      {
+        title: 'stepConfigs is empty',
+        stepController: Em.Object.create({
+          stepConfigs: []
+        }),
+        result: []
+      },
+      {
+        title: 'No configs in service',
+        stepController: Em.Object.create({
+          stepConfigs: [
+            Em.Object.create({configs: []})
+          ]
+        }),
+        result: []
+      }
+    ];
+
+    testCases.forEach(function (test) {
+      it(test.title, function () {
+        sinon.stub(App.db, 'setSecureConfigProperties', Em.K);
+        controller.saveServiceConfigProperties(test.stepController);
+        expect(App.db.setSecureConfigProperties.calledWith(test.result)).to.be.true;
+        expect(controller.get('content.serviceConfigProperties')).to.eql(test.result);
+        App.db.setSecureConfigProperties.restore();
+      });
+    });
+    it('Service has config', function () {
+      var  stepController = Em.Object.create({
+        stepConfigs: [
+          Em.Object.create({configs: [
+            Em.Object.create({
+              name: 'config1',
+              value: 'value1'
+            })
+          ]})
+        ]
+      });
+      sinon.stub(App.db, 'setSecureConfigProperties', Em.K);
+      controller.saveServiceConfigProperties(stepController);
+      expect(App.db.setSecureConfigProperties.calledOnce).to.be.true;
+      expect(controller.get('content.serviceConfigProperties').mapProperty('name')).to.eql(['config1']);
+      App.db.setSecureConfigProperties.restore();
+    });
+  });
 });

+ 59 - 0
ambari-web/test/controllers/main/admin/security/add/step1_test.js

@@ -0,0 +1,59 @@
+/**
+ * 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/security/add/step1');
+require('models/service');
+
+describe('App.MainAdminSecurityAddStep1Controller', function () {
+
+  var controller = App.MainAdminSecurityAddStep1Controller.create({
+    content: {}
+  });
+
+  describe('#isATSInstalled()', function() {
+    it('content.services is empty', function() {
+      controller.set('content.services', []);
+      expect(controller.isATSInstalled()).to.be.false;
+    });
+    it('content.services does not contain YARN', function() {
+      controller.set('content.services', [{serviceName: 'HDFS'}]);
+      expect(controller.isATSInstalled()).to.be.false;
+    });
+    it('YARN does not have ATS', function() {
+      sinon.stub(App.Service, 'find', function(){
+        return Em.Object.create({hostComponents: []})
+      });
+      controller.set('content.services', [{serviceName: 'YARN'}]);
+      expect(controller.isATSInstalled()).to.be.false;
+      App.Service.find.restore();
+    });
+    it('YARN has ATS', function() {
+      sinon.stub(App.Service, 'find', function(){
+        return Em.Object.create({hostComponents: [{
+          componentName: 'APP_TIMELINE_SERVER'
+        }]})
+      });
+      controller.set('content.services', [{serviceName: 'YARN'}]);
+      expect(controller.isATSInstalled()).to.be.true;
+      App.Service.find.restore();
+    });
+  });
+});