Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/apt/apt.conf.d/99lastore.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion etc/apt/apt.conf.d/99mirrors.conf
Original file line number Diff line number Diff line change
@@ -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/";
6 changes: 5 additions & 1 deletion src/lastore-smartmirror-daemon/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lastore-smartmirror-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
1 change: 1 addition & 0 deletions src/lastore-smartmirror-daemon/smartmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
107 changes: 107 additions & 0 deletions src/lastore-smartmirror-daemon/smartmirror_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading