Last active
December 12, 2018 22:07
-
-
Save blag/b9beb31b6f58d90172690285bf21b1a3 to your computer and use it in GitHub Desktop.
Bash script to test st2chatops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# This script runs through the chatops introduction in the StackStorm documentation. | |
# Dependencies: | |
# * slackcat | |
# - from https://github.com/rlister/slackcat | |
# - install with `sudo gem install slackcat` | |
# - ensure that it's in your PATH when you run with `sudo` | |
# - all configuration should be done through environment variables | |
# + NOT through a ~/.slackcat config file | |
# + if you need a ~/.slackcat config file you are using the wrong slackcat | |
# There are two different bots to configure: | |
# | |
# * st2chatops bot - configure with Slack with the usual `st2chatops.env` | |
# * slackcat bot - configured with environment variables, this behaves like a typical Slack user conversing with the st2chatops bot | |
# Requires the following environment variables: | |
# * SLACK_TOKEN for slackcat - this is the _user_ token (the Slack user who posts '!help') | |
# * SLACK_CHANNEL for slackcat - only one bot user can be in this channel at a time, but you can specify a different channel just for you | |
# * ST2_SLACK_BOT - the Slack bot name of the st2 chatops bot - only one bot can be in the channel at a time! | |
# * ST2_AUTH_TOKEN - the st2 auth token for the st2 client | |
# Please ensure that you remove your st2chatops bot from the channel so that others may run their own tests. | |
# Finally, you must assert/check the Slack channel yourself to ensure that the st2chatops | |
# (server) bot responds with the correct output. | |
PREVIOUS_DIR=$(pwd) | |
PACK_NAME=my_chatops | |
echo "\`---------------- SETUP ST2CHATOPS TESTS ----------------\`" | slackcat -p | |
# Remove the pack if it exists | |
if st2 pack get $PACK_NAME 1>/dev/null; then | |
echo "Removing existing \`$PACK_NAME\` pack" | slackcat -p | |
sudo rm -rf /opt/stackstorm/packs/$PACK_NAME | |
st2 pack remove $PACK_NAME | |
sleep 5 | |
sudo st2ctl restart --register-all | |
sleep 30 | |
fi | |
# List packs | |
( echo \`\`\`; st2 pack list; echo \`\`\`; ) | slackcat -p | |
# Subtly test that '!help' not at the beginning of a message does NOT trigger | |
# a command listing | |
echo "Testing !help command listing" | slackcat -p | |
# Assert bot doesn't list commands | |
echo '!help' | slackcat -p | |
# Assert bot lists commands | |
sleep 30 | |
# Subtly test that '!help' not at the beginning of a message does NOT trigger a | |
# command listing | |
echo "Testing !help command listing" | slackcat -p | |
# Assert bot doesn't list commands | |
echo "@${ST2_SLACK_BOT} help" | slackcat -p | |
# Assert bot lists commands | |
sleep 30 | |
echo "Recreating \`$PACK_NAME\` pack" | slackcat -p | |
cd /opt/stackstorm/packs/ | |
sudo mkdir -p $PACK_NAME/{actions,rules,sensors,aliases} | |
sudo chown $(whoami):$(whoami) -R $PACK_NAME | |
cd $PACK_NAME | |
sudo cat >pack.yaml <<EOF | |
--- | |
ref: $PACK_NAME | |
name: My ChatOps Tests | |
description: ChatOps tests | |
keywords: | |
- example | |
- tests | |
- chatops | |
version: 0.0.0 | |
author: stanley | |
email: [email protected] | |
EOF | |
echo "\`---------------- STARTING ST2CHATOPS TESTS ----------------\`" | slackcat -p | |
echo "Testing simple run command" | slackcat -p | |
sudo cat >aliases/remote.yaml <<EOF | |
--- | |
name: remote_chatops_cmd | |
description: Execute a command on a remote host via SSH. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
EOF | |
echo "Before registering alias:" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
# Assert 'remote_chatops_cmd' not in $ALIAS_ST2_LIST | |
echo "Reloading" | slackcat -p | |
(echo "\`\`\`"; sudo st2ctl reload --register-aliases; echo "\`\`\`") | slackcat -p | |
sleep 5 | |
echo "After reloading:" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
# Assert 'remote_chatops_cmd' in $ALIAS_ST2_LIST | |
echo "Restarting:" | slackcat -p | |
(echo "\`\`\`"; sudo st2ctl restart; echo "\`\`\`") | slackcat -p | |
sleep 30 | |
echo "After restarting:" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
# Assert 'remote_chatops_cmd' in $ALIAS_ST2_LIST | |
sleep 5 | |
echo '!run date on localhost' | slackcat -p | |
sleep 5 | |
# Assert using regex | |
echo '!run "echo Chatops run exact command on localhost" on localhost' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
echo '!run "echo Chatops run exact command on multiple hosts" on localhost,127.0.0.1' | slackcat -p | |
sleep 5 | |
# Assert exact match on both hosts | |
echo "Before deleting the alias:" | slackcat -p | |
(echo "\`\`\`"; ls aliases; echo "\`\`\`") | slackcat -p | |
# Assert 'actions' in $ALIAS_LS | |
# Assert 'aliases' in $ALIAS_LS | |
# Assert 'rules' in $ALIAS_LS | |
# Assert 'sensors' in $ALIAS_LS | |
echo "Removing alias file and deleting alias with \`st2\` command..." | slackcat -p | |
sudo rm aliases/remote.yaml | |
(echo "\`\`\`"; st2 action-alias delete $PACK_NAME.remote_chatops_cmd; echo "\`\`\`") | slackcat -p | |
sleep 5 | |
echo "After deleting the alias; before reloading:" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
st2 action-alias list | grep -qv $PACK_NAME.remote_chatops_cmd | |
# Assert 'remote_chatops_cmd' in $ALIAS_ST2_LIST | |
sudo st2ctl reload --register-aliases | |
sleep 5 | |
echo "After reloading; before restarting:" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
st2 action-alias list | grep -qv $PACK_NAME.remote_chatops_cmd | |
sudo st2ctl restart | |
sleep 30 | |
echo "After restarting, \`$PACK_NAME.remote_chatops_cmd\` should be gone:" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
st2 action-alias list | grep -qv $PACK_NAME.remote_chatops_cmd | |
echo "Testing alias with default parameter value" | slackcat -p | |
sudo cat >aliases/remote-default-parameter-value.yaml <<EOF | |
--- | |
name: remote_default_parameter_value | |
description: Execute a command on a remote host via SSH - default parameter value. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} {{hosts=localhost}}" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with default host"' | slackcat -p | |
# echo "Chatops run exact command with default host" | slackcat -p | |
# echo '!run "echo Hello world" ' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
sudo rm aliases/remote-default-parameter-value.yaml | |
st2 action-alias delete $PACK_NAME.remote_default_parameter_value | |
echo "Testing alias with regex and default parameter" | slackcat -p | |
sudo cat >aliases/remote-regex-and-default-parameter.yaml <<EOF | |
--- | |
name: remote_regex_and_default_parameter | |
description: Execute a command on a remote host via SSH - regex and default parameter value. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "(run|execute) {{cmd}}( on {{hosts=localhost}})?[!.]?" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with regex and default host"' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
echo '!execute "echo Chatops execute exact command with regex and default host"' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
sudo rm aliases/remote-regex-and-default-parameter.yaml | |
st2 action-alias delete $PACK_NAME.remote_regex_and_default_parameter | |
echo "Testing alias with extra parameters" | slackcat -p | |
sudo cat >aliases/remote-extra-parameters.yaml <<EOF | |
--- | |
name: remote_extra_parameters | |
description: Execute a command on a remote host via SSH - extra parameters. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with extra parameter" on localhost timeout=120' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
sudo rm aliases/remote-extra-parameters.yaml | |
st2 action-alias delete $PACK_NAME.remote_extra_parameters | |
echo "Testing alias with weird representations" | slackcat -p | |
(echo "\`\`\`"; st2 action-alias list; echo "\`\`\`") | slackcat -p | |
echo '!help' | slackcat -p | |
sudo cat >aliases/remote-weird-representations.yaml <<EOF | |
--- | |
name: remote_weird_representations | |
description: Execute a command on a remote host via SSH - multiple formats, weird representations. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- display: "run remote command {{cmd}} on {{hosts}}" | |
representation: | |
- "run remote command {{cmd}} on {{hosts}}" | |
- "(run|execute) {{cmd}}( on {{hosts=localhost}})?[!.]?" | |
- "ssh to hosts {{hosts}} and run command {{cmd}}" | |
- "OMG st2 just run this command {{cmd}} on ma boxes {{hosts}} already" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command multiple formats" on localhost' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
echo '!run remote command "echo Chatops run exact command interesting format representation" on localhost' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
echo '!ssh to hosts localhost and run command "echo Chatops complex format"' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
echo '!OMG st2 just run this command "echo Chatops weird format" on ma boxes localhost already' | slackcat -p | |
sleep 5 | |
# Assert exact string match | |
sudo rm aliases/remote-weird-representations.yaml | |
st2 action-alias delete $PACK_NAME.remote_weird_representations | |
echo "Testing alias custom ack" | slackcat -p | |
sudo cat >aliases/remote-custom-ack.yaml <<EOF | |
--- | |
name: remote_custom_ack | |
description: Execute a command on a remote host via SSH - custom ack. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
ack: | |
enabled: true | |
append_url: false | |
format: "Running the command(s) for you" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with ack format" on localhost' | slackcat -p | |
sleep 5 | |
# Assert ack exact match | |
# Assert exact string match on reply | |
sudo rm aliases/remote-custom-ack.yaml | |
st2 action-alias delete $PACK_NAME.remote_custom_ack | |
echo "Testing alias disabled custom ack" | slackcat -p | |
sudo cat >aliases/remote-disabled-custom-ack.yaml <<EOF | |
--- | |
name: remote_disabled_custom_ack | |
description: Execute a command on a remote host via SSH - disabled custom ack. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
ack: | |
enabled: false | |
append_url: false | |
format: "Running the command(s) for you" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with ack format" on localhost' | slackcat -p | |
sleep 5 | |
# Assert no ack | |
# Assert exact string match on success | |
echo '!run "echof Chatops run exact command with ack failure format" on localhost' | slackcat -p | |
sleep 5 | |
# Assert no ack | |
# Assert exact string match on failure | |
sudo rm aliases/remote-disabled-custom-ack.yaml | |
st2 action-alias delete $PACK_NAME.remote_disabled_custom_ack | |
echo "Testing alias with custom result format" | slackcat -p | |
sudo cat >aliases/remote-custom-result-format.yaml <<EOF | |
--- | |
name: remote_custom_result_format | |
description: Execute a command on a remote host via SSH - custom result format. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
result: | |
format: | | |
Ran command \`{{ execution.parameters.cmd }}\` on \`{{ execution.parameters.hosts|length }}\` host{% if execution.parameters.hosts|length > 1 %}s{% endif %}. | |
Details are as follows: | |
{% for host in execution.result -%} | |
Host: \`{{ host }}\` | |
---> stdout: {{ execution.result[host].stdout }} | |
---> stderr: {{ execution.result[host].stderr }} | |
{%+ endfor %} | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with custom result format" on localhost' | slackcat -p | |
sleep 5 | |
# Assert ack | |
# Assert exact string match on success | |
sudo rm aliases/remote-custom-result-format.yaml | |
st2 action-alias delete $PACK_NAME.remote_custom_result_format | |
echo "Testing alias with disabled custom result format" | slackcat -p | |
sudo cat >aliases/remote-disabled-result.yaml <<EOF | |
--- | |
name: remote_disabled_result | |
description: Execute a command on a remote host via SSH - disabled custom result format. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
result: | |
enabled: false | |
format: | | |
Ran command \`{{ execution.parameters.cmd }}\` on \`{{ execution.parameters.hosts|length }}\` host{% if execution.parameters.hosts|length > 1 %}s{% endif %}. | |
Details are as follows: | |
{% for host in execution.result -%} | |
Host: \`{{ host }}\` | |
---> stdout: {{ execution.result[host].stdout }} | |
---> stderr: {{ execution.result[host].stderr }} | |
{%+ endfor %} | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with disabled custom result format" on localhost' | slackcat -p | |
sleep 5 | |
# Assert ack | |
# Assert no result | |
sudo rm aliases/remote-disabled-result.yaml | |
st2 action-alias delete $PACK_NAME.remote_disabled_result | |
echo "Testing alias with plaintext and attachment" | slackcat -p | |
sudo cat >aliases/remote-plaintext-and-attachment.yaml <<EOF | |
--- | |
name: remote_plaintext_and_attachment | |
description: Execute a command on a remote host via SSH - custom result format with plaintext and attachment. | |
pack: $PACK_NAME | |
action_ref: core.remote | |
formats: | |
- "run {{cmd}} on {{hosts}}" | |
result: | |
format: "action completed! {~} {{ execution.result }}" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!run "echo Chatops run exact command with custom result format with plaintext and attachment" on localhost' | slackcat -p | |
sleep 5 | |
# Assert ack | |
# Assert exact string match on success | |
sudo rm aliases/remote-plaintext-and-attachment.yaml | |
st2 action-alias delete $PACK_NAME.remote_plaintext_and_attachment | |
echo "Testing alias with Slack attachment parameters" | slackcat -p | |
sudo cat >aliases/kitten.yaml <<EOF | |
--- | |
name: kitten | |
description: NOOP action - result with Slack attachment parameters. | |
pack: $PACK_NAME | |
action_ref: core.noop | |
formats: | |
- "kitten pic" | |
result: | |
format: "your kittens are here! {~} Regards from the Box Kingdom." | |
extra: | |
slack: | |
image_url: "http://i.imgur.com/Gb9kAYK.jpg" | |
fields: | |
- title: Kitten headcount | |
value: Eight. | |
short: true | |
- title: Number of boxes | |
value: A bunch | |
short: true | |
color: "#00AA00" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!help' | slackcat -p | |
sleep 5 | |
echo '!kitten pic' | slackcat -p | |
sleep 5 | |
# Assert ack | |
# Assert exact string match on success | |
sudo rm aliases/kitten.yaml | |
st2 action-alias delete $PACK_NAME.kitten | |
echo "Testing alias with Slack attachment parameters with Jinja" | slackcat -p | |
sudo cat >aliases/noop-jinja.yaml <<EOF | |
--- | |
name: noop_jinja | |
description: NOOP action - result with Slack attachment parameters with Jinja. | |
pack: $PACK_NAME | |
action_ref: core.noop | |
formats: | |
- "say {{phrase}} in {{color}}" | |
result: | |
extra: | |
slack: | |
title: "Noop" | |
pretext: "Interpolated with Jinja" | |
text: "{{ execution.parameters.phrase }}" | |
color: "{{ execution.parameters.color }}" | |
EOF | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!help' | slackcat -p | |
sleep 5 | |
echo '!say Hello in #88CCEE' | slackcat -p | |
sleep 5 | |
# Assert ack | |
# Assert exact string match on success | |
sudo rm aliases/noop-jinja.yaml | |
st2 action-alias delete $PACK_NAME.noop_jinja | |
echo "Testing listing StackStorm sensors" | slackcat -p | |
sudo cat >aliases/st2_list.yaml <<EOF | |
--- | |
name: st2_sensors_list | |
description: List available StackStorm sensors. | |
pack: $PACK_NAME | |
action_ref: st2.sensors.list | |
formats: | |
- "list sensors" | |
- "list sensors from {{ pack }}" | |
- "sensors list" | |
EOF | |
sudo st2ctl reload --register-aliases 2>&1 | slackcat -p | |
sudo st2ctl restart 2>&1 | slackcat -p | |
sleep 30 | |
st2 action-alias list | grep -qv 'my_chatops.st2_sensors_list' | |
st2 action-alias list --pack=$PACK_NAME | slackcat -p | |
echo '!sensors list' | slackcat -p | |
sleep 5 | |
# Assert alias doesn't exist | |
echo '!list sensors' | slackcat -p | |
sleep 5 | |
# Assert alias doesn't exist | |
echo '!sensors list pack=examples' | slackcat -p | |
sleep 5 | |
# Assert alias doesn't exist | |
echo '!list sensors from examples' | slackcat -p | |
sleep 5 | |
# Assert alias doesn't exist | |
echo '!list sensors from examples limit=2' | slackcat -p | |
sleep 5 | |
# Assert alias doesn't exist | |
sudo rm aliases/st2_list.yaml | |
# Clean up | |
echo "\`---------------- CLEANUP ST2CHATOPS TESTS ----------------\`" | slackcat -p | |
# Removing | |
echo "Removing existing \`$PACK_NAME\` pack" | slackcat -p | |
cd $PREVIOUS_DIR | |
sudo rm -rf /opt/stackstorm/packs/$PACK_NAME | |
st2 pack remove $PACK_NAME | |
sudo st2ctl reload --register-aliases | |
sudo st2ctl restart | |
sleep 30 | |
echo '!help' | slackcat -p | |
sleep 5 | |
# Assert noop-jinja doesn't exist | |
echo 'Done testing - SUCCESS!' | slackcat -p | |
echo "\`---------------- ENDING ST2CHATOPS TESTS ----------------\`" | slackcat -p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment