4
4
package oracle .weblogic .kubernetes ;
5
5
6
6
import java .util .List ;
7
+ import java .util .concurrent .Callable ;
7
8
8
9
import io .kubernetes .client .custom .Quantity ;
9
10
import io .kubernetes .client .openapi .models .V1LocalObjectReference ;
30
31
import static oracle .weblogic .kubernetes .TestConstants .METRICS_SERVER_YAML ;
31
32
import static oracle .weblogic .kubernetes .TestConstants .MII_BASIC_IMAGE_NAME ;
32
33
import static oracle .weblogic .kubernetes .TestConstants .MII_BASIC_IMAGE_TAG ;
34
+ import static oracle .weblogic .kubernetes .TestConstants .OKE_CLUSTER ;
33
35
import static oracle .weblogic .kubernetes .TestConstants .SKIP_CLEANUP ;
34
36
import static oracle .weblogic .kubernetes .TestConstants .TEST_IMAGES_REPO_SECRET_NAME ;
35
37
import static oracle .weblogic .kubernetes .assertions .TestAssertions .podDoesNotExist ;
36
38
import static oracle .weblogic .kubernetes .assertions .TestAssertions .podReady ;
39
+ import static oracle .weblogic .kubernetes .assertions .TestAssertions .serviceExists ;
37
40
import static oracle .weblogic .kubernetes .utils .ClusterUtils .createClusterAndVerify ;
38
41
import static oracle .weblogic .kubernetes .utils .ClusterUtils .createClusterResource ;
39
42
import static oracle .weblogic .kubernetes .utils .CommonMiiTestUtils .createDomainResource ;
45
48
import static oracle .weblogic .kubernetes .utils .ImageUtils .createBaseRepoSecret ;
46
49
import static oracle .weblogic .kubernetes .utils .ImageUtils .createTestRepoSecret ;
47
50
import static oracle .weblogic .kubernetes .utils .OperatorUtils .installAndVerifyOperator ;
51
+ import static oracle .weblogic .kubernetes .utils .PodUtils .checkPodReady ;
48
52
import static oracle .weblogic .kubernetes .utils .SecretUtils .createSecretWithUsernamePassword ;
49
53
import static oracle .weblogic .kubernetes .utils .ThreadSafeLogger .getLogger ;
50
54
import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
51
55
import static org .junit .jupiter .api .Assertions .assertNotNull ;
52
56
import static org .junit .jupiter .api .Assertions .assertTrue ;
53
57
58
+ /**
59
+ * Test Horizontal Pod Autoscaler using Kubernetes Metrics Server
60
+ * by increasing the CPU utilization.
61
+ * This test is not run on OKE as the CPU utilization is not
62
+ * going up intermittently after increasing the load.
63
+ */
54
64
@ DisplayName ("Test to a create MII domain and test autoscaling using HPA" )
55
65
@ IntegrationTest
56
- @ Tag ("oke-parallel" )
57
66
@ Tag ("kind-parallel" )
58
67
public class ItHorizontalPodAutoscaler {
59
68
private static String domainNamespace = null ;
@@ -240,8 +249,10 @@ private void createHPA() {
240
249
241
250
private void createLoadOnCpuAndVerifyAutoscaling () {
242
251
// execute command to increase cpu usage
252
+ int duration = (OKE_CLUSTER == true ) ? 60 : 30 ;
243
253
String cmd = "kubectl exec -t " + managedServerPrefix + "1 -n "
244
- + domainNamespace + " -- timeout --foreground -s 2 30 dd if=/dev/zero of=/dev/null" ;
254
+ + domainNamespace + " -- timeout --foreground -s 2 "
255
+ + duration + " dd if=/dev/zero of=/dev/null" ;
245
256
CommandParams params = new CommandParams ().defaults ();
246
257
params .command (cmd );
247
258
ExecResult result = Command .withParams (params ).executeAndReturnResult ();
@@ -250,15 +261,30 @@ private void createLoadOnCpuAndVerifyAutoscaling() {
250
261
251
262
// check cluster is scaled up
252
263
for (int i = 1 ; i <= 4 ; i ++) {
253
- checkPodReadyAndServiceExists (managedServerPrefix + i , domainUid , domainNamespace );
264
+ final int j = i ;
265
+ testUntil (
266
+ withLongRetryPolicy ,
267
+ assertDoesNotThrow (() -> checkHPAAndServiceExists (managedServerPrefix + j ,
268
+ domainNamespace ),
269
+ String .format ("serviceExists failed with ApiException for service %s in namespace %s" ,
270
+ managedServerPrefix + j , domainNamespace )),
271
+ logger ,
272
+ "service {0} to exist in namespace {1}" ,
273
+ managedServerPrefix + j ,
274
+ managedServerPrefix + j );
275
+
276
+ logger .info ("Waiting for pod {0} to be ready in namespace {1}" ,
277
+ managedServerPrefix + i , domainNamespace );
278
+ checkPodReady (managedServerPrefix + i , domainUid , domainNamespace );
254
279
}
255
280
256
281
// the command to increase cpu load is ran for 30 sec, after that
257
282
// it takes some time to autoscale down the cluster
258
283
for (int i = 3 ; i <= 4 ; i ++) {
259
284
final int j = i ;
260
285
testUntil (withLongRetryPolicy ,
261
- assertDoesNotThrow (() -> podDoesNotExist (managedServerPrefix + j , domainUid , domainNamespace ),
286
+ assertDoesNotThrow (() -> checkHPAAndpodDoesNotExist (
287
+ managedServerPrefix + j , domainUid , domainNamespace ),
262
288
String .format ("podDoesNotExist failed with ApiException for pod %s in namespace %s" ,
263
289
managedServerPrefix + i , domainNamespace )),
264
290
logger ,
@@ -274,9 +300,11 @@ private void createLoadOnCpuAndVerifyAutoscaling() {
274
300
// verify hpa is getting the metrics
275
301
private boolean verifyHPA (String namespace , String hpaName ) {
276
302
CommandParams params = new CommandParams ().defaults ();
303
+ params .saveResults (true );
277
304
params .command ("kubectl get hpa " + hpaName + " -n " + namespace );
278
305
279
306
ExecResult result = Command .withParams (params ).executeAndReturnResult ();
307
+ logger .info ("Get HPA result " + result );
280
308
/* check if hpa output contains something like 7%/50%
281
309
* kubectl get hpa --all-namespaces
282
310
* NAMESPACE NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
@@ -287,4 +315,14 @@ private boolean verifyHPA(String namespace, String hpaName) {
287
315
*/
288
316
return result .stdout ().contains ("%/" );
289
317
}
318
+
319
+ private Callable <Boolean > checkHPAAndpodDoesNotExist (String podName , String domainUid , String namespace ) {
320
+ verifyHPA (namespace , clusterResName );
321
+ return podDoesNotExist (podName , domainUid , namespace );
322
+ }
323
+
324
+ private Callable <Boolean > checkHPAAndServiceExists (String serviceName , String namespace ) {
325
+ verifyHPA (namespace , clusterResName );
326
+ return serviceExists (serviceName , null , namespace );
327
+ }
290
328
}
0 commit comments