Skip to content

Commit

Permalink
🧪 test(e2e): finish the plugins test
Browse files Browse the repository at this point in the history
Signed-off-by: yi-ge-dian <1085266008@qq.com>
  • Loading branch information
yi-ge-dian committed Aug 22, 2023
1 parent e1e5c85 commit 8e32771
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
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/**
35 changes: 14 additions & 21 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ on:
- "test/**"
- "package.json"
- "package-lock.json"
# pull_request:
# branches: [ master ]
# paths:
# - '.github/workflows/**'
# - 'docs/**'
# - 'src/**'
# - 'test/**'
# - 'package.json'
# - 'package-lock.json'

jobs:
e2e_test:
Expand All @@ -28,22 +19,24 @@ jobs:
strategy:
matrix:
case:
# - name: knative http e2e test
# e2e: "test/e2e/knative-http/e2e.yaml"
# - name: knative cloudevent e2e test
# e2e: "test/e2e/knative-cloudevent/e2e.yaml"
# - name: knative event e2e test
# e2e: "test/e2e/knative-event/e2e.yaml"
# - name: knative openfunction bindings e2e test
# e2e: "test/e2e/knative-openfunction-bindings/e2e.yaml"
# - name: knative openfunction pubsub e2e test
# e2e: "test/e2e/knative-openfunction-pubsub/e2e.yaml"
- name: knative http e2e test
e2e: "test/e2e/knative-http/e2e.yaml"
- name: knative cloudevent e2e test
e2e: "test/e2e/knative-cloudevent/e2e.yaml"
- name: knative event e2e test
e2e: "test/e2e/knative-event/e2e.yaml"
- name: knative openfunction bindings e2e test
e2e: "test/e2e/knative-openfunction-bindings/e2e.yaml"
- name: knative openfunction pubsub e2e test
e2e: "test/e2e/knative-openfunction-pubsub/e2e.yaml"
# - name: async openfunction bindings e2e test
# e2e: "test/e2e/async-openfunction-bindings/e2e.yaml"
# - name: async openfunction pubsub e2e test
# e2e: "test/e2e/async-openfunction-pubsub/e2e.yaml"
- name: knative openfunction state e2e test
e2e: "test/e2e/knative-openfunction-state/e2e.yaml"
# - name: knative openfunction state e2e test
# e2e: "test/e2e/knative-openfunction-state/e2e.yaml"
- name: openfunction plugins e2e test
e2e: "test/e2e/openfunction-plugins/e2e.yaml"
steps:
- uses: actions/checkout@v2

Expand Down
33 changes: 33 additions & 0 deletions test/e2e/openfunction-plugins/.env-cmdrc.js
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 test/e2e/openfunction-plugins/Docker.openfunction.skywalking
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"]
58 changes: 58 additions & 0 deletions test/e2e/openfunction-plugins/e2e.yaml
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
18 changes: 18 additions & 0 deletions test/e2e/openfunction-plugins/expected.data.yaml
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
16 changes: 16 additions & 0 deletions test/e2e/openfunction-plugins/function.js
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,
};
68 changes: 68 additions & 0 deletions test/e2e/openfunction-plugins/manifests.yaml
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"
12 changes: 12 additions & 0 deletions test/e2e/openfunction-plugins/package.json
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"
}
}
8 changes: 8 additions & 0 deletions test/e2e/openfunction-plugins/pg-values.yaml
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
57 changes: 57 additions & 0 deletions test/e2e/openfunction-plugins/plugins/skywalking.mjs
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;
}
}
17 changes: 17 additions & 0 deletions test/e2e/openfunction-plugins/plugins/ticktock.mjs
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--;
}
}
Loading

0 comments on commit 8e32771

Please sign in to comment.