From 4fe3a49ad3511d0dc3638d4ddd8b877f2f102555 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Thu, 10 Aug 2017 13:50:36 -0700 Subject: [PATCH] Finally a testing framework, fixes #9 --- Dockerfile | 7 ++----- Dockerfile-test-env | 14 ++++++++++++++ README.md | 10 +++++----- app/dragon-bot.py | 18 ++++++++++-------- pull_and_run_latest.sh | 2 +- test-dragon-bot.sh | 10 ++++++++++ 6 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 Dockerfile-test-env create mode 100755 test-dragon-bot.sh diff --git a/Dockerfile b/Dockerfile index 55475b2d..778ff7af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,9 @@ FROM python:3.6.2-slim +LABEL name="Dragon Bot" RUN apt-get update && apt-get install curl -y &&\ curl -Lks get.docker.com | bash -RUN pip install requests discord.py docker pybooru termcolor - +RUN pip install requests discord.py docker pybooru ADD app /app -# Run this to test that the python compiles when we actually go live -RUN python /app/dragon-bot.py test - CMD python app/dragon-bot.py \ No newline at end of file diff --git a/Dockerfile-test-env b/Dockerfile-test-env new file mode 100644 index 00000000..a05299ee --- /dev/null +++ b/Dockerfile-test-env @@ -0,0 +1,14 @@ +FROM python:3.6.2-slim +LABEL name="Dragon Bot Test environment" +RUN apt-get update && apt-get install curl -y &&\ + curl -Lks get.docker.com | bash + +RUN pip install requests discord.py docker pybooru pylint +ADD app /app + +RUN echo "\nTesting your python code for errors\n" && \ + pylint -E /app/*.py + +RUN echo "\n#########################\nRun dragon bot by typing \npython dragon-bot.py\n#########################\n" + +WORKDIR /app \ No newline at end of file diff --git a/README.md b/README.md index ef861f28..77aa375b 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ A discord bot for the trifecta discord channel * `curl -Ls get.docker.com | bash` ### Testing your changes ### -* Test that you can actually build the container - * `docker build -t testing .` - * `docker run -ti testing bash` - * Then, if you've added print statements to the script for testing, you can see them by running: - * `python /app/dragon-bot.py` +* Build the testing container by running + * `./test-dragon-bot.sh` + * The dragon-bot-test bot will connect to the server. + * If you want to test what the bot is seeing when you type commands, add LOTS of of print statements to dragon-bot.py + * you will see the output in the terminal. This is useful for debugging * pipeline [here](https://cloud.docker.com/app/ldooks/repository/docker/ldooks/dragon-bot/builds) ### Contribution guidelines ### diff --git a/app/dragon-bot.py b/app/dragon-bot.py index 8d9b128b..83ed8e54 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -1,19 +1,26 @@ import random import sys import requests +import os import help_methods import discord import docker from pybooru import Danbooru -from termcolor import colored # Client object client = discord.Client() +tokens = { + 'test': 'MzQ1MjkwMTI5OTQ4Mjc4Nzg0.DG5IBw._9umb82PrL22bPe7GjmHClU-NtU', + 'prod': 'MzM5NDQ2NTgwMTU3MzQ5ODg4.DG5K5Q.2kIonA_XHLXU4_Sq4O63OzCb0Jc' +} +token = os.getenv('DRAGON_ENV') @client.event async def on_ready(): - print("Dragon bot up and ready to roll.\n") + print("\n********************************") + print("\nDRAGON BOT RUNNING IN {} MODE".format(token.upper())) + print("\n********************************") @client.event @@ -253,10 +260,5 @@ async def on_message(message): else: await client.send_message(message.channel, "There arent {} lines of output yet".format(num_lines)) -# Check if there is a command line argument after script -if len(sys.argv) > 1: - print(colored("If you're seeing this, the python compiled", 'green')) - sys.exit() -token = "MzM5NDQ2NTgwMTU3MzQ5ODg4.DFkGYg.z-XD17nFP4rtBq-YsNbOJHuBWfQ" # get from the bot page. must be a bot, not a discord app -client.run(token) +client.run(tokens[token]) \ No newline at end of file diff --git a/pull_and_run_latest.sh b/pull_and_run_latest.sh index 30db2827..1b56a428 100755 --- a/pull_and_run_latest.sh +++ b/pull_and_run_latest.sh @@ -6,5 +6,5 @@ printf "[+] Done\n" # Run that shit and mount the docker socket so it can talk to the sky-factory container printf "\n[-] Pulling latest build of container\n" docker pull ldooks/dragon-bot:latest -docker run -d --name dragon-bot -v /var/run/docker.sock:/var/run/docker.sock --restart always ldooks/dragon-bot:latest +docker run -d --name dragon-bot -e "DRAGON_ENV=prod" -v /var/run/docker.sock:/var/run/docker.sock --restart always ldooks/dragon-bot:latest printf "[+] Done\n" diff --git a/test-dragon-bot.sh b/test-dragon-bot.sh new file mode 100755 index 00000000..b092334d --- /dev/null +++ b/test-dragon-bot.sh @@ -0,0 +1,10 @@ +# Remove the running container so we cna re-use the container name 'dragon-bot' +printf "\n[-] Deleting old dragon-bot container from system\n" +docker rm -f dragon-bot-test +printf "[+] Done\n" + +# Run that shit and mount the docker socket so it can talk to the sky-factory container +printf "\n[-] Running dragon bot in test mode\n" +docker build -f ./Dockerfile-test-env -t dragon-bot-test . || exit +docker run -ti --name dragon-bot-test -e DRAGON_ENV=test -v /var/run/docker.sock:/var/run/docker.sock dragon-bot-test bash +printf "[+] Done\n"