Skip to content

Commit dc2870e

Browse files
committed
fix(smartmirror): correct apt smart mirror source configuration
- Add debug log output to facilitate process tracking Bug: https://pms.uniontech.com/bug-view-344087.html
1 parent 3d7da21 commit dc2870e

6 files changed

Lines changed: 115 additions & 4 deletions

File tree

etc/apt/apt.conf.d/99lastore.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ DPkg::Post-Invoke { "/var/lib/lastore/scripts/build_system_info || true "}
55
Acquire::SmartMirrors::Enable true;
66
Acquire::SmartMirrors::Debug false;
77
Acquire::SmartMirrors::GuestURI "/usr/bin/lastore-smartmirror";
8-
Acquire::SmartMirrors::MainSource "https://cdn-community-packages.deepin.com/deepin";
8+
Acquire::SmartMirrors::MainSource "https://community-packages.deepin.com/";
99
Acquire::SmartMirrors::DomainList:: "deepin.com";
1010
Acquire::SmartMirrors::DomainList:: "deepin.org";
1111
Acquire::SmartMirrors::DomainList:: "uniontech.com";

etc/apt/apt.conf.d/99mirrors.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#Don't wirte anything except MirrorSource, otherwise the config may lost
22

3-
Acquire::SmartMirrors::MirrorSource "https://cdn-community-packages.deepin.com/deepin";
3+
Acquire::SmartMirrors::MirrorSource "https://cdn-community-packages.deepin.com/";

src/lastore-smartmirror-daemon/header.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,14 @@ func buildRequest(header map[string]string, method string, url string) *http.Req
9797
}
9898

9999
// handleRequest wait request reply and close connection quickly
100-
func handleRequest(r *http.Request) (string, int) {
100+
func handleRequest(r *http.Request) (resultURL string, statusCode int) {
101101
if r == nil {
102102
return "", -1
103103
}
104+
defer func() {
105+
logger.Debugf("handleRequest: r.Url=%s, return url=%s, statusCode=%v", r.URL, resultURL, statusCode)
106+
}()
107+
104108
resp, err := httpClient.Do(r)
105109
if err != nil {
106110
return "", -2

src/lastore-smartmirror-daemon/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func main() {
6767
logger.Debug("Started service at system bus")
6868

6969
if *runDaemon {
70-
logger.Debug("Run as daemon and not exist")
70+
logger.Debug("Run as daemon and not auto exit")
7171
service.SetAutoQuitHandler(time.Second*5, func() bool {
7272
return false
7373
})

src/lastore-smartmirror-daemon/smartmirror.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (s *SmartMirror) SetEnable(enable bool) *dbus.Error {
114114

115115
// Query the best source
116116
func (s *SmartMirror) Query(original, officialMirror, mirrorHost string) (url string, busErr *dbus.Error) {
117+
logger.Debug("Query", original, officialMirror, mirrorHost)
117118
s.service.DelayAutoQuit()
118119
if !s.Enable {
119120
source := strings.Replace(original, officialMirror, mirrorHost, 1)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
"time"
9+
10+
"github.com/linuxdeepin/go-lib/log"
11+
)
12+
13+
func TestSmartMirror_makeChoice(t *testing.T) {
14+
if os.Getenv("NO_TEST_NETWORK") == "1" {
15+
t.Skip("Skipping test due to NO_TEST_NETWORK=1")
16+
}
17+
logger.SetLogLevel(log.LevelDebug)
18+
// 创建临时测试目录
19+
tempDir := t.TempDir()
20+
defer func() {
21+
for i := 0; i < 5; i++ {
22+
err := os.RemoveAll(tempDir)
23+
if err == nil {
24+
break
25+
}
26+
if i < 4 {
27+
time.Sleep(100 * time.Millisecond)
28+
} else {
29+
t.Logf("warn: Failed to remove temp directory after 5 attempts: %v", err)
30+
}
31+
}
32+
}()
33+
stateDirectory = tempDir
34+
35+
// 准备测试数据
36+
testSources := []map[string]interface{}{
37+
{
38+
"id": "default",
39+
"name": "official",
40+
"url": "https://x-cdn-community-packages.deepin.com/",
41+
"weight": 100,
42+
},
43+
{
44+
"id": "TUNA",
45+
"name": "[CN] Tsinghua University",
46+
"url": "http://mirrors.tuna.tsinghua.edu.cn/deepin/",
47+
"name_locale": map[string]string{"zh_CN": "[CN] 清华大学", "zh_TW": "[CN] 清華大學"},
48+
"weight": 60000,
49+
"country": "CN",
50+
"adjust_delay": 0,
51+
},
52+
}
53+
54+
// 写入 mirrors.json
55+
mirrorsData, _ := json.MarshalIndent(testSources, "", " ")
56+
err := os.WriteFile(filepath.Join(tempDir, "mirrors.json"), mirrorsData, 0644)
57+
if err != nil {
58+
t.Fatalf("Failed to create test mirrors.json: %v", err)
59+
}
60+
61+
// 写入空的配置文件
62+
configData := []byte(`{"enable":true}`)
63+
err = os.WriteFile(filepath.Join(tempDir, "smartmirror_config.json"), configData, 0644)
64+
if err != nil {
65+
t.Fatalf("Failed to create test config: %v", err)
66+
}
67+
68+
// 写入质量数据文件
69+
qualityData := map[string]interface{}{
70+
"https://x-cdn-community-packages.deepin.com/": map[string]int{
71+
"detect_count": 50,
72+
"access_count": 50,
73+
"failed_count": 5,
74+
"average_delay": 150,
75+
},
76+
"http://mirrors.tuna.tsinghua.edu.cn/deepin/": map[string]int{
77+
"detect_count": 80,
78+
"access_count": 80,
79+
"failed_count": 2,
80+
"average_delay": 110,
81+
},
82+
}
83+
qualityJSON, _ := json.MarshalIndent(qualityData, "", " ")
84+
err = os.WriteFile(filepath.Join(tempDir, "smartmirror_quality.json"), qualityJSON, 0644)
85+
if err != nil {
86+
t.Fatalf("Failed to create test quality.json: %v", err)
87+
}
88+
89+
// 创建 SmartMirror 实例
90+
s := newSmartMirror(nil)
91+
originalURL := "https://community-packages.deepin.com/beige/pool/main/f/fish/fish_3.7.1-1deepin1_amd64.deb"
92+
officialMirror := "https://community-packages.deepin.com/"
93+
94+
t.Logf("Loaded sources count: %d", len(s.sources))
95+
for i, source := range s.sources {
96+
t.Logf("Source[%d]: %s - %s", i, source.Name, source.Url)
97+
}
98+
99+
result := s.makeChoice(originalURL, officialMirror)
100+
t.Logf("Result URL: %s", result)
101+
102+
// 验证结果不为空
103+
if result == "" {
104+
t.Error("makeChoice returned empty result")
105+
}
106+
}

0 commit comments

Comments
 (0)