Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit ce46190

Browse files
ddworkenmmou
andauthored
Add tests and docs for ping/pong interaction (#93)
* Add tests and docs for ping/pong interaction * Fix golangci-lint * Update docs/getting_started.md Co-Authored-By: M Ember Mou <[email protected]> * Update tests/tests/test_env_1.py Co-Authored-By: M Ember Mou <[email protected]> Co-authored-by: M Ember Mou <[email protected]>
1 parent ae5bbd9 commit ce46190

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

docs/getting_started.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ Now on the server where you wish to run the chatbot, start the chatbot itself:
5252
make serve # Runs inside of docker for ease of use
5353
```
5454

55-
Now download the kssh binary and start SSHing! See https://github.com/keybase/bot-sshca/releases to download the most
55+
You can confirm that the bot is running by sending the message `ping @bot_username` in any of the configured team chat
56+
channels (if `CHAT_CHANNEL` is configured, the message must be sent in that specific channel). The bot should reply with
57+
`pong @your_username`.
58+
59+
Now you can download the kssh binary and start SSHing! See https://github.com/keybase/bot-sshca/releases to download the most
5660
recent version of kssh for your platform.
5761

5862
```bash
@@ -72,4 +76,3 @@ If you update any environment variables, it is necessary to restart the keybasec
7276
by running `make restart`. Note that it is not required to re-run `make generate`.
7377

7478
Note that this means `kssh` will not work for a brief period of time while the container restarts.
75-

src/keybaseca/bot/bot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func StartBot(conf config.Config) error {
8888
if shared.IsPingRequest(messageBody, kbc.GetUsername()) {
8989
// Respond to messages of the form `ping @botName` with `pong @senderName`
9090
log.Debug("Responding to ping with pong")
91-
_, err = kbc.SendMessageByConvID(msg.Message.ConvID, fmt.Sprintf(shared.GeneratePingResponse(msg.Message.Sender.Username)))
91+
_, err = kbc.SendMessageByConvID(msg.Message.ConvID, shared.GeneratePingResponse(msg.Message.Sender.Username))
9292
if err != nil {
9393
LogError(conf, kbc, msg, err)
9494
continue

src/shared/chat_types.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ func IsAckResponse(msg string) bool {
8282
}
8383

8484
// Generate a ping request message
85-
func GeneratePingRequest(botname string) string {
86-
return fmt.Sprintf("ping @%s", botname)
85+
func GeneratePingRequest(username string) string {
86+
return fmt.Sprintf("ping @%s", username)
8787
}
8888

8989
// Returns whether the given message is a ping request
9090
func IsPingRequest(msg, botUsername string) bool {
91-
return strings.TrimSpace(msg) == fmt.Sprintf("ping @%s", botUsername)
91+
return strings.TrimSpace(msg) == GeneratePingRequest(botUsername)
9292
}
9393

9494
// Generate a ping response message
95-
func GeneratePingResponse(botname string) string {
96-
return fmt.Sprintf("pong @%s", botname)
95+
func GeneratePingResponse(username string) string {
96+
return fmt.Sprintf("pong @%s", username)
9797
}
9898

9999
// Returns whether the given message is a ping response
100100
func IsPingResponse(msg, localUsername string) bool {
101-
return strings.TrimSpace(msg) == fmt.Sprintf("pong @%s", localUsername)
101+
return strings.TrimSpace(msg) == GeneratePingResponse(localUsername)
102102
}

tests/bot-entrypoint.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def load_env():
2828
"echo yes | bin/keybaseca backup > /shared/cakey.backup\n"
2929
# The output from this sign operation is tested in test_env_1.py
3030
"ssh-keygen -t ed25519 -f /shared/userkey -N '' && bin/keybaseca sign --public-key /shared/userkey.pub > /shared/keybaseca-sign.out\n"
31-
"bin/keybaseca service &"
31+
"bin/keybaseca --debug service > /tmp/debug.out &"
3232
) % (shlex.quote(path)))
3333
# Sleep so keybaseca has time to start
34-
time.sleep(5)
34+
time.sleep(10)
3535
return "OK"
3636

3737
if __name__ == '__main__':

tests/tests/lib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def outputs_audit_log(tc: TestConfig, filename: str, expected_number: int):
127127
yield
128128

129129
# And sleep to give KBFS some time
130-
time.sleep(1.5)
130+
time.sleep(2.5)
131131

132132
# Then see if there are new lines using set difference. This is only safe/reasonable since we include a
133133
# timestamp in audit log lines.

tests/tests/test_env_1.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def configure_env(self):
1616
def test_config(self):
1717
return TestConfig.getDefaultTestConfig()
1818

19+
def test_ping_pong_command(self, test_config):
20+
run_command(f"keybase chat send --channel ssh-provision {test_config.subteam}.ssh 'ping @{test_config.bot_username}'")
21+
time.sleep(5)
22+
recent_messages = run_command(f"keybase chat list-unread --since 1m")
23+
assert (b"pong @%s" % test_config.username.encode('utf-8')) in recent_messages
24+
1925
def test_kssh_staging_user(self, test_config):
2026
# Test ksshing into staging as user
2127
with outputs_audit_log(test_config, filename=test_env_1_log_filename, expected_number=1):
@@ -166,4 +172,4 @@ def test_kssh_alternate_binary(self, test_config):
166172
assert_contains_hash(test_config.expected_hash, run_command_with_agent("bin/kssh -q -o StrictHostKeyChecking=no user@sshd-staging 'sha1sum /etc/unique'"))
167173
run_command("bin/kssh --set-keybase-binary ''")
168174
finally:
169-
run_command("sudo rm /usr/local/bin/keybase")
175+
run_command("sudo rm /usr/local/bin/keybase")

0 commit comments

Comments
 (0)