Skip to content
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

TT-10886 added initial proposal for tests in CI #361

Merged
merged 53 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
b045d87
added initial proposal for tests in ci
sredxny Dec 28, 2023
e0bac3c
set tests as required
sredxny Dec 28, 2023
8b4d516
added lint check
sredxny Dec 28, 2023
122b0dd
fix identation
sredxny Dec 28, 2023
924df63
change checkout action
sredxny Dec 28, 2023
03ae65b
fetch base branch
sredxny Dec 28, 2023
a499a57
added sonarcloud check
sredxny Dec 28, 2023
2acf56d
fix indentation
sredxny Dec 28, 2023
84c2917
use gh action for golint
sredxny Jan 8, 2024
620476e
update arguments for gh action
sredxny Jan 8, 2024
aad7013
remove golint and use gh action
sredxny Jan 8, 2024
620ce9d
remove --out-format and use gh action default
sredxny Jan 8, 2024
d4b7712
et permissions
sredxny Jan 8, 2024
d7f7bb8
remove checkstyle
sredxny Jan 8, 2024
7dd3cfe
setting timeout=600ms
sredxny Jan 8, 2024
f54cca8
disable cache
sredxny Jan 8, 2024
e19cfa2
skip cache
sredxny Jan 8, 2024
6f28adf
fix args
sredxny Jan 8, 2024
68843b5
tryint to override out format
sredxny Jan 8, 2024
d1828a5
Merge branch 'master' into implement-ci-job-for-tests
sredxny Jan 8, 2024
0c9273a
enabling build cache
sredxny Jan 8, 2024
764b4fd
Merge branch 'implement-ci-job-for-tests' of github.com:TykTechnologi…
sredxny Jan 8, 2024
726da73
enabling pkg caching
sredxny Jan 8, 2024
de9533c
Merge branch 'master' into implement-ci-job-for-tests
sredxny Jan 10, 2024
0a49cc6
renamed job, removed step to pull deps
sredxny Jan 11, 2024
90974af
Merge branch 'implement-ci-job-for-tests' of github.com:TykTechnologi…
sredxny Jan 11, 2024
d35c351
updated required tests
sredxny Jan 11, 2024
7b15c13
added support for multiple dbs in ci as well as setup the containers
sredxny Jan 12, 2024
c0ce822
added support for coverage
sredxny Jan 12, 2024
62e3f48
fix path of ci script
sredxny Jan 12, 2024
936e9a4
install goimports
sredxny Jan 12, 2024
683b8b6
set tests as required to pass
sredxny Jan 12, 2024
7ded0f0
fix conflicts
sredxny Jan 12, 2024
af1c24f
update go version
sredxny Jan 12, 2024
214403e
run workflow on pr and push to master and release branches
sredxny Jan 15, 2024
c2ca273
removed unused workflow step
sredxny Jan 15, 2024
04800cc
fetch app code without event ref
sredxny Jan 15, 2024
ba0bfaf
fmt
sredxny Jan 15, 2024
0135a56
change how to checkout code of TIB in golangci-lint
sredxny Jan 15, 2024
36b7b4f
set app version
sredxny Jan 16, 2024
fba6121
added test coverage for mongo dataloader
sredxny Jan 16, 2024
683b5cf
go imports
sredxny Jan 16, 2024
bcff7f3
separated how env vars are read
sredxny Jan 16, 2024
964db33
simplify func to know if mongo
sredxny Jan 16, 2024
4e66231
add test cvg
sredxny Jan 16, 2024
e84f34c
removed unnecesary drop table
sredxny Jan 16, 2024
45e223c
skip when is not mongo tests
sredxny Jan 17, 2024
c8ede29
fix TestCreateDataMongoLoader
sredxny Jan 17, 2024
3b4df25
fix mongo url
sredxny Jan 17, 2024
0060f09
fetch base branch
sredxny Jan 17, 2024
f0a5917
fetch master branch
sredxny Jan 17, 2024
108f4c0
clean code
sredxny Jan 17, 2024
a59e58f
clean code
sredxny Jan 17, 2024
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
140 changes: 140 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
name: Go Test Workflow

on:
push:
branches:
- master
- release-**
pull_request:
branches:
- master
- release-**
env:
TYK_IB_STORAGE_STORAGETYPE: file

permissions:
contents: read

