Skip to content

Commit

Permalink
gorm compilable queries (#2)
Browse files Browse the repository at this point in the history
* update mockery version

* fix typos in Authentication, resource and constructor

* fix route for auth unit tests

* remove error that is always nil

* add TODO to the changes to be done in the session service

* add error management to middlewareJSON

* add CommandInitializer to init configuration keys

* init database configuration keys

* init session configuration keys

* init initialization configuration keys

* init server configuration keys

* init logger configuration keys

* remove unused r.md file in commands

* move time to utils

* create super user when adding the auth controller

* update info controller and routes creation

* change the way badaas server is created

* move test e2e to test_e2e/ and docker to docker/

* do not run auto migration on test e2e execution + automigration refactor

* update docs and developers utils

* remove unnecesary init.sh for cockroach and add health check

* move db connection to orm module and make automigration possible by fx

* info and auth modules now also provide the corresponding services and middlewares

* use gotestsum to run tests + create tests report + tool to install dependencies

* add base models to orm

* add crud services and repositories in orm module

* adapt existing services and controllers to use orm module

* add integration tests

* update documentation

* update changelog

* cli: gorm compilable queries

* fix ci
  • Loading branch information
FrancoLiberali authored Dec 16, 2023
1 parent 502d9db commit 59786d2
Show file tree
Hide file tree
Showing 161 changed files with 7,196 additions and 1,799 deletions.
45 changes: 43 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,53 @@ jobs:
with:
go-version: '^1.18'
cache: true
- name: Install gotestsum
run: go install gotest.tools/gotestsum@latest
- name: Run unit tests
run: go test ./... -coverprofile=coverage_unit.out -v
run: gotestsum --junitfile unit-tests.xml $(go list ./... | grep -v testintegration) -coverpkg=./... -coverprofile=coverage_unit.out
- uses: actions/upload-artifact@v3
with:
name: coverage_unit
path: coverage_unit.out
- name: Test Report
uses: dorny/test-reporter@v1
if: always() # run this step even if previous steps failed
with:
name: Unit Tests Report # Name of the check run which will be created
path: unit-tests.xml # Path to test results
reporter: java-junit # Format of test results

integration-tests:
name: Integration tests
needs: [unit-tests]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v3
with:
go-version: '^1.18'
cache: true
- name: Install gotestsum
run: go install gotest.tools/gotestsum@latest
- name: Start containers
run: docker compose -f "docker/test_db/docker-compose.yml" up -d
- name: Run test
run: gotestsum --junitfile integration-tests.xml ./testintegration -coverpkg=./... -coverprofile=coverage_int.out
- name: Test Report
uses: dorny/test-reporter@v1
if: always() # run this step even if previous steps failed
with:
name: Integration Tests Report # Name of the check run which will be created
path: integration-tests.xml # Path to test results
reporter: java-junit # Format of test results
- uses: actions/upload-artifact@v3
with:
name: coverage_int
path: coverage_int.out
- name: Stop containers
run: docker stop badaas-test-db

e2e-tests:
name: E2E Tests
Expand Down Expand Up @@ -68,4 +109,4 @@ jobs:
- uses: actions/upload-artifact@v3
with:
name: docker-compose-e2e-logs
path: app.log
path: app.log
39 changes: 28 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
# Contribute to the development of badaas

- [Tests](#tests)
- [Unit tests](#unit-tests)
- [Feature tests (of end to end tests)](#feature-tests-of-end-to-end-tests)
- [Logger](#logger)
- [Directory structure](#directory-structure)
- [Git](#git)
- [Branch naming policy](#branch-naming-policy)
- [Default branch](#default-branch)
- [How to release](#how-to-release)
- [Contribute to the development of badaas](#contribute-to-the-development-of-badaas)
- [Tests](#tests)
- [Dependencies](#dependencies)
- [Unit tests](#unit-tests)
- [Integration tests](#integration-tests)
- [Feature tests (or end to end tests)](#feature-tests-or-end-to-end-tests)
- [Logger](#logger)
- [Directory structure](#directory-structure)
- [Git](#git)
- [Branch naming policy](#branch-naming-policy)
- [Default branch](#default-branch)
- [How to release](#how-to-release)

## Tests

### Dependencies

Running tests have some dependencies as: `mockery`, `gotestsum`, etc.. Install them with `make install dependencies`.

### Unit tests

We use the standard test suite in combination with [github.com/stretchr/testify](https://github.com/stretchr/testify) to do our unit testing. Mocks are generated using [mockery](https://github.com/vektra/mockery) a mock generator using this command `make test_generate_mocks`.
Expand All @@ -22,7 +29,15 @@ To run them, please run:
make test_unit
```

### Feature tests (of end to end tests)
### Integration tests

Integration tests have a database and the dependency injection system.

```sh
make test_integration
```

### Feature tests (or end to end tests)

We use docker to run a Badaas instance in combination with one node of CockroachDB.

Expand All @@ -48,10 +63,12 @@ This is the directory structure we use for the project:
- `test_db/` : Contains the Dockerfile to build a development/test version of CockroachDB.
- `test_api/` : Contains files to build a development/test version of the api.
- `test_e2e/`: Contains all the feature and steps for e2e tests.
- `testintegration/`: Contains all the integration tests.
- `logger/` *(Go code)*: Contains the logger creation logic. Please don't call it from your own services and code, use the dependency injection system.
- `orm/` *(Go code)*: Contains the code of the orm used by badaas.
- `persistance/` *(Go code)*:
- `gormdatabase/` *(Go code)*: Contains the logic to create a <https://gorm.io> database. Also contains a go package named `gormzap`: it is a compatibility layer between *gorm.io/gorm* and *github.com/uber-go/zap*.
- `models/` *(Go code)*: Contains the models. (For a structure to me considered a valid model, it has to embed `models.BaseModel` and satisfy the `models.Tabler` interface. This interface returns the name of the sql table.).
- `models/` *(Go code)*: Contains the models (for a structure to me considered a valid model, it has to embed `badaas/orm.UUIDModel` or `badaas/orm.UIntModel`).
- `dto/` *(Go code)*: Contains the Data Transfer Objects. They are used mainly to decode json payloads.
- `pagination/` *(Go code)*: Contains the pagination logic.
- `repository/` *(Go code)*: Contains the repository interfaces and implementations to manage queries to the database.
Expand Down
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
PATHS = $(shell go list ./... | grep -v testintegration)

install_dependencies:
go install gotest.tools/gotestsum@latest
go install github.com/vektra/mockery/[email protected]
go install github.com/ditrit/badaas-cli@latest

lint:
golangci-lint run

test_unit:
go test ./... -v
gotestsum --format pkgname $(PATHS)

test_integration:
docker compose -f "docker/test_db/docker-compose.yml" up -d
./docker/wait_for_db.sh
gotestsum --format testname ./testintegration

test_e2e:
docker compose -f "docker/test_db/docker-compose.yml" -f "docker/test_api/docker-compose.yml" up -d
Expand All @@ -12,5 +24,5 @@ test_e2e:
test_generate_mocks:
mockery --all --keeptree

.PHONY: test_unit test_e2e
.PHONY: test_unit test_integration test_e2e

61 changes: 37 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@

Badaas enables the effortless construction of ***distributed, resilient, highly available and secure applications by design***, while ensuring very simple deployment and management (NoOps).

Badaas provides several key features:

- **Authentication**: Badaas can authenticate users using its internal authentication scheme or externally by using protocols such as OIDC, SAML, Oauth2...
- **Authorization**: On resource access, Badaas will check if the user is authorized using a RBAC model.
- **Distribution**: Badaas is built to run in clusters by default. Communications between nodes are TLS encrypted using [shoset](https://github.com/ditrit/shoset).
- **Persistence**: Applicative objects are persisted as well as user files. Those resources are shared across the clusters to increase resiliency.
- **Querying Resources**: Resources are accessible via a REST API.
- **Posix compliant**: Badaas strives towards being a good unix citizen and respecting commonly accepted norms. (see [Configuration](#configuration))
- **Advanced logs management**: Badaas provides an interface to interact with the logs produced by the clusters. Logs are formatted in json by default.

- [Quickstart](#quickstart)
- [Example](#example)
- [Step-by-step instructions](#step-by-step-instructions)
- [Configuration](#configuration)
- [Contributing](#contributing)
- [License](#license)
> **Warning**
> BaDaaS is still under development. Each of its components can have a different state of evolution that you can consult in [Features and components](#features-and-components)
- [BADAAS: Backend And Distribution As A Service](#badaas-backend-and-distribution-as-a-service)
- [Features and components](#features-and-components)
- [Quickstart](#quickstart)
- [Example](#example)
- [Step-by-step instructions](#step-by-step-instructions)
- [Config badaas functionalities](#config-badaas-functionalities)
- [Add your own functionalities](#add-your-own-functionalities)
- [Run it](#run-it)
- [Provided functionalities](#provided-functionalities)
- [InfoModule](#infomodule)
- [AuthModule](#authmodule)
- [Configuration](#configuration)
- [Contributing](#contributing)
- [License](#license)

## Features and components

Badaas provides several key features, each provided by a component that can be used independently and has a different state of evolution:

- **Authentication**(unstable): Badaas can authenticate users using its internal authentication scheme or externally by using protocols such as OIDC, SAML, Oauth2...
- **Authorization**(wip_unstable): On resource access, Badaas will check if the user is authorized using a RBAC model.
- **Distribution**(todo): Badaas is built to run in clusters by default. Communications between nodes are TLS encrypted using [shoset](https://github.com/ditrit/shoset).
- **Persistence**(wip_unstable): Applicative objects are persisted as well as user files. Those resources are shared across the clusters to increase resiliency. To achieve this, BaDaaS uses the [BaDaaS ORM](https://github.com/ditrit/badaas/orm) component.
- **Querying Resources**(unstable): Resources are accessible via a REST API.
- **Posix compliant**(stable): Badaas strives towards being a good unix citizen and respecting commonly accepted norms. (see [Configuration](#configuration))
- **Advanced logs management**(todo): Badaas provides an interface to interact with the logs produced by the clusters. Logs are formatted in json by default.

## Quickstart

Expand Down Expand Up @@ -54,8 +67,8 @@ You are free to choose which badaas functionalities you wish to use. To add them
```go
func main() {
badaas.BaDaaS.AddModules(
controllers.InfoControllerModule,
controllers.AuthControllerModule,
badaas.InfoModule,
badaas.AuthModule,
).Provide(
NewAPIVersion,
).Start()
Expand Down Expand Up @@ -111,14 +124,14 @@ Once you have defined the functionalities of your project (the `/hello` route in

### Provided functionalities

#### InfoControllerModule
#### InfoModule

`InfoControllerModule` adds the path `/info`, where the api version will be answered. To set the version you want to be responded you must provide a function that returns it:
`InfoModule` adds the path `/info`, where the api version will be answered. To set the version you want to be responded you must provide a function that returns it:

```go
func main() {
badaas.BaDaaS.AddModules(
controllers.InfoControllerModule,
badaas.InfoModule,
).Provide(
NewAPIVersion,
).Start()
Expand All @@ -129,14 +142,14 @@ func NewAPIVersion() *semver.Version {
}
```

#### AuthControllerModule
#### AuthModule

`AuthControllerModule` adds `/login` and `/logout`, which allow us to add authentication to our application in a simple way:
`AuthModule` adds `/login` and `/logout`, which allow us to add authentication to our application in a simple way:

```go
func main() {
badaas.BaDaaS.AddModules(
controllers.AuthControllerModule,
badaas.AuthModule,
).Start()
}
```
Expand Down
2 changes: 0 additions & 2 deletions badaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ditrit/badaas/logger"
"github.com/ditrit/badaas/persistence"
"github.com/ditrit/badaas/router"
"github.com/ditrit/badaas/services"
"github.com/ditrit/verdeter"
)

Expand Down Expand Up @@ -69,7 +68,6 @@ func (badaas BaDaaSInitializer) runHTTPServer(cmd *cobra.Command, args []string)
router.RouterModule,
logger.LoggerModule,
persistence.PersistanceModule,
services.ServicesModule,

// logger for fx
fx.WithLogger(func(logger *zap.Logger) fxevent.Logger {
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a dto that is returned on a successful login.
- Update verdeter to version v0.4.0.
- Transform BadAas into a library.
- Add badaas-orm with the compilable query system.

[unreleased]: https://github.com/ditrit/badaas/blob/main/changelog.md#unreleased
17 changes: 12 additions & 5 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
- [Install with go install](#install-with-go-install)
- [Build from sources](#build-from-sources)
- [Commands](#commands)
- [badaas-cli gen](#badaas-cli-gen)
- [badaas-cli gen docker](#badaas-cli-gen-docker)
- [badaas-cli gen conditions](#badaas-cli-gen-conditions)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -54,20 +55,26 @@ For more information about the functionality provided and how to use each comman
badaas-cli help [command]
```

### badaas-cli gen
### badaas-cli gen docker

gen is the command you can use to generate the files and configurations necessary for your project to use BadAss in a simple way.
gen docker is the command you can use to generate the files and configurations necessary for your project to use badaas in a simple way.

`gen` will generate the docker and configuration files needed to run the application in the `badaas/docker` and `badaas/config` folders respectively.
`gen docker` will generate the docker and configuration files needed to run the application in the `badaas/docker` and `badaas/config` folders respectively.

All these files can be modified in case you need different values than those provided by default. For more information about the configuration head to [configuration docs](../../configuration.md).
All these files can be modified in case you need different values than those provided by default. For more information about the configuration head to [configuration docs](github.com/ditrit/badaas/configuration.md).

A Makefile will be generated for the execution of a badaas server, with the command:

```bash
make badaas_run
```

### badaas-cli gen conditions

gen conditions is the command you can use to generate conditions to query your objects using badaas-orm. For each BaDaaS Model found in the input packages a file containing all possible Conditions on that object will be generated, allowing you to use badaas-orm.

Its use is recommended through `go generate`. To see an example of how to do it click [here](https://github.com/ditrit/badaa-orm-example/blob/main/standalone/conditions/orm.go).

## Contributing

See [this section](./CONTRIBUTING.md).
Expand Down
3 changes: 2 additions & 1 deletion cli/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add generation of docker and configuration files
- Add gen conditions to generate the conditions for the badaas' compilable query system.

[unreleased]: https://github.com/ditrit/badaas-cli/blob/main/changelog.md#unreleased
[unreleased]: https://github.com/ditrit/badaas-orm/cli/blob/main/changelog.md#unreleased
2 changes: 1 addition & 1 deletion cli/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var genCommand = verdeter.BuildVerdeterCommand(verdeter.VerdeterConfig{
const destBadaasDir = "badaas"

func init() {
rootCommand.AddSubCommand(genCommand)
rootCmd.AddSubCommand(genCommand)
}

// copies all docker and configurations related files from the embed file system to the destination folder
Expand Down
Loading

0 comments on commit 59786d2

Please sign in to comment.