changeset 234:7d4f6ee803fb

Handle command channel failures when loading and unloading byteman rules Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/025230.html
author Andrew Azores <aazores@redhat.com>
date Fri, 29 Sep 2017 12:57:16 -0400
parents 9831c5578719
children 0058493033d9
files src/app/components/jvm-info/byteman/byteman.service.js src/app/components/jvm-info/byteman/rules/byteman-rules.controller.js src/app/components/jvm-info/byteman/rules/byteman-rules.controller.spec.js src/app/components/jvm-info/byteman/rules/byteman-rules.html src/app/components/jvm-info/byteman/rules/en.locale.yaml
diffstat 5 files changed, 142 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/components/jvm-info/byteman/byteman.service.js	Thu Sep 28 15:14:47 2017 -0400
+++ b/src/app/components/jvm-info/byteman/byteman.service.js	Fri Sep 29 12:57:16 2017 -0400
@@ -137,7 +137,8 @@
             status: success.payload.respType.value === this._cmdChan.responseCodes.OK.value,
             reason: success.payload.respType.message
           });
-        }
+        },
+        defer.reject
       );
     });
 
--- a/src/app/components/jvm-info/byteman/rules/byteman-rules.controller.js	Thu Sep 28 15:14:47 2017 -0400
+++ b/src/app/components/jvm-info/byteman/rules/byteman-rules.controller.js	Fri Sep 29 12:57:16 2017 -0400
@@ -35,6 +35,11 @@
     this._translate = $translate;
     this._svc = bytemanService;
 
+    this.showErr = false;
+    this.errMessage = '';
+
+    this._translate('byteman.rules.COMMAND_CHANNEL_REQUEST_FAILED_TITLE').then(title => this.errTitle = title);
+
     this.loadedRule = '';
   }
 
@@ -60,13 +65,37 @@
     if (!this.loadedRule) {
       return;
     }
-    return this._svc.unloadRules(this.systemId, this.jvmId)
-      .then(() => this._updateRules());
+    return this._svc.unloadRules(this.systemId, this.jvmId).then(
+      response => {
+        if (response.status) {
+          this.showErr = false;
+        } else {
+          this.showErr = true;
+          this.errMessage = response.reason;
+        }
+      },
+      failure => {
+        this.showErr = true;
+        this.errMessage = failure;
+      }
+    ).finally(() => this._updateRules());
   }
 
   push () {
-    return this._svc.loadRule(this.systemId, this.jvmId, this.ruleText)
-      .then(() => this._updateRules());
+    return this._svc.loadRule(this.systemId, this.jvmId, this.ruleText).then(
+      response => {
+        if (response.status) {
+          this.showErr = false;
+        } else {
+          this.showErr = true;
+          this.errMessage = response.reason;
+        }
+      },
+      failure => {
+        this.showErr = true;
+        this.errMessage = failure;
+      }
+    ).finally(() => this._updateRules());
   }
 
   pull () {
--- a/src/app/components/jvm-info/byteman/rules/byteman-rules.controller.spec.js	Thu Sep 28 15:14:47 2017 -0400
+++ b/src/app/components/jvm-info/byteman/rules/byteman-rules.controller.spec.js	Fri Sep 29 12:57:16 2017 -0400
@@ -39,6 +39,9 @@
     };
 
     translate = sinon.stub();
+    translate.withArgs('byteman.rules.COMMAND_CHANNEL_REQUEST_FAILED_TITLE').returns({
+      then: sinon.stub().yields('Request Failed')
+    });
     translate.then = sinon.stub();
     translate.returns({ then: translate.then });
 
