Override default Distribution Download URL with customTestDistributionUrl for OpenSearch, which is passed as a parameter from a plugin.#1737
Override default Distribution Download URL with customTestDistributionUrl for OpenSearch, which is passed as a parameter from a plugin.#1737Rishikesh1159 wants to merge 9 commits intoopensearch-project:mainfrom Rishikesh1159:main
Conversation
…nUrl for OpenSearch, which is passed as a parameter from a plugin. Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
|
Can one of the admins verify this patch? |
|
start gradle check |
dblock
left a comment
There was a problem hiding this comment.
I expect the behavior of this feature to be "if I set the distribution URL, that's what's used and nothing else". So this is close! Let's document this as well as part of this PR.
| private NamedDomainObjectContainer<OpenSearchDistribution> distributionsContainer; | ||
| private NamedDomainObjectContainer<DistributionResolution> distributionsResolutionStrategiesContainer; | ||
|
|
||
| private static String customUrl; |
There was a problem hiding this comment.
Name consistently, I would just do distributionUrl and setDistributionUrl since it's downloading ... a distribution.
There was a problem hiding this comment.
Sure, I will rename them accordingly
| if (customUrl != null && !customUrl.isEmpty()) { | ||
| addIvyRepo(project, DOWNLOAD_REPO_NAME, customUrl, FAKE_IVY_GROUP, ""); | ||
| addIvyRepo(project, SNAPSHOT_REPO_NAME, customUrl, FAKE_SNAPSHOT_IVY_GROUP, ""); | ||
| customUrl = null; |
There was a problem hiding this comment.
When I was testing in local, if I don't reset the customUrl to null it is getting cached by gradle and when someother plugin/method which don't want to use customUrl calls this method setupDownloadServiceRepo(), the customurl value is still being set to old value and condition in if block will be true and executed. So, to avoid this I had to reset the customUrl value. I observed when a plugin individually is tested against Opensearch it executes this method setupDownloadServiceRepo() only once.
There was a problem hiding this comment.
You have added a static property, so when you set it, it gets set globally, and forever. You need to figure out a way of how to set it only for the scope/instance that you are interested in instead.
There was a problem hiding this comment.
Yes I thought about it, but there is certain way the parameter is being passed to DistributionDownloadPlugin.java which makes to use static variable.
--> First when plugin in ran against Opensearch it calls "TestClustersPlugin.java", here a call to DistributionDownoadPlugin.java is happening only once with project as parameter.
--> But Project values are still unset when a call to distributionDownloadPlugin.java is happening. So, I currently don't have custom URL passed by user from plugin. This file uses afterEvaluate() method which is listener interface and make some methods of file execute after project is evaluated.
--> Custom URL is passed to OpenSearchCluster.java using a setter method. This file is called by TestClustersPlugin.java after calling DistributionDownloadPlugin.java. So by the time I have custom URL from plugin, call to DistributionDownloadPlugin.java is already done.
--> If I try to create another instance of DistributionDownloadPlugin.java after getting custom URL from plugin with same project as parameter, it throws an error that "this project is already present".
--> So, the only option I had to send custom URL to DistributionDownloadPlugin.java was to use a static setter and call it by it's class name.
--> I have tried different ways to avoid setting a static variable, but they were failing. I will try asking other developers for suggestions to avoid it.
Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
nknize
left a comment
There was a problem hiding this comment.
/cc @saratvemulapalli can you review? Make sure this is the approach that was discussed on the issue.
Let's also make sure this isn't opening something nefarious by exposing unfriendly URLs to the build process. Before approval be sure to have good documentation for this (e.g., javadocs) public methods that enable changing the download URL are sneaky so this isn't clear whether this is a benign change.
| // This method is called by plugins to override default Url | ||
| public static void setDistributionUrl(String distributionUrl) { | ||
| DistributionDownloadPlugin.setDistributionUrl(distributionUrl); | ||
| } | ||
|
|
There was a problem hiding this comment.
-
Instead of exposing a public static method, let the interface define this method
TestClusterConfiguration,OpenSearchCluster.javaandOpenSearchNode.javacan override it. -
And since its really a custom url, I would rename it to
setCustomDistributionUrl -
Instead of directly exposing OpenSearchCluster to DistributionDownloadPlugin, pass this information to OpenSearchNode.java which should set the right distribution. OpenSearchNode.java is designed to setup up the node for tests.
| // This method is called by plugins to override default Url | |
| public static void setDistributionUrl(String distributionUrl) { | |
| DistributionDownloadPlugin.setDistributionUrl(distributionUrl); | |
| } | |
| // This method sets a custom distribution url overriding Ivy repository configured. | |
| @Override | |
| public void setCustomDistributionUrl(String customDistributionUrl) { | |
| ... | |
| } | |
| public static void setDistributionUrl(String distributionUrl) { | ||
| DistributionDownloadPlugin.distributionUrl = distributionUrl; | ||
| } | ||
|
|
||
| // pkg private for tests and set up Download Repository |
There was a problem hiding this comment.
Is there a way to avoid distributionUrl to be static?
Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
|
start gradle check |
|
start gradle check |
|
I figured out a new approach for solving this issue which is much cleaner and simple. Here we will directly set customDistributionUrl as an external property (in ext {}section) in build.gradle file of plugin (ex:anomaly detection plugin). We can access this external property by method "project.findProperty(“customDistributionUrl”)." This is possible because we are moving setupDownloadServiceRepo() to setupDistributions() in DistributionDownloadPlugin.java. We are moving this into setupDistribution() method because this method is executed after project is evaluated and by then project is configured and we have all properties set. I think this approach will solve all of requested changes for this PR. Please comment on this approach, if this is any better than previous one and any changes needed |
There was a problem hiding this comment.
Very nice work @Rishikesh1159 !!
Love this new approach.
Overall it looks good to me, dropped a comment on tests.
| assertEquals(distro.getType(), Type.ARCHIVE); | ||
| } | ||
|
|
||
| public void testCustomDistributionUrl() { |
There was a problem hiding this comment.
Could you add another test which sets the customDistributionUrl and make sure DistributionDownloadPlugin downloads the artifact?
There was a problem hiding this comment.
Sure @saratvemulapalli I will add another test for this scenario.
| addIvyRepo(project, SNAPSHOT_REPO_NAME, "https://artifacts.opensearch.org", FAKE_SNAPSHOT_IVY_GROUP, SNAPSHOT_PATTERN_LAYOUT); | ||
| // checks if custom Distribution Url has been passed by user from plugins | ||
| if (project.findProperty("customDistributionUrl") != null) { | ||
| String customDistributionUrl = project.findProperty("customDistributionUrl").toString(); |
There was a problem hiding this comment.
Nit: avoid doing 2 lookups.
... customDistributionUrl = project.findProperty("customDistributionUrl")
if (customDistributionUrl != null) {
...
…at changes. Signed-off-by: Rishikesh1159 <rishireddy1159@gmail.com>
As both addIvyRepo() snd setupDownloadServiceRepo() are private methods, so I find only way to mock them is using PowerMocks. If we don't want use PowerMocks we need to change access modifiers of both these methods. @saratvemulapalli can you please help me how proceed further with this? |
|
@Rishikesh1159 Any good reason not make these methods public? |
|
Accidently deleted the fork repo in which I opened this PR. Closing this PR now. New PR is opened to solve this, PR #2086 . Changes suggested and new discussion can found in new PR. |
Override default Distribution Download URL with custom URL
Signed-off-by: Rishikesh1159 rishireddy1159@gmail.com
Description
It Overrides Distribution Download URL with customTestDistributionUrl which is passed as a parameter from plugin. Along with changes made in this PR, plugins should make subsequent change to allow user to pass custom url from CLI.
Issues Resolved
#1240
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.