Skip to content

Commit

Permalink
gh workflow with linter and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Aug 9, 2024
1 parent f1c66af commit 550ad36
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 57 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Main
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.22.x
cache: true
- name: Run golangci-lint
# run: |
# curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0
# $(go env GOPATH)/bin/golangci-lint run --timeout=5m -c .golangci.yml
uses: golangci/golangci-lint-action@v3
### golangci-lint will take much time if loading multiple linters in .golangci.yml
with:
version: latest
args: --timeout 5m --verbose
skip-cache: false
skip-pkg-cache: false
skip-build-cache: false
only-new-issues: true

test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Go environment
uses: actions/setup-go@v4
with:
go-version: "1.22.x"
- name: Tidy go module
run: |
go mod tidy
if [[ $(git status --porcelain) ]]; then
git diff
echo
echo "go mod tidy made these changes, please run 'go mod tidy' and include those changes in a commit"
exit 1
fi
- name: Run Go test
run: go test ./...
- name: Run Go test -race
if: github.ref == 'refs/heads/stage' || startsWith(github.ref, 'refs/heads/release')
run: go test -vet=off -timeout=15m -race ./... # note that -race can easily make the crypto stuff 10x slower

docker-release:
runs-on: ubuntu-latest
needs: [test, lint]
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/stage' || startsWith(github.ref, 'refs/heads/release') || startsWith(github.ref, 'refs/heads/aragon')
steps:
- name: Check out the repo
uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get short commit sha and branch name
id: vars
run: |
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "branch_name=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-' )" >> $GITHUB_OUTPUT
- name: Push to Docker Hub and ghcr.io
uses: docker/build-push-action@v3
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
tags: |
vocdoni/${{ github.event.repository.name }}:latest, vocdoni/${{ github.event.repository.name }}:${{ steps.vars.outputs.branch_name }},
ghcr.io/vocdoni/${{ github.event.repository.name }}:latest, ghcr.io/vocdoni/${{ github.event.repository.name }}:${{ steps.vars.outputs.branch_name }}
cache-from: type=gha
cache-to: type=gha,mode=max
6 changes: 5 additions & 1 deletion db/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func (ms *MongoStorage) collectionNames(ctx context.Context, database string) ([
if err != nil {
return nil, err
}
defer collectionsCursor.Close(ctx)
defer func() {
if err := collectionsCursor.Close(ctx); err != nil {
log.Warnw("failed to close collections cursor", "error", err)
}
}()
collections := []bson.D{}
if err := collectionsCursor.All(ctx, &collections); err != nil {
return nil, err
Expand Down
46 changes: 29 additions & 17 deletions db/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
)

func TestOrganization(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// test not found organization
address := "childOrgToGet"
Expand Down Expand Up @@ -40,7 +44,11 @@ func TestOrganization(t *testing.T) {
}

func TestSetOrganization(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new organization
address := "orgToSet"
Expand Down Expand Up @@ -73,27 +81,29 @@ func TestSetOrganization(t *testing.T) {
}), qt.IsNotNil)
// try to create a new organization with a not found creator
newOrgName := "New Organization 2"
newOrgCreator := "[email protected]"
newOrgCreatorPassword := "password"
c.Assert(db.SetOrganization(&Organization{
Address: newOrgAddress,
Name: newOrgName,
Creator: newOrgCreator,
Creator: testUserEmail,
}), qt.IsNotNil)
// register the creator and retry to create the organization
c.Assert(db.SetUser(&User{
Email: newOrgCreator,
Password: newOrgCreatorPassword,
Email: testUserEmail,
Password: testUserPass,
}), qt.IsNil)
c.Assert(db.SetOrganization(&Organization{
Address: newOrgAddress,
Name: newOrgName,
Creator: newOrgCreator,
Creator: testUserEmail,
}), qt.IsNil)
}

func TestDeleteOrganization(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new organization and delete it
address := "orgToDelete"
Expand All @@ -116,31 +126,33 @@ func TestDeleteOrganization(t *testing.T) {
}

func TestReplaceCreatorEmail(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new organization with a creator
address := "orgToReplaceCreator"
name := "Organization to replace creator"
creator := "[email protected]"
creatorPassword := "password"
c.Assert(db.SetUser(&User{
Email: creator,
Password: creatorPassword,
Email: testUserPass,
Password: testUserPass,
}), qt.IsNil)
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: name,
Creator: creator,
Creator: testUserEmail,
}), qt.IsNil)
org, _, err := db.Organization(address, false)
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
c.Assert(org.Address, qt.Equals, address)
c.Assert(org.Name, qt.Equals, name)
c.Assert(org.Creator, qt.Equals, creator)
c.Assert(org.Creator, qt.Equals, testUserEmail)
// replace the creator email
newCreator := "[email protected]"
c.Assert(db.ReplaceCreatorEmail(creator, newCreator), qt.IsNil)
c.Assert(db.ReplaceCreatorEmail(testUserEmail, newCreator), qt.IsNil)
org, _, err = db.Organization(address, false)
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
Expand Down
99 changes: 60 additions & 39 deletions db/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,99 +6,116 @@ import (
qt "github.com/frankban/quicktest"
)

