22
22
import java .util .Optional ;
23
23
24
24
import org .junit .jupiter .api .Test ;
25
+ import org .kie .kogito .addons .quarkus .k8s .test .utils .KubeTestUtils ;
26
+ import org .kie .kogito .addons .quarkus .k8s .test .utils .OpenShiftMockServerTestResource ;
25
27
26
28
import io .fabric8 .kubernetes .api .model .ContainerPortBuilder ;
27
29
import io .fabric8 .kubernetes .api .model .IntOrString ;
30
32
import io .fabric8 .kubernetes .api .model .ServicePortBuilder ;
31
33
import io .fabric8 .kubernetes .api .model .apps .Deployment ;
32
34
import io .fabric8 .kubernetes .api .model .apps .ReplicaSet ;
33
- import io .fabric8 .kubernetes .client .server .mock .KubernetesServer ;
35
+ import io .fabric8 .openshift .client .OpenShiftClient ;
36
+ import io .quarkus .test .common .QuarkusTestResource ;
34
37
import io .quarkus .test .junit .QuarkusTest ;
35
- import io .quarkus .test .kubernetes .client .KubernetesTestServer ;
36
- import io .quarkus .test .kubernetes .client .WithKubernetesTestServer ;
37
38
38
39
import jakarta .inject .Inject ;
39
40
40
41
import static org .junit .jupiter .api .Assertions .assertEquals ;
41
42
import static org .junit .jupiter .api .Assertions .assertTrue ;
42
43
43
- /**
44
- * This tests also covers the queryServiceByLabelOrSelector method from {@link ServiceUtils}
45
- * and queryPodByOwnerReference from {@link PodUtils}
46
- */
47
44
@ QuarkusTest
48
- @ WithKubernetesTestServer
45
+ @ QuarkusTestResource ( OpenShiftMockServerTestResource . class )
49
46
public class DeploymentUtilsTest {
50
47
51
- @ KubernetesTestServer
52
- KubernetesServer mockServer ;
53
-
48
+ private final String namespace = "serverless-workflow-greeting-quarkus" ;
49
+ @ Inject
50
+ OpenShiftClient client ;
54
51
@ Inject
55
52
KubernetesResourceDiscovery discovery ;
56
53
57
- private final String namespace = "serverless-workflow-greeting-quarkus" ;
58
-
59
54
@ Test
60
55
public void testNotFoundDeployment () {
61
- Deployment deployment = mockServer . getClient () .apps ().deployments ().inNamespace (namespace )
62
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment.yaml" )).item ();
56
+ Deployment deployment = client .apps ().deployments ().inNamespace (namespace )
57
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment.yaml" )).item ();
63
58
deployment .getMetadata ().setName ("test" );
64
- mockServer .getClient ().resource (deployment ).inNamespace (namespace ).createOrReplace ();
59
+
60
+ KubeTestUtils .createWithStatusPreserved (client , deployment , namespace , Deployment .class );
61
+
65
62
assertEquals (Optional .empty (),
66
63
discovery .query (KubernetesResourceUri .parse ("deployments.v1.apps/" + namespace + "/invalid" )));
67
64
}
@@ -70,13 +67,13 @@ public void testNotFoundDeployment() {
70
67
public void testDeploymentWithService () {
71
68
var kubeURI = KubernetesResourceUri .parse ("deployments.v1.apps/" + namespace + "/example-deployment-with-service" );
72
69
73
- Deployment deployment = mockServer . getClient () .apps ().deployments ().inNamespace (namespace )
74
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment.yaml" )).item ();
75
- mockServer . getClient (). resource ( deployment ). inNamespace ( namespace ). createOrReplace ( );
70
+ Deployment deployment = client .apps ().deployments ().inNamespace (namespace )
71
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment.yaml" )).item ();
72
+ KubeTestUtils . createWithStatusPreserved ( client , deployment , namespace , Deployment . class );
76
73
77
- Service service = mockServer . getClient () .services ().inNamespace (namespace )
78
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-service.yaml" )).item ();
79
- mockServer . getClient (). resource ( service ). inNamespace ( namespace ). createOrReplace ( );
74
+ Service service = client .services ().inNamespace (namespace )
75
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-service.yaml" )).item ();
76
+ KubeTestUtils . createWithStatusPreserved ( client , service , namespace , Service . class );
80
77
81
78
Optional <String > url = discovery .query (kubeURI ).map (URI ::toString );
82
79
assertEquals ("http://10.10.10.11:80" , url .get ());
@@ -86,21 +83,21 @@ public void testDeploymentWithService() {
86
83
public void testDeploymentWithServiceWithCustomPortName () {
87
84
var kubeURI = KubernetesResourceUri .parse ("deployments.v1.apps/" + namespace + "/custom-port-deployment?port-name=my-custom-port" );
88
85
89
- Deployment deployment = mockServer . getClient () .apps ().deployments ().inNamespace (namespace )
90
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment.yaml" )).item ();
86
+ Deployment deployment = client .apps ().deployments ().inNamespace (namespace )
87
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment.yaml" )).item ();
91
88
deployment .getMetadata ().setName ("custom-port-deployment" );
92
89
deployment .getSpec ().getTemplate ().getSpec ().getContainers ().get (0 ).getPorts ()
93
90
.add (new ContainerPortBuilder ().withName ("test-port" ).withContainerPort (4000 ).build ());
94
- mockServer . getClient (). resource ( deployment ). inNamespace ( namespace ). createOrReplace ( );
91
+ KubeTestUtils . createWithStatusPreserved ( client , deployment , namespace , Deployment . class );
95
92
96
- Service service = mockServer . getClient () .services ().inNamespace (namespace )
97
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-service.yaml" )).item ();
93
+ Service service = client .services ().inNamespace (namespace )
94
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-service.yaml" )).item ();
98
95
service .getMetadata ().setName ("custom-port-name-service" );
99
96
service .getSpec ().getPorts ().add (new ServicePortBuilder ()
100
97
.withName ("my-custom-port" )
101
98
.withTargetPort (new IntOrString ("test-port" ))
102
99
.withPort (4009 ).build ());
103
- mockServer . getClient (). resource ( service ). inNamespace ( namespace ). createOrReplace ( );
100
+ KubeTestUtils . createWithStatusPreserved ( client , service , namespace , Service . class );
104
101
105
102
Optional <String > url = discovery .query (kubeURI ).map (URI ::toString );
106
103
assertEquals ("http://10.10.10.11:4009" , url .get ());
@@ -110,20 +107,20 @@ public void testDeploymentWithServiceWithCustomPortName() {
110
107
public void testDeploymentNoService () {
111
108
var kubeURI = KubernetesResourceUri .parse ("deployments.v1.apps/" + namespace + "/example-deployment-no-service" );
112
109
113
- Deployment deployment = mockServer . getClient () .apps ().deployments ().inNamespace (namespace )
114
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-no-service.yaml" )).item ();
115
- Deployment createdDeployment = mockServer . getClient (). resource ( deployment ). inNamespace ( namespace ). createOrReplace ( );
110
+ Deployment deployment = client .apps ().deployments ().inNamespace (namespace )
111
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-no-service.yaml" )).item ();
112
+ deployment = KubeTestUtils . createWithStatusPreserved ( client , deployment , namespace , Deployment . class );
116
113
117
- ReplicaSet rs = mockServer . getClient () .apps ().replicaSets ().inNamespace (namespace )
118
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/replica-set-deployment-no-service.yaml" )).item ();
119
- rs .getMetadata ().getOwnerReferences ().get (0 ).setUid (createdDeployment .getMetadata ().getUid ());
120
- ReplicaSet createdRs = mockServer . getClient (). resource ( rs ). inNamespace ( namespace ). createOrReplace ( );
114
+ ReplicaSet rs = client .apps ().replicaSets ().inNamespace (namespace )
115
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/replica-set-deployment-no-service.yaml" )).item ();
116
+ rs .getMetadata ().getOwnerReferences ().get (0 ).setUid (deployment .getMetadata ().getUid ());
117
+ rs = KubeTestUtils . createWithStatusPreserved ( client , rs , namespace , ReplicaSet . class );
121
118
122
- Pod pod = mockServer . getClient () .pods ().inNamespace (namespace )
123
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/pod-deployment-no-service.yaml" )).item ();
119
+ Pod pod = client .pods ().inNamespace (namespace )
120
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/pod-deployment-no-service.yaml" )).item ();
124
121
pod .getMetadata ().setName ("pod-deployment-no-service" );
125
- pod .getMetadata ().getOwnerReferences ().get (0 ).setUid (createdRs .getMetadata ().getUid ());
126
- mockServer . getClient (). resource ( pod ). inNamespace ( namespace ). createOrReplace ( );
122
+ pod .getMetadata ().getOwnerReferences ().get (0 ).setUid (rs .getMetadata ().getUid ());
123
+ KubeTestUtils . createWithStatusPreserved ( client , pod , namespace , Pod . class );
127
124
128
125
Optional <String > url = discovery .query (kubeURI ).map (URI ::toString );
129
126
assertEquals ("http://172.17.0.11:8080" , url .get ());
@@ -133,23 +130,23 @@ public void testDeploymentNoService() {
133
130
public void testDeploymentNoService2Replicas () {
134
131
var kubeURI = KubernetesResourceUri .parse ("deployments.v1.apps/" + namespace + "/example-deployment-no-service-2-replicas" );
135
132
136
- Deployment deployment = mockServer . getClient () .apps ().deployments ().inNamespace (namespace )
137
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-no-service.yaml" )).item ();
133
+ Deployment deployment = client .apps ().deployments ().inNamespace (namespace )
134
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-no-service.yaml" )).item ();
138
135
deployment .getMetadata ().setName ("example-deployment-no-service-2-replicas" );
139
136
deployment .getStatus ().setReplicas (2 );
140
- Deployment createdDeployment = mockServer . getClient (). resource ( deployment ). inNamespace ( namespace ). createOrReplace ( );
137
+ KubeTestUtils . createWithStatusPreserved ( client , deployment , namespace , Deployment . class );
141
138
142
- ReplicaSet rs = mockServer . getClient () .apps ().replicaSets ().inNamespace (namespace )
143
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/replica-set-deployment-no-service.yaml" )).item ();
139
+ ReplicaSet rs = client .apps ().replicaSets ().inNamespace (namespace )
140
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/replica-set-deployment-no-service.yaml" )).item ();
144
141
rs .getMetadata ().setName ("rs-2-replicas" );
145
- rs .getMetadata ().getOwnerReferences ().get (0 ).setUid (createdDeployment .getMetadata ().getUid ());
146
- ReplicaSet createdRs = mockServer . getClient (). resource ( rs ). inNamespace ( namespace ). createOrReplace ( );
142
+ rs .getMetadata ().getOwnerReferences ().get (0 ).setUid (deployment .getMetadata ().getUid ());
143
+ KubeTestUtils . createWithStatusPreserved ( client , rs , namespace , ReplicaSet . class );
147
144
148
- Pod pod = mockServer . getClient () .pods ().inNamespace (namespace )
149
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/pod-deployment-no-service.yaml" )).item ();
145
+ Pod pod = client .pods ().inNamespace (namespace )
146
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/pod-deployment-no-service.yaml" )).item ();
150
147
pod .getMetadata ().setName ("pod-2-replicas" );
151
- pod .getMetadata ().getOwnerReferences ().get (0 ).setUid (createdRs .getMetadata ().getUid ());
152
- mockServer . getClient (). resource ( pod ). inNamespace ( namespace ). createOrReplace ( );
148
+ pod .getMetadata ().getOwnerReferences ().get (0 ).setUid (rs .getMetadata ().getUid ());
149
+ KubeTestUtils . createWithStatusPreserved ( client , pod , namespace , Pod . class );
153
150
154
151
Optional <String > url = discovery .query (kubeURI ).map (URI ::toString );
155
152
assertTrue (url .isEmpty ());
@@ -159,27 +156,25 @@ public void testDeploymentNoService2Replicas() {
159
156
public void testDeploymentNoServiceCustomPortName () {
160
157
var kubeURI = KubernetesResourceUri .parse ("deployments.v1.apps/" + namespace + "/custom-port-deployment-1?port-name=my-custom-port" );
161
158
162
- Deployment deployment = mockServer . getClient () .apps ().deployments ().inNamespace (namespace )
163
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-no-service.yaml" )).item ();
159
+ Deployment deployment = client .apps ().deployments ().inNamespace (namespace )
160
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/deployment-no-service.yaml" )).item ();
164
161
deployment .getMetadata ().setName ("custom-port-deployment-1" );
165
162
deployment .getSpec ().getTemplate ().getSpec ().getContainers ().get (0 ).getPorts ()
166
163
.add (new ContainerPortBuilder ().withName ("test-port" ).withContainerPort (4000 ).build ());
167
- Deployment createdDeployment = mockServer . getClient (). resource ( deployment ). inNamespace ( namespace ). createOrReplace ( );
164
+ deployment = KubeTestUtils . createWithStatusPreserved ( client , deployment , namespace , Deployment . class );
168
165
169
- ReplicaSet rs = mockServer . getClient () .apps ().replicaSets ().inNamespace (namespace )
170
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/replica-set-deployment-no-service.yaml" )).item ();
166
+ ReplicaSet rs = client .apps ().replicaSets ().inNamespace (namespace )
167
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/replica-set-deployment-no-service.yaml" )).item ();
171
168
rs .getMetadata ().setName ("custom-port-rs" );
172
- rs .getMetadata ().getOwnerReferences ().get (0 ).setUid (createdDeployment .getMetadata ().getUid ());
173
- ReplicaSet createdRs = mockServer . getClient (). resource ( rs ). inNamespace ( namespace ). createOrReplace ( );
169
+ rs .getMetadata ().getOwnerReferences ().get (0 ).setUid (deployment .getMetadata ().getUid ());
170
+ rs = KubeTestUtils . createWithStatusPreserved ( client , rs , namespace , ReplicaSet . class );
174
171
175
- Pod pod = mockServer . getClient () .pods ().inNamespace (namespace )
176
- .load (this . getClass ().getClassLoader ().getResourceAsStream ("deployment/pod-deployment-no-service.yaml" )).item ();
177
- pod .getMetadata ().getOwnerReferences ().get (0 ).setUid (createdRs .getMetadata ().getUid ());
172
+ Pod pod = client .pods ().inNamespace (namespace )
173
+ .load (getClass ().getClassLoader ().getResourceAsStream ("deployment/pod-deployment-no-service.yaml" )).item ();
174
+ pod .getMetadata ().getOwnerReferences ().get (0 ).setUid (rs .getMetadata ().getUid ());
178
175
pod .getSpec ().getContainers ().get (0 ).getPorts ()
179
- .add (new ContainerPortBuilder ()
180
- .withName ("my-custom-port" )
181
- .withContainerPort (4009 ).build ());
182
- mockServer .getClient ().resource (pod ).inNamespace (namespace ).createOrReplace ();
176
+ .add (new ContainerPortBuilder ().withName ("my-custom-port" ).withContainerPort (4009 ).build ());
177
+ KubeTestUtils .createWithStatusPreserved (client , pod , namespace , Pod .class );
183
178
184
179
Optional <String > url = discovery .query (kubeURI ).map (URI ::toString );
185
180
assertEquals ("http://172.17.0.11:4009" , url .get ());
0 commit comments