|
| 1 | +import { $ } from "dax"; |
| 2 | +import { assert, assertEquals } from "@std/assert"; |
| 3 | + |
| 4 | +if (!Deno.env.get("DENO_DEPLOY_TOKEN")) { |
| 5 | + console.error("DENO_DEPLOY_TOKEN environment variable is required."); |
| 6 | + Deno.exit(1); |
| 7 | +} |
| 8 | + |
| 9 | +const sandbox = async (...args: string[]) => { |
| 10 | + console.log(`deno sandbox ${args.join(" ")}`); |
| 11 | + return (await $.raw`deno sandbox ${args.join(" ")}`.text()).trim(); |
| 12 | +}; |
| 13 | + |
| 14 | +Deno.test("sandbox create", async () => { |
| 15 | + const sandboxId = await sandbox( |
| 16 | + "create", |
| 17 | + "--quiet", |
| 18 | + "--lifetime", |
| 19 | + "60s", |
| 20 | + "echo", |
| 21 | + "test", |
| 22 | + ); |
| 23 | + await sandbox("kill", sandboxId); |
| 24 | +}); |
| 25 | + |
| 26 | +Deno.test("sandbox exec", async () => { |
| 27 | + const sandboxId = await sandbox("create", "--quiet", "--lifetime", "60s"); |
| 28 | + |
| 29 | + const res = await sandbox("exec", sandboxId, "echo", "'exec test'"); |
| 30 | + assertEquals(res, "exec test"); |
| 31 | + |
| 32 | + await sandbox("kill", sandboxId); |
| 33 | +}); |
| 34 | + |
| 35 | +Deno.test("sandbox copy", async () => { |
| 36 | + const sandboxId = await sandbox("create", "--quiet", "--lifetime", "60s"); |
| 37 | + |
| 38 | + await Deno.writeTextFile("test.txt", "test content"); |
| 39 | + |
| 40 | + await sandbox("copy", "test.txt", `${sandboxId}:/tmp/test.txt`); |
| 41 | + |
| 42 | + await sandbox("copy", `${sandboxId}:/tmp/test.txt`, "./downloaded.txt"); |
| 43 | + const downloadedContent = await Deno.readTextFile("./downloaded.txt"); |
| 44 | + assertEquals(downloadedContent, "test content"); |
| 45 | + |
| 46 | + await Deno.remove("test.txt"); |
| 47 | + await Deno.remove("downloaded.txt"); |
| 48 | + |
| 49 | + await sandbox("kill", sandboxId); |
| 50 | +}); |
| 51 | + |
| 52 | +Deno.test("sandbox extend", async () => { |
| 53 | + const sandboxId = await sandbox( |
| 54 | + "create", |
| 55 | + "--quiet", |
| 56 | + "--lifetime", |
| 57 | + "60s", |
| 58 | + "sleep", |
| 59 | + "10", |
| 60 | + ); |
| 61 | + |
| 62 | + await sandbox("extend", sandboxId, "120s"); |
| 63 | + |
| 64 | + await sandbox("kill", sandboxId); |
| 65 | +}); |
| 66 | + |
| 67 | +Deno.test("sandbox exec with complex commands", async () => { |
| 68 | + const sandboxId = await sandbox("create", "--quiet", "--lifetime", "60s"); |
| 69 | + |
| 70 | + const result = await sandbox("exec", sandboxId, "'echo hello && echo world'"); |
| 71 | + assert(result.includes("hello")); |
| 72 | + assert(result.includes("world")); |
| 73 | + |
| 74 | + await sandbox("kill", sandboxId); |
| 75 | +}); |
| 76 | + |
| 77 | +Deno.test("sandbox copy directory structure", async () => { |
| 78 | + const sandboxId = await sandbox("create", "--quiet", "--lifetime", "60s"); |
| 79 | + |
| 80 | + await Deno.mkdir("testdir", { recursive: true }); |
| 81 | + await Deno.writeTextFile("testdir/file1.txt", "content1"); |
| 82 | + await Deno.writeTextFile("testdir/file2.txt", "content2"); |
| 83 | + |
| 84 | + await sandbox("exec", sandboxId, "'mkdir -p /tmp/testdir'"); |
| 85 | + await sandbox( |
| 86 | + "copy", |
| 87 | + "testdir/file1.txt", |
| 88 | + `${sandboxId}:/tmp/testdir/file1.txt`, |
| 89 | + ); |
| 90 | + await sandbox( |
| 91 | + "copy", |
| 92 | + "testdir/file2.txt", |
| 93 | + `${sandboxId}:/tmp/testdir/file2.txt`, |
| 94 | + ); |
| 95 | + |
| 96 | + const result = await sandbox("exec", sandboxId, "ls", "/tmp/testdir"); |
| 97 | + assert(result.includes("file1.txt")); |
| 98 | + assert(result.includes("file2.txt")); |
| 99 | + |
| 100 | + await Deno.remove("testdir", { recursive: true }); |
| 101 | + await sandbox("kill", sandboxId); |
| 102 | +}); |
| 103 | + |
| 104 | +Deno.test("volumes create", async () => { |
| 105 | + const volumeName = `test-vol-${crypto.randomUUID()}`.slice(0, 32); |
| 106 | + |
| 107 | + const volumeId = await sandbox( |
| 108 | + "volumes", |
| 109 | + "create", |
| 110 | + volumeName, |
| 111 | + "--capacity", |
| 112 | + "1gb", |
| 113 | + "--region", |
| 114 | + "ord", |
| 115 | + ); |
| 116 | + |
| 117 | + await sandbox("volumes", "delete", volumeId); |
| 118 | +}); |
| 119 | + |
| 120 | +Deno.test("sandbox with volume mount", async () => { |
| 121 | + const volumeName = `test-vol-${crypto.randomUUID()}`.slice(0, 32); |
| 122 | + |
| 123 | + const volumeId = await sandbox( |
| 124 | + "volumes", |
| 125 | + "create", |
| 126 | + volumeName, |
| 127 | + "--capacity", |
| 128 | + "1gb", |
| 129 | + "--region", |
| 130 | + "ord", |
| 131 | + ); |
| 132 | + |
| 133 | + const sandboxId = await sandbox( |
| 134 | + "create", |
| 135 | + "--quiet", |
| 136 | + "--lifetime", |
| 137 | + "60s", |
| 138 | + "--volume", |
| 139 | + `${volumeId}:/data/dataset`, |
| 140 | + ); |
| 141 | + |
| 142 | + await sandbox( |
| 143 | + "exec", |
| 144 | + sandboxId, |
| 145 | + "\"echo 'volume test' > /data/dataset/test.txt\"", |
| 146 | + ); |
| 147 | + const result = await sandbox( |
| 148 | + "exec", |
| 149 | + sandboxId, |
| 150 | + "cat", |
| 151 | + "/data/dataset/test.txt", |
| 152 | + ); |
| 153 | + assertEquals(result, "volume test"); |
| 154 | + |
| 155 | + await sandbox("kill", sandboxId); |
| 156 | + await sandbox("volumes", "delete", volumeId); |
| 157 | +}); |
0 commit comments