Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineF-dev committed Jun 6, 2024
1 parent 72266aa commit fa4b62a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
38 changes: 30 additions & 8 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -x
set -e
set -o pipefail

Expand Down Expand Up @@ -97,24 +98,45 @@ function kube_state_metrics_up() {
function test_daemonset() {
sed -i "s|${KUBE_STATE_METRICS_CURRENT_IMAGE_NAME}:v.*|${KUBE_STATE_METRICS_IMAGE_NAME}:${KUBE_STATE_METRICS_IMAGE_TAG}|g" ./examples/daemonsetsharding/deployment.yaml
sed -i "s|${KUBE_STATE_METRICS_CURRENT_IMAGE_NAME}:v.*|${KUBE_STATE_METRICS_IMAGE_NAME}:${KUBE_STATE_METRICS_IMAGE_TAG}|g" ./examples/daemonsetsharding/daemonset.yaml
sed -i "s|${KUBE_STATE_METRICS_CURRENT_IMAGE_NAME}:v.*|${KUBE_STATE_METRICS_IMAGE_NAME}:${KUBE_STATE_METRICS_IMAGE_TAG}|g" ./examples/daemonsetsharding/deployment-no-node-pods.yaml

kubectl get deployment -n kube-system
kubectl create -f ./examples/daemonsetsharding
ls ./examples/daemonsetsharding
kube_state_metrics_up
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-shard:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/pod-metrics
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/nonpod-metrics
m1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/pod-metrics | grep "# TYPE kube_pod_info" || true)"
m2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/nonpod-metrics | grep "# TYPE kube_pod_info" || true)"
if [[ -z "${m1}" ]]; then
echo "can't found metric kube_pod_info from pod metrics service"
kubectl apply -f ./tests/e2e/testdata/pods.yaml
sleep 10
kubectl get deployment -n default
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-shard:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-no-node-pods:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics

cat ${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics >> ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics
cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "kube_pod_info"
runningpod1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "runningpod1" | grep -c "kube_pod_info" )"
pendingpod2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "pendingpod2" | grep -c "kube_pod_info" )"
pendingpod2_in_deployment="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics | grep "pendingpod2" | grep -c "kube_pod_info" )"
expected_num_pod=1
if [ "${runningpod1}" != "${expected_num_pod}" ]; then
echo "metric kube_pod_info for runningpod1 doesn't show up only once, got ${runningpod1} times"
exit 1
fi
if [[ -n "${m2}" ]]; then
echo "shouldn't find metric kube_pod_info from non-pod metrics service"
cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics | grep "kube_pod_info"
if [ "${pendingpod2_in_deployment}" != "${expected_num_pod}" ]; then
echo "metric kube_pod_info for pendingpod2 doesn't show up fonly once from service deployment-scraped-no-node-metrics"
exit 1
fi
if [ "${pendingpod2}" != "${expected_num_pod}" ]; then
echo "metric kube_pod_info for pendingpod2 doesn't show up only once, got ${runningpod1} times"
exit 1
fi
m1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep -c "# TYPE kube_node_info" )"
if [ "${m1}" != "1" ]; then
echo "metric kube_node_info doesn't show up only once, got ${m1} times"
exit 1
fi

kubectl delete -f ./tests/e2e/testdata/pods.yaml
kubectl delete -f ./examples/daemonsetsharding
sleep 20
}
Expand Down
75 changes: 75 additions & 0 deletions tests/e2e/testdata/pods.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pendingpod2
name: pendingpod2
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: pendingpod2
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: pendingpod2
spec:
nodeSelector:
debug-node: "non"
containers:
- command:
- /agnhost
- netexec
- --http-port=8080q
image: registry.k8s.io/e2e-test-images/agnhost:2.39
imagePullPolicy: IfNotPresent
name: agnhost
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: runningpod1
name: runningpod1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: runningpod1
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: runningpod1
spec:
containers:
- command:
- /agnhost
- netexec
- --http-port=8080
image: registry.k8s.io/e2e-test-images/agnhost:2.39
imagePullPolicy: IfNotPresent
name: agnhost
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30

0 comments on commit fa4b62a

Please sign in to comment.