-
Notifications
You must be signed in to change notification settings - Fork 34
JENKINS-33925 Sample realization of the cps invoker interceptor #107
Conversation
@sparshev Can you use
And then in setup code you call |
Hi @dwnusbaum When I checked the possible ways to intercept the call I thought I saw this way only for the SandboxInvoker (probably it's not working for DefaultInvoker which is used to call the trusted sources like shared libraries), but I will check it again to confirm. Thank you) |
So, my experiments showing the next results:
Right now not sure how to properly register my invoker outside of the executing thread, but looks like scriptsecurity plugin made it - so it's possible. Let me try to investigate this way, for sure it's much better to use the existing intercepting method - for example by Extension. |
@dwnusbaum probably there is no such extension - I checked stacktraces from the interesting registration of GroovyInterceptor by GroovySandbox.enter():
Looks like it's hardcoded in the CpsGroovyShell Maybe you see any way to run |
Hello @dwnusbaum, I suppose there was no movement in such direction? How I can help with introducing such tracing for jenkins pipeline jobs? The hardcode looks not great I think... |
@sparshev Is this something that you still care about? In general, we do not really have the bandwidth to support any direct usage of this library outside of |
Hi @dwnusbaum , Yeah I agree they should be simpler, but it will not help if I will give such recommendation to the users. The need of unit testing will not go anyway even with simpler pipelines (you know, they are not simple in the wild anyway). The only thing I can do is to simplify their lives with better interfaces and tools to handle them. Unfortunately pipelines become more and more complicated (because they are actually useful) and I suppose there should be a way to properly test the libraries and validate their steps (even if they are simple ) for security and stability reasons. For sure there is much more important work (like Jenkins clusterization or at least database hooks/plugins) - but I believe this kind of proposed interface will make the life of regular pipeline users much easier if they would be able to run the same Jenkins with needed plugins and dry-run the pipelines they have to check the unit-tests asserts. Maybe there is another way to dry-run the pipelines - but the proposed change is the best I was able to find for now... Thank you |
@sparshev Sorry I should have been more specific: are you actively using this patch and/or still trying to get it merged? I do not quite understand your concerns related to extending from |
Yeah, the change was born to show how to simplify the pipeline steps interception which is handy for the tracers - right now in griddynamics/mpl#49 I have to override the entire So I don't use this way until it will be stabilized - too dangerous and still hope for merge or some another way to have such pipeline dry-run / interception capabilities built in jenkins. The proposed changes creates "class interface" for easy override of the simple interface-purposed |
Sorry if I am missing something obvious, but your usage just shows a definition of an abstract class. It's not clear to me how this does anything at all. It's also not clear to me what you mean by "overriding" To say it another way, how does the new |
@dwnusbaum for sure it's not obvious, sorry my explanations are misleading. Let me try to deep dive into the changes: When I put something in In griddynamics/mpl#49 - I override the Hopefully now it's more clear. Just keep in mind - this change & mpl#49 implements different logic to achieve almost the same results - if this change will be accepted I will be able to make mpl#49 a bit simpler with much more confidence in the near future. |
Ok, thanks for explaining! That seems like a somewhat unusual approach, and I would not really consider it an API even with this PR which would simplify things for you. In general we just do not have a good answer here. I do not think it makes sense to merge a PR like this when we are not willing to provide any guarantees about supporting it. In recent PRs like #121 we are actively trying to get rid of anything in this library that appears to be unused by
Maybe you could prepend code to the beginning of the Pipeline for each test case to instantiate some custom That said, I do not think the current state of griddynamics/mpl#49 is all that bad. Sure, you might have to update it once a year or something to adapt to code changes, but you will already need to spend some effort updating your tool to to new versions of Jenkins and For now I am going to close this PR, just because our current position is that the only supported consumer of this library is |
Thank you @dwnusbaum , will check the ways you've described and maybe will come with another approach :) |
fixes: #106
Realization:
JenkinsRule allows to run actual jenkins with actual plugins, but it's hard to intercept the calls. That could be done through CpsScript & CPSClosure (workflow-cps-plugin) or through Invoker (groovy-cps). Intercepting of CpsScript is hard (final method, too much logic...), but intercepting the Invoker is easier and allows better control of the execution, so the best candidate to intercept.
The main idea behind the changes is to provide a relatively easy way for custom interceptions. I think it’s the most secure way that requires a minimal number of changes and allows to prepare a custom logic for the ones who want to prepare flexible unit/integration tests of the CPS logic.
It changes the interface for the invokers which could be intercepted, but should not break not break the basic Invoker interface.
I thought about a number of interceptions to the existing code:
com.cloudbees.groovy.cps.sandbox.DefaultInvoker
andcom.cloudbees.groovy.cps.sandbox.SandboxInvoker
- looks ok, but not for debugging - it’s hard to understand what the class is used - the original one or the overridden one.com.cloudbees.groovy.cps.sandbox.Invoker
andcom.cloudbees.groovy.cps.impl.FunctionCallEnv
with creating extensionsMPLDefaultInvoker
&MPLSandboxInvoker
- the traces looks better and showing where the issue and that the Invoker classes are overridden, but still requires too much original code from groovy-cps.Usage
With this changes - all the user need to do - is to add an override the original
InvokerInterceptor
:test/java/com/cloudbees/groovy/cps/sandbox/InvokerInterceptor.java
:Notice
Quite the same was prepared for MPL (right now using the second solution)