Repository: attzonko/mmpy_bot (language: python)

Issue:
New release?
Hi,

I just moved our irc bot to this and it was very painless, thanks!

Would you consider pushing a new release to pypi? There have been some useful commits since the last one.

Many thanks,

David
Run mattermost in docker for travis ci
I made this after the discussion in https://github.com/attzonko/mmpy_bot/issues/129

I'm guessing there is some setup in travis ci which is not contained within this repo (specifically setting `BOT_URL`) so I've no idea if this will work.

I think we'll also need to setup the username and password and the channels once mattermost is running, so by itself this pull request is not very useful.


Discussion / hints:
My dev environment for this has been on hiatus for a while. I will try and get it back up to do this. I would like to re-enable Travis CI to make sure all the code is still passing the automated CI, as a few commits were merged without it. 

How urgent is this for you?
Not urgent, we can always install from git.

Anything I can do to help?

On Fri, 31 Jul 2020, 23:57 Alex Tzonkov, <notifications@github.com> wrote:

> My dev environment for this has been on hiatus for a while. I will try and
> get it back up to do this. I would like to re-enable Travis CI to make sure
> all the code is still passing the automated CI, as a few commits were
> merged without it.
>
> How urgent is this for you?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/attzonko/mmpy_bot/issues/129#issuecomment-667418748>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABRDLYLXC5FMMPVPYINKBLDR6ND5PANCNFSM4OUQXXAQ>
> .
>

Looking at the travis failures it's because they connect to mmpy-bot.duckdns.org which isn't responding.

From your comment above I assume this means that it's a mattermost instance running on your personal network.

What could be done is to run [mattermost via docker](https://github.com/mattermost/mattermost-docker) in the `before_install` section and connect to that instead, the hard part would be boostrapping mattermost
There's a one-liner in the documentation: https://docs.mattermost.com/install/docker-local-machine.html#one-line-docker-install

I don't have experience of configuring travis, but I might give it a go
Oh nice! I might give that a go myself if I can find time :)
Code Climate has analyzed commit e1356181 and detected **0 issues** on this pull request.

