Skip to content

Commit

Permalink
Add script test files exist and owner
Browse files Browse the repository at this point in the history
  • Loading branch information
yenienserrano committed Dec 22, 2023
1 parent bd4fe7a commit 6ac9c23
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dev-tools/test-packages/deb/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:latest
ARG PACKAGE
RUN mkdir -p /tmp
RUN apt-get update --fix-missing
RUN apt-get install -y curl libcap2-bin
COPY ${PACKAGE} /tmp/wazuh.deb
RUN dpkg -i /tmp/wazuh.deb
11 changes: 11 additions & 0 deletions dev-tools/test-packages/rpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM centos:latest

RUN mkdir -p /tmp
FROM centos
ARG PACKAGE
RUN cd /etc/yum.repos.d/
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
RUN yum update -y
COPY ${PACKAGE} /tmp/wazuh.rpm
RUN yum install /tmp/wazuh.rpm -y
88 changes: 88 additions & 0 deletions dev-tools/test-packages/test-packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/sh

# Definir el nombre del contenedor y los archivos que se van a verificar
PACKAGE=""
CONTAINER_NAME="wazuh-dashboard"
FILES="/etc/wazuh-dashboard/opensearch_dashboards.yml /usr/share/wazuh-dashboard"
FILE_OWNER="wazuh-dashboard"

clean() {
docker stop $CONTAINER_NAME
docker rmi $CONTAINER_NAME
}

files_exist() {
for FILE in $FILES; do
if docker exec $CONTAINER_NAME ls $FILE >/dev/null 2>&1; then
file_owner=$(docker exec $CONTAINER_NAME stat -c '%U' $FILE)
if [ "$file_owner" != "$FILE_OWNER" ]; then
echo "ERROR: $FILE is owned by $file_owner instead of $FILE_OWNER"
clean
exit 1
fi
echo "$FILE exist and is owned by $FILE_OWNER"
else
echo "ERROR: $FILE does not exist"
clean
exit 1
fi
done
}


test() {

if [[ $PACKAGE == *".deb" ]]; then
docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./deb/
docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME
elif [[ $PACKAGE == *".rpm" ]]; then
docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./rpm/
docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME
else
echo "ERROR: $PACKAGE is not a valid package (valid packages are .deb and .rpm ))"
exit 1
fi

files_exist
}


help() {
echo
echo "Usage: $0 [OPTIONS]"
echo
echo " -p, --package <path> Set Wazuh Dashboard rpm package name,which has to be in the <repository>/dev-tools/test-packages/<DISTRIBUTION>/ folder."
echo
exit $1
}

main() {
while [ -n "${1}" ]; do
case "${1}" in
"-h" | "--help")
help 0
;;
"-p" | "--package")
if [ -n "${2}" ]; then
PACKAGE="${2}"
shift 2
else
help 1
fi
;;
*)
help 1
;;
esac
done

if [ -z "$PACKAGE" ] ; then
help 1
fi

test

clean
}

main "$@"

0 comments on commit 6ac9c23

Please sign in to comment.