diff --git a/etc/apt/apt.conf.d/99lastore.conf b/etc/apt/apt.conf.d/99lastore.conf index a9ae48728..114f9c740 100644 --- a/etc/apt/apt.conf.d/99lastore.conf +++ b/etc/apt/apt.conf.d/99lastore.conf @@ -5,7 +5,7 @@ DPkg::Post-Invoke { "/var/lib/lastore/scripts/build_system_info || true "} Acquire::SmartMirrors::Enable true; Acquire::SmartMirrors::Debug false; Acquire::SmartMirrors::GuestURI "/usr/bin/lastore-smartmirror"; -Acquire::SmartMirrors::MainSource "https://cdn-community-packages.deepin.com/deepin"; +Acquire::SmartMirrors::MainSource "https://community-packages.deepin.com/"; Acquire::SmartMirrors::DomainList:: "deepin.com"; Acquire::SmartMirrors::DomainList:: "deepin.org"; Acquire::SmartMirrors::DomainList:: "uniontech.com"; diff --git a/etc/apt/apt.conf.d/99mirrors.conf b/etc/apt/apt.conf.d/99mirrors.conf index cfb3a360d..f88e78d16 100644 --- a/etc/apt/apt.conf.d/99mirrors.conf +++ b/etc/apt/apt.conf.d/99mirrors.conf @@ -1,3 +1,3 @@ #Don't wirte anything except MirrorSource, otherwise the config may lost -Acquire::SmartMirrors::MirrorSource "https://cdn-community-packages.deepin.com/deepin"; +Acquire::SmartMirrors::MirrorSource "https://cdn-community-packages.deepin.com/"; diff --git a/src/lastore-smartmirror-daemon/header.go b/src/lastore-smartmirror-daemon/header.go index 8dfa23a43..39d43122d 100644 --- a/src/lastore-smartmirror-daemon/header.go +++ b/src/lastore-smartmirror-daemon/header.go @@ -97,10 +97,14 @@ func buildRequest(header map[string]string, method string, url string) *http.Req } // handleRequest wait request reply and close connection quickly -func handleRequest(r *http.Request) (string, int) { +func handleRequest(r *http.Request) (resultURL string, statusCode int) { if r == nil { return "", -1 } + defer func() { + logger.Debugf("handleRequest: r.Url=%s, return url=%s, statusCode=%v", r.URL, resultURL, statusCode) + }() + resp, err := httpClient.Do(r) if err != nil { return "", -2 diff --git a/src/lastore-smartmirror-daemon/main.go b/src/lastore-smartmirror-daemon/main.go index 38921d60d..05b396f8c 100644 --- a/src/lastore-smartmirror-daemon/main.go +++ b/src/lastore-smartmirror-daemon/main.go @@ -67,7 +67,7 @@ func main() { logger.Debug("Started service at system bus") if *runDaemon { - logger.Debug("Run as daemon and not exist") + logger.Debug("Run as daemon and not auto exit") service.SetAutoQuitHandler(time.Second*5, func() bool { return false }) diff --git a/src/lastore-smartmirror-daemon/smartmirror.go b/src/lastore-smartmirror-daemon/smartmirror.go index 224525c1f..3ed9bfb59 100644 --- a/src/lastore-smartmirror-daemon/smartmirror.go +++ b/src/lastore-smartmirror-daemon/smartmirror.go @@ -114,6 +114,7 @@ func (s *SmartMirror) SetEnable(enable bool) *dbus.Error { // Query the best source func (s *SmartMirror) Query(original, officialMirror, mirrorHost string) (url string, busErr *dbus.Error) { + logger.Debug("Query", original, officialMirror, mirrorHost) s.service.DelayAutoQuit() if !s.Enable { source := strings.Replace(original, officialMirror, mirrorHost, 1) diff --git a/src/lastore-smartmirror-daemon/smartmirror_test.go b/src/lastore-smartmirror-daemon/smartmirror_test.go new file mode 100644 index 000000000..7b4104777 --- /dev/null +++ b/src/lastore-smartmirror-daemon/smartmirror_test.go @@ -0,0 +1,107 @@ +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + "time" + + "github.com/linuxdeepin/go-lib/log" +) + +func TestSmartMirror_makeChoice(t *testing.T) { + if os.Getenv("NO_TEST_NETWORK") == "1" { + t.Skip("Skipping test due to NO_TEST_NETWORK=1") + } + logger.SetLogLevel(log.LevelDebug) + // Create temporary test directory + tempDir := t.TempDir() + defer func() { + for i := 0; i < 5; i++ { + err := os.RemoveAll(tempDir) + if err == nil { + break + } + if i < 4 { + time.Sleep(100 * time.Millisecond) + } else { + t.Logf("warn: Failed to remove temp directory after 5 attempts: %v", err) + } + } + }() + stateDirectory = tempDir + + // Prepare test data + testSources := []map[string]interface{}{ + { + "id": "default", + "name": "official", + // NOTE: Intentionally using an incorrect CDN address here + "url": "https://x-cdn-community-packages.deepin.com/", + "weight": 100, + }, + { + "id": "TUNA", + "name": "[CN] Tsinghua University", + "url": "http://mirrors.tuna.tsinghua.edu.cn/deepin/", + "name_locale": map[string]string{"zh_CN": "[CN] 清华大学", "zh_TW": "[CN] 清華大學"}, + "weight": 60000, + "country": "CN", + "adjust_delay": 0, + }, + } + + // Write mirrors.json + mirrorsData, _ := json.MarshalIndent(testSources, "", " ") + err := os.WriteFile(filepath.Join(tempDir, "mirrors.json"), mirrorsData, 0644) + if err != nil { + t.Fatalf("Failed to create test mirrors.json: %v", err) + } + + // Write empty config file + configData := []byte(`{"enable":true}`) + err = os.WriteFile(filepath.Join(tempDir, "smartmirror_config.json"), configData, 0644) + if err != nil { + t.Fatalf("Failed to create test config: %v", err) + } + + // Write quality data file + qualityData := map[string]interface{}{ + "https://x-cdn-community-packages.deepin.com/": map[string]int{ + "detect_count": 50, + "access_count": 50, + "failed_count": 5, + "average_delay": 150, + }, + "http://mirrors.tuna.tsinghua.edu.cn/deepin/": map[string]int{ + "detect_count": 80, + "access_count": 80, + "failed_count": 2, + "average_delay": 110, + }, + } + qualityJSON, _ := json.MarshalIndent(qualityData, "", " ") + err = os.WriteFile(filepath.Join(tempDir, "smartmirror_quality.json"), qualityJSON, 0644) + if err != nil { + t.Fatalf("Failed to create test quality.json: %v", err) + } + + // Create SmartMirror instance + s := newSmartMirror(nil) + originalURL := "https://community-packages.deepin.com/beige/pool/main/f/fish/fish_3.7.1-1deepin1_amd64.deb" + officialMirror := "https://community-packages.deepin.com/" + + t.Logf("Loaded sources count: %d", len(s.sources)) + for i, source := range s.sources { + t.Logf("Source[%d]: %s - %s", i, source.Name, source.Url) + } + + result := s.makeChoice(originalURL, officialMirror) + t.Logf("Result URL: %s", result) + + // Verify result is not empty + if result == "" { + t.Error("makeChoice returned empty result") + } +}