Skip to content

Commit b62994e

Browse files
authored
test: ci unit test (#4)
* test: ci unit test * test: add env flag
1 parent 9557b34 commit b62994e

File tree

5 files changed

+140
-15
lines changed

5 files changed

+140
-15
lines changed

.github/workflows/unit_test.yml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Unit Test
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
7+
defaults:
8+
run:
9+
shell: bash
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
env:
15+
GO_VERSION: 1.23.4
16+
steps:
17+
- name: Checkout current repository
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
path: agent
22+
23+
- name: Checkout framework repository
24+
uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0
27+
repository: infinilabs/framework
28+
path: framework
29+
30+
- name: Checkout framework-vendor
31+
uses: actions/checkout@v4
32+
with:
33+
ref: main
34+
fetch-depth: 0
35+
repository: infinilabs/framework-vendor
36+
path: vendor
37+
38+
- name: Set up go toolchain
39+
uses: actions/setup-go@v5
40+
with:
41+
go-version: ${{ env.GO_VERSION }}
42+
check-latest: false
43+
cache: true
44+
45+
- name: Check go toolchain
46+
run: go version
47+
48+
- name: Unit test
49+
env:
50+
GOFLAGS: -tags=ci
51+
run: |
52+
echo Home path is $HOME
53+
export WORKBASE=$HOME/go/src/infini.sh
54+
export WORK=$WORKBASE/agent
55+
56+
# for test workspace
57+
mkdir -p $HOME/go/src/
58+
ln -s $GITHUB_WORKSPACE $WORKBASE
59+
60+
# check work folder
61+
ls -lrt $WORKBASE/
62+
ls -alrt $WORK
63+
64+
# for unit test
65+
cd $WORK
66+
echo Testing code at $PWD ...
67+
make test

plugin/api/discover.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package api
66

77
import (
88
"fmt"
9+
"net/http"
10+
911
log "github.com/cihub/seelog"
1012
"infini.sh/agent/lib/process"
1113
httprouter "infini.sh/framework/core/api/router"
@@ -14,10 +16,9 @@ import (
1416
"infini.sh/framework/core/env"
1517
"infini.sh/framework/core/global"
1618
"infini.sh/framework/core/util"
17-
"net/http"
1819
)
1920

20-
//local exists nodes, find new nodes in runtime
21+
// local exists nodes, find new nodes in runtime
2122
func (handler *AgentAPI) getESNodes(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
2223
var configs []elastic.ElasticsearchConfig
2324
appCfg, err := getAppConfig()
@@ -50,11 +51,11 @@ func getAppConfig() (*config.Config, error) {
5051
configDir := global.Env().GetConfigDir()
5152
parentCfg, err := config.LoadFile(configFile)
5253
if err != nil {
53-
return nil, fmt.Errorf("failed to load config file: ", err, ", path: ", configFile)
54+
return nil, fmt.Errorf("failed to load config file: %v, path: %s", err, configFile)
5455
}
5556
childCfg, err := config.LoadPath(configDir)
5657
if err != nil {
57-
return nil, fmt.Errorf("failed to load config dir: ", err, ", path: ", configDir)
58+
return nil, fmt.Errorf("failed to load config dir: %v, path: %s", err, configDir)
5859
}
5960
err = parentCfg.Merge(childCfg)
6061
return parentCfg, nil
@@ -68,7 +69,7 @@ func (handler *AgentAPI) getESNodeInfo(w http.ResponseWriter, req *http.Request,
6869
return
6970
}
7071

71-
if global.Env().IsDebug{
72+
if global.Env().IsDebug {
7273
log.Debug("esConfig: ", util.MustToJSON(esConfig))
7374
}
7475

plugin/elastic/esinfo.go

+59-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package elastic
22

33
import (
44
"bufio"
5-
"github.com/shirou/gopsutil/v3/process"
6-
"infini.sh/framework/core/util"
5+
"net"
76
"regexp"
87
"runtime"
8+
"strconv"
99
"strings"
10+
11+
log "github.com/cihub/seelog"
12+
"github.com/shirou/gopsutil/v3/process"
13+
"infini.sh/framework/core/util"
1014
)
1115

1216
type PathPort struct {
@@ -71,7 +75,8 @@ func GetNodeInfoFromProcess() ([]*PathPort, error) {
7175
return pathPorts, nil
7276
}
7377

74-
/**
78+
/*
79+
*
7580
从进程信息里,解析es配置文件路径
7681
通过getProcessInfo()获取进程信息
7782
*/
@@ -154,6 +159,56 @@ func parseESPort(infos []string) []int {
154159
return getPortByPid(pid)
155160
}
156161

162+
func getPortByPid(pid string) []int {
163+
pidInt, err := strconv.Atoi(pid)
164+
if err != nil {
165+
return []int{}
166+
}
167+
p, err := process.NewProcess(int32(pidInt)) // Create a new process instance by PID
168+
if err != nil {
169+
return []int{}
170+
}
171+
172+
// Get all network connections for this process (both listening and connected)
173+
conns, err := p.Connections()
174+
if err != nil {
175+
return []int{}
176+
}
177+
178+
listeningPorts := make([]int, 0) // Initialize a slice to hold the listening ports
179+
for _, conn := range conns { // Iterate through all connection stats
180+
// Check if the connection is in "LISTEN" status
181+
if conn.Status == "LISTEN" {
182+
// Get the local IP and port
183+
localAddr := conn.Laddr.IP
184+
localPort := conn.Laddr.Port
185+
186+
if localAddr == "0.0.0.0" {
187+
// If listening on 0.0.0.0, it's listening on all interfaces, so include port
188+
listeningPorts = append(listeningPorts, int(localPort))
189+
} else {
190+
// If listening on a specific IP, we need to check if it's a local IP of this machine
191+
addrs, err := net.InterfaceAddrs()
192+
if err != nil {
193+
log.Error("Error getting network interface addresses: %v", err)
194+
continue // If an error occur get next connection info
195+
}
196+
197+
for _, addr := range addrs {
198+
ipnet, ok := addr.(*net.IPNet)
199+
// Check if this is a valid non-loopback local IP
200+
if ok && !ipnet.IP.IsLoopback() && ipnet.IP.Equal(net.ParseIP(localAddr)) {
201+
listeningPorts = append(listeningPorts, int(localPort))
202+
break // If local IP matches connection IP , add to the listening ports
203+
}
204+
}
205+
}
206+
207+
}
208+
}
209+
return listeningPorts
210+
}
211+
157212
func parseESConfigPath(infos []string) string {
158213
for _, str := range infos {
159214
if strings.HasPrefix(str, "-Des.path.conf") {
@@ -177,7 +232,7 @@ func RemoveCommentInFile(content string) string {
177232
return builder.String()
178233
}
179234

180-
//nodeInfo : 通过GET /_nodes/_local 获得的信息
235+
// nodeInfo : 通过GET /_nodes/_local 获得的信息
181236
func ParseNodeInfo(nodeInfo string) map[string]string {
182237

183238
result := make(map[string]string)

plugin/elastic/esinfo_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
)
88

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

1413
func readFile(fname string) {

plugin/elastic/metric/node_stats/node_stats_test.go

+7-4
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)