diff --git a/.github/workflows/unit_test.yml b/.github/workflows/unit_test.yml new file mode 100644 index 00000000..7bea004b --- /dev/null +++ b/.github/workflows/unit_test.yml @@ -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 \ No newline at end of file diff --git a/cmd/anomalyzer/anomalyze_test.go b/cmd/anomalyzer/anomalyze_test.go index b7a417d0..72cb46bd 100644 --- a/cmd/anomalyzer/anomalyze_test.go +++ b/cmd/anomalyzer/anomalyze_test.go @@ -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() { diff --git a/common/flow.go b/common/flow.go index 9c89ea17..d869c74f 100644 --- a/common/flow.go +++ b/common/flow.go @@ -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 { @@ -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 @@ -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() { @@ -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 { @@ -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 @@ -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 @@ -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 } diff --git a/pipeline/index_diff/index_diff.go b/pipeline/index_diff/index_diff.go index fa9b7f89..ad63335f 100644 --- a/pipeline/index_diff/index_diff.go +++ b/pipeline/index_diff/index_diff.go @@ -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() } } diff --git a/proxy/filters/security/ldap/ldap_test.go b/proxy/filters/security/ldap/ldap_test.go index 5bb83ad8..3fa878e7 100644 --- a/proxy/filters/security/ldap/ldap_test.go +++ b/proxy/filters/security/ldap/ldap_test.go @@ -21,37 +21,42 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +//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") } diff --git a/proxy/output/http/http.go b/proxy/output/http/http.go index 979ad2a3..ad4df13a 100644 --- a/proxy/output/http/http.go +++ b/proxy/output/http/http.go @@ -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 { @@ -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) }