mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 11:44:51 +00:00
testing travis
This commit is contained in:
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
sudo: required
|
||||||
|
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- IMAGE_NAME=ci/testing
|
||||||
|
- DOCKERFILE=docker/Dockerfile.testing
|
||||||
|
|
||||||
|
script:
|
||||||
|
- docker build -f "$DOCKERFILE" -t "$IMAGE_NAME" .
|
||||||
|
- docker run -it --rm $(IMAGE_NAME)
|
||||||
7
Makefile
7
Makefile
@@ -3,6 +3,13 @@ PROJECT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
|
|||||||
DEV_IMAGE = local/pspy-development:latest
|
DEV_IMAGE = local/pspy-development:latest
|
||||||
DEV_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.development
|
DEV_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.development
|
||||||
|
|
||||||
|
TEST_IMAGE = local/pspy-testing:latest
|
||||||
|
TEST_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.testing
|
||||||
|
|
||||||
|
test:
|
||||||
|
docker build -f $(TEST_DOCKERFILE) -t $(TEST_IMAGE) .
|
||||||
|
docker run -it --rm $(TEST_IMAGE)
|
||||||
|
|
||||||
dev-build:
|
dev-build:
|
||||||
docker build -f $(DEV_DOCKERFILE) -t $(DEV_IMAGE) .
|
docker build -f $(DEV_DOCKERFILE) -t $(DEV_IMAGE) .
|
||||||
|
|
||||||
|
|||||||
35
docker/Dockerfile.testing
Normal file
35
docker/Dockerfile.testing
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
FROM golang:1.10-stretch
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get -y install cron python3 sudo procps
|
||||||
|
|
||||||
|
### Test and build ###
|
||||||
|
# copy source code
|
||||||
|
COPY main.go /go/src/github.com/dominicbreuker/pspy/main.go
|
||||||
|
COPY cmd /go/src/github.com/dominicbreuker/pspy/cmd
|
||||||
|
COPY internal /go/src/github.com/dominicbreuker/pspy/internal
|
||||||
|
COPY vendor /go/src/github.com/dominicbreuker/pspy/vendor
|
||||||
|
|
||||||
|
# run tests
|
||||||
|
WORKDIR /go/src/github.com/dominicbreuker/pspy
|
||||||
|
RUN go test ./...
|
||||||
|
# build executable
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -o bin/pspy main.go
|
||||||
|
|
||||||
|
### Prepare integration test ###
|
||||||
|
# install root cronjob
|
||||||
|
COPY docker/var/spool/cron/crontabs /var/spool/cron/crontabs
|
||||||
|
RUN chmod 600 /var/spool/cron/crontabs/root
|
||||||
|
COPY docker/root/scripts /root/scripts
|
||||||
|
|
||||||
|
# set up unpriviledged user
|
||||||
|
# allows passwordless sudo to start cron as root on startup
|
||||||
|
RUN useradd -ms /bin/bash myuser && \
|
||||||
|
adduser myuser sudo && \
|
||||||
|
echo 'myuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
|
||||||
|
USER myuser
|
||||||
|
|
||||||
|
|
||||||
|
# drop into bash shell
|
||||||
|
COPY docker/entrypoint-testing.sh /entrypoint.sh
|
||||||
|
RUN sudo chmod +x /entrypoint.sh
|
||||||
|
CMD ["/entrypoint.sh"]
|
||||||
28
docker/entrypoint-testing.sh
Normal file
28
docker/entrypoint-testing.sh
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
sudo cron -f &
|
||||||
|
sleep 1
|
||||||
|
sudo ps | grep cron 1>/dev/null
|
||||||
|
echo "[+] cron started"
|
||||||
|
|
||||||
|
echo "[+] Running as user `id`"
|
||||||
|
|
||||||
|
echo "[+] Executing test"
|
||||||
|
# exec /bin/bash
|
||||||
|
rm /home/myuser/log.txt
|
||||||
|
bin/pspy > /home/myuser/log.txt &
|
||||||
|
|
||||||
|
for i in `seq 1 25`; do
|
||||||
|
echo "Waiting for cron job detection..."
|
||||||
|
sleep 5;
|
||||||
|
|
||||||
|
QUERY_RESULT=$(grep ' | passwd myuser' /home/myuser/log.txt | grep -v grep)
|
||||||
|
if [ "'$QUERY_RESULT'" != "''" ]; then
|
||||||
|
echo "Cron job execution detected!"
|
||||||
|
echo "Complete log of pspy (may contain commands run in this test):"
|
||||||
|
cat /home/myuser/log.txt
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "Failed to detect cron job..."
|
||||||
|
exit 1
|
||||||
@@ -42,19 +42,6 @@ func (pl procList) refresh(eventCh chan string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pl procList) addPid(pid int, eventCh chan string) {
|
|
||||||
cmd, err := getCmd(pid)
|
|
||||||
if err != nil {
|
|
||||||
cmd = "???" // process probably terminated
|
|
||||||
}
|
|
||||||
uid, err := getUID(pid)
|
|
||||||
if err != nil {
|
|
||||||
uid = "???"
|
|
||||||
}
|
|
||||||
eventCh <- fmt.Sprintf("UID=%-4s PID=%-6d | %s", uid, pid, cmd)
|
|
||||||
pl[pid] = cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPIDs() ([]int, error) {
|
func getPIDs() ([]int, error) {
|
||||||
proc, err := procDirReader()
|
proc, err := procDirReader()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -87,6 +74,19 @@ func file2Pid(f os.FileInfo) (int, error) {
|
|||||||
return pid, nil
|
return pid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pl procList) addPid(pid int, eventCh chan string) {
|
||||||
|
cmd, err := getCmd(pid)
|
||||||
|
if err != nil {
|
||||||
|
cmd = "???" // process probably terminated
|
||||||
|
}
|
||||||
|
uid, err := getUID(pid)
|
||||||
|
if err != nil {
|
||||||
|
uid = "???"
|
||||||
|
}
|
||||||
|
eventCh <- fmt.Sprintf("UID=%-4s PID=%-6d | %s", uid, pid, cmd)
|
||||||
|
pl[pid] = cmd
|
||||||
|
}
|
||||||
|
|
||||||
func getCmd(pid int) (string, error) {
|
func getCmd(pid int) (string, error) {
|
||||||
cmd, err := cmdLineReader(pid)
|
cmd, err := cmdLineReader(pid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user