jobs:
golangci-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout Tyk Identity Broker
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Fetch base branch
if: ${{ github.event_name == 'pull_request' }}
run: git fetch origin ${{ github.base_ref }}
- name: golangci-lint
if: ${{ github.event_name == 'pull_request' }}
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --out-format=checkstyle:golanglint.xml --timeout=600s --max-issues-per-linter=0 --max-same-issues=0 --new-from-rev=origin/${{ github.base_ref }}
- uses: actions/upload-artifact@v2
with:
name: golangcilint
retention-days: 1
path: |
golanglint.xml

ci-test:
name: '${{ matrix.databases }}'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
databases:
- mongo-mgo
- mongo-official
- file
redis-version: [5]
mongodb-version: [4.2]
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '^1.21'

- name: Install Dependencies and basic hygiene test
id: hygiene
run: |
go install golang.org/x/tools/cmd/goimports@latest

- name: Start Redis
uses: supercharge/redis-github-action@1.2.0
with:
redis-version: ${{ matrix.redis-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.2.0
with:
mongodb-version: '${{ matrix.mongodb-version }}'

- name: Run tests
run: |
./bin/ci-tests.sh ${{ matrix.databases }}
- uses: actions/upload-artifact@v2
with:
name: coverage
retention-days: 1
path: |
*cov

sonar-cloud-analysis:
alephnull marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
needs: [ci-test, golangci-lint]
steps:
- name: Checkout TIB
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Fetch base branch
if: ${{ github.event_name == 'pull_request' }}
run: git fetch origin ${{ github.base_ref }}
- name: Setup Golang
uses: actions/setup-go@v3
with:
go-version: 1.21
- name: Download coverage artifacts
uses: actions/download-artifact@v2
with:
name: coverage
- name: Download golangcilint artifacts
uses: actions/download-artifact@v2
with:
name: golangcilint
- name: Check reports existence
id: check_files
uses: andstor/file-existence-action@v1
with:
files: '*.cov, golanglint.xml'
- name: Install Dependencies
run: >
go install github.com/wadey/gocovmerge@latest

- name: merge reports
run: |
./bin/merge-cov.sh
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
with:
args: >
-Dsonar.organization=tyktechnologies
-Dsonar.projectKey=TykTechnologies_tyk-identity-broker
-Dsonar.sources=.
-Dsonar.exclusions=ci/**
-Dsonar.coverage.exclusions=**/*_test.go,**/mocks/*.go
-Dsonar.test.inclusions=**/*_test.go
-Dsonar.tests=.
-Dsonar.go.coverage.reportPaths=*.cov
-Dsonar.go.golangci-lint.reportPaths=golanglint.xml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
69 changes: 69 additions & 0 deletions bin/ci-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

set -e

export GO111MODULE=on

# print a command and execute it
show() {
echo "$@" >&2
eval "$@"
}

fatal() {
echo "$@" >&2
exit 1
}

TEST_TIMEOUT=10m
db="file"

if [[ -n $1 ]]; then
db=$1
fi

if [[ $db = "mongo-mgo" ]]; then
export TYK_IB_STORAGE_STORAGETYPE="mongo"
export TYK_IB_STORAGE_MONGOCONF_MONGOURL="mongodb://localhost/tyk_identity_broker"
export TYK_IB_STORAGE_MONGOCONF_DRIVER="mgo"
: # do nothing
elif [[ $db = "mongo-official" ]]; then
export TYK_IB_STORAGE_STORAGETYPE="mongo"
export TYK_IB_STORAGE_MONGOCONF_MONGOURL="mongodb://localhost/tyk_identity_broker"
export TYK_IB_STORAGE_MONGOCONF_DRIVER="mongo-go"
elif [[ $db = "file" ]]; then
export TYK_IB_STORAGE_STORAGETYPE="file"
export TYK_IB_PROFILEDIR="./"
else
fatal "unsupported database: $db"
fi

echo "Running with $TYK_IB_STORAGE_STORAGETYPE database using $TYK_IB_STORAGE_MONGOCONF_DRIVER driver"

show go vet . || fatal "go vet errored"

GO_FILES=$(find * -name '*.go' )

echo "Formatting checks..."

FMT_FILES="$(gofmt -s -l ${GO_FILES})"
if [[ -n ${FMT_FILES} ]]; then
fatal "Run 'gofmt -s -w' on these files:\n$FMT_FILES"
fi

echo "gofmt check is ok!"

IMP_FILES="$(goimports -l ${GO_FILES})"
if [[ -n ${IMP_FILES} ]]; then
fatal "Run 'goimports -w' on these files:\n$IMP_FILES"
fi

echo "goimports check is ok!"

for pkg in $(go list github.com/TykTechnologies/tyk-identity-broker/...);
do
race="-race"
echo "Testing... $pkg"
coveragefile=`echo "$pkg-$db" | awk -F/ '{print $NF}'`
show go test -timeout ${TEST_TIMEOUT} ${race} --coverprofile=${coveragefile}.cov -v ${pkg}
done
26 changes: 26 additions & 0 deletions bin/merge-cov.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -e

export GO111MODULE=on

# print a command and execute it
show() {
echo "$@" >&2
eval "$@"
}

fatal() {
echo "$@" >&2
exit 1
}

for pkg in $(go list github.com/TykTechnologies/tyk-identity-broker/...);
do
coveragefile=`echo "$pkg.cov" | awk -F/ '{print $NF}'`
mgo_cov=`echo "$pkg-mongo-mgo.cov" | awk -F/ '{print $NF}'`
mongo_cov=`echo "$pkg-mongo-official.cov" | awk -F/ '{print $NF}'`
file_cov=`echo "$pkg-file.cov" | awk -F/ '{print $NF}'`
show gocovmerge $mongo_cov $mgo_cov $file_cov > $coveragefile
rm $mongo_cov $mgo_cov $file_cov
done
66 changes: 57 additions & 9 deletions data_loader/data_loader_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,79 @@
//go:build test_mongo
// +build test_mongo

package data_loader_test
package data_loader

import (
"os"
"testing"

"github.com/TykTechnologies/storage/persistent"
"github.com/TykTechnologies/tyk-identity-broker/backends"
"github.com/stretchr/testify/assert"

"github.com/TykTechnologies/tyk-identity-broker/configuration"
"github.com/TykTechnologies/tyk-identity-broker/data_loader"
)

func TestCreateDataMongoLoader(t *testing.T) {
isMongo := isMongoEnv()
if !isMongo {
t.Skip()
}

url, driver := MongoEnvConf()

conf := configuration.Configuration{
Storage: &configuration.Storage{
StorageType: configuration.MONGO,
StorageType: "mongo",
MongoConf: &configuration.MongoConf{
MongoURL: "mongodb://tyk-mongo:27017/tyk_tib",
MongoURL: url,
Driver: driver,
},
},
}

dataLoader, err := data_loader.CreateDataLoader(conf, nil)
dataLoader, err := CreateDataLoader(conf, "")
if err != nil {
t.Fatalf("creating Mongo data loader: %v", err)
}

if _, ok := dataLoader.(*data_loader.MongoLoader); !ok {
if _, ok := dataLoader.(*MongoLoader); !ok {
t.Fatalf("type of data loader is not correct; expected '*data_loader.MongoLoader' but got '%T'", dataLoader)
}
}

func TestFlush(t *testing.T) {

isMongo := isMongoEnv()
if !isMongo {
t.Skip()
}

url, driver := MongoEnvConf()
loader := MongoLoader{}

var err error
loader.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{
ConnectionString: url,
UseSSL: false,
Type: driver,
})

assert.Nil(t, err)

authStore := &backends.InMemoryBackend{}
err = loader.Flush(authStore)
assert.Nil(t, err)
}

func isMongoEnv() bool {
storageType := os.Getenv("TYK_IB_STORAGE_STORAGETYPE")

// Check if MongoDB is the storage type and both URL and driver are set
return storageType == "mongo"
}

func MongoEnvConf() (string, string) {
mongoURL := os.Getenv("TYK_IB_STORAGE_MONGOCONF_MONGOURL")
mongoDriver := os.Getenv("TYK_IB_STORAGE_MONGOCONF_DRIVER")

return mongoURL, mongoDriver

}
17 changes: 12 additions & 5 deletions data_loader/mongo_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func (m *MongoLoader) LoadIntoStore(store tap.AuthRegisterBackend) error {
return nil
}

func handleEmptyProfilesError(err error) error {
if err != nil {
if !utils.IsErrNoRows(err) {
dataLogger.WithError(err).Error("emptying profiles collection")
return err
}
}
return nil
}

// Flush creates a backup of the current loaded config
func (m *MongoLoader) Flush(store tap.AuthRegisterBackend) error {
//read all
Expand All @@ -77,11 +87,8 @@ func (m *MongoLoader) Flush(store tap.AuthRegisterBackend) error {

//empty to store new changes
err := m.store.Delete(context.Background(), tap.Profile{}, nil)
if err != nil {
if !utils.IsErrNoRows(err) {
dataLogger.WithError(err).Error("emptying profiles collection")
return err
}
if handledErr := handleEmptyProfilesError(err); handledErr != nil {
return handledErr
}

for _, p := range updatedSet {
Expand Down
Loading
Loading