Просмотр исходного кода

AMBARI-5709. Unit tests for step controllers. (onechiporenko)

Oleg Nechiporenko 11 лет назад
Родитель
Сommit
c991897b4e

+ 9 - 4
ambari-web/app/controllers/wizard/step2_controller.js

@@ -342,6 +342,7 @@ App.WizardStep2Controller = Em.Controller.extend({
 
   /**
    * setup bootstrap data and completion callback for bootstrap call
+   * @method setupBootStrap
    */
   setupBootStrap: function () {
     var self = this;
@@ -360,11 +361,12 @@ App.WizardStep2Controller = Em.Controller.extend({
 
   /**
    * show warning for host names without dots or IP addresses
+   * @return {App.ModalPopup}
    * @method warningPopup
    */
   warningPopup: function () {
     var self = this;
-    App.ModalPopup.show({
+    return App.ModalPopup.show({
       header: Em.I18n.t('common.warning'),
       onPrimary: function () {
         this.hide();
@@ -378,11 +380,12 @@ App.WizardStep2Controller = Em.Controller.extend({
 
   /**
    * show popup with the list of hosts that are already part of the cluster
+   * @return {App.ModalPopup}
    * @method installedHostsPopup
    */
   installedHostsPopup: function () {
     var self = this;
-    App.ModalPopup.show({
+    return App.ModalPopup.show({
       header: Em.I18n.t('common.warning'),
       onPrimary: function () {
         self.proceedNext();
@@ -401,10 +404,11 @@ App.WizardStep2Controller = Em.Controller.extend({
    * Show popup with hosts generated by pattern
    * @method hostNamePatternPopup
    * @param {string[]} hostNames
+   * @return {App.ModalPopup}
    */
   hostNamePatternPopup: function (hostNames) {
     var self = this;
-    App.ModalPopup.show({
+    return App.ModalPopup.show({
       header: Em.I18n.t('installer.step2.hostName.pattern.header'),
       onPrimary: function () {
         self.proceedNext();
@@ -431,11 +435,12 @@ App.WizardStep2Controller = Em.Controller.extend({
   /**
    * Show notify that installation is manual
    * save hosts
+   * @return {App.ModalPopup}
    * @method manualInstallPopup
    */
   manualInstallPopup: function () {
     var self = this;
-    App.ModalPopup.show({
+    return App.ModalPopup.show({
       header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
       onPrimary: function () {
         this.hide();

+ 4 - 2
ambari-web/app/controllers/wizard/step4_controller.js

@@ -294,6 +294,7 @@ App.WizardStep4Controller = Em.ArrayController.extend({
    *    ]
    *  </code>
    * @param {string} i18nSuffix
+   * @return {App.ModalPopup}
    * @method needToAddServicePopup
    */
   needToAddServicePopup: function (services, i18nSuffix) {
@@ -301,7 +302,7 @@ App.WizardStep4Controller = Em.ArrayController.extend({
       services = [services];
     }
     var self = this;
-    App.ModalPopup.show({
+    return App.ModalPopup.show({
       header: Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.header'),
       body: Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.body'),
       onPrimary: function () {
@@ -316,10 +317,11 @@ App.WizardStep4Controller = Em.ArrayController.extend({
 
   /**
    * Show popup with info about not selected (but should be selected) services
+   * @return {App.ModalPopup}
    * @method monitoringCheckPopup
    */
   monitoringCheckPopup: function () {
-    App.ModalPopup.show({
+    return App.ModalPopup.show({
       header: Em.I18n.t('installer.step4.monitoringCheck.popup.header'),
       body: Em.I18n.t('installer.step4.monitoringCheck.popup.body'),
       onPrimary: function () {

+ 57 - 0
ambari-web/test/installer/step10_test.js

@@ -411,4 +411,61 @@ describe('App.WizardStep10Controller', function () {
 
   });
 
+  describe('#isNagiosRestartRequired', function() {
+    Em.A([
+      {
+        controllerName: 'addServiceController',
+        isLoaded: true,
+        e: true
+      },
+      {
+        controllerName: 'installerController',
+        isLoaded: true,
+        e: false
+      },
+      {
+        controllerName: 'addServiceController',
+        isLoaded: false,
+        e: false
+      },
+      {
+        controllerName: 'installerController',
+        isLoaded: false,
+        e: false
+      }
+    ]).forEach(function (test) {
+        it(test.controllerName + ' Nagios loaded' + test.isLoaded.toString(), function () {
+          controller.set('content.controllerName', test.controllerName);
+          sinon.stub(App.Service, 'find', function() {
+            return Em.Object.create({
+              isLoaded: test.isLoaded
+            })
+          });
+          expect(controller.get('isNagiosRestartRequired')).to.equal(test.e);
+          App.Service.find.restore();
+        });
+      });
+  });
+
+  describe('#loadRegisteredHosts', function() {
+    it('should add object to clusterInfo', function() {
+      var masterComponentHosts = [{hostName: 'h1'}, {hostName: 'h2'}, {hostName: 'h3'}],
+        slaveComponentHosts = [{hosts: [{hostName: 'h1'}, {hostName: 'h4'}]}, {hosts: [{hostName: 'h2'}, {hostName: 'h5'}]}],
+        hosts = [{hostName: 'h6'}, {hostName: 'h3'}, {hostName: 'h7'}];
+      controller.set('content.masterComponentHosts', masterComponentHosts);
+      controller.set('content.slaveComponentHosts', slaveComponentHosts);
+      controller.set('clusterInfo', []);
+      sinon.stub(App.Host, 'find', function() {
+        return hosts;
+      });
+      var obj = controller.loadRegisteredHosts();
+      App.Host.find.restore();
+      expect(obj.id).to.equal(1);
+      expect(obj.color).to.equal('text-info');
+      expect(obj.displayStatement).to.equal(Em.I18n.t('installer.step10.hostsSummary').format(7));
+      expect(obj.status).to.eql([]);
+      expect(controller.get('clusterInfo.firstObject')).to.eql(obj);
+    });
+  });
+
 });

+ 136 - 23
ambari-web/test/installer/step2_test.js

@@ -22,9 +22,13 @@ require('controllers/wizard/step2_controller');
 require('models/host');
 require('models/host_component');
 require('messages');
-
+var c;
 describe('App.WizardStep2Controller', function () {
 
+  beforeEach(function() {
+    c = App.WizardStep2Controller.create();
+  });
+
   describe('#isInstaller', function() {
     it('true if controllerName is installerController', function() {
       var controller = App.WizardStep2Controller.create({content: {controllerName: 'installerController'}});
@@ -362,16 +366,78 @@ describe('App.WizardStep2Controller', function () {
 
   describe('#proceedNext()', function () {
 
-    it('should call manualInstallPopup if manualInstall is true', function (done) {
-      var controller = App.WizardStep2Controller.create({
+    it('should call warningPopup if not isAllHostNamesValid and no warningConfirmed', function() {
+      c.reopen({
+        isAllHostNamesValid: function() {
+          return false;
+        },
+        warningPopup: Em.K
+      });
+      sinon.spy(c, 'warningPopup');
+      var r = c.proceedNext(false);
+      expect(r).to.equal(false);
+      expect(c.warningPopup.calledOnce).to.equal(true);
+    });
+
+    it('should call manualInstallPopup if manualInstall is true', function () {
+      c.reopen({
         hostNames: '',
         manualInstall: true,
-        manualInstallPopup: function () {
-          done();
+        manualInstallPopup: Em.K
+      });
+      sinon.spy(c, 'manualInstallPopup');
+      var r = c.proceedNext(true);
+      expect(r).to.equal(false);
+      expect(c.manualInstallPopup.calledOnce).to.equal(true);
+    });
+
+    it ('should save hosts and proceed next if skipBootstrap is true', function() {
+      sinon.stub(App, 'get', function(k) {
+        if ('skipBootstrap' === k) {
+          return true;
         }
+        return Em.get(App, k);
       });
-      controller.proceedNext(true);
-    })
+      sinon.stub(App.router, 'send', Em.K);
+      c.reopen({
+        hostNameArr: ['h1'],
+        isAllHostNamesValid: function() {return true;},
+        content: {
+          installOptions: {},
+          hosts: null
+        }
+      });
+      var r = c.proceedNext();
+      expect(r).to.equal(true);
+      expect(Em.keys(c.get('content.hosts'))).to.eql(['h1']);
+      expect(App.router.send.calledWith('next')).to.equal(true);
+      App.get.restore();
+      App.router.send.restore();
+    });
+
+    it('should call setupBootStrap', function() {
+      sinon.stub(App, 'get', function(k) {
+        if ('skipBootstrap' === k) {
+          return false;
+        }
+        return Em.get(App, k);
+      });
+      c.reopen({
+        hostNameArr: ['h1'],
+        isAllHostNamesValid: function() {return true;},
+        content: {
+          installOptions: {},
+          hosts: null
+        }
+      });
+      sinon.stub(c, 'setupBootStrap', Em.K);
+      var r = c.proceedNext();
+      expect(r).to.equal(true);
+      expect(c.setupBootStrap.calledOnce).to.eql(true);
+      App.get.restore();
+      c.setupBootStrap.restore();
+    });
+
   });
 
   describe('#isSubmitDisabled', function () {
@@ -394,59 +460,83 @@ describe('App.WizardStep2Controller', function () {
   });
 
   describe('#installedHostsPopup', function() {
-    before(function() {
+    beforeEach(function() {
       sinon.spy(App.ModalPopup, 'show');
+      sinon.stub(c, 'proceedNext', Em.K);
     });
-    after(function() {
+    afterEach(function() {
       App.ModalPopup.show.restore();
+      c.proceedNext.restore();
     });
     it('should call App.ModalPopup.show', function() {
-      var controller = App.WizardStep2Controller.create();
-      controller.installedHostsPopup();
+      c.installedHostsPopup();
       expect(App.ModalPopup.show.calledOnce).to.equal(true);
     });
+    it('should proceed next on primary', function() {
+      c.installedHostsPopup().onPrimary();
+      expect(c.proceedNext.calledOnce).to.equal(true);
+    });
   });
 
   describe('#warningPopup', function() {
-    before(function() {
+    beforeEach(function() {
       sinon.spy(App.ModalPopup, 'show');
+      sinon.stub(c, 'proceedNext', Em.K);
     });
-    after(function() {
+    afterEach(function() {
       App.ModalPopup.show.restore();
+      c.proceedNext.restore();
+
     });
     it('should call App.ModalPopup.show', function() {
-      var controller = App.WizardStep2Controller.create();
-      controller.warningPopup();
+      c.warningPopup();
       expect(App.ModalPopup.show.calledOnce).to.equal(true);
     });
+    it('should proceed next on primary', function() {
+      c.warningPopup().onPrimary();
+      expect(c.proceedNext.calledWith(true)).to.equal(true);
+    });
   });
 
   describe('#hostNamePatternPopup', function() {
-    before(function() {
+    beforeEach(function() {
       sinon.spy(App.ModalPopup, 'show');
+      sinon.stub(c, 'proceedNext', Em.K);
     });
-    after(function() {
+    afterEach(function() {
       App.ModalPopup.show.restore();
+      c.proceedNext.restore();
     });
     it('should call App.ModalPopup.show', function() {
-      var controller = App.WizardStep2Controller.create();
-      controller.hostNamePatternPopup();
+      c.hostNamePatternPopup();
       expect(App.ModalPopup.show.calledOnce).to.equal(true);
     });
+    it('should proceed next on primary', function() {
+      c.hostNamePatternPopup().onPrimary();
+      expect(c.proceedNext.calledOnce).to.equal(true);
+    });
   });
 
   describe('#manualInstallPopup', function() {
-    before(function() {
+    beforeEach(function() {
       sinon.spy(App.ModalPopup, 'show');
+      sinon.stub(App.router, 'send', Em.K);
+      sinon.stub(c, 'saveHosts', Em.K);
     });
-    after(function() {
+    afterEach(function() {
       App.ModalPopup.show.restore();
+      App.router.send.restore();
+      c.saveHosts.restore();
     });
     it('should call App.ModalPopup.show', function() {
-      var controller = App.WizardStep2Controller.create();
-      controller.manualInstallPopup();
+      c.manualInstallPopup();
       expect(App.ModalPopup.show.calledOnce).to.equal(true);
     });
+    it('should save hosts and go next on primary', function() {
+      c.manualInstallPopup().onPrimary();
+      expect(c.saveHosts.calledOnce).to.equal(true);
+      expect(App.router.send.calledWith('next')).to.equal(true);
+    });
   });
 
   describe('#manualInstallWarningPopup', function() {
@@ -499,4 +589,27 @@ describe('App.WizardStep2Controller', function () {
     });
   });
 
+  describe('#saveHosts', function() {
+    beforeEach(function() {
+      sinon.stub(c, 'setAmbariJavaHome', Em.K);
+      c.reopen({
+        hostNameArr: ['h1'],
+        content: {
+          hosts: null
+        }
+      });
+    });
+    afterEach(function() {
+      c.setAmbariJavaHome.restore();
+    });
+    it('should call setAmbariJavaHome', function() {
+      c.saveHosts();
+      expect(c.setAmbariJavaHome.calledOnce).to.equal(true);
+    });
+    it('should set content.hosts', function() {
+      c.saveHosts();
+      expect(Em.keys(c.get('content.hosts'))).to.eql(['h1']);
+    });
+  });
+
 });

+ 135 - 1
ambari-web/test/installer/step4_test.js

@@ -345,6 +345,140 @@ describe('App.WizardStep4Controller', function () {
       expect(App.ModalPopup.show.calledOnce).to.equal(true);
       App.ModalPopup.show.restore();
     });
-  })
+    it('onPrimary should proceed to next step', function() {
+      sinon.stub(App.router, 'send', Em.K);
+      controller.monitoringCheckPopup().onPrimary();
+      expect(App.router.send.calledWith('next')).to.equal(true);
+      App.router.send.restore();
+    });
+  });
+
+  describe('#needToAddServicePopup', function() {
+    Em.A([
+        {
+          m: 'one service',
+          services: {selected: true, serviceName: 's1'},
+          content: [Em.Object.create({serviceName: 's1', isSelected: false})],
+          e: [true]
+        },
+        {
+          m: 'many services',
+          services: [{selected: true, serviceName: 's1'}, {selected: false, serviceName: 's2'}],
+          content: [Em.Object.create({serviceName: 's1', isSelected: false}),
+            Em.Object.create({serviceName: 's2', isSelected: true})],
+          e: [true, false]
+        }
+      ]).forEach(function (test) {
+        it(test.m, function () {
+          sinon.stub(controller, 'submit', Em.K);
+          controller.set('content', test.content);
+          controller.needToAddServicePopup(test.services, '').onPrimary();
+          expect(controller.submit.calledOnce).to.equal(true);
+          expect(controller.mapProperty('isSelected')).to.eql(test.e);
+          controller.submit.restore();
+        });
+      });
+  });
+
+  describe('#validateMonitoring', function() {
+    Em.A([
+        {
+          gangliaOrNagiosNotSelected: true,
+          e: {
+            monitoringCheckPopup: true,
+            send: false
+          }
+        },
+        {
+          gangliaOrNagiosNotSelected: false,
+          e: {
+            monitoringCheckPopup: false,
+            send: true
+          }
+        }
+      ]).forEach(function (test) {
+        it(test.m, function () {
+          sinon.stub(controller, 'monitoringCheckPopup', Em.K);
+          sinon.stub(App.router, 'send', Em.K);
+          sinon.stub(controller, 'gangliaOrNagiosNotSelected', function() {
+            return test.gangliaOrNagiosNotSelected;
+          });
+          controller.validateMonitoring();
+          if (test.e.monitoringCheckPopup) {
+           expect(controller.monitoringCheckPopup.calledOnce).to.equal(true);
+          }
+          else {
+            expect(controller.monitoringCheckPopup.called).to.equal(false);
+          }
+          if (test.e.send) {
+            expect(App.router.send.calledWith('next')).to.equal(true);
+          }
+          else {
+            expect(App.router.send.called).to.equal(false);
+          }
+          controller.gangliaOrNagiosNotSelected.restore();
+          controller.monitoringCheckPopup.restore();
+          App.router.send.restore();
+        });
+      });
+  });
+
+  describe('#submit', function() {
+    beforeEach(function() {
+      sinon.stub(controller, 'validateMonitoring', Em.K);
+    });
+    afterEach(function() {
+      controller.validateMonitoring.restore();
+    });
+    it('if not isSubmitDisabled shound\'t do nothing', function() {
+      controller.reopen({isSubmitDisabled: true});
+      controller.submit();
+      expect(controller.validateMonitoring.called).to.equal(false);
+    });
+    it('if isSubmitDisabled and not submitChecks should call validateMonitoring', function() {
+      controller.reopen({
+        isSubmitDisabled: false,
+        submitChecks: []
+      });
+      controller.submit();
+      expect(controller.validateMonitoring.calledOnce).to.equal(true);
+    });
+    it('if isSubmitDisabled and some submitChecks true shouldn\'t call validateMonitoring', function() {
+      controller.reopen({
+        isSubmitDisabled: false,
+        submitChecks: [
+          {
+            checkCallback: 'needToAddMapReduce',
+            popupParams: [
+              {serviceName: 'MAPREDUCE', selected: true},
+              'mapreduceCheck'
+            ]
+          }
+        ]
+      });
+      sinon.stub(controller, 'needToAddMapReduce', function() {return true;});
+      controller.submit();
+      expect(controller.validateMonitoring.called).to.equal(false);
+      controller.needToAddMapReduce.restore();
+    });
+    it('if isSubmitDisabled and some submitChecks false should call validateMonitoring', function() {
+      controller.reopen({
+        isSubmitDisabled: false,
+        submitChecks: [
+          {
+            checkCallback: 'needToAddMapReduce',
+            popupParams: [
+              {serviceName: 'MAPREDUCE', selected: true},
+              'mapreduceCheck'
+            ]
+          }
+        ]
+      });
+      sinon.stub(controller, 'needToAddMapReduce', function() {return false;});
+      controller.submit();
+      expect(controller.validateMonitoring.calledOnce).to.equal(true);
+      controller.needToAddMapReduce.restore();
+    });
+  });
 
 });