Skip to content

Commit 43f4520

Browse files
committed
Refactor GetApplicationEnvironments function and add unit tests
1 parent 16f9cbd commit 43f4520

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
1111
github.com/spf13/cobra v1.8.0
1212
github.com/spf13/viper v1.18.2
13+
github.com/stretchr/testify v1.8.4
1314
go.uber.org/zap v1.27.0
1415
gomodules.xyz/jsonpatch/v2 v2.4.0
1516
gopkg.in/natefinch/lumberjack.v2 v2.2.1
@@ -49,6 +50,7 @@ require (
4950
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5051
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
5152
github.com/pkg/errors v0.9.1 // indirect
53+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5254
github.com/sagikazarmark/locafero v0.4.0 // indirect
5355
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
5456
github.com/sourcegraph/conc v0.3.0 // indirect

pkg/resource/control.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type ApplicationEnvResponse struct {
2020

2121
func GetApplicationEnvironments(namespace, application string) (map[string]string, error) {
2222
url := fmt.Sprintf(
23-
"%s/v1/getApplicationEnvironment?application=%s&namespace=%s",
24-
config.ControlPlaneUrl, application, namespace,
23+
"%s/v1/ns/%s/application/%s/environments",
24+
config.ControlPlaneUrl, namespace, application,
2525
)
2626
resp, err := http.Get(url)
2727
if err != nil {

pkg/resource/control_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package resource
2+
3+
import (
4+
"fmt"
5+
"github.com/jd-opensource/joylive-injector/pkg/config"
6+
"github.com/stretchr/testify/assert"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
)
11+
12+
func TestGetApplicationEnvironments(t *testing.T) {
13+
// 模拟 HTTP 服务器
14+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15+
assert.Equal(t, "/v1/ns/test-namespace/application/test-application/environments", r.URL.Path)
16+
w.WriteHeader(http.StatusOK)
17+
w.Write([]byte(`{
18+
"data": {
19+
"ENV_VAR1": "value1",
20+
"ENV_VAR2": "value2"
21+
}
22+
}`))
23+
}))
24+
defer mockServer.Close()
25+
26+
// 替换配置中的 ControlPlaneUrl
27+
originalURL := config.ControlPlaneUrl
28+
config.ControlPlaneUrl = mockServer.URL
29+
defer func() { config.ControlPlaneUrl = originalURL }()
30+
31+
// 调用被测试函数
32+
data, err := GetApplicationEnvironments("test-namespace", "test-application")
33+
34+
// 验证结果
35+
assert.NoError(t, err)
36+
assert.Equal(t, map[string]string{
37+
"ENV_VAR1": "value1",
38+
"ENV_VAR2": "value2",
39+
}, data)
40+
}
41+
42+
func TestGetApplicationEnvironments2(t *testing.T) {
43+
config.ControlPlaneUrl = "http://localhost:8000"
44+
45+
// 调用被测试函数
46+
data, err := GetApplicationEnvironments("default1", "showcase-agent-a1")
47+
48+
// 验证结果
49+
assert.NoError(t, err)
50+
fmt.Print(data)
51+
}

0 commit comments

Comments
 (0)