const (
testUserEmail = "[email protected]"
testUserPass = "testPassword"
testUserFullName = "User Name"
)

func TestUserByEmail(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// test not found user
email := "[email protected]"
user, err := db.UserByEmail(email)
user, err := db.UserByEmail(testUserEmail)
c.Assert(user, qt.IsNil)
c.Assert(err, qt.Equals, ErrNotFound)
// create a new user with the email
password := "password"
fullName := "User Name"
c.Assert(db.SetUser(&User{
Email: email,
Password: password,
FullName: fullName,
Email: testUserEmail,
Password: testUserPass,
FullName: testUserFullName,
}), qt.IsNil)
// test found user
user, err = db.UserByEmail(email)
user, err = db.UserByEmail(testUserEmail)
c.Assert(err, qt.IsNil)
c.Assert(user, qt.Not(qt.IsNil))
c.Assert(user.Email, qt.Equals, email)
c.Assert(user.Password, qt.Equals, password)
c.Assert(user.FullName, qt.Equals, fullName)
c.Assert(user.Email, qt.Equals, testUserEmail)
c.Assert(user.Password, qt.Equals, testUserPass)
c.Assert(user.FullName, qt.Equals, testUserFullName)
}

func TestUser(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// test not found user
id := uint64(100)
user, err := db.User(id)
c.Assert(user, qt.IsNil)
c.Assert(err, qt.Equals, ErrNotFound)
// create a new user with the ID
email := "[email protected]"
password := "password"
fullName := "User Name"
c.Assert(db.SetUser(&User{
Email: email,
Password: password,
FullName: fullName,
Email: testUserEmail,
Password: testUserPass,
FullName: testUserFullName,
}), qt.IsNil)
// get the user ID
user, err = db.UserByEmail(email)
user, err = db.UserByEmail(testUserEmail)
c.Assert(err, qt.IsNil)
c.Assert(user, qt.Not(qt.IsNil))
// test found user by ID
user, err = db.User(user.ID)
c.Assert(err, qt.IsNil)
c.Assert(user, qt.Not(qt.IsNil))
c.Assert(user.Email, qt.Equals, email)
c.Assert(user.Password, qt.Equals, password)
c.Assert(user.FullName, qt.Equals, fullName)
c.Assert(user.Email, qt.Equals, testUserEmail)
c.Assert(user.Password, qt.Equals, testUserPass)
c.Assert(user.FullName, qt.Equals, testUserFullName)
}

func TestSetUser(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// trying to create a new user with invalid email
user := &User{
Email: "invalid-email",
Password: "password",
FullName: "User Name",
Password: testUserPass,
FullName: testUserFullName,
}
c.Assert(db.SetUser(user), qt.IsNotNil)
// trying to update a non existing user
user.ID = 100
c.Assert(db.SetUser(user), qt.Equals, ErrInvalidData)
// unset the ID to create a new user
user.ID = 0
user.Email = "[email protected]"
user.Email = testUserEmail
// create a new user
c.Assert(db.SetUser(user), qt.IsNil)
// update the user
user.FullName = "New User Name"
newFullName := "New User Name"
user.FullName = newFullName
c.Assert(db.SetUser(user), qt.IsNil)
// get the user
user, err := db.UserByEmail(user.Email)
c.Assert(err, qt.IsNil)
c.Assert(user, qt.Not(qt.IsNil))
c.Assert(user.Email, qt.Equals, user.Email)
c.Assert(user.Password, qt.Equals, user.Password)
c.Assert(user.FullName, qt.Equals, user.FullName)
c.Assert(user.Email, qt.Equals, testUserEmail)
c.Assert(user.Password, qt.Equals, testUserPass)
c.Assert(user.FullName, qt.Equals, newFullName)
}

func TestDelUser(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new user
user := &User{
Email: "[email protected]",
Password: "password",
FullName: "User Name",
Email: testUserEmail,
Password: testUserPass,
FullName: testUserFullName,
}
c.Assert(db.SetUser(user), qt.IsNil)
// get the user
Expand All @@ -109,7 +126,7 @@ func TestDelUser(t *testing.T) {
user.Email = ""
c.Assert(db.DelUser(user), qt.IsNil)
// restore the email and try to get the user
user.Email = "[email protected]"
user.Email = testUserEmail
_, err = db.UserByEmail(user.Email)
c.Assert(err, qt.Equals, ErrNotFound)
// insert the user again with the same email but no ID
Expand All @@ -123,12 +140,16 @@ func TestDelUser(t *testing.T) {
}

func TestIsMemberOf(t *testing.T) {
defer db.Reset()
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new user with some organizations
user := &User{
Email: "[email protected]",
Password: "password",
Email: testUserEmail,
Password: testUserPass,
Organizations: []OrganizationMember{
{Address: "adminOrg", Role: AdminRole},
{Address: "managerOrg", Role: ManagerRole},
Expand Down

0 comments on commit 550ad36

Please sign in to comment.