|
| 1 | +import beamClient from "../lib"; |
| 2 | +import { Sandbox, SandboxInstance } from "../lib/resources/abstraction/sandbox"; |
| 3 | +import { EStubType } from "../lib/types/stub"; |
| 4 | + |
| 5 | +describe("Sandbox network parity", () => { |
| 6 | + beforeEach(() => { |
| 7 | + jest.spyOn(console, "log").mockImplementation(() => undefined); |
| 8 | + jest.spyOn(console, "warn").mockImplementation(() => undefined); |
| 9 | + jest.spyOn(console, "error").mockImplementation(() => undefined); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.restoreAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + test("rejects sandbox configs that set both blockNetwork and allowList", () => { |
| 17 | + expect(() => { |
| 18 | + new Sandbox({ |
| 19 | + name: "networked-sandbox", |
| 20 | + blockNetwork: true, |
| 21 | + allowList: ["8.8.8.8/32"], |
| 22 | + }); |
| 23 | + }).toThrow( |
| 24 | + "Cannot specify both 'blockNetwork=true' and 'allowList'. Use 'allowList' with CIDR notation to allow specific ranges, or use 'blockNetwork=true' to block all outbound traffic." |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + test("includes allowList in stub creation requests", async () => { |
| 29 | + const requestMock = jest.spyOn(beamClient, "request").mockResolvedValue({ |
| 30 | + data: { |
| 31 | + ok: true, |
| 32 | + stubId: "stub-123", |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + const sandbox = new Sandbox({ |
| 37 | + name: "networked-sandbox", |
| 38 | + allowList: ["8.8.8.8/32"], |
| 39 | + }); |
| 40 | + |
| 41 | + sandbox.stub.imageAvailable = true; |
| 42 | + sandbox.stub.filesSynced = true; |
| 43 | + sandbox.stub.objectId = "object-123"; |
| 44 | + sandbox.stub.config.image.id = "image-123"; |
| 45 | + |
| 46 | + await expect( |
| 47 | + sandbox.stub.prepareRuntime(undefined, EStubType.Sandbox, true, ["*"]) |
| 48 | + ).resolves.toBe(true); |
| 49 | + |
| 50 | + expect(requestMock).toHaveBeenCalledWith( |
| 51 | + expect.objectContaining({ |
| 52 | + method: "POST", |
| 53 | + url: "/api/v1/gateway/stubs", |
| 54 | + data: expect.objectContaining({ |
| 55 | + block_network: false, |
| 56 | + allow_list: ["8.8.8.8/32"], |
| 57 | + }), |
| 58 | + }) |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test("updates network permissions with the sandbox update endpoint", async () => { |
| 63 | + const requestMock = jest.spyOn(beamClient, "request").mockResolvedValue({ |
| 64 | + data: { |
| 65 | + ok: true, |
| 66 | + errorMsg: "", |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + const instance = new SandboxInstance( |
| 71 | + { |
| 72 | + containerId: "sandbox-123", |
| 73 | + stubId: "stub-123", |
| 74 | + url: "", |
| 75 | + ok: true, |
| 76 | + errorMsg: "", |
| 77 | + }, |
| 78 | + new Sandbox({ name: "networked-sandbox" }) |
| 79 | + ); |
| 80 | + |
| 81 | + await expect(instance.updateNetworkPermissions(true)).resolves.toBeUndefined(); |
| 82 | + |
| 83 | + expect(requestMock).toHaveBeenCalledWith({ |
| 84 | + method: "POST", |
| 85 | + url: "/api/v1/gateway/pods/sandbox-123/network/update", |
| 86 | + data: { |
| 87 | + stubId: "stub-123", |
| 88 | + blockNetwork: true, |
| 89 | + allowList: [], |
| 90 | + }, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test("rejects conflicting network permission updates before making a request", async () => { |
| 95 | + const requestMock = jest.spyOn(beamClient, "request"); |
| 96 | + |
| 97 | + const instance = new SandboxInstance( |
| 98 | + { |
| 99 | + containerId: "sandbox-123", |
| 100 | + stubId: "stub-123", |
| 101 | + url: "", |
| 102 | + ok: true, |
| 103 | + errorMsg: "", |
| 104 | + }, |
| 105 | + new Sandbox({ name: "networked-sandbox" }) |
| 106 | + ); |
| 107 | + |
| 108 | + await expect( |
| 109 | + instance.updateNetworkPermissions(true, ["8.8.8.8/32"]) |
| 110 | + ).rejects.toThrow("Cannot specify both blockNetwork=true and allowList"); |
| 111 | + |
| 112 | + expect(requestMock).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + test("returns exposed URLs keyed by port", async () => { |
| 116 | + jest.spyOn(beamClient, "request").mockResolvedValue({ |
| 117 | + data: { |
| 118 | + ok: true, |
| 119 | + urls: { |
| 120 | + "3000": "https://3000.example.com", |
| 121 | + "8080": "https://8080.example.com", |
| 122 | + }, |
| 123 | + errorMsg: "", |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + const instance = new SandboxInstance( |
| 128 | + { |
| 129 | + containerId: "sandbox-123", |
| 130 | + stubId: "stub-123", |
| 131 | + url: "", |
| 132 | + ok: true, |
| 133 | + errorMsg: "", |
| 134 | + }, |
| 135 | + new Sandbox({ name: "networked-sandbox" }) |
| 136 | + ); |
| 137 | + |
| 138 | + await expect(instance.listUrls()).resolves.toEqual({ |
| 139 | + 3000: "https://3000.example.com", |
| 140 | + 8080: "https://8080.example.com", |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments