| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as containeranalysis from "@distilled.cloud/gcp/containeranalysis_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { |
| 8 | location, |
| 9 | logLevel, |
| 10 | currentProject, |
| 11 | TEST_ATTESTATION, |
| 12 | TEST_RESOURCE_URI, |
| 13 | } from "./common.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: GCP.providers() }); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | containeranalysis.getProjectsLocationsOccurrences({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsOccurrences on a missing occurrence fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const project = yield* currentProject; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | containeranalysis.getProjectsLocationsOccurrences({ |
| 37 | name: `projects/${project}/locations/${location}/occurrences/alchemy-missing-occurrence`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:containeranalysis", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, and delete a locations occurrence", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const note = yield* GCP.ContainerAnalysis.LocationsNote("Authority", { |
| 59 | location, |
| 60 | shortDescription: "regional occurrence attestor", |
| 61 | attestation: { hint: { humanReadableName: "Alchemy Regional" } }, |
| 62 | }); |
| 63 | const occurrence = yield* GCP.ContainerAnalysis.LocationsOccurrence( |
| 64 | "Signed", |
| 65 | { |
| 66 | location, |
| 67 | noteName: note.name, |
| 68 | resourceUri: TEST_RESOURCE_URI, |
| 69 | remediation: "initial", |
| 70 | attestation: TEST_ATTESTATION, |
| 71 | }, |
| 72 | ); |
| 73 | return { note, occurrence }; |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | expect(created.occurrence.name).toContain( |
| 78 | `/locations/${location}/occurrences/`, |
| 79 | ); |
| 80 | expect(created.occurrence.location).toEqual(location); |
| 81 | expect(created.occurrence.noteName).toEqual(created.note.name); |
| 82 | expect(created.occurrence.resourceUri).toEqual(TEST_RESOURCE_URI); |
| 83 | expect(created.occurrence.kind).toEqual("ATTESTATION"); |
| 84 | |
| 85 | const fetched = yield* containeranalysis.getProjectsLocationsOccurrences({ |
| 86 | name: created.occurrence.name, |
| 87 | }); |
| 88 | expect(fetched.name).toEqual(created.occurrence.name); |
| 89 | expect(fetched.remediation).toContain("[alchemy "); |
| 90 | expect(fetched.kind).toEqual("ATTESTATION"); |
| 91 | |
| 92 | const updated = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | const note = yield* GCP.ContainerAnalysis.LocationsNote("Authority", { |
| 95 | noteId: created.note.noteId, |
| 96 | location, |
| 97 | shortDescription: "regional occurrence attestor", |
| 98 | attestation: { hint: { humanReadableName: "Alchemy Regional" } }, |
| 99 | }); |
| 100 | const occurrence = yield* GCP.ContainerAnalysis.LocationsOccurrence( |
| 101 | "Signed", |
| 102 | { |
| 103 | location, |
| 104 | noteName: note.name, |
| 105 | resourceUri: TEST_RESOURCE_URI, |
| 106 | remediation: "rebuild from a patched base", |
| 107 | attestation: TEST_ATTESTATION, |
| 108 | }, |
| 109 | ); |
| 110 | return { note, occurrence }; |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(updated.occurrence.name).toEqual(created.occurrence.name); |
| 115 | expect(updated.occurrence.remediation).toEqual( |
| 116 | "rebuild from a patched base", |
| 117 | ); |
| 118 | |
| 119 | yield* stack.destroy(); |
| 120 | const gone = yield* waitUntilGone(created.occurrence.name); |
| 121 | expect(gone).toEqual("gone"); |
| 122 | }).pipe(logLevel), |
| 123 | { |
| 124 | tags: ["provider:gcp", "provider:gcp:containeranalysis", "live"], |
| 125 | timeout: 90_000, |
| 126 | }, |
| 127 | ); |