Skip to content

Draft: Autogenerate mocks #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions .github/workflows/autogen.yaml

This file was deleted.

55 changes: 53 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Test
on:
workflow_run:
workflows: ["Autogenerate"]
types:
- completed
push:
paths-ignore:
- "**.md"
Expand All @@ -13,12 +17,59 @@ on:
workflow_dispatch:

jobs:
autogen:
name: Generate mocks
runs-on: ubuntu-latest

# Permissions are needed to push the changes back to the repository
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.17
- name: Install Mockery
uses: jaxxstorm/[email protected]
with: # Grab a specific tag
repo: vektra/mockery
tag: v3.5.3
- name: Generate mocks
run: mockery --config .mockery.yaml
- name: Cache generated mocks
uses: actions/cache@v4
with:
path: |
ocpp1.6_test/mocks/
ocpp2.0.1_test/mocks/
ocppj/mocks/
ws/mocks/
key: ${{ runner.os }}-mocks-${{ github.sha }}
unit:
runs-on: ubuntu-latest
name: Unit tests
needs: autogen
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Restore generated mocks
id: cache-mocks
uses: actions/cache/restore@v4
with:
path: |
ocpp1.6_test/mocks/
ocpp2.0.1_test/mocks/
ocppj/mocks/
ws/mocks/
key: ${{ runner.os }}-mocks-${{ github.sha }}
- name: Check cache hit
if: steps.cache-mocks.outputs.cache-hit != 'true'
run: exit 1
- name: Run tests
run: |
docker compose -f docker-compose.test.yaml up unit_test --abort-on-container-exit
Expand Down Expand Up @@ -54,7 +105,7 @@ jobs:
publish_coverage:
runs-on: ubuntu-latest
# Unit and integration tests must be run before publishing coverage
needs: [ unit, integration ]
needs: [unit, integration]
name: Publish coverage
steps:
- name: Checkout code
Expand All @@ -68,4 +119,4 @@ jobs:
with:
path-to-profile: coverage.out
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
certs/

central_system_sim
charge_point_sim
charge_point_sim

# Generatd files
ocpp1.6_test/mocks/
ocpp2.0.1_test/mocks/
ocppj/mocks/
ws/mocks/
175 changes: 73 additions & 102 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,139 +1,110 @@
with-expecter: true
dir: "{{.InterfaceDir}}/mocks"
outpkg: mocks
filename: "mock_{{.InterfaceName}}.go"
mockname: "Mock{{.InterfaceName}}"
all: true

dir: '{{.InterfaceDir}}/mocks'
structname: Mock{{.InterfaceName}}
pkgname: mocks
template: testify
template-data:
unroll-variadic: true
packages:
github.com/lorenzodonini/ocpp-go/ocpp1.6/core:
github.com/lorenzodonini/ocpp-go/ocpp1.6:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_core_{{.InterfaceName|snakecase}}.go"
mockname: "MockCore{{.InterfaceName}}"
dir: ocpp1.6_test/mocks
interfaces:
CentralSystemHandler:
ChargePointHandler:

CentralSystem: {}
ChargePoint: {}
ChargePointConnection: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/certificates:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_certificates_{{.InterfaceName|snakecase}}.go"
mockname: "MockCertificates{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockCertificates{{.InterfaceName}}
filename: mock_certificates_{{.InterfaceName|snakecase}}.go
interfaces:
ChargePointHandler:

github.com/lorenzodonini/ocpp-go/ocpp1.6/logging:
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/core:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_logging_{{.InterfaceName|snakecase}}.go"
mockname: "MockLog{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockCore{{.InterfaceName}}
filename: mock_core_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:

CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/extendedtriggermessage:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_extended_trigger_message_{{.InterfaceName|snakecase}}.go"
mockname: "MockExtendedTriggerMessage{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockExtendedTriggerMessage{{.InterfaceName}}
filename: mock_extended_trigger_message_{{.InterfaceName|snakecase}}.go
interfaces:
ChargePointHandler:

ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/firmware:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_firmware_{{.InterfaceName|snakecase}}.go"
mockname: "MockFirmware{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockFirmware{{.InterfaceName}}
filename: mock_firmware_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:

CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/localauth:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_local_auth_list_{{.InterfaceName|snakecase}}.go"
mockname: "MockLocalAuthList{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockLocalAuthList{{.InterfaceName}}
filename: mock_local_auth_list_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/logging:
config:
dir: ocpp1.6_test/mocks
structname: MockLog{{.InterfaceName}}
filename: mock_logging_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:


CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_remote_trigger_{{.InterfaceName|snakecase}}.go"
mockname: "MockRemoteTrigger{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockRemoteTrigger{{.InterfaceName}}
filename: mock_remote_trigger_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:

CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/reservation:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_reservation_{{.InterfaceName|snakecase}}.go"
mockname: "MockReservation{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockReservation{{.InterfaceName}}
filename: mock_reservation_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:

CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/securefirmware:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_secure_firmware_{{.InterfaceName}}.go"
mockname: "MockSecureFirmware{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockSecureFirmware{{.InterfaceName}}
filename: mock_secure_firmware_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:


CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/security:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_security_{{.InterfaceName|snakecase}}.go"
mockname: "MockSecurity{{.InterfaceName}}"

dir: ocpp1.6_test/mocks
structname: MockSecurity{{.InterfaceName}}
filename: mock_security_{{.InterfaceName|snakecase}}.go
interfaces:
CentralSystemHandler:
ChargePointHandler:


CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_smart_charging_{{.InterfaceName|snakecase}}.go"
mockname: "MockSmartCharging{{.InterfaceName}}"

interfaces:
CentralSystemHandler:
ChargePointHandler:


github.com/lorenzodonini/ocpp-go/ocpp1.6:
config:
dir: "ocpp1.6_test/mocks"
filename: "mock_ocpp16.go"

dir: ocpp1.6_test/mocks
structname: MockSmartCharging{{.InterfaceName}}
filename: mock_smart_charging_{{.InterfaceName|snakecase}}.go
interfaces:
ChargePointConnection:
ChargePoint:
CentralSystem:

CentralSystemHandler: {}
ChargePointHandler: {}
github.com/lorenzodonini/ocpp-go/ocppj:
interfaces:
ServerQueueMap:
RequestQueue:

RequestQueue: {}
ServerQueueMap: {}
github.com/lorenzodonini/ocpp-go/ws:
interfaces:
WsClient:
WsServer:
Channel:
Channel: {}
Client: {}
Server: {}
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
test:
docker compose -f docker-compose.test.yaml up toxiproxy integration_test --abort-on-container-exit

mocks:
docker run --rm -v $(PWD):/src -w /src vektra/mockery:v3.5.3 --config .mockery.yaml

.PHONY: test mocks
16 changes: 13 additions & 3 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ make test
For generating mocks, the `mockery` tool is used. For `mockery` installation, follow the instructions on
the [official docs](https://vektra.github.io/mockery/latest/).

**Note**: Mock generation is also run as part of the CI pipeline, so you can check if the mocks are up-to-date by
running the tests. However, It is recommended to run the tests locally before pushing your changes.
**Note**: Mocks are not checked in and are instead generated on-the-fly as part of the CI pipeline. If your local tests fail, it may be due to your mocks being out-of-date. This can be fixed by re-generating the mocks. It is recommended to run the tests locally before pushing your changes.

When adding new interfaces and needing to generate mocks, you should:

Expand All @@ -26,6 +25,17 @@ When adding new interfaces and needing to generate mocks, you should:

2. Run the following command:
```sh
mockery
mockery --config .mockery.yaml
```
Alternatively, you may generate the mocks via make target:
```sh
make mocks
```

## Toxiproxy

For testing the resilience of the library against network issues, some tests use Toxiproxy.
If you wish to run the network tests locally (without docker compose) you need to:
- Install [toxiproxy](https://github.com/Shopify/toxiproxy) for your platform
- Start a local toxiproxy - `toxiproxy-server -port 8474 -host localhost`
- Run the tests - `go fmt ./... && go vet ./... && go test -v -count=1 -failfast ./...`
Loading