-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathset-test-tokens.sh
More file actions
executable file
·127 lines (110 loc) · 3.31 KB
/
Copy pathset-test-tokens.sh
File metadata and controls
executable file
·127 lines (110 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env bash
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Generate two LiveKit access tokens via `lk` and set the environment variables
# required by the C++ SDK's integration and stress tests (data tracks, RPC,
# media multistream, etc.).
#
# source scripts/set-test-tokens.sh
# eval "$(bash scripts/set-test-tokens.sh)"
#
# Exports:
# LIVEKIT_TOKEN_A
# LIVEKIT_TOKEN_B
# LIVEKIT_URL=ws://localhost:7880
#
_sourced=0
if [[ -n "${BASH_VERSION:-}" ]] && [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
_sourced=1
elif [[ -n "${ZSH_VERSION:-}" ]] && [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]]; then
_sourced=1
fi
_fail() {
echo "set_test_tokens: $1" >&2
if [[ "$_sourced" -eq 1 ]]; then
return "${2:-1}"
fi
exit "${2:-1}"
}
if [[ "$_sourced" -eq 0 ]]; then
set -euo pipefail
fi
LIVEKIT_ROOM="cpp_data_track_test"
LIVEKIT_IDENTITY_A="cpp-test-a"
LIVEKIT_IDENTITY_B="cpp-test-b"
if [[ $# -ne 0 ]]; then
_fail "this script is hard-coded and does not accept arguments" 2
fi
LIVEKIT_API_KEY="devkey"
LIVEKIT_API_SECRET="secret"
LIVEKIT_VALID_FOR="99999h"
LIVEKIT_URL="ws://localhost:7880"
_grant_json='{"canPublish":true,"canSubscribe":true,"canPublishData":true}'
if ! command -v lk >/dev/null 2>&1; then
_fail "'lk' CLI not found. Install: https://docs.livekit.io/home/cli/" 2
fi
_create_token() {
local identity="$1"
local output=""
local command_status=0
local token=""
output="$(
bash -c '
lk token create \
--api-key "$1" \
--api-secret "$2" \
-i "$3" \
--join \
--valid-for "$4" \
--room "$5" \
--grant "$6" 2>&1
' _ "$LIVEKIT_API_KEY" "$LIVEKIT_API_SECRET" "$identity" \
"$LIVEKIT_VALID_FOR" "$LIVEKIT_ROOM" "$_grant_json"
)"
command_status=$?
if [[ "$command_status" -ne 0 ]]; then
echo "$output" >&2
_fail "lk token create failed for identity '$identity'" 1
fi
while IFS= read -r line || [[ -n "${line}" ]]; do
if [[ "$line" == "Access token: "* ]]; then
token="${line#Access token: }"
break
fi
done <<< "$output"
if [[ -z "$token" ]]; then
echo "$output" >&2
_fail "could not parse Access token for identity '$identity'" 1
fi
printf '%s' "$token"
}
LIVEKIT_TOKEN_A="$(_create_token "$LIVEKIT_IDENTITY_A")"
LIVEKIT_TOKEN_B="$(_create_token "$LIVEKIT_IDENTITY_B")"
_apply() {
export LIVEKIT_TOKEN_A
export LIVEKIT_TOKEN_B
export LIVEKIT_URL
}
_emit_eval() {
printf 'export LIVEKIT_TOKEN_A=%q\n' "$LIVEKIT_TOKEN_A"
printf 'export LIVEKIT_TOKEN_B=%q\n' "$LIVEKIT_TOKEN_B"
printf 'export LIVEKIT_URL=%q\n' "$LIVEKIT_URL"
}
if [[ "$_sourced" -eq 1 ]]; then
_apply
echo "LIVEKIT_TOKEN_A, LIVEKIT_TOKEN_B, and LIVEKIT_URL set for this shell." >&2
else
_emit_eval
echo "set_test_tokens: for this shell run: source $0 or: eval \"\$(bash $0)\"" >&2
fi