Skip to content

Conversation

wind57
Copy link
Contributor

@wind57 wind57 commented May 20, 2021

No description provided.

@wind57 wind57 changed the title first attempt move fabric8-config to junit-5 May 20, 2021
import io.fabric8.kubernetes.api.model.DoneableConfigMap;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the changes here are obvious: move to junit-5 + minor nitpick on a raw type.

import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

trivial changes also : junit-5 + assertJ to be consistent


@BeforeClass
public static void setUpBeforeClass() {
// injected because of @EnableKubernetesMockClient. Because of the way
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hope this comment makes sense of why the configuration has moved in here from the common class. It is just the way @EnableKubernetesMockClient "injects" fields.


HashMap<String, String> data = new HashMap<>();
data.put("bean.greeting", "Hello ConfigMap, %s!");
server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

mock client works slightly differently now and there is no need to mock paths, instead everything happens on the client itself. It is the same exact thing, logically.


mockClient.secrets().inNamespace(DEFAULT_NAMESPACE).create(secret1);

Map<String, String> metadata2 = new HashMap() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactor the anti-pattern away

setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.config.enabled=false",
"spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.reload.enabled=true");
public void kubernetesReloadEnabledButSecretAndConfigDisabled() {
setup(KubernetesClientTestConfiguration.class, "spring.cloud.kubernetes.enabled=true",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

setup method was slightly changed to get Class<?> as input, and I pass the KubernetesClientTestConfiguration.class configuration in there. everything else stays the same

private static void createConfigmap(KubernetesClient client, String configMapName, String namespace,
Map<String, String> data) {

server.expect().withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no need to mock paths anymore

protected static final String FIRST_FILE_NAME_DUPLICATED_FULL_PATH = FILES_ROOT_PATH + "/" + FILES_SUB_PATH + "/"
+ FIRST_FILE_NAME;

@ClassRule
Copy link
Contributor Author

@wind57 wind57 May 20, 2021

Choose a reason for hiding this comment

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

this is now injected by @EnableKubernetesMockClient(crud = true, https = false) so no need for the server. the rest of the changes are trivial, imho, here.


protected static final String APPLICATION_NAME = "configmap-mixed-example";

@ClassRule
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is now injected by @EnableKubernetesMockClient(crud = true, https = false) so no need for the server. the rest of the changes are trivial, imho, here.


HashMap<String, String> data = new HashMap<>();
data.put("bean.morning", "Buenos Dias ConfigMap, %s");
server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no need to mock paths anymore

@SuppressWarnings("unchecked")
Map<String, String> data = (Map<String, String>) configMapList.getAdditionalProperties().get("data");
assertThat(data.get("KEY")).isEqualTo("123");
assertThat(configMapList.getItems().size()).isEqualTo(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

it tests the same thing, but where to find the "thing" to assert against is in a different place

@Test
public void testConfigMapFromSingleApplicationProperties() {
String configMapName = "app-properties-test";
String namespace = "app-props";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

another interesting change is that KubernetesServer used to return a NamespacedKubernetesClient, so you could do : this.server.getClient().inNamespace(namespace), specifically call inNamespace with a certain namespace. KubernetesClient that is injected can't do that so I used it's default namespace test.

As such, I deleted String namespace = "app-props";. Fundamentally, the test does the same assertions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the same change is across all tests in this class


@Autowired(required = false)
private KubernetesClient client;
// not a fan of changing the type from KubernetesClient, but because of:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was already fixed by me, waiting for a review. I've also asked if this will be back-ported. If it is (and you are OK with that, of course), I will upgrade the fabric8 libraries and revert this back.

Copy link
Contributor

Choose a reason for hiding this comment

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

What version of fabric8 was it changed in? Maybe we should make that change first, before making these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is still pending review from my PR here. The good news is that it is targeted in 4.13.4 and not 5.x.x. I am more than OK to wait for 4.13.4 release and only then merge this one.

That being said - I do have a minor question here. Why isn't this project moving towards fabric8 - 5.x.x? thank you

Copy link
Contributor

Choose a reason for hiding this comment

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

OK cool, its up to you. If you don't mind going back and making that change I am happy to merge this now.

We will move to 5.x.x in our next release train. We just don't want to make a major change like that and cause it to break people in a service release.

Copy link
Contributor Author

@wind57 wind57 May 24, 2021

Choose a reason for hiding this comment

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

I will make the change in a separate PR, as soon as 4.13.4 is released. pinky-promise!

your explanation makes perfect sense, btw. thx

@EnableKubernetesMockClient(crud = true, https = false)
public class KubernetesConfigConfigurationTest extends KubernetesConfigTestBase {

// injected because of @EnableKubernetesMockClient. Because of the way
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why this is needed in this manner is explained in a similar class, I hope it makes sense.

context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class,
ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class)
protected void setup(Class<?> mockClientConfiguration, String... env) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this method take one more argument now

@wind57
Copy link
Contributor Author

wind57 commented May 20, 2021

@ryanjbaxter ready to review. The changes are not that big (but I am biased since I stayed quite a while in this code). The one change that I wish I could perform and really can't is to drop all junit-4 dependencies (transitive ones), but because of this issue that is internally used by KubernetesMockServerExtension that is declared on @EnableKubernetesMockClient - I can't. It's not a big deal, but I wish I could have done that...

@ryanjbaxter
Copy link
Contributor

Can you mark it ready to review?

@wind57 wind57 marked this pull request as ready for review May 21, 2021 01:58
@wind57
Copy link
Contributor Author

wind57 commented May 21, 2021

done


@Autowired(required = false)
private KubernetesClient client;
// not a fan of changing the type from KubernetesClient, but because of:
Copy link
Contributor

Choose a reason for hiding this comment

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

What version of fabric8 was it changed in? Maybe we should make that change first, before making these?

@ryanjbaxter
Copy link
Contributor

Thanks for this, just one minor question!

@ryanjbaxter ryanjbaxter added this to the 2.0.3 milestone May 24, 2021
@ryanjbaxter ryanjbaxter merged commit 82afaca into spring-cloud:main May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants