Skip to content

Commit 2e00871

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 2e00871

6 files changed

Lines changed: 116 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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
// Create temporary test directory
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+
// Prepare test data
36+
testSources := []map[string]interface{}{
37+
{
38+
"id": "default",
39+
"name": "official",
40+
// NOTE: Intentionally using an incorrect CDN address here
41+
"url": "https://x-cdn-community-packages.deepin.com/",
42+
"weight": 100,
43+
},
44+
{
45+
"id": "TUNA",
46+
"name": "[CN] Tsinghua University",
47+
"url": "http://mirrors.tuna.tsinghua.edu.cn/deepin/",
48+
"name_locale": map[string]string{"zh_CN": "[CN] 清华大学", "zh_TW": "[CN] 清華大學"},
49+
"weight": 60000,
50+
"country": "CN",
51+
"adjust_delay": 0,
52+
},
53+
}
54+
55+
// Write mirrors.json
56+
mirrorsData, _ := json.MarshalIndent(testSources, "", " ")
57+
err := os.WriteFile(filepath.Join(tempDir, "mirrors.json"), mirrorsData, 0644)
58+
if err != nil {
59+
t.Fatalf("Failed to create test mirrors.json: %v", err)
60+
}
61+
62+
// Write empty config file
63+
configData := []byte(`{"enable":true}`)
64+
err = os.WriteFile(filepath.Join(tempDir, "smartmirror_config.json"), configData, 0644)
65+
if err != nil {
66+
t.Fatalf("Failed to create test config: %v", err)
67+
}
68+
69+
// Write quality data file
70+
qualityData := map[string]interface{}{
71+
"https://x-cdn-community-packages.deepin.com/": map[string]int{
72+
"detect_count": 50,
73+
"access_count": 50,
74+
"failed_count": 5,
75+
"average_delay": 150,
76+
},
77+
"http://mirrors.tuna.tsinghua.edu.cn/deepin/": map[string]int{
78+
"detect_count": 80,
79+
"access_count": 80,
80+
"failed_count": 2,
81+
"average_delay": 110,
82+
},
83+
}
84+
qualityJSON, _ := json.MarshalIndent(qualityData, "", " ")
85+
err = os.WriteFile(filepath.Join(tempDir, "smartmirror_quality.json"), qualityJSON, 0644)
86+
if err != nil {
87+
t.Fatalf("Failed to create test quality.json: %v", err)
88+
}
89+
90+
// Create SmartMirror instance
91+
s := newSmartMirror(nil)
92+
originalURL := "https://community-packages.deepin.com/beige/pool/main/f/fish/fish_3.7.1-1deepin1_amd64.deb"
93+
officialMirror := "https://community-packages.deepin.com/"
94+
95+
t.Logf("Loaded sources count: %d", len(s.sources))
96+
for i, source := range s.sources {
97+
t.Logf("Source[%d]: %s - %s", i, source.Name, source.Url)
98+
}
99+
100+
result := s.makeChoice(originalURL, officialMirror)
101+
t.Logf("Result URL: %s", result)
102+
103+
// Verify result is not empty
104+
if result == "" {
105+
t.Error("makeChoice returned empty result")
106+
}
107+
}

0 commit comments

Comments
 (0)