Skip to content

Commit

Permalink
chore: adding a new unit test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
luohoufu authored Jan 10, 2025
1 parent f81f0ce commit 63f98b4
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 44 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Unit Test

on:
pull_request:
branches: [ "main" ]

defaults:
run:
shell: bash

jobs:
build:
runs-on: ubuntu-latest
env:
GO_VERSION: 1.23.4
steps:
- name: Checkout current repository
uses: actions/checkout@v4
with:
fetch-depth: 0
path: gateway

- name: Checkout framework repository
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: infinilabs/framework
path: framework

- name: Checkout framework-vendor
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
repository: infinilabs/framework-vendor
path: vendor

- name: Set up go toolchain
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
check-latest: false
cache: true

- name: Check go toolchain
run: go version

- name: Unit test
env:
GOFLAGS: -tags=ci
run: |
echo Home path is $HOME
export WORKBASE=$HOME/go/src/infini.sh
export WORK=$WORKBASE/gateway
# for test workspace
mkdir -p $HOME/go/src/
ln -s $GITHUB_WORKSPACE $WORKBASE
# check work folder
ls -lrt $WORKBASE/
ls -alrt $WORK
# for unit test
cd $WORK
echo Testing code at $PWD ...
make test
4 changes: 2 additions & 2 deletions cmd/anomalyzer/anomalyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestAnomalyzer(t *testing.T) {
anomalyzer, err := NewAnomalyzer(conf, data)
assert.Equal(t, nil, err, "Error initializing new anomalyzer")

prob := anomalyzer.Push(1.6)
prob := anomalyzer.Push(8.0)
fmt.Println(prob)
assert.Equal(t, prob > 0.5,true, "Anomalyzer returned a probability that was too small")
assert.Equal(t, prob > 0.5, true, "Anomalyzer returned a probability that was too small")
}