View more on [Code Climate](https://codeclimate.com/github/attzonko/mmpy_bot/pull/136).

This alone won't do it, but this is going in the right direction. I think we might need to add some .env file to synchronize the credentials Travis CI is expecting. I'll add this to my TODO list 👍 

Fix patch:
diff --git a/.travis.yml b/.travis.yml
index 50e70689..34dfcd48 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,36 +1,43 @@
-dist: trusty
 sudo: false
 env:
   global:
     - CC_TEST_REPORTER_ID=b0fed3b6b9b09683bf744113f1effa146b532c2fc05f21d0b3f29787807bca01
+    - BOT_URL='http://localhost:8065/api/v4'
+    - DRIVERBOT_LOGIN='driverbot@test.com'
+    - DRIVERBOT_NAME='driverbot'
+    - DRIVERBOT_PASSWORD='driverbot_password'
+    - TESTBOT_LOGIN='testbot@test.com'
+    - TESTBOT_NAME='testbot'
+    - TESTBOT_PASSWORD='testbot_password'
+    - BOT_TEAM='test-team'
+    - DOCKER_CONTAINER_NAME=mattermost
+    - DOCKER_NETWORK=default
+    - BOT_CHANNEL=public
+    - BOT_PRIVATE_CHANNEL=private
 
 branches:
   only:
     - master
 
+services:
+  - docker
+
 language: python
 
 matrix:
   include:
-  - python: "2.7"
-    env: BOT_CHANNEL=PUB_2_7 BOT_PRIVATE_CHANNEL=PRIV_2_7
-  - python: "3.4"
-    env: BOT_CHANNEL=PUB_3_4 BOT_PRIVATE_CHANNEL=PRIV_3_4
-  - python: "3.5"
-    env: BOT_CHANNEL=PUB_3_5 BOT_PRIVATE_CHANNEL=PRIV_3_5
   - python: "3.6"
-    env: BOT_CHANNEL=PUB_3_6 BOT_PRIVATE_CHANNEL=PRIV_3_6
+  - python: "3.7"
+  - python: "3.8"
+  - python: "3.9"
     
 install:
   - pip install -r requirements.txt
   - pip install pytest-cov
-  - if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then pip install mock; fi
   - pip install .
 
 before_script:
-  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
-  - chmod +x ./cc-test-reporter
-  - ./cc-test-reporter before-build
+  - tests/setup.sh
 
 script: pytest --cov=mmpy_bot tests/ --cov-report xml
 


Test patch:
diff --git a/docs/testing.rst b/docs/testing.rst
index 07fce206..1ccd0cd3 100644
--- a/docs/testing.rst
+++ b/docs/testing.rst
@@ -139,4 +139,23 @@ Using "--cov-report" parameter to write report into "cov_html" folder by html fo
 
 .. code-block:: bash
 
-	py.test --cov-report html:logs\cov_html --cov=mmpy_bot tests\
\ No newline at end of file
+	py.test --cov-report html:logs\cov_html --cov=mmpy_bot tests\
+
+Running tests in docker:
+------------------------
+
+If you have docker installed you can spawn a temporary mattermost instance and
+run all the tests by running the following:
+
+.. code-block:: bash
+
+    $ ./tests/run-tests-in-docker.sh
+
+This has predefined environment variables which you can customise.
+Variables in addition to the ones above are:
+
+:code:`DOCKER_CONTAINER_NAME` which specifies the name of the conatiner that runs mattermost
+
+:code:`DOCKER_NETWORK` the docker network to run in, this is created automatically if it doesn't exist
+
+:code:`PYTHON_VERSION` the python version to use
diff --git a/tests/behavior_tests/bots/driver.py b/tests/behavior_tests/bots/driver.py
index b7367b1a..66230a59 100644
--- a/tests/behavior_tests/bots/driver.py
+++ b/tests/behavior_tests/bots/driver.py
@@ -81,8 +81,11 @@ def _rtm_read_forever(self):
             json_data = self._websocket_safe_read()
             if json_data != '':
                 with self._events_lock:
-                    self.events.extend(
-                        [json.loads(d) for d in json_data.split('\n')])
+                    for d in json_data.split('\n'):
+                        try:
+                            self.events.extend([json.loads(d)])
+                        except:
+                            pass
             time.sleep(1)
 
     def _retrieve_bot_user_ids(self):
diff --git a/tests/behavior_tests/test_conversation.py b/tests/behavior_tests/test_conversation.py
index e3e54144..f4a5f17c 100644
--- a/tests/behavior_tests/test_conversation.py
+++ b/tests/behavior_tests/test_conversation.py
@@ -28,8 +28,6 @@ def test_bot_reply_to_channel_message(driver):  # noqa: F811
     driver.wait_for_bot_channel_message('hello sender!')
     driver.send_channel_message('hello', colon=False)
     driver.wait_for_bot_channel_message('hello sender!')
-    driver.send_channel_message('hello', space=False)
-    driver.wait_for_bot_channel_message('hello sender!')
     driver.send_channel_message('hello', colon=False, space=False)
     driver.wait_for_bot_channel_message('hello channel!', tosender=False)
 
diff --git a/tests/docker-entrypoint.sh b/tests/docker-entrypoint.sh
new file mode 100755
index 00000000..19ea226d
--- /dev/null
+++ b/tests/docker-entrypoint.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+set -euo pipefail
+pip install -r requirements.txt
+pip install pytest-cov
+pip install .
+"$@"
diff --git a/tests/run-tests-in-docker.sh b/tests/run-tests-in-docker.sh
new file mode 100755
index 00000000..9d5af352
--- /dev/null
+++ b/tests/run-tests-in-docker.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+set -euo pipefail
+: ${DRIVERBOT_LOGIN:='driverbot@test.com'}
+: ${DRIVERBOT_NAME:='driverbot'}
+: ${DRIVERBOT_PASSWORD:='driverbot_password'}
+: ${TESTBOT_LOGIN:='testbot@test.com'}
+: ${TESTBOT_NAME:='testbot'}
+: ${TESTBOT_PASSWORD:='testbot_password'}
+: ${BOT_TEAM:='test-team'}
+: ${BOT_CHANNEL:=public}
+: ${BOT_PRIVATE_CHANNEL:=private}
+: ${DOCKER_CONTAINER_NAME:=mmpybot-mattermost}
+: ${BOT_URL:="http://${DOCKER_CONTAINER_NAME}:8065/api/v4"}
+: ${DOCKER_NETWORK:=mmpybot-mattermost}
+: ${PYTHON_VERSION=3.6}
+. ./tests/setup.sh
+docker run -ti --rm \
+--net "$DOCKER_NETWORK" \
+-v "$PWD:/mmpy_bot" \
+-e BOT_URL="$BOT_URL" \
+-e DRIVERBOT_LOGIN="$DRIVERBOT_LOGIN" \
+-e DRIVERBOT_NAME="$DRIVERBOT_NAME" \
+-e DRIVERBOT_PASSWORD="$DRIVERBOT_PASSWORD" \
+-e TESTBOT_LOGIN="$TESTBOT_LOGIN" \
+-e TESTBOT_NAME="$TESTBOT_NAME" \
+-e TESTBOT_PASSWORD="$TESTBOT_PASSWORD" \
+-e BOT_TEAM="$BOT_TEAM" \
+-e BOT_CHANNEL="$BOT_CHANNEL" \
+-e BOT_PRIVATE_CHANNEL="$BOT_PRIVATE_CHANNEL" \
+--entrypoint=/mmpy_bot/tests/docker-entrypoint.sh \
+-w /mmpy_bot \
+python:"$PYTHON_VERSION" \
+pytest
diff --git a/tests/setup.sh b/tests/setup.sh
new file mode 100755
index 00000000..39c174aa
--- /dev/null
+++ b/tests/setup.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+set -ue
+curl -sL https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
+chmod +x ./cc-test-reporter
+./cc-test-reporter before-build
+docker pull mattermost/mattermost-preview
+docker kill "$DOCKER_CONTAINER_NAME" || true
+docker rm "$DOCKER_CONTAINER_NAME" || true
+docker network create "$DOCKER_NETWORK" || true
+docker run --name "$DOCKER_CONTAINER_NAME" \
+--net "$DOCKER_NETWORK" \
+-d \
+--publish 8065:8065 \
+--add-host dockerhost:127.0.0.1 \
+mattermost/mattermost-preview
+echo -n "Waiting for mattermost to start"
+while ! docker logs "$DOCKER_CONTAINER_NAME"  2>&1 | grep -q "Server is listening"
+do
+    sleep 1
+    echo -n "."
+done
+echo ''
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json user create --email="$DRIVERBOT_LOGIN" --password="$DRIVERBOT_PASSWORD" --username "$DRIVERBOT_NAME"
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json user create --email="$TESTBOT_LOGIN" --password="$TESTBOT_PASSWORD" --username "$TESTBOT_NAME"
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json team create --name "$BOT_TEAM" --display_name "$BOT_TEAM"
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json channel create --team "$BOT_TEAM" --name "$BOT_CHANNEL" --display_name "$BOT_CHANNEL"
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json channel create --team "$BOT_TEAM" --name "$BOT_PRIVATE_CHANNEL" --display_name "$BOT_PRIVATE_CHANNEL" --private
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json team add "$BOT_TEAM" "$TESTBOT_NAME" "$DRIVERBOT_NAME"
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json channel add  "$BOT_TEAM:$BOT_CHANNEL" "$TESTBOT_NAME" "$DRIVERBOT_NAME"
+docker exec "$DOCKER_CONTAINER_NAME" mattermost -c /mm/mattermost/config/config_docker.json channel add  "$BOT_TEAM:$BOT_PRIVATE_CHANNEL" "$TESTBOT_NAME" "$DRIVERBOT_NAME"