forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧪 test(e2e): finish the plugins test
Signed-off-by: yi-ge-dian <1085266008@qq.com>
- Loading branch information
1 parent
e1e5c85
commit 8e32771
Showing
13 changed files
with
363 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
**/node_modules | ||
build/ | ||
test/data/esm_* | ||
test/data/plugins | ||
docs/ | ||
**/plugins/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module.exports = { | ||
knative: { | ||
FUNC_CONTEXT: JSON.stringify({ | ||
name: 'openfunction-plugins-context', | ||
version: '1.0.0', | ||
runtime: 'knative', | ||
states: { | ||
// there is a policy, if you don't specify the state component, we will use the first one | ||
// or you need specify the state name, then the context will use the one you specify | ||
// hint : specify the name is the record key, not the component name | ||
pg: { | ||
componentName: 'pg', | ||
componentType: 'state.postgresql', | ||
}, | ||
}, | ||
pluginsTracing: { | ||
enabled: true, | ||
provider: { | ||
name: 'skywalking', | ||
oapServer: 'localhost:11800', | ||
}, | ||
tags: { | ||
tag1: 'value1', | ||
tag2: 'value2', | ||
}, | ||
baggage: { | ||
key: 'key1', | ||
value: 'value1', | ||
}, | ||
} | ||
}), | ||
}, | ||
}; |
22 changes: 22 additions & 0 deletions
22
test/e2e/openfunction-plugins/Docker.openfunction.skywalking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FROM node:16.17.0-bullseye-slim | ||
|
||
# Set working directory | ||
WORKDIR /ff | ||
|
||
# Copy project files to container | ||
COPY package.json package-lock.json tsconfig.json /ff/ | ||
COPY src /ff/src | ||
COPY test /ff/test | ||
|
||
# Install dependencies | ||
RUN npm install | ||
|
||
RUN npm run build | ||
|
||
# Set working directory to /ff/test/e2e/openfunction-plugins | ||
WORKDIR /ff/test/e2e/openfunction-plugins | ||
|
||
RUN npm install | ||
|
||
# Run the ts-node command to execute src/main.ts | ||
CMD ["npm", "run","knative:plugin:run:func"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
setup: | ||
env: kind | ||
file: ../kind.yaml | ||
steps: | ||
- name: install dapr | ||
command: | | ||
dapr -v || (wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash) | ||
dapr init -k --runtime-version 1.11.0 --log-as-json --wait --timeout 600 | ||
wait: | ||
- namespace: dapr-system | ||
resource: pod | ||
label-selector: app.kubernetes.io/name=dapr | ||
for: condition=Ready | ||
|
||
- name: install helm | ||
command: | | ||
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash | ||
- name: install postgres | ||
command: | | ||
helm repo add bitnami https://charts.bitnami.com/bitnami | ||
helm install postgres -n default bitnami/postgresql -f test/e2e/openfunction-plugins/pg-values.yaml | ||
wait: | ||
- namespace: default | ||
resource: pod | ||
for: condition=Ready | ||
|
||
- name: build image | ||
command: | | ||
docker build -t openfunction-skywalking-plugins:latest -f test/e2e/openfunction-plugins/Docker.openfunction.skywalking . | ||
kind load docker-image openfunction-skywalking-plugins:latest | ||
- name: setup manifests | ||
path: manifests.yaml | ||
wait: | ||
- namespace: default | ||
resource: pod | ||
for: condition=Ready | ||
|
||
timeout: 20m | ||
|
||
cleanup: | ||
# always never success failure | ||
on: success | ||
|
||
trigger: | ||
action: "" | ||
|
||
verify: | ||
# verify with retry strategy | ||
retry: | ||
# max retry count | ||
count: 60 | ||
# the interval between two attempts, e.g. 10s, 1m. | ||
interval: 60s | ||
cases: | ||
- query: bash test/e2e/openfunction-plugins/verify.sh 127.0.0.1 80 | yq eval -P | ||
expected: expected.data.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
endpointName: openfunction-plugins-context | ||
type: Entry | ||
component: OpenFunction | ||
layer: FAAS | ||
isError: false | ||
tags: | ||
- key: runtime | ||
value: knative | ||
- key: http.method | ||
value: POST | ||
- key: http.url | ||
value: / | ||
- key: tag1 | ||
value: value1 | ||
- key: tag2 | ||
value: value2 | ||
- key: func | ||
value: openfunction-plugins-context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
async function writeSkywalkingLocalBindings(ctx, data) { | ||
console.log('✅ Function should receive request: %o', data); | ||
await ctx.state | ||
.save(data) | ||
.then(res => { | ||
console.log('✅ Success query'); | ||
console.log(JSON.stringify(res)); | ||
}) | ||
.catch(err => { | ||
console.log('❌ Failure occurred: %o', err); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
writeSkywalkingLocalBindings, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: openfunction-skywalking-plugins-deployment | ||
labels: | ||
app: openfunction-skywalking-plugins | ||
case: knative | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: openfunction-skywalking-plugins | ||
case: knative | ||
template: | ||
metadata: | ||
labels: | ||
app: openfunction-skywalking-plugins | ||
case: knative | ||
annotations: | ||
dapr.io/enabled: "true" | ||
dapr.io/app-id: "openfunction-plugins-app" | ||
dapr.io/app-port: "8080" | ||
dapr.io/log-as-json: "true" | ||
dapr.io/app-protocol: "http" # GRPC is not well supported so far in Dapr Node.js SDK | ||
spec: | ||
containers: | ||
- name: oap | ||
image: apache/skywalking-oap-server:9.2.0 | ||
imagePullPolicy: IfNotPresent | ||
ports: | ||
- containerPort: 11800 | ||
name: grpc | ||
protocol: TCP | ||
- containerPort: 12800 | ||
name: collector | ||
protocol: TCP | ||
- name: my-openfunction-skywalking-plugins | ||
image: openfunction-skywalking-plugins:latest | ||
imagePullPolicy: IfNotPresent | ||
ports: | ||
- containerPort: 8080 | ||
name: function-port | ||
protocol: TCP | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: openfunction-skywalking-plugins-service | ||
spec: | ||
type: NodePort | ||
selector: | ||
app: openfunction-skywalking-plugins | ||
ports: | ||
- protocol: TCP | ||
port: 12345 | ||
targetPort: 8080 | ||
nodePort: 31234 | ||
--- | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: pg | ||
spec: | ||
type: state.postgresql | ||
version: v1 | ||
metadata: | ||
- name: connectionString | ||
value: "host=postgres-postgresql user=postgres password=password port=5432 connect_timeout=10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"dependencies": { | ||
"@openfunction/functions-framework": "file:../../../", | ||
"concurrently": "file:../../../node_modules/concurrently", | ||
"debug": "file:../../../node_modules/debug", | ||
"env-cmd": "file:../../../node_modules/env-cmd", | ||
"wait-on": "file:../../../node_modules/wait-on" | ||
}, | ||
"scripts": { | ||
"knative:plugin:run:func": "env-cmd -e knative functions-framework --signature-type=openfunction --target=writeSkywalkingLocalBindings" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
image: | ||
repository: bitnami/postgresql | ||
tag: 15.4.0-debian-11-r0 | ||
|
||
global: | ||
postgresql: | ||
auth: | ||
postgresPassword: password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { GraphQLClient, gql } from 'graphql-request'; | ||
|
||
import { Plugin } from '../../../../build/src/index.js'; | ||
|
||
export class SkyPathfinder extends Plugin { | ||
span = {}; | ||
|
||
constructor() { | ||
super('sky-pathfinder', 'v1'); | ||
} | ||
|
||
async execPostHook(ctx) { | ||
// Wait a while for trace logging | ||
await new Promise(r => setTimeout(r, 1000)); | ||
|
||
const traceId = ctx.locals.traceId; | ||
const { trace } = await this.queryGraph(traceId); | ||
|
||
this.span = trace.spans[0]; | ||
|
||
// console.log('[ span ] >', ctx.locals.traceId, this.span); | ||
delete this.span.traceId; | ||
console.log(JSON.stringify(this.span)); | ||
} | ||
|
||
async queryGraph(traceId, endpoint = 'http://localhost:12800/graphql') { | ||
const client = new GraphQLClient(endpoint, { | ||
headers: { | ||
// Why need this header? https://github.com/prisma-labs/graphql-request/issues/140 | ||
accept: 'application/json', | ||
}, | ||
}); | ||
|
||
// Spec: https://github.com/apache/skywalking-query-protocol/blob/master/trace.graphqls | ||
const query = gql` | ||
query ($traceId: ID!) { | ||
trace: queryTrace(traceId: $traceId) { | ||
spans { | ||
traceId | ||
endpointName | ||
type | ||
component | ||
layer | ||
isError | ||
tags { | ||
key | ||
value | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const data = await client.request(query, { traceId }); | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Plugin } from '../../../../build/src/index.js'; | ||
|
||
export class TickTock extends Plugin { | ||
value = 0; | ||
|
||
constructor() { | ||
super('ticktock', 'v1'); | ||
} | ||
|
||
async execPreHook(ctx, plugins) { | ||
this.value++; | ||
} | ||
|
||
async execPostHook(ctx, plugins) { | ||
this.value--; | ||
} | ||
} |
Oops, something went wrong.