Skip to content

Commit c7a9ab7

Browse files
authored
Merge pull request #99 from Random-Liu/add-test-kernel-log-generator
Add test kernel log generator.
2 parents b3b5766 + 5822164 commit c7a9ab7

File tree

11 files changed

+120
-81
lines changed

11 files changed

+120
-81
lines changed

demo/demo

Lines changed: 0 additions & 81 deletions
This file was deleted.

test/kernel_log_generator/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2016 The Kubernetes Authors All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM bashell/alpine-bash
16+
MAINTAINER Random Liu <[email protected]>
17+
18+
# Use oom kill problem as default
19+
ENV PROBLEM /problems/oom_kill
20+
21+
ADD problems /problems
22+
ADD generator.sh /generator.sh
23+
ENTRYPOINT ["bash", "-c", "/generator.sh"]

test/kernel_log_generator/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Build the kernel problem generator.
16+
17+
.PHONY: all build push
18+
19+
PROJ ?= gcr.io/google_containers
20+
TAG := 0.1
21+
22+
all: push
23+
24+
build:
25+
docker build -t $(PROJ)/kernel_log_generator:$(TAG) .
26+
27+
push:
28+
gcloud docker -- push $(PROJ)/kernel_log_generator:$(TAG)
29+
30+
clean:
31+
docker rmi $(PROJ)/kernel_log_generator:$(TAG)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
# Copyright 2017 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This script generate kernel log in given rate.
18+
# Note that this only works for journald.
19+
# Note that this script can't be used to test multi-line patterns, because
20+
# the test kernel log lines may be splitted by real kernel log.
21+
22+
# PROBLEM is the path to the problem log.
23+
PROBLEM=${PROBLEM:-""}
24+
# LINES_PER_SECOND is the log generating rate.
25+
LINES_PER_SECOND=${LINES_PER_SECOND:-1}
26+
# LOG_PATH is the path to generate kernel logs.
27+
LOG_PATH=${LOG_PATH:-"/dev/kmsg"}
28+
# RUN_ONCE specifies whether to keep generating problem logs or only
29+
# generate once.
30+
RUN_ONCE=${RUN_ONCE:-"false"}
31+
32+
if [[ -z ${PROBLEM} ]]; then
33+
echo "$(basename $0): PROBLEM is empty"
34+
exit 1
35+
fi
36+
37+
# now returns the current unix time in micro seconds
38+
now() {
39+
echo $(($(date +'%s%N') / 1000))
40+
}
41+
42+
prefix="kernel: "
43+
logs=$(<${PROBLEM})
44+
lines=$(printf "%s\n" "${logs}" | wc -l)
45+
current_lines=0
46+
start=$(now)
47+
while true; do
48+
while read log; do
49+
echo "${prefix}${log}" >> ${LOG_PATH}
50+
((current_lines++))
51+
if [[ ${current_lines} == ${LINES_PER_SECOND} ]]; then
52+
current_lines=0
53+
current=$(now)
54+
duration=$((1000000 - current + start))
55+
if [[ ${duration} -gt 0 ]]; then
56+
echo "generated ${LINES_PER_SECOND} lines of log, sleep for ${duration}"
57+
usleep ${duration}
58+
fi
59+
start=$(now)
60+
fi
61+
done <<< "$logs"
62+
if [[ "${RUN_ONCE}" == "true" ]]; then
63+
echo "stop the generator"
64+
exit 0
65+
fi
66+
done
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)