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

test: ci unit test #4

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
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: agent

- 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/agent

# 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
11 changes: 6 additions & 5 deletions plugin/api/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package api

import (
"fmt"
"net/http"

log "github.com/cihub/seelog"
"infini.sh/agent/lib/process"
httprouter "infini.sh/framework/core/api/router"
Expand All @@ -14,10 +16,9 @@ import (
"infini.sh/framework/core/env"
"infini.sh/framework/core/global"
"infini.sh/framework/core/util"
"net/http"
)

//local exists nodes, find new nodes in runtime
// local exists nodes, find new nodes in runtime
func (handler *AgentAPI) getESNodes(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
var configs []elastic.ElasticsearchConfig
appCfg, err := getAppConfig()
Expand Down Expand Up @@ -50,11 +51,11 @@ func getAppConfig() (*config.Config, error) {
configDir := global.Env().GetConfigDir()
parentCfg, err := config.LoadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to load config file: ", err, ", path: ", configFile)
return nil, fmt.Errorf("failed to load config file: %v, path: %s", err, configFile)
}
childCfg, err := config.LoadPath(configDir)
if err != nil {
return nil, fmt.Errorf("failed to load config dir: ", err, ", path: ", configDir)
return nil, fmt.Errorf("failed to load config dir: %v, path: %s", err, configDir)
}
err = parentCfg.Merge(childCfg)
return parentCfg, nil
Expand All @@ -68,7 +69,7 @@ func (handler *AgentAPI) getESNodeInfo(w http.ResponseWriter, req *http.Request,
return
}

if global.Env().IsDebug{
if global.Env().IsDebug {
log.Debug("esConfig: ", util.MustToJSON(esConfig))
}

Expand Down
63 changes: 59 additions & 4 deletions plugin/elastic/esinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package elastic

import (
"bufio"
"github.com/shirou/gopsutil/v3/process"
"infini.sh/framework/core/util"
"net"
"regexp"
"runtime"
"strconv"
"strings"

log "github.com/cihub/seelog"
"github.com/shirou/gopsutil/v3/process"
"infini.sh/framework/core/util"
)

type PathPort struct {
Expand Down Expand Up @@ -71,7 +75,8 @@ func GetNodeInfoFromProcess() ([]*PathPort, error) {
return pathPorts, nil
}

/**
/*
*
从进程信息里,解析es配置文件路径
通过getProcessInfo()获取进程信息
*/
Expand Down Expand Up @@ -154,6 +159,56 @@ func parseESPort(infos []string) []int {
return getPortByPid(pid)
}

func getPortByPid(pid string) []int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems not related to test, if it is a feature or a fix, you should raise another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add the missing function to pass test. need check where use the esinfo

pidInt, err := strconv.Atoi(pid)
if err != nil {
return []int{}
}
p, err := process.NewProcess(int32(pidInt)) // Create a new process instance by PID
if err != nil {
return []int{}
}

// Get all network connections for this process (both listening and connected)
conns, err := p.Connections()
if err != nil {
return []int{}
}

listeningPorts := make([]int, 0) // Initialize a slice to hold the listening ports
for _, conn := range conns { // Iterate through all connection stats
// Check if the connection is in "LISTEN" status
if conn.Status == "LISTEN" {
// Get the local IP and port
localAddr := conn.Laddr.IP
localPort := conn.Laddr.Port

if localAddr == "0.0.0.0" {
// If listening on 0.0.0.0, it's listening on all interfaces, so include port
listeningPorts = append(listeningPorts, int(localPort))
} else {
// If listening on a specific IP, we need to check if it's a local IP of this machine
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Error("Error getting network interface addresses: %v", err)
continue // If an error occur get next connection info
}

for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
// Check if this is a valid non-loopback local IP
if ok && !ipnet.IP.IsLoopback() && ipnet.IP.Equal(net.ParseIP(localAddr)) {
listeningPorts = append(listeningPorts, int(localPort))
break // If local IP matches connection IP , add to the listening ports
}
}
}

}
}
return listeningPorts
}

func parseESConfigPath(infos []string) string {
for _, str := range infos {
if strings.HasPrefix(str, "-Des.path.conf") {
Expand All @@ -177,7 +232,7 @@ func RemoveCommentInFile(content string) string {
return builder.String()
}

//nodeInfo : 通过GET /_nodes/_local 获得的信息
// nodeInfo : 通过GET /_nodes/_local 获得的信息
func ParseNodeInfo(nodeInfo string) map[string]string {

result := make(map[string]string)
Expand Down
3 changes: 1 addition & 2 deletions plugin/elastic/esinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
)

func TestESInfo(t *testing.T) {
//fmt.Println(parseClusterUUID("/Users/chengkai/Downloads"))
readFile("/Users/chengkai/Downloads/local-es-7.15.2_server1.json")
t.Skip()
}

func readFile(fname string) {
Expand Down
11 changes: 7 additions & 4 deletions plugin/elastic/metric/node_stats/node_stats_test.go

Large diffs are not rendered by default.

Loading