@@ -60,6 +63,10 @@
   });
 
   describe('$onInit ()', () => {
+    it('should set error message title', () => {
+      ctrl.errTitle.should.equal('Request Failed');
+    });
+
     it('should load injected rules', () => {
       svc.getLoadedRules.should.not.be.called();
       svc.getLoadedRules.returns({
@@ -107,11 +114,53 @@
         then: sinon.stub().yields('')
       });
       svc.unloadRules.returns({
-        then: sinon.stub().yields()
+        then: sinon.stub().yields({
+          status: true,
+          reason: ''
+        }).returns({
+          finally: sinon.stub().yields()
+        })
       });
       ctrl.unload();
       ctrl.loadedRule.should.equal('');
     });
+
+    it('should set error flag if request succeeds with non-OK response', () => {
+      svc.getLoadedRules.returns({
+        then: sinon.stub().yields('fake rule')
+      });
+      ctrl.refresh();
+      svc.unloadRules.returns({
+        then: sinon.stub().yields({
+          status: false,
+          reason: 'some error message'
+        }).returns({
+          finally: sinon.stub().yields()
+        })
+      });
+
+      ctrl.unload();
+
+      ctrl.showErr.should.be.true();
+      ctrl.errMessage.should.equal('some error message');
+    });
+
+    it('should set error flag if request fails', () => {
+      svc.getLoadedRules.returns({
+        then: sinon.stub().yields('fake rule')
+      });
+      ctrl.refresh();
+      svc.unloadRules.returns({
+        then: sinon.stub().callsArgWith(1, 'some error message').returns({
+          finally: sinon.stub().yields()
+        })
+      });
+
+      ctrl.unload();
+
+      ctrl.showErr.should.be.true();
+      ctrl.errMessage.should.equal('some error message');
+    });
   });
 
   describe('push ()', () => {
@@ -119,7 +168,12 @@
       const injectedRule = 'injected rule';
       ctrl.ruleText = injectedRule;
       svc.loadRule.returns({
-        then: sinon.stub().yields()
+        then: sinon.stub().yields({
+          status: true,
+          reason: ''
+        }).returns({
+          finally: sinon.stub().yields()
+        })
       });
       svc.getLoadedRules.returns({
         then: sinon.stub().yields(injectedRule)
@@ -127,6 +181,52 @@
 
       ctrl.push();
 
+      ctrl.showErr.should.be.false();
+      svc.loadRule.should.be.calledOnce();
+      svc.loadRule.should.be.calledWith(stateParams.systemId, stateParams.jvmId, injectedRule);
+      ctrl.loadedRule.should.equal(injectedRule);
+    });
+
+    it('should set error flag if request succeeds with non-OK response', () => {
+      const injectedRule = 'injected rule';
+      ctrl.ruleText = injectedRule;
+      svc.loadRule.returns({
+        then: sinon.stub().yields({
+          status: false,
+          reason: 'some error message'
+        }).returns({
+          finally: sinon.stub().yields()
+        })
+      });
+      svc.getLoadedRules.returns({
+        then: sinon.stub().yields(injectedRule)
+      });
+
+      ctrl.push();
+
+      ctrl.showErr.should.be.true();
+      ctrl.errMessage.should.equal('some error message');
+      svc.loadRule.should.be.calledOnce();
+      svc.loadRule.should.be.calledWith(stateParams.systemId, stateParams.jvmId, injectedRule);
+      ctrl.loadedRule.should.equal(injectedRule);
+    });
+
+    it('should set error flag if request fails', () => {
+      const injectedRule = 'injected rule';
+      ctrl.ruleText = injectedRule;
+      svc.loadRule.returns({
+        then: sinon.stub().callsArgWith(1, 'some error message').returns({
+          finally: sinon.stub().yields()
+        })
+      });
+      svc.getLoadedRules.returns({
+        then: sinon.stub().yields(injectedRule)
+      });
+
+      ctrl.push();
+
+      ctrl.showErr.should.be.true();
+      ctrl.errMessage.should.equal('some error message');
       svc.loadRule.should.be.calledOnce();
       svc.loadRule.should.be.calledWith(stateParams.systemId, stateParams.jvmId, injectedRule);
       ctrl.loadedRule.should.equal(injectedRule);
@@ -171,9 +271,10 @@
         then: sinon.stub().yields('com.example.FooClass')
       });
       translate.then.yields('rule template');
+      translate.should.be.calledOnce();
       ctrl.generateTemplate();
-      translate.should.be.calledOnce();
-      translate.should.be.calledWith('byteman.rules.RULE_TEMPLATE', { mainClass: 'com.example.FooClass' });
+      translate.should.be.calledTwice();
+      translate.secondCall.should.be.calledWith('byteman.rules.RULE_TEMPLATE', { mainClass: 'com.example.FooClass' });
       ctrl.ruleText.should.equal('rule template');
     });
   });
--- a/src/app/components/jvm-info/byteman/rules/byteman-rules.html	Thu Sep 28 15:14:47 2017 -0400
+++ b/src/app/components/jvm-info/byteman/rules/byteman-rules.html	Fri Sep 29 12:57:16 2017 -0400
@@ -1,3 +1,4 @@
+<customizable-error-message ng-show="$ctrl.showErr" dismissible="true" err-title="$ctrl.errTitle" err-message="$ctrl.errMessage"></customizable-error-message>
 <div class="col-md-8">
 
   <div class="row">
--- a/src/app/components/jvm-info/byteman/rules/en.locale.yaml	Thu Sep 28 15:14:47 2017 -0400
+++ b/src/app/components/jvm-info/byteman/rules/en.locale.yaml	Fri Sep 29 12:57:16 2017 -0400
@@ -17,3 +17,4 @@
       DO
       send("foo-marker", "action", "{{mainClass}}.main() called");
       ENDRULE
+    COMMAND_CHANNEL_REQUEST_FAILED_TITLE: Command failed.