@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 271 lines 10.1 KB
Raw
1 /**
2 * CLI upgrade command e2e test
3 *
4 * Verifies that the compiled Linux binary can replace its running executable.
5 */
6
7 import { e2e, type E2ETestResult } from '@e2e-tests/utils';
8 import { TIMEOUTS } from '@e2e-tests/utils/const';
9 import { beforeAll, describe, expect, it } from 'bun:test';
10
11 // The "downloaded" bundle is laid out like a release archive from before release
12 // manifests: the live companions with their real contents, plus the legacy paths
13 // as empty placeholders.
14
15 // RUN_COMPANION_MODULE_FILENAMES
16 const liveCompanionFileNames = ['run-helpers-runtime.mjs', 'generation-runtime.mjs'] as const;
17
18 const runOutputMarker = 'composio-upgrade-run-companions-loaded';
19
20 // RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS
21 const legacyPlaceholderRelativePaths = [
22 'run-subagent-shared.mjs',
23 'run-subagent-acp.mjs',
24 'run-subagent-legacy.mjs',
25 'run-subagent-output-mcp.mjs',
26 'acp-adapters/claude-code-acp.mjs',
27 'acp-adapters/cli.js',
28 'acp-adapters/codex/darwin-arm64/codex-acp',
29 'acp-adapters/codex/darwin-x64/codex-acp',
30 'acp-adapters/codex/linux-arm64/codex-acp',
31 'acp-adapters/codex/linux-x64/codex-acp',
32 ] as const;
33
34 const legacyBundleSetup = [
35 'mkdir -p "$source_dir/services"',
36 ...liveCompanionFileNames.flatMap(fileName => [
37 `cp "/usr/local/bin/${fileName}" "$source_dir/${fileName}"`,
38 `cp "/usr/local/bin/services/${fileName}" "$source_dir/services/${fileName}"`,
39 ]),
40 ...legacyPlaceholderRelativePaths.map(
41 relativePath =>
42 `mkdir -p "$(dirname "$source_dir/${relativePath}")"\n: > "$source_dir/${relativePath}"`
43 ),
44 ].join('\n');
45
46 // RELEASE_MANIFEST_FILENAME
47 const manifestFileName = 'release-manifest.json';
48
49 const manifestCompanions = ['run-helpers-runtime.mjs', 'services/run-helpers-runtime.mjs'];
50
51 const releaseManifest = (minimumUpgradeFrom: string) => ({
52 schemaVersion: 1,
53 minimumUpgradeFrom,
54 companions: manifestCompanions,
55 });
56
57 /**
58 * A bundle from a release that dropped the generation runtime and the legacy
59 * placeholders: its manifest lists only what it ships.
60 */
61 const manifestBundleSetup = (minimumUpgradeFrom: string) =>
62 [
63 'mkdir -p "$source_dir/services"',
64 ...manifestCompanions.map(
65 relativePath => `cp "/usr/local/bin/${relativePath}" "$source_dir/${relativePath}"`
66 ),
67 `cat > "$source_dir/${manifestFileName}" <<'EOF'`,
68 JSON.stringify(releaseManifest(minimumUpgradeFrom), null, 2),
69 'EOF',
70 ].join('\n');
71
72 const upgradeCommand = ({
73 executablePath,
74 copyExecutable,
75 bundleSetup = legacyBundleSetup,
76 }: {
77 executablePath: string;
78 copyExecutable: boolean;
79 bundleSetup?: string;
80 }) => `
81 set -u
82 executable_path=${executablePath}
83 source_dir=/tmp/composio-upgrade-source
84 mkdir -p "$source_dir"
85 ${copyExecutable ? 'mkdir -p "$(dirname "$executable_path")"\ncp /usr/local/bin/composio "$executable_path"' : ''}
86 cp /usr/local/bin/composio "$source_dir/composio"
87 ${bundleSetup}
88
89 before_inode=$(stat -c '%i' "$executable_path")
90 upgrade_status=0
91 DEBUG_OVERRIDE_UPGRADE_TARGET="$source_dir/composio" "$executable_path" upgrade || upgrade_status=$?
92 after_inode=$(stat -c '%i' "$executable_path" 2>/dev/null)
93
94 if [ -x "$executable_path" ]; then
95 executable_status=0
96 else
97 executable_status=1
98 fi
99
100 version_output=$("$executable_path" version)
101 version_status=$?
102 run_output=$("$executable_path" run 'console.log(z.string().parse("${runOutputMarker}"))')
103 run_status=$?
104 installed_manifest=$(cat "$(dirname "$executable_path")/${manifestFileName}" 2>/dev/null | tr -d '\n ')
105 printf 'before_inode=%s\nafter_inode=%s\nupgrade_status=%s\nexecutable_status=%s\nversion_status=%s\nversion=%s\nrun_status=%s\nrun_output=%s\ninstalled_manifest=%s\n' \
106 "$before_inode" "$after_inode" "$upgrade_status" "$executable_status" "$version_status" "$version_output" "$run_status" "$run_output" "$installed_manifest"
107
108 if [ "$upgrade_status" -eq 0 ] &&
109 [ "$executable_status" -eq 0 ] &&
110 [ "$version_status" -eq 0 ] &&
111 [ "$run_status" -eq 0 ] &&
112 [ "$run_output" = "${runOutputMarker}" ] &&
113 [ -n "$before_inode" ] &&
114 [ -n "$after_inode" ] &&
115 [ "$before_inode" != "$after_inode" ]; then
116 exit 0
117 fi
118 exit 1
119 `;
120
121 /**
122 * An upgrade the process is not allowed to perform: the install directory is
123 * read-only and the CLI runs as an ordinary user (root would ignore the mode).
124 */
125 const readOnlyInstallCommand = `
126 set -u
127 install_dir=/opt/composio-readonly
128 source_dir=/tmp/composio-upgrade-source
129 mkdir -p "$install_dir" "$source_dir"
130 cp -r /usr/local/bin/. "$install_dir/"
131 cp /usr/local/bin/composio "$source_dir/composio"
132 ${legacyBundleSetup}
133 chmod -R a+rX "$source_dir"
134 chmod -R a-w "$install_dir"
135
136 before=$(stat -c '%i %s' "$install_dir/composio")
137 upgrade_status=0
138 HOME=/tmp/unprivileged-home DEBUG_OVERRIDE_UPGRADE_TARGET="$source_dir/composio" \
139 setpriv --reuid=1000 --regid=1000 --clear-groups "$install_dir/composio" upgrade || upgrade_status=$?
140 after=$(stat -c '%i %s' "$install_dir/composio")
141 version_output=$("$install_dir/composio" version)
142 version_status=$?
143 leftovers=$(find "$install_dir" -name '.composio-atomic-replace-*' | wc -l)
144 printf 'upgrade_status=%s\nbefore=%s\nafter=%s\nversion_status=%s\nversion=%s\nleftovers=%s\n' \
145 "$upgrade_status" "$before" "$after" "$version_status" "$version_output" "$leftovers"
146 `;
147
148 const belowMinimumCommand = `
149 set -u
150 install_dir=/opt/composio-below-minimum
151 source_dir=/tmp/composio-upgrade-source
152 mkdir -p "$install_dir" "$source_dir"
153 cp -r /usr/local/bin/. "$install_dir/"
154 printf '%s\\n' '@composio/cli@0.5.0' > "$install_dir/release-tag.txt"
155 cp /usr/local/bin/composio "$source_dir/composio"
156 ${manifestBundleSetup('99.0.0')}
157
158 before=$(stat -c '%i %s' "$install_dir/composio")
159 upgrade_status=0
160 DEBUG_OVERRIDE_UPGRADE_TARGET="$source_dir/composio" "$install_dir/composio" upgrade || upgrade_status=$?
161 after=$(stat -c '%i %s' "$install_dir/composio")
162 if [ -e "$install_dir/${manifestFileName}" ]; then manifest_installed=1; else manifest_installed=0; fi
163 release_tag=$(cat "$install_dir/release-tag.txt")
164 printf 'upgrade_status=%s\\nbefore=%s\\nafter=%s\\nmanifest_installed=%s\\nrelease_tag=%s\\n' \\
165 "$upgrade_status" "$before" "$after" "$manifest_installed" "$release_tag"
166 `;
167
168 const outputField = (result: E2ETestResult, name: string): string => {
169 const match = result.stdout.match(new RegExp(`^${name}=(.*)$`, 'm'));
170 expect(match).not.toBeNull();
171 return match?.[1] ?? '';
172 };
173
174 const expectAtomicUpgrade = (result: E2ETestResult) => {
175 expect(result.exitCode).toBe(0);
176 expect(result.stderr).not.toMatch(/ETXTBSY|text[ -]file(?: is)?[ -]busy/i);
177 expect(outputField(result, 'upgrade_status')).toBe('0');
178 expect(outputField(result, 'executable_status')).toBe('0');
179 expect(outputField(result, 'version_status')).toBe('0');
180 expect(outputField(result, 'version')).toMatch(/\d+\.\d+\.\d+/);
181 expect(outputField(result, 'run_status')).toBe('0');
182 expect(outputField(result, 'run_output')).toBe(runOutputMarker);
183 expect(outputField(result, 'after_inode')).not.toBe(outputField(result, 'before_inode'));
184 };
185
186 e2e(import.meta.url, {
187 versions: {
188 cli: ['current'],
189 },
190 defineTests: ({ runCmd }) => {
191 let installedResult: E2ETestResult;
192 let copiedResult: E2ETestResult;
193 let readOnlyResult: E2ETestResult;
194 let manifestResult: E2ETestResult;
195 let belowMinimumResult: E2ETestResult;
196
197 beforeAll(async () => {
198 installedResult = await runCmd(
199 upgradeCommand({ executablePath: '/usr/local/bin/composio', copyExecutable: false })
200 );
201 copiedResult = await runCmd(
202 upgradeCommand({ executablePath: '/tmp/composio-copy/composio', copyExecutable: true })
203 );
204 readOnlyResult = await runCmd(readOnlyInstallCommand);
205 manifestResult = await runCmd(
206 upgradeCommand({
207 executablePath: '/usr/local/bin/composio',
208 copyExecutable: false,
209 bundleSetup: manifestBundleSetup('0.1.26'),
210 })
211 );
212 belowMinimumResult = await runCmd(belowMinimumCommand);
213 }, TIMEOUTS.FIXTURE);
214
215 describe('composio upgrade', () => {
216 it('replaces the running installed executable', () => {
217 expectAtomicUpgrade(installedResult);
218 });
219
220 it('replaces an executable running from a copied path', () => {
221 expectAtomicUpgrade(copiedResult);
222 });
223 });
224
225 describe('composio upgrade to a bundle with a release manifest', () => {
226 it('installs a bundle that omits the generation runtime and every placeholder', () => {
227 expectAtomicUpgrade(manifestResult);
228 });
229
230 it('installs the manifest next to the executable', () => {
231 expect(outputField(manifestResult, 'installed_manifest')).toBe(
232 JSON.stringify(releaseManifest('0.1.26'))
233 );
234 });
235 });
236
237 describe('composio upgrade to a bundle that requires a newer CLI', () => {
238 it('refuses and names the reinstall path', () => {
239 expect(outputField(belowMinimumResult, 'upgrade_status')).toBe('1');
240 expect(belowMinimumResult.stderr).toContain('99.0.0');
241 expect(belowMinimumResult.stderr).toContain('curl -fsSL https://composio.dev/install | sh');
242 });
243
244 it('leaves the install untouched', () => {
245 expect(outputField(belowMinimumResult, 'after')).toBe(
246 outputField(belowMinimumResult, 'before')
247 );
248 expect(outputField(belowMinimumResult, 'manifest_installed')).toBe('0');
249 expect(outputField(belowMinimumResult, 'release_tag')).toBe('@composio/cli@0.5.0');
250 });
251 });
252
253 describe('composio upgrade without write permission on the install directory', () => {
254 it('fails and says which file it could not replace', () => {
255 expect(outputField(readOnlyResult, 'upgrade_status')).toBe('1');
256 expect(readOnlyResult.stderr).toContain('permission denied');
257 expect(readOnlyResult.stderr).toContain('/opt/composio-readonly/');
258 });
259
260 it('leaves the installed executable untouched and runnable', () => {
261 expect(outputField(readOnlyResult, 'after')).toBe(outputField(readOnlyResult, 'before'));
262 expect(outputField(readOnlyResult, 'version_status')).toBe('0');
263 expect(outputField(readOnlyResult, 'version')).toMatch(/\d+\.\d+\.\d+/);
264 });
265
266 it('leaves no temporary files behind', () => {
267 expect(outputField(readOnlyResult, 'leftovers')).toBe('0');
268 });
269 });
270 },
271 });