123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /**
- * 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/alerts_controller');
- require('models/alert');
- describe('MainAlertsController', function () {
- var controller = App.MainAlertsController.create();
- describe('#loadAlerts()', function () {
- before(function () {
- sinon.stub(controller, 'getFromServer', Em.K);
- });
- after(function () {
- controller.getFromServer.restore();
- });
- it('getFromServer should be called', function () {
- controller.set('resourceName', null);
- controller.set('isLoaded', true);
- controller.set('resourceType', null);
- controller.loadAlerts('name1', 'type1');
- expect(controller.get("isLoaded")).to.be.false;
- expect(controller.get("resourceName")).to.equal('name1');
- expect(controller.get("resourceType")).to.equal('type1');
- expect(controller.getFromServer.calledOnce).to.be.true;
- });
- });
- describe('#update()', function () {
- var clock;
- beforeEach(function () {
- clock = sinon.useFakeTimers();
- sinon.stub(controller, 'getFromServer', Em.K);
- sinon.spy(controller, 'update');
- });
- afterEach(function () {
- clock.restore();
- controller.getFromServer.restore();
- controller.update.restore();
- });
- it('isUpdating = true', function () {
- controller.set('isUpdating', true);
- expect(controller.get("updateTimer")).not.to.be.null;
- clock.tick(App.componentsUpdateInterval);
- expect(controller.getFromServer.calledOnce).to.be.true;
- expect(controller.update.calledTwice).to.be.true;
- });
- it('isUpdating = false', function () {
- controller.set('isUpdating', false);
- expect(controller.update.calledOnce).to.be.true;
- });
- });
- describe('#getFromServer()', function () {
- var obj = Em.Object.create({isNagiosInstalled: false});
- beforeEach(function () {
- sinon.stub(controller, 'getAlertsByService', Em.K);
- sinon.stub(controller, 'getAlertsByHost', Em.K);
- sinon.stub(App.router, 'get', function() {return obj.get('isNagiosInstalled')});
- });
- afterEach(function () {
- controller.getAlertsByService.restore();
- controller.getAlertsByHost.restore();
- App.router.get.restore();
- });
- it('Nagios is not installed', function () {
- obj.set('isNagiosInstalled', false);
- controller.set('isLoaded', false);
- controller.getFromServer();
- expect(controller.get('isLoaded')).to.be.true;
- controller.set('isLoaded', false);
- });
- it('Nagios installed, SERVICE resource type', function () {
- obj.set('isNagiosInstalled', true);
- controller.set('resourceType', 'SERVICE');
- controller.getFromServer();
- expect(controller.get('isLoaded')).to.be.false;
- expect(controller.getAlertsByService.calledOnce).to.be.true;
- });
- it('Nagios installed, HOST resource type', function () {
- obj.set('isNagiosInstalled', true);
- controller.set('resourceType', 'HOST');
- controller.getFromServer();
- expect(controller.get('isLoaded')).to.be.false;
- expect(controller.getAlertsByHost.calledOnce).to.be.true;
- });
- it('Nagios installed, unknown resource type', function () {
- obj.set('isNagiosInstalled', true);
- controller.set('resourceType', 'RS1');
- controller.getFromServer();
- expect(controller.get('isLoaded')).to.be.false;
- expect(controller.getAlertsByService.called).to.be.false;
- expect(controller.getAlertsByHost.called).to.be.false;
- });
- });
- describe('#getAlertsByHost()', function () {
- beforeEach(function () {
- sinon.stub(App.ajax, 'send', Em.K);
- });
- afterEach(function () {
- App.ajax.send.restore();
- });
- it('resourceName is null', function () {
- controller.set('resourceName', null);
- expect(controller.getAlertsByHost()).to.be.false;
- expect(App.ajax.send.called).to.be.false;
- });
- it('resourceName is correct', function () {
- controller.set('resourceName', 'host1');
- expect(controller.getAlertsByHost()).to.be.true;
- expect(App.ajax.send.calledOnce).to.be.true;
- });
- });
- describe('#getAlertsByService()', function () {
- beforeEach(function () {
- sinon.stub(App.ajax, 'send', Em.K);
- });
- afterEach(function () {
- App.ajax.send.restore();
- });
- it('resourceName is null', function () {
- controller.set('resourceName', null);
- expect(controller.getAlertsByService()).to.be.false;
- expect(App.ajax.send.called).to.be.false;
- });
- it('resourceName is correct', function () {
- controller.set('resourceName', 'service1');
- expect(controller.getAlertsByService()).to.be.true;
- expect(App.ajax.send.calledOnce).to.be.true;
- });
- });
- describe('#getAlertsSuccessCallback()', function () {
- var testCases = [
- {
- title: 'data is null',
- data: null,
- result: []
- },
- {
- title: 'data.legacy_alerts is null',
- data: {legacy_alerts: null},
- result: []
- },
- {
- title: 'data.legacy_alerts.detail is null',
- data: {legacy_alerts: {detail: null}},
- result: []
- },
- {
- title: 'data.legacy_alerts.detail is empty',
- data: {legacy_alerts: {detail: []}},
- result: []
- }
- ];
- testCases.forEach(function (test) {
- it(test.title, function () {
- controller.set('isLoaded', false);
- controller.getAlertsSuccessCallback(test.data);
- expect(controller.get('alerts')).to.eql(test.result);
- expect(controller.get('isLoaded')).to.be.true;
- });
- });
- var data = {legacy_alerts: {detail: [
- {
- description: 't1',
- service_name: "s1",
- status_time: 1,
- status: 'OK',
- output: 'o1',
- host_name: 'h1',
- last_status_time: 1
- }
- ]}};
- var testCasesOfStatus = [
- {
- title: 'data.legacy_alerts.detail is correct, OK status',
- status: 'OK',
- result: '0'
- },
- {
- title: 'data.legacy_alerts.detail is correct, WARNING status',
- status: 'WARNING',
- result: '1'
- },
- {
- title: 'data.legacy_alerts.detail is correct, CRITICAL status',
- status: 'CRITICAL',
- result: '2'
- },
- {
- title: 'data.legacy_alerts.detail is correct, PASSIVE status',
- status: 'PASSIVE',
- result: '3'
- },
- {
- title: 'data.legacy_alerts.detail is correct, unknown status',
- status: '',
- result: '4'
- }
- ];
- testCasesOfStatus.forEach(function (test) {
- it(test.title, function () {
- controller.set('isLoaded', false);
- data.legacy_alerts.detail[0].status = test.status;
- controller.getAlertsSuccessCallback(data);
- expect(controller.get('alerts.length')).to.equal(1);
- expect(controller.get('alerts').objectAt(0).get('status')).to.equal(test.result);
- expect(controller.get('isLoaded')).to.be.true;
- });
- });
- });
- describe('#getAlertsErrorCallback()', function () {
- it('isLoaded was false', function () {
- controller.set('isLoaded', false);
- controller.getAlertsErrorCallback();
- expect(controller.get('isLoaded')).to.be.true;
- });
- it('isLoaded was true', function () {
- controller.set('isLoaded', true);
- controller.getAlertsErrorCallback();
- expect(controller.get('isLoaded')).to.be.true;
- });
- });
- });
|