Skip to content

Commit

Permalink
Create a single backend acceptance test suite. (#1597)
Browse files Browse the repository at this point in the history
* Create a single backend acceptance test suite.

A new integration test suite is added under internal/testing/backend to test
all backends with the same set of tests. All the test scaffolding is seutp to
run all the backends. Only "CertifyBad" tests have been copied in in this
first commit.

Signed-off-by: Jeff Mendoza <[email protected]>

* Fix typo

Signed-off-by: Mihai Maruseac <[email protected]>
Signed-off-by: Mihai Maruseac <[email protected]>

---------

Signed-off-by: Jeff Mendoza <[email protected]>
Signed-off-by: Mihai Maruseac <[email protected]>
Signed-off-by: Mihai Maruseac <[email protected]>
Co-authored-by: Mihai Maruseac <[email protected]>
  • Loading branch information
jeffmendoza and mihaimaruseac authored Dec 22, 2023
1 parent 545e294 commit c5d84b6
Show file tree
Hide file tree
Showing 37 changed files with 1,857 additions and 106 deletions.
33 changes: 8 additions & 25 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,6 @@ jobs:
test-integration:
runs-on: ubuntu-latest
name: CI for integration tests
services:
neo4j:
image: neo4j:latest # TODO(mihaimaruseac): Pin to hash
env:
NEO4J_AUTH: none
ports:
- 7687:7687
postgres:
image: postgres:16.0@sha256:f007ec48ff3ef9b75dc473d915a3ea3713167ba015340316f6bcabfa86a7b4a6
env:
POSTGRES_USER: guac
POSTGRES_PASSWORD: guac
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
arango:
image: arangodb:latest@sha256:085b45e8c56d5d4114e409482694d40fc8d1678c6b5d98d774bab31193034d6a
env:
ARANGO_ROOT_PASSWORD: test123
ports:
- 8529:8529
steps:
- name: Checkout code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # tag=v3
Expand All @@ -72,6 +47,14 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Setup the project
run: go mod download
- name: Run backends
shell: bash
run: |
set -euo pipefail
cd internal/testing/backend
docker-compose up -d
sleep 10
echo "backends started"
- name: Run integration tests
env:
ENT_TEST_DATABASE_URL: 'postgresql://guac:guac@localhost/guac?sslmode=disable'
Expand Down
51 changes: 51 additions & 0 deletions internal/testing/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# GUAC Backend Acceptance Test Suite

## How to run

Run all the backend containers using the compose yaml and then run the tests:

```shell
cd internal/testing/backend
docker-compose up -d
go test -v --tags=integration .
```

Also, these tests run in CI. See the "CI for integration tests" job in
[ci.yaml](/.github/workflows/ci.yaml)

All the files here must use the `//go:build integration` tag, so they are not
run with normal unit tests.

## Writing more tests

* Write normal go test functions. For example
`func TestMytestname(t *testing.T) {`. These will be executed multiple times,
once for each backend.

* Tests should receive a `backends.Backend` object by calling
`setupTest(t)`. All testing is done on the received backend.

* Use `github.com/google/go-cmp/cmp` to compare expected and recieved
values. The `commonOpts` global is used for any `Diff()` calls to that
library. Add any needed options to that global in `setupOpts()`.

* Use `/internal/testing/testdata` for any test node data.

* Put any simple test data in `helpers_test.go` for reuse, such as `curTime`.

## Adding a backend

* Create a new `<name>_test.go` file and implement the `backend` interface.

* In [main_test.go](main_test.go):

* Add a new global string to name the backend.

* Add the backend to the `testBackends` map, with a constructor for the
`backend` interface.

* Add any needed skips to the `skipMatrix` map, if the new backend does not
support all the node types yet.

* Before submitting, update this [docker-compose.yaml](docker-compose.yaml) to start any
external dependencies needed.
66 changes: 66 additions & 0 deletions internal/testing/backend/arango_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration

package backend_test

import (
"context"

"github.com/guacsec/guac/pkg/assembler/backends"
"github.com/guacsec/guac/pkg/assembler/backends/arangodb"
)

type arangoBE struct {
be backends.Backend
args *arangodb.ArangoConfig
}

func newArango() backend {
return &arangoBE{
args: &arangodb.ArangoConfig{
User: "root",
Pass: "test123",
DBAddr: "http://localhost:8529",
},
}
}

func (m *arangoBE) Setup() error {
if err := arangodb.DeleteDatabase(context.Background(), m.args); err != nil {
return err
}
be, err := backends.Get("arango", context.Background(), m.args)
m.be = be
return err
}

func (m *arangoBE) Get() backends.Backend {
return m.be
}

func (m *arangoBE) Clear() error {
if err := arangodb.DeleteDatabase(context.Background(), m.args); err != nil {
return err
}
// For some reason, need to call Get again after delete
be, err := backends.Get("arango", context.Background(), m.args)
m.be = be
return err
}

func (m *arangoBE) Cleanup() {
}
Loading

0 comments on commit c5d84b6

Please sign in to comment.