From 944fb34526530a6c8302186f096837a52c0bc82e Mon Sep 17 00:00:00 2001 From: anthony_lai Date: Fri, 28 May 2021 19:08:10 +0000 Subject: [PATCH 1/2] fix bug scalingAction.sh with more than one domain --- operator/scripts/scaling/scalingAction.sh | 41 +++++------- operator/src/test/sh/scaling/cluster1.json | 63 +++++++++---------- .../src/test/sh/scaling/cluster_min3.json | 63 +++++++++---------- .../test/sh/scaling/cluster_noreplicas.json | 43 ++++++------- 4 files changed, 94 insertions(+), 116 deletions(-) diff --git a/operator/scripts/scaling/scalingAction.sh b/operator/scripts/scaling/scalingAction.sh index e0a162a10f5..4424e304183 100755 --- a/operator/scripts/scaling/scalingAction.sh +++ b/operator/scripts/scaling/scalingAction.sh @@ -148,7 +148,7 @@ function get_custom_resource_domain() { -v \ --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \ -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \ - $kubernetes_master/apis/weblogic.oracle/$domain_api_version/namespaces/$wls_domain_namespace/domains/$domain_uid) + $kubernetes_master/apis/weblogic.oracle/$domain_api_version/namespaces/$wls_domain_namespace/domains/$wls_domain_uid) if [ $? -ne 0 ]; then trace "Failed to retrieve WebLogic Domain Custom Resource Definition" exit 1 @@ -164,7 +164,7 @@ function is_defined_in_clusters() { local in_cluster_startup="False" if jq_available; then - local inClusterStartupCmd="(.items[].spec.clusters[] | select (.clusterName == \"${wls_cluster_name}\"))" + local inClusterStartupCmd="(.spec.clusters[] | select (.clusterName == \"${wls_cluster_name}\"))" local clusterDefinedInCRD=$(echo "${DOMAIN}" | jq "${inClusterStartupCmd}" 2>> ${log_file_name}) if [ "${clusterDefinedInCRD}" != "" ]; then in_cluster_startup="True" @@ -173,13 +173,11 @@ function is_defined_in_clusters() { cat > cmds-$$.py << INPUT import sys, json outer_loop_must_break = False -for i in json.load(sys.stdin)["items"]: - j = i["spec"]["clusters"] - for index, cs in enumerate(j): - if j[index]["clusterName"] == "$wls_cluster_name": - outer_loop_must_break = True - print (True) - break +for j in json.load(sys.stdin)["spec"]["clusters"]: + if j["clusterName"] == "$wls_cluster_name": + outer_loop_must_break = True + print (True) + break if outer_loop_must_break == False: print (False) INPUT @@ -195,16 +193,14 @@ function get_num_ms_in_cluster() { local DOMAIN="$1" local num_ms if jq_available; then - local numManagedServersCmd="(.items[].spec.clusters[] | select (.clusterName == \"${wls_cluster_name}\") | .replicas)" + local numManagedServersCmd="(.spec.clusters[] | select (.clusterName == \"${wls_cluster_name}\") | .replicas)" num_ms=$(echo "${DOMAIN}" | jq "${numManagedServersCmd}" 2>> ${log_file_name}) else cat > cmds-$$.py << INPUT import sys, json -for i in json.load(sys.stdin)["items"]: - j = i["spec"]["clusters"] - for index, cs in enumerate(j): - if j[index]["clusterName"] == "$wls_cluster_name": - print (j[index]["replicas"]) +for j in json.load(sys.stdin)["spec"]["clusters"]: + if j["clusterName"] == "$wls_cluster_name": + print (j["replicas"]) INPUT num_ms=`echo ${DOMAIN} | python cmds-$$.py 2>> ${log_file_name}` fi @@ -223,12 +219,11 @@ function get_num_ms_domain_scope() { local DOMAIN="$1" local num_ms if jq_available; then - num_ms=$(echo "${DOMAIN}" | jq -r '.items[].spec.replicas' 2>> ${log_file_name}) + num_ms=$(echo "${DOMAIN}" | jq -r '.spec.replicas' 2>> ${log_file_name}) else cat > cmds-$$.py << INPUT import sys, json -for i in json.load(sys.stdin)["items"]: - print (i["spec"]["replicas"]) +print (json.load(sys.stdin)["spec"]["replicas"]) INPUT num_ms=`echo ${DOMAIN} | python cmds-$$.py 2>> ${log_file_name}` fi @@ -254,17 +249,15 @@ function get_min_replicas { eval $__result=0 if jq_available; then - minReplicaCmd="(.items[].status.clusters[] | select (.clusterName == \"${clusterName}\")) \ + minReplicaCmd="(.status.clusters[] | select (.clusterName == \"${clusterName}\")) \ | .minimumReplicas" minReplicas=$(echo ${domainJson} | jq "${minReplicaCmd}" 2>> ${log_file_name}) else cat > cmds-$$.py << INPUT import sys, json -for i in json.load(sys.stdin)["items"]: - j = i["status"]["clusters"] - for index, cs in enumerate(j): - if j[index]["clusterName"] == "$clusterName": - print (j[index]["minimumReplicas"]) +for j in json.load(sys.stdin)["status"]["clusters"]: + if j["clusterName"] == "$clusterName": + print (j["minimumReplicas"]) INPUT minReplicas=`echo ${DOMAIN} | python cmds-$$.py 2>> ${log_file_name}` fi diff --git a/operator/src/test/sh/scaling/cluster1.json b/operator/src/test/sh/scaling/cluster1.json index ce08b931534..62a7ac3250d 100644 --- a/operator/src/test/sh/scaling/cluster1.json +++ b/operator/src/test/sh/scaling/cluster1.json @@ -1,36 +1,31 @@ { - "apiVersion":"weblogic.oracle/v8", - "items":[ - { - "apiVersion": "weblogic.oracle/v8", - "kind": "Domain", - "spec": { - "clusters": [ - { - "clusterName": "cluster-1", - "replicas": 1, - "serverStartState": "RUNNING" - } - ], - "dataHome": "", - "domainHome": "/shared/domains/domain1", - "domainHomeSourceType": "PersistentVolume", - "httpAccessLogInLogHome": true, - "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", - "imagePullPolicy": "IfNotPresent", - "includeServerOutInPodLog": false, - "logHome": "/shared/logs/domain1", - "logHomeEnabled": true, - "replicas": 2 - }, - "status": { - "clusters": [ - { - "clusterName": "cluster-1", - "minimumReplicas": 1 - } - ] - } - } - ] + "apiVersion": "weblogic.oracle/v8", + "kind": "Domain", + "spec": { + "clusters": [ + { + "clusterName": "cluster-1", + "replicas": 1, + "serverStartState": "RUNNING" + } + ], + "dataHome": "", + "domainHome": "/shared/domains/domain1", + "domainHomeSourceType": "PersistentVolume", + "httpAccessLogInLogHome": true, + "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", + "imagePullPolicy": "IfNotPresent", + "includeServerOutInPodLog": false, + "logHome": "/shared/logs/domain1", + "logHomeEnabled": true, + "replicas": 2 + }, + "status": { + "clusters": [ + { + "clusterName": "cluster-1", + "minimumReplicas": 1 + } + ] + } } diff --git a/operator/src/test/sh/scaling/cluster_min3.json b/operator/src/test/sh/scaling/cluster_min3.json index 3ef8b40c366..3dfaf2b8fb9 100644 --- a/operator/src/test/sh/scaling/cluster_min3.json +++ b/operator/src/test/sh/scaling/cluster_min3.json @@ -1,36 +1,31 @@ { - "apiVersion":"weblogic.oracle/v8", - "items":[ - { - "apiVersion": "weblogic.oracle/v8", - "kind": "Domain", - "spec": { - "clusters": [ - { - "clusterName": "cluster-1", - "replicas": 1, - "serverStartState": "RUNNING" - } - ], - "dataHome": "", - "domainHome": "/shared/domains/domain1", - "domainHomeSourceType": "PersistentVolume", - "httpAccessLogInLogHome": true, - "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", - "imagePullPolicy": "IfNotPresent", - "includeServerOutInPodLog": false, - "logHome": "/shared/logs/domain1", - "logHomeEnabled": true, - "replicas": 2 - }, - "status": { - "clusters": [ - { - "clusterName": "cluster-1", - "minimumReplicas": 3 - } - ] - } - } - ] + "apiVersion": "weblogic.oracle/v8", + "kind": "Domain", + "spec": { + "clusters": [ + { + "clusterName": "cluster-1", + "replicas": 1, + "serverStartState": "RUNNING" + } + ], + "dataHome": "", + "domainHome": "/shared/domains/domain1", + "domainHomeSourceType": "PersistentVolume", + "httpAccessLogInLogHome": true, + "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", + "imagePullPolicy": "IfNotPresent", + "includeServerOutInPodLog": false, + "logHome": "/shared/logs/domain1", + "logHomeEnabled": true, + "replicas": 2 + }, + "status": { + "clusters": [ + { + "clusterName": "cluster-1", + "minimumReplicas": 3 + } + ] + } } diff --git a/operator/src/test/sh/scaling/cluster_noreplicas.json b/operator/src/test/sh/scaling/cluster_noreplicas.json index 050d86973ba..457f1d07a44 100644 --- a/operator/src/test/sh/scaling/cluster_noreplicas.json +++ b/operator/src/test/sh/scaling/cluster_noreplicas.json @@ -1,26 +1,21 @@ { - "apiVersion":"weblogic.oracle/v8", - "items":[ - { - "apiVersion": "weblogic.oracle/v8", - "kind": "Domain", - "spec": { - "clusters": [ - { - "clusterName": "cluster-1", - "serverStartState": "RUNNING" - } - ], - "dataHome": "", - "domainHome": "/shared/domains/domain1", - "domainHomeSourceType": "PersistentVolume", - "httpAccessLogInLogHome": true, - "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", - "imagePullPolicy": "IfNotPresent", - "includeServerOutInPodLog": false, - "logHome": "/shared/logs/domain1", - "logHomeEnabled": true - } - } - ] + "apiVersion": "weblogic.oracle/v8", + "kind": "Domain", + "spec": { + "clusters": [ + { + "clusterName": "cluster-1", + "serverStartState": "RUNNING" + } + ], + "dataHome": "", + "domainHome": "/shared/domains/domain1", + "domainHomeSourceType": "PersistentVolume", + "httpAccessLogInLogHome": true, + "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", + "imagePullPolicy": "IfNotPresent", + "includeServerOutInPodLog": false, + "logHome": "/shared/logs/domain1", + "logHomeEnabled": true + } } From 11d94f96b45c39faa8cfe68347e546857c2f63d3 Mon Sep 17 00:00:00 2001 From: anthony_lai Date: Fri, 28 May 2021 21:25:30 +0000 Subject: [PATCH 2/2] add test cases where returning json contains 2 clusters for same domain --- operator/src/test/sh/scaling/2clusters.json | 40 ++++++++ .../src/test/sh/scaling/scalingActionTest.sh | 95 +++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 operator/src/test/sh/scaling/2clusters.json diff --git a/operator/src/test/sh/scaling/2clusters.json b/operator/src/test/sh/scaling/2clusters.json new file mode 100644 index 00000000000..d61f16360a0 --- /dev/null +++ b/operator/src/test/sh/scaling/2clusters.json @@ -0,0 +1,40 @@ +{ + "apiVersion": "weblogic.oracle/v8", + "kind": "Domain", + "spec": { + "clusters": [ + { + "clusterName": "cluster-1", + "replicas": 1, + "serverStartState": "RUNNING" + }, + { + "clusterName": "cluster-2", + "replicas": 2, + "serverStartState": "RUNNING" + } + ], + "dataHome": "", + "domainHome": "/shared/domains/domain1", + "domainHomeSourceType": "PersistentVolume", + "httpAccessLogInLogHome": true, + "image": "container-registry.oracle.com/middleware/weblogic:12.2.1.4", + "imagePullPolicy": "IfNotPresent", + "includeServerOutInPodLog": false, + "logHome": "/shared/logs/domain1", + "logHomeEnabled": true, + "replicas": 2 + }, + "status": { + "clusters": [ + { + "clusterName": "cluster-1", + "minimumReplicas": 1 + }, + { + "clusterName": "cluster-2", + "minimumReplicas": 2 + } + ] + } +} diff --git a/operator/src/test/sh/scaling/scalingActionTest.sh b/operator/src/test/sh/scaling/scalingActionTest.sh index 3609edc73b1..65eb3de95c9 100644 --- a/operator/src/test/sh/scaling/scalingActionTest.sh +++ b/operator/src/test/sh/scaling/scalingActionTest.sh @@ -129,6 +129,39 @@ test_is_defined_in_clusters_jq() { assertEquals 'True' "${result}" } +test_is_defined_in_clusters_2clusters() { + + DOMAIN_FILE="${testdir}/2clusters.json" + + domain_json=`command cat ${DOMAIN_FILE}` + + wls_cluster_name='cluster-1' + result1=$(is_defined_in_clusters "${domain_json}") + + wls_cluster_name='cluster-2' + result2=$(is_defined_in_clusters "${domain_json}") + + assertEquals 'True' "${result1}" + assertEquals 'True' "${result2}" +} + +test_is_defined_in_clusters_2clusters_jq() { + skip_if_jq_not_installed + + DOMAIN_FILE="${testdir}/2clusters.json" + + domain_json=`command cat ${DOMAIN_FILE}` + + wls_cluster_name='cluster-1' + result1=$(is_defined_in_clusters "${domain_json}") + + wls_cluster_name='cluster-2' + result2=$(is_defined_in_clusters "${domain_json}") + + assertEquals 'True' "${result1}" + assertEquals 'True' "${result2}" +} + test_is_defined_in_clusters_no_matching() { DOMAIN_FILE="${testdir}/cluster1.json" @@ -185,6 +218,37 @@ test_get_num_ms_in_cluster_jq() { assertEquals '1' "${result}" } +test_get_num_ms_in_cluster_2clusters() { + + DOMAIN_FILE="${testdir}/2clusters.json" + + domain_json=`command cat ${DOMAIN_FILE}` + + wls_cluster_name='cluster-1' + result=$(get_num_ms_in_cluster "${domain_json}") + assertEquals '1' "${result}" + + wls_cluster_name='cluster-2' + result=$(get_num_ms_in_cluster "${domain_json}") + assertEquals '2' "${result}" +} + +test_get_num_ms_in_cluster_2clusters_jq() { + skip_if_jq_not_installed + + DOMAIN_FILE="${testdir}/2clusters.json" + + domain_json=`command cat ${DOMAIN_FILE}` + + wls_cluster_name='cluster-1' + result=$(get_num_ms_in_cluster "${domain_json}") + assertEquals '1' "${result}" + + wls_cluster_name='cluster-2' + result=$(get_num_ms_in_cluster "${domain_json}") + assertEquals '2' "${result}" +} + test_get_num_ms_in_cluster_no_replics() { DOMAIN_FILE="${testdir}/cluster_noreplicas.json" @@ -324,6 +388,37 @@ test_get_replica_count_from_cluster_jq() { assertEquals '1' "${result}" } +test_get_replica_count_from_cluster_2clusters() { + + DOMAIN_FILE="${testdir}/2clusters.json" + + domain_json=`command cat ${DOMAIN_FILE}` + + wls_cluster_name='cluster-1' + result=$(get_replica_count 'True' "${domain_json}") + assertEquals '1' "${result}" + + wls_cluster_name='cluster-2' + result=$(get_replica_count 'True' "${domain_json}") + assertEquals '2' "${result}" +} + +test_get_replica_count_from_cluster_2clusters_jq() { + skip_if_jq_not_installed + + DOMAIN_FILE="${testdir}/2clusters.json" + + domain_json=`command cat ${DOMAIN_FILE}` + + wls_cluster_name='cluster-1' + result=$(get_replica_count 'True' "${domain_json}") + assertEquals '1' "${result}" + + wls_cluster_name='cluster-2' + result=$(get_replica_count 'True' "${domain_json}") + assertEquals '2' "${result}" +} + test_get_replica_count_from_domain() { DOMAIN_FILE="${testdir}/cluster1.json"