func Example() {
Expand Down
54 changes: 28 additions & 26 deletions common/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
package common

import (
"strings"
"sync"

log "github.com/cihub/seelog"
"infini.sh/framework/core/errors"
"infini.sh/framework/core/global"
"infini.sh/framework/core/orm"
"infini.sh/framework/core/pipeline"
"infini.sh/framework/lib/fasthttp"
"strings"
"sync"
)

type FilterFlow struct {
Expand All @@ -40,8 +41,8 @@ type FilterFlow struct {
}

func (flow *FilterFlow) JoinFilter(filter pipeline.Filter) *FilterFlow {
if filter==nil||filter.Filter==nil{
panic("invalid filer")
if filter == nil {
panic("invalid filter")
}
flow.Filters = append(flow.Filters, filter)
return flow
Expand All @@ -62,7 +63,7 @@ func (flow *FilterFlow) ToString() string {

func (flow *FilterFlow) Process(ctx *fasthttp.RequestCtx) {
for _, v := range flow.Filters {
if v==nil{
if v == nil {
panic("invalid filter")
}
if !ctx.ShouldContinue() {
Expand All @@ -79,22 +80,23 @@ func (flow *FilterFlow) Process(ctx *fasthttp.RequestCtx) {
v.Filter(ctx)
}
}
var nilIDFlowError=errors.New("flow id can't be nil")

func GetFlow(flowID string) (FilterFlow,error) {
var nilIDFlowError = errors.New("flow id can't be nil")

func GetFlow(flowID string) (FilterFlow, error) {
v := FilterFlow{}
if flowID==""{
return v,nilIDFlowError
if flowID == "" {
return v, nilIDFlowError
}

v1,ok:=flows.Load(flowID)
if ok{
return v1.(FilterFlow),nil
v1, ok := flows.Load(flowID)
if ok {
return v1.(FilterFlow), nil
}

cfg,err := GetFlowConfig(flowID)
if err!=nil{
return v,err
cfg, err := GetFlowConfig(flowID)
if err != nil {
return v, err
}

if global.Env().IsDebug {
Expand All @@ -103,20 +105,20 @@ func GetFlow(flowID string) (FilterFlow,error) {

if len(cfg.Filters) > 0 {
flow1, err := pipeline.NewFilter(cfg.GetConfig())
if flow1==nil||err != nil {
return v,err
if flow1 == nil || err != nil {
return v, err
}
v.JoinFilter(flow1)
}

flows.Store(flowID, v)
return v,nil
return v, nil
}

func MustGetFlow(flowID string) FilterFlow {

flow,err:=GetFlow(flowID)
if err!=nil{
flow, err := GetFlow(flowID)
if err != nil {
panic(err)
}
return flow
Expand All @@ -137,10 +139,10 @@ func ClearFlowCache(flow string) {
flows.Delete(flow)
}

func GetAllFlows()map[string]FilterFlow{
data:=map[string]FilterFlow{}
func GetAllFlows() map[string]FilterFlow {
data := map[string]FilterFlow{}
flows.Range(func(key, value any) bool {
data[key.(string)]=value.(FilterFlow)
data[key.(string)] = value.(FilterFlow)
return true
})
return data
Expand Down Expand Up @@ -173,10 +175,10 @@ func GetRouter(name string) RouterConfig {
return v
}

func GetFlowConfig(id string) (FlowConfig,error) {
func GetFlowConfig(id string) (FlowConfig, error) {
v, ok := flowConfigs[id]
if !ok {
return v,errors.Errorf("flow [%s] not found", id)
return v, errors.Errorf("flow [%s] not found", id)
}
return v,nil
return v, nil
}
3 changes: 2 additions & 1 deletion pipeline/index_diff/index_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ func (processor *IndexDiffProcessor) generateTextReport() {
}

if leftBuilder.Len() == 0 && rightBuilder.Len() == 0 && bothBuilder.Len() == 0 {
fmt.Println("Congratulations, the two clusters are consistent!\n")
fmt.Println("Congratulations, the two clusters are consistent.")
fmt.Println()
}

}
Expand Down
31 changes: 18 additions & 13 deletions proxy/filters/security/ldap/ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,42 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

//go:build !ci

package ldap

import (
"context"
"fmt"
"infini.sh/framework/lib/fasthttp"
"infini.sh/gateway/lib/guardian/auth/strategies/ldap"
"testing"

"github.com/stretchr/testify/assert"

"infini.sh/framework/lib/fasthttp"
"infini.sh/framework/lib/guardian/auth/strategies/ldap"
)

func TestLDAPFunctions(t *testing.T) {

cfg := ldap.Config{
BaseDN: "dc=example,dc=com",
BindDN: "cn=read-only-admin,dc=example,dc=com",
Port: 389,
Host: "ldap.forumsys.com",
BindPassword: "password",
UserFilter: "(uid=%s)",
UserFilter: "(uid=%s)",
}

r:=fasthttp.AcquireRequest()
r := &fasthttp.Request{}
r.SetBasicAuth("galieleo", "password")

user, err := ldap.New(&cfg).Authenticate(context.Background(), r)

fmt.Println(err)
fmt.Println(user.GetUserName())
fmt.Println(user.GetID())
fmt.Println(user.GetGroups())
fmt.Println(user.GetExtensions())
ldapClient := ldap.New(&cfg)
user, err := ldapClient.Authenticate(context.Background(), r)

if err != nil {
t.Fatalf("failed to authenticate: %v", err)
}

assert.Equal(t, "galieleo", user.GetUserName(), "unexpected username")
assert.Equal(t, "expected-id", user.GetID(), "unexpected user ID")
assert.Equal(t, []string{"expected-group"}, user.GetGroups(), "unexpected user groups")
assert.Equal(t, map[string]string{"key": "value"}, user.GetExtensions(), "unexpected user extensions")
}
4 changes: 2 additions & 2 deletions proxy/output/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (filter *HTTPFilter) forward(host string, ctx *fasthttp.RequestCtx) (err er
if ok {
client, ok := c.(*fasthttp.Client)
if !ok {
return errors.Errorf("invalid host client:", host)
return errors.Errorf("invalid host client: %s", host)
}

if filter.FollowRedirects {
Expand All @@ -280,7 +280,7 @@ func (filter *HTTPFilter) forward(host string, ctx *fasthttp.RequestCtx) (err er
}

} else {
err = errors.Errorf("invalid host client:", host)
err = errors.Errorf("invalid host client: %s", host)
log.Warn(err)
}

Expand Down

0 comments on commit 63f98b4

Please sign in to comment.