Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Kube API Test #5711

Merged
merged 41 commits into from Feb 20, 2024
Merged

feat: Kube API Test #5711

merged 41 commits into from Feb 20, 2024

Conversation

csviri
Copy link
Contributor

@csviri csviri commented Jan 17, 2024

Description

Moving and renaming Jenvtest here.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

@csviri
Copy link
Contributor Author

csviri commented Jan 17, 2024

TODO for myself:

  • Add documentation
  • Update changelog

@@ -123,6 +123,7 @@
<asm.bundle.version>8.0.1</asm.bundle.version>
<jackson.bundle.version>${jackson.version}</jackson.bundle.version>
<slf4j.version>2.0.11</slf4j.version>
<log4j.version>2.22.0</log4j.version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll eventually want to unify the logging system

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is just for tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, but the idea is to keep dependencies to the minimum. Anyway, this is not urgent or critical.

pom.xml Outdated Show resolved Hide resolved
private void setKubernetesClientToField(ExtensionContext extensionContext,
Field kubeClientField, KubeAPIServer kubeApiServer) {
try {
var target = extensionContext.getTestInstance()
Copy link
Member

@manusa manusa Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self:
Once merged create issue to track the creation of a common junit module that provides the tooling to set fields, especially with the consideration for @Nested

Comment on lines +92 to +94
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self:
Curious about this one. Review current usage, see if can be easily replaced by Vert.x.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can try to replace it with Very.x. Maybe a subsequent PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, don't worry, this is just a reminder for me, especially with regards to #5632

import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

public class CertManager {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Vert.x provides this functionality, see SelfSignedCertificate.create

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you ok to have a separate PR for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

new TrustManager[] {
new X509ExtendedTrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType,

Check failure

Code scanning / SonarCloud

Server certificates should be verified during SSL/TLS connections High

Enable server certificate validation on this SSL/TLS connection. See more on SonarCloud
return null;
}

public void checkClientTrusted(

Check failure

Code scanning / SonarCloud

Server certificates should be verified during SSL/TLS connections High

Enable server certificate validation on this SSL/TLS connection. See more on SonarCloud
final String a_auth_type) {
}

public void checkServerTrusted(

Check failure

Code scanning / SonarCloud

Server certificates should be verified during SSL/TLS connections High

Enable server certificate validation on this SSL/TLS connection. See more on SonarCloud
final String a_auth_type) {
}

public void checkServerTrusted(

Check failure

Code scanning / SonarCloud

Server certificates should be verified during SSL/TLS connections High

Enable server certificate validation on this SSL/TLS connection. See more on SonarCloud
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType,

Check failure

Code scanning / SonarCloud

Server certificates should be verified during SSL/TLS connections High

Enable server certificate validation on this SSL/TLS connection. See more on SonarCloud

}

public void checkServerTrusted(

Check failure

Code scanning / SonarCloud

Server certificates should be verified during SSL/TLS connections High

Enable server certificate validation on this SSL/TLS connection. See more on SonarCloud
@Test
@EnableKubeAPIServer
void simpleTest2() {
TestUtils.simpleTest(client);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reusing static method for shared tests, do you think making an abstract class for simpleTest and making these classes extend that class would be better? Just like we do in httpclient tests...

Copy link
Contributor Author

@csviri csviri Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I don't really like that pattern, when we extending tests. But if everybody agrees it should be done that way in this project, I can change it.
I personally like it better with this "composition" over inheritance approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the naming is not really good imo, maybe TestCase / TestCaseUtils might be better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like abstract tests either. However, I don't like the usage of this static test methods :).
Anyway, I'm sure we'll find a way to refactor the tests later on, especially if they are all doing the same.


import java.util.Map;

public class TestUtils {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class looks very much the same as io.fabric8.kubeapitest.junit.TestUtils . Is it possible to reduce duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is an elegant way to do this, other than test-jar:
https://stackoverflow.com/questions/7751860/how-do-i-include-a-dependencys-test-jar-into-a-maven-projects-deployment
But personally I prefer code duplication over to this.

Comment on lines 76 to 94
public static int findFreePort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {

}
return -1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, borrowed some code from jkube. should be fixed :)

Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some logic duplication that we'll need to find a way yo unify in the future.
It might also be interesting to mark these modules as (preview) with a non-stable API, just in case we need to modify a few things once we start getting broader feedback.
Other than that, seems good to go for me.

doc/kube-api-test.md Outdated Show resolved Hide resolved
doc/kube-api-test.md Outdated Show resolved Hide resolved

class CertManagerTest {

File certsDir = new File("target", "certs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong here but would it be better to create this under a@TempDir rather than in project directory itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, that would be better, for sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thx

csviri and others added 23 commits February 20, 2024 09:17
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
…junit/KubeAPIServerExtension.java

Co-authored-by: Rohan Kumar  <rohan.kumar.kanojia@gmail.com>
Co-authored-by: Rohan Kumar  <rohan.kumar.kanojia@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Signed-off-by: Attila Mészáros <csviri@gmail.com>
Copy link

sonarcloud bot commented Feb 20, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
5 Security Hotspots
D Security Rating on New Code (required ≥ A)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

idea Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

@csviri
Copy link
Contributor Author

csviri commented Feb 20, 2024

Sorry 🙏, I forgot to merge and the PR got stale :(

Please, rebase and I'll merge as soon as the checks are green.

np, rebased, the Openshift builds seem to be failing on all PR, not sure if the sonar results are good enough this way.

@manusa manusa added this to the 6.11.0 milestone Feb 20, 2024
@manusa manusa merged commit 2ccf558 into fabric8io:main Feb 20, 2024
16 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants