|
| 1 | +import grpc from "k6/net/grpc"; |
| 2 | +import { check, group } from "k6"; |
| 3 | + |
| 4 | +import * as constant from "./const.js"; |
| 5 | +import * as helper from "./helper.js"; |
| 6 | + |
| 7 | +const publicClient = new grpc.Client(); |
| 8 | +const privateClient = new grpc.Client(); |
| 9 | + |
| 10 | +publicClient.load(["proto"], "pipeline/v1beta/pipeline_public_service.proto"); |
| 11 | +privateClient.load(["proto"], "pipeline/v1beta/pipeline_private_service.proto"); |
| 12 | + |
| 13 | +// ============================================================================ |
| 14 | +// Private Service API Tests |
| 15 | +// These tests cover admin/private service APIs (PipelinePrivateService) |
| 16 | +// that are only accessible internally (port 3081, not exposed externally) |
| 17 | +// |
| 18 | +// For service-to-service communication, we pass internal headers: |
| 19 | +// - Instill-User-Uid: The authenticated user's UUID |
| 20 | +// - Instill-Requester-Uid: The requester's UUID (namespace making the request) |
| 21 | +// ============================================================================ |
| 22 | + |
| 23 | +/** |
| 24 | + * Get internal service metadata with user/requester UIDs. |
| 25 | + * This simulates what the API Gateway would inject after authentication. |
| 26 | + */ |
| 27 | +function getInternalServiceMetadata() { |
| 28 | + // Get the admin user's UID from the database |
| 29 | + var userUid = helper.getNamespaceUidFromId(constant.defaultUsername); |
| 30 | + if (!userUid) { |
| 31 | + console.log(`[WARN] Could not get user UID for ${constant.defaultUsername}, using fallback`); |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + return { |
| 36 | + metadata: { |
| 37 | + "Instill-User-Uid": userUid, |
| 38 | + "Instill-Requester-Uid": userUid, // For admin user, requester is the same |
| 39 | + }, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Test LookUpPipelineAdmin (private service) |
| 45 | + * This endpoint is admin-only and requires the internal UID |
| 46 | + * NOTE: Private service port (3081) is not exposed outside container, |
| 47 | + * so this test only runs when executing from within the container. |
| 48 | + */ |
| 49 | +export function CheckLookUpPipelineAdmin(data) { |
| 50 | + // Skip if running from host - private service port not exposed |
| 51 | + if (constant.isHostMode) { |
| 52 | + group("Pipelines API: Look up a pipeline by UID (Admin) [SKIPPED - host mode]", () => { |
| 53 | + console.log("SKIPPED: Private service tests require running inside container"); |
| 54 | + }); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + group("Pipelines API: Look up a pipeline by UID (Admin)", () => { |
| 59 | + publicClient.connect(constant.pipelineGRPCPublicHost, { |
| 60 | + plaintext: true, |
| 61 | + }); |
| 62 | + privateClient.connect(constant.pipelineGRPCPrivateHost, { |
| 63 | + plaintext: true, |
| 64 | + }); |
| 65 | + |
| 66 | + // Get internal service metadata (simulates what API Gateway injects) |
| 67 | + var internalMeta = getInternalServiceMetadata(); |
| 68 | + if (!internalMeta) { |
| 69 | + console.log("[SKIP] Could not get internal service metadata, skipping LookUpPipelineAdmin"); |
| 70 | + publicClient.close(); |
| 71 | + privateClient.close(); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + var reqBody = Object.assign( |
| 76 | + {}, |
| 77 | + constant.simplePipelineWithYAMLRecipe |
| 78 | + ); |
| 79 | + |
| 80 | + // Create a pipeline via public API |
| 81 | + var createRes = publicClient.invoke( |
| 82 | + "pipeline.v1beta.PipelinePublicService/CreateNamespacePipeline", |
| 83 | + { |
| 84 | + parent: constant.namespace, |
| 85 | + pipeline: reqBody, |
| 86 | + }, |
| 87 | + data.metadata |
| 88 | + ); |
| 89 | + check(createRes, { |
| 90 | + "CreateNamespacePipeline response StatusOK": (r) => r.status === grpc.StatusOK, |
| 91 | + }); |
| 92 | + |
| 93 | + if (createRes.status !== grpc.StatusOK || !createRes.message || !createRes.message.pipeline) { |
| 94 | + console.log("Failed to create pipeline in CheckLookUpPipelineAdmin, skipping remaining tests"); |
| 95 | + publicClient.close(); |
| 96 | + privateClient.close(); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + var pipelineId = createRes.message.pipeline.id; |
| 101 | + |
| 102 | + // Get the internal UID from database |
| 103 | + var pipelineUid = helper.getPipelineUidFromId(pipelineId); |
| 104 | + check(pipelineUid, { |
| 105 | + "Got pipeline UID from database": (uid) => uid !== null && uid !== undefined, |
| 106 | + }); |
| 107 | + |
| 108 | + if (!pipelineUid) { |
| 109 | + console.log(`Failed to get pipeline UID for id=${pipelineId}, skipping LookUpAdmin test`); |
| 110 | + // Cleanup |
| 111 | + publicClient.invoke( |
| 112 | + "pipeline.v1beta.PipelinePublicService/DeleteNamespacePipeline", |
| 113 | + { name: `${constant.namespace}/pipelines/${pipelineId}` }, |
| 114 | + data.metadata |
| 115 | + ); |
| 116 | + publicClient.close(); |
| 117 | + privateClient.close(); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + // LookUpPipelineAdmin by UID permalink (private service) |
| 122 | + // Use internal service headers for service-to-service communication |
| 123 | + var lookupRes = privateClient.invoke( |
| 124 | + "pipeline.v1beta.PipelinePrivateService/LookUpPipelineAdmin", |
| 125 | + { |
| 126 | + permalink: `pipelines/${pipelineUid}`, |
| 127 | + }, |
| 128 | + internalMeta // Use internal service-to-service headers |
| 129 | + ); |
| 130 | + check(lookupRes, { |
| 131 | + "LookUpPipelineAdmin response StatusOK": (r) => r.status === grpc.StatusOK, |
| 132 | + "LookUpPipelineAdmin response has pipeline": (r) => |
| 133 | + r.message && r.message.pipeline, |
| 134 | + "LookUpPipelineAdmin response pipeline has correct id": (r) => |
| 135 | + r.message && r.message.pipeline && r.message.pipeline.id === pipelineId, |
| 136 | + "LookUpPipelineAdmin response pipeline has name": (r) => |
| 137 | + r.message && r.message.pipeline && r.message.pipeline.name && |
| 138 | + r.message.pipeline.name.includes("/pipelines/"), |
| 139 | + }); |
| 140 | + |
| 141 | + // Delete the pipeline |
| 142 | + publicClient.invoke( |
| 143 | + "pipeline.v1beta.PipelinePublicService/DeleteNamespacePipeline", |
| 144 | + { |
| 145 | + name: `${constant.namespace}/pipelines/${pipelineId}`, |
| 146 | + }, |
| 147 | + data.metadata |
| 148 | + ); |
| 149 | + |
| 150 | + publicClient.close(); |
| 151 | + privateClient.close(); |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Test ListPipelinesAdmin (private service) |
| 157 | + * This endpoint lists all pipelines across all namespaces (admin only) |
| 158 | + * NOTE: Private service port (3081) is not exposed outside container, |
| 159 | + * so this test only runs when executing from within the container. |
| 160 | + */ |
| 161 | +export function CheckListPipelinesAdmin(data) { |
| 162 | + // Skip if running from host - private service port not exposed |
| 163 | + if (constant.isHostMode) { |
| 164 | + group("Pipelines API: List all pipelines (Admin) [SKIPPED - host mode]", () => { |
| 165 | + console.log("SKIPPED: Private service tests require running inside container"); |
| 166 | + }); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + group("Pipelines API: List all pipelines (Admin)", () => { |
| 171 | + publicClient.connect(constant.pipelineGRPCPublicHost, { |
| 172 | + plaintext: true, |
| 173 | + }); |
| 174 | + privateClient.connect(constant.pipelineGRPCPrivateHost, { |
| 175 | + plaintext: true, |
| 176 | + }); |
| 177 | + |
| 178 | + // Get internal service metadata (simulates what API Gateway injects) |
| 179 | + var internalMeta = getInternalServiceMetadata(); |
| 180 | + if (!internalMeta) { |
| 181 | + console.log("[SKIP] Could not get internal service metadata, skipping ListPipelinesAdmin"); |
| 182 | + publicClient.close(); |
| 183 | + privateClient.close(); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + var reqBody = Object.assign( |
| 188 | + {}, |
| 189 | + constant.simplePipelineWithYAMLRecipe |
| 190 | + ); |
| 191 | + |
| 192 | + // Create a pipeline via public API |
| 193 | + var createRes = publicClient.invoke( |
| 194 | + "pipeline.v1beta.PipelinePublicService/CreateNamespacePipeline", |
| 195 | + { |
| 196 | + parent: constant.namespace, |
| 197 | + pipeline: reqBody, |
| 198 | + }, |
| 199 | + data.metadata |
| 200 | + ); |
| 201 | + check(createRes, { |
| 202 | + "CreateNamespacePipeline response StatusOK": (r) => r.status === grpc.StatusOK, |
| 203 | + }); |
| 204 | + |
| 205 | + if (createRes.status !== grpc.StatusOK || !createRes.message || !createRes.message.pipeline) { |
| 206 | + console.log("Failed to create pipeline in CheckListPipelinesAdmin, skipping remaining tests"); |
| 207 | + publicClient.close(); |
| 208 | + privateClient.close(); |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + var pipelineId = createRes.message.pipeline.id; |
| 213 | + |
| 214 | + // ListPipelinesAdmin (private service) |
| 215 | + // Use internal service headers for service-to-service communication |
| 216 | + var listRes = privateClient.invoke( |
| 217 | + "pipeline.v1beta.PipelinePrivateService/ListPipelinesAdmin", |
| 218 | + { |
| 219 | + pageSize: 100, |
| 220 | + }, |
| 221 | + internalMeta // Use internal service-to-service headers |
| 222 | + ); |
| 223 | + check(listRes, { |
| 224 | + "ListPipelinesAdmin response StatusOK": (r) => r.status === grpc.StatusOK, |
| 225 | + "ListPipelinesAdmin response has pipelines": (r) => |
| 226 | + r.message && r.message.pipelines, |
| 227 | + "ListPipelinesAdmin response contains created pipeline": (r) => |
| 228 | + r.message && r.message.pipelines && |
| 229 | + r.message.pipelines.some(p => p.id === pipelineId), |
| 230 | + }); |
| 231 | + |
| 232 | + // Delete the pipeline |
| 233 | + publicClient.invoke( |
| 234 | + "pipeline.v1beta.PipelinePublicService/DeleteNamespacePipeline", |
| 235 | + { |
| 236 | + name: `${constant.namespace}/pipelines/${pipelineId}`, |
| 237 | + }, |
| 238 | + data.metadata |
| 239 | + ); |
| 240 | + |
| 241 | + publicClient.close(); |
| 242 | + privateClient.close(); |
| 243 | + }); |
| 244 | +} |
0 commit comments