Skip to content

Commit a790a48

Browse files
chore: refresh examples + docs + add security scanning (#71)
* chore: refresh examples + docs + add security scanning #71
1 parent b55b179 commit a790a48

File tree

11 files changed

+190
-96
lines changed

11 files changed

+190
-96
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: ci-aqua-security-trivy-tests
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
types:
8+
- opened
9+
- reopened
10+
- synchronize
11+
- ready_for_review
12+
branches:
13+
- master
14+
schedule:
15+
- cron: "0 * * * *"
16+
jobs:
17+
build:
18+
name: trivy-tests
19+
runs-on: ubuntu-20.04
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v3
23+
- name: Run Trivy vulnerability scanner
24+
uses: aquasecurity/trivy-action@master
25+
with:
26+
scan-type: 'fs'
27+
scan-ref: '.'
28+
format: 'sarif'
29+
output: 'trivy-results.sarif'
30+
- name: Upload Trivy scan results to GitHub Security tab
31+
uses: github/codeql-action/upload-sarif@v2
32+
with:
33+
sarif_file: 'trivy-results.sarif'
+33-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
name: ci-dgraph-js-http-tests
3-
43
on:
54
push:
65
branches:
@@ -13,27 +12,49 @@ on:
1312
- ready_for_review
1413
branches:
1514
- master
16-
1715
jobs:
1816
build:
19-
2017
runs-on: ubuntu-20.04
21-
2218
strategy:
19+
fail-fast: false
2320
matrix:
24-
node-version: [14.x, 16.x, 18.x, 19.x]
21+
node-version: [16.x, 18.x, 19.x, 20.x]
2522
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
26-
2723
steps:
28-
- uses: actions/checkout@v3
29-
- name: Use Node.js ${{ matrix.node-version }}
24+
- name: Checkout dgraph-js-http repo
25+
uses: actions/checkout@v3
26+
with:
27+
path: dgraph-js-http
28+
repository: dgraph-io/dgraph-js-http
29+
ref: ${{ github.ref }}
30+
- name: Checkout dgraph repo
31+
uses: actions/checkout@v3
32+
with:
33+
path: dgraph
34+
repository: dgraph-io/dgraph
35+
ref: main
36+
- name: Get Go Version
37+
run: |
38+
#!/bin/bash
39+
cd dgraph
40+
GOVERSION=$({ [ -f .go-version ] && cat .go-version; })
41+
echo "GOVERSION=$GOVERSION" >> $GITHUB_ENV
42+
- name: Set up Go
43+
uses: actions/setup-go@v3
44+
with:
45+
go-version: ${{ env.GOVERSION }}
46+
- name: Build dgraph binary
47+
run: cd dgraph && make docker-image # also builds dgraph binary
48+
- name: Move dgraph binary to gopath
49+
run: cd dgraph && mv dgraph/dgraph ~/go/bin/dgraph
50+
- name: Setup node.js ${{ matrix.node-version }}
3051
uses: actions/setup-node@v3
3152
with:
3253
node-version: ${{ matrix.node-version }}
3354
cache: 'npm'
34-
- name: Install Dependencies
35-
run: bash ./scripts/install_dgraph.sh
36-
- name: run tests
55+
cache-dependency-path: dgraph-js-http/package-lock.json
56+
- name: Run dgraph-js-http tests
57+
working-directory: dgraph-js-http
3758
run: |
3859
npm ci --legacy-peer-deps
39-
bash ./scripts/build.sh
60+
bash ./scripts/run-tests.sh

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ coverage/
3737
Thumbs.db
3838

3939
# Dgraph
40-
4140
data/**
41+
dgraph-local-data/
42+
43+
# npm test cruft
44+
t/

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ and understand how to run and work with Dgraph.
1414

1515
[docs.dgraph.io]: https://docs.dgraph.io
1616

17-
**Use [Discuss Issues](https://discuss.dgraph.io/c/issues/35) for reporting issues about this repository.**
18-
1917
## Table of contents
2018

2119
- [Install](#install)
@@ -47,7 +45,7 @@ yarn add dgraph-js-http
4745
or npm:
4846

4947
```sh
50-
npm install dgraph-js-http --save
48+
npm install dgraph-js-http
5149
```
5250

5351
You will also need a Promise polyfill for
@@ -62,9 +60,9 @@ use a different version of this client.
6260

6361
| Dgraph version | dgraph-js-http version |
6462
| :------------: | :--------------------: |
65-
| >= 21.03.0 | >= _21.3.0_ |
66-
| >= 20.03.0 | >= _20.3.0_ |
67-
| >= 1.1 | >= _1.1.0_ |
63+
| 21.3.X | *21.3.0* |
64+
| 22.0.X | *21.3.0* |
65+
| 23.0.X | *23.0.0* |
6866

6967
## Quickstart
7068

@@ -418,8 +416,8 @@ npm run build
418416

419417
### Running tests
420418

421-
Make sure you have a Dgraph server running on localhost before you run this task.
419+
The script `run-tests.sh` spins up a local cluster and runs the npm tests.
422420

423421
```sh
424-
npm test
422+
bash scripts/run-tests.sh
425423
```

examples/simple/README.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,46 @@ client for Dgraph.
77

88
## Running
99

10-
### Start Dgraph server
10+
### Start dgraph alpha
1111

12-
You will need to install [Dgraph v1.0.6 or above][releases] and run it.
12+
You will need to install [Dgraph v21.3.2 or above][releases] and run it.
1313

1414
[releases]: https://github.com/dgraph-io/dgraph/releases
1515

1616
You can run the commands below to start a clean Dgraph server every time, for
1717
testing and exploration.
1818

19-
First, create two separate directories for `dgraph zero` and `dgraph server`.
19+
First, create two separate directories for `dgraph zero` and `dgraph alpha`.
2020

2121
```sh
22-
mkdir -p dgraphdata/zero dgraphdata/data
22+
mkdir -p local-dgraph-data/zero local-dgraph-data/data
2323
```
2424

2525
Then start `dgraph zero`:
2626

2727
```sh
28-
cd dgraphdata/zero
28+
cd local-dgraph-data/zero
2929
rm -r zw; dgraph zero
3030
```
3131

32-
Finally, start the `dgraph server`:
32+
Finally, start the `dgraph alpha`:
3333

3434
```sh
35-
cd dgraphdata/data
36-
rm -r p w; dgraph server --lru_mb=1024 --zero localhost:5080
35+
cd local-dgraph-data/data
36+
rm -r p w; dgraph alpha --zero localhost:5080 -o 100
3737
```
3838

39+
Notice that in the command above, we shifted the ports by 100 from the default ports of 7080 for
40+
internal traffic, 8080 for http, and 9080 for GRPC, which means the alpha server is binding to
41+
the port 7180 for internal traffic, 8180 for http, and 9180 for GRPC.
42+
3943
For more configuration options, and other details, refer to
4044
[docs.dgraph.io](https://docs.dgraph.io)
4145

4246
## Install dependencies
4347

4448
```sh
45-
yarn install
49+
npm install
4650
```
4751

4852
## Run the sample code

examples/simple/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple",
33
"dependencies": {
4-
"dgraph-js-http": "^0.1.0"
4+
"dgraph-js-http": "^21.3.1"
55
}
66
}

scripts/build.sh

-20
This file was deleted.

scripts/functions.sh

-41
This file was deleted.

scripts/install_dgraph.sh

-3
This file was deleted.

scripts/run-tests.sh

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
sleepTime=5
4+
5+
function wait-for-healthy() {
6+
printf 'wait-for-healthy: waiting for %s to return 200 OK\n' "$1"
7+
tries=0
8+
until curl -sL -w "%{http_code}\\n" "$1" -o /dev/null | grep -q 200; do
9+
tries=$tries+1
10+
if [[ $tries -gt 300 ]]; then
11+
printf "wait-for-healthy: Took longer than 1 minute to be healthy.\n"
12+
printf "wait-for-healthy: Waiting stopped.\n"
13+
return 1
14+
fi
15+
sleep 0.2
16+
done
17+
printf "wait-for-healthy: done.\n"
18+
}
19+
20+
function errorCheck {
21+
EXIT_CODE=$1
22+
ERROR_MESSAGE=$2
23+
24+
if [[ EXIT_CODE -ne 0 ]]; then
25+
echo $ERROR_MESSAGE
26+
stopCluster
27+
exit $EXIT_CODE
28+
fi
29+
return 0
30+
}
31+
32+
function stopCluster {
33+
echo "shutting down dgraph alpha and zero..."
34+
kill -9 $(pgrep -f "dgraph zero") > /dev/null # kill dgraph zero
35+
kill -9 $(pgrep -f "dgraph alpha") > /dev/null # kill dgraph alpha
36+
37+
if pgrep -x dgraph > /dev/null
38+
then
39+
echo "sleeping for 5 seconds so dgraph can shutdown"
40+
sleep 5
41+
fi
42+
43+
echo "cluster teardown complete"
44+
return 0
45+
}
46+
47+
function startAlpha {
48+
echo -e "starting dgraph alpha..."
49+
head -c 1024 /dev/random > $SRCDIR/dgraph-local-data/acl-secret.txt
50+
dgraph alpha -p $SRCDIR/dgraph-local-data/p \
51+
-w $SRCDIR/dgraph-local-data/w \
52+
--bindall \
53+
--my localhost:7080 \
54+
--acl "access-ttl=1h; refresh-ttl=1d; secret-file=$SRCDIR/dgraph-local-data/acl-secret.txt" \
55+
> $SRCDIR/dgraph-local-data/alpha.log 2>&1 &
56+
57+
# wait for alpha to be healthy
58+
ALPHA_HTTP_ADDR="localhost:8080"
59+
wait-for-healthy $ALPHA_HTTP_ADDR/health
60+
errorCheck $? "dgraph alpha could not come up"
61+
sleep $sleepTime
62+
return 0
63+
}
64+
65+
function startZero {
66+
echo -e "starting dgraph zero..."
67+
dgraph zero --my localhost:5080 --bindall \
68+
-w $SRCDIR/dgraph-local-data/wz > $SRCDIR/dgraph-local-data/zero.log 2>&1 &
69+
70+
# wait for zero to be healthy
71+
ZERO_HTTP_ADDR="localhost:6080"
72+
wait-for-healthy $ZERO_HTTP_ADDR/health
73+
errorCheck $? "dgraph zero could not come up"
74+
sleep $sleepTime
75+
}
76+
77+
function init {
78+
echo -e "initializing..."
79+
rm -rf $SRCDIR/dgraph-local-data
80+
mkdir $SRCDIR/dgraph-local-data
81+
}
82+
83+
# find parent directory of test script
84+
readonly _SRCDIR=$(readlink -f ${BASH_SOURCE[0]%/*})
85+
SRCDIR=$(dirname $_SRCDIR)
86+
87+
init
88+
startZero
89+
startAlpha
90+
sleep 10 # need time to create Groot user
91+
92+
npm run build
93+
94+
npm test
95+
errorCheck $? "dgraph-js-http client tests FAILED"
96+
97+
stopCluster
98+
rm -rf $SRCDIR/local-dgraph-data
99+
exit 0

tests/integration/versionDetect.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("clientStub version detection", () => {
88
legacyApi: USE_LEGACY_API,
99
});
1010
return expect(stub.detectApiVersion()).resolves.toMatch(
11-
/^v[0-9]+(.[0-9]+){2}.*/,
11+
/^v[0-9]+(.[0-9]+){2}.*|^[a-z0-9]*$/,
1212
);
1313
});
1414
});

0 commit comments

Comments
 (0)