2bec257289
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。 - Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件) - 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、 库名 xiujie_admin → iotlcm、哨兵名占位 - 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异, 不破坏「校验 MSISDN 格式」等测试断言) - 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像 digest 记录 - Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23), 移除写死的 git 凭据 - README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 已知:3 个测试包在源仓同一提交上同样失败(client/upstream、 service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
228 lines
6.7 KiB
Go
228 lines
6.7 KiB
Go
package inbound
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
|
serviceinbound "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/inbound"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
)
|
|
|
|
func TestResolveLocalOssPath(t *testing.T) {
|
|
uploadRoot := t.TempDir()
|
|
expectedPath := filepath.Join(uploadRoot, "000000", "user", "file", "20260721", "source.xlsx")
|
|
|
|
tests := []struct {
|
|
name string
|
|
tenantID string
|
|
storedPath string
|
|
wantPath string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "relative Admin path",
|
|
tenantID: "000000",
|
|
storedPath: "resource/upload/000000/user/file/20260721/source.xlsx",
|
|
wantPath: expectedPath,
|
|
},
|
|
{
|
|
name: "absolute Admin path",
|
|
tenantID: "000000",
|
|
storedPath: "/app/upload/000000/user/file/20260721/source.xlsx",
|
|
wantPath: expectedPath,
|
|
},
|
|
{
|
|
name: "default tenant",
|
|
tenantID: "",
|
|
storedPath: "resource/upload/default/user/file/20260721/source.xlsx",
|
|
wantPath: filepath.Join(uploadRoot, "default", "user", "file", "20260721", "source.xlsx"),
|
|
},
|
|
{
|
|
name: "tenant is missing",
|
|
tenantID: "000000",
|
|
storedPath: "resource/upload/000001/user/file/20260721/source.xlsx",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "path traversal is rejected",
|
|
tenantID: "000000",
|
|
storedPath: "resource/upload/000000/../secret.xlsx",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotPath, err := resolveLocalOssPath(uploadRoot, tt.tenantID, tt.storedPath)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("resolveLocalOssPath() error = %v", err)
|
|
}
|
|
if gotPath != tt.wantPath {
|
|
t.Fatalf("resolveLocalOssPath() = %q, want %q", gotPath, tt.wantPath)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveLocalOssPathReturnsReadableFile(t *testing.T) {
|
|
uploadRoot := t.TempDir()
|
|
storedPath := "resource/upload/000000/user/file/20260721/source.xlsx"
|
|
localPath, err := resolveLocalOssPath(uploadRoot, "000000", storedPath)
|
|
if err != nil {
|
|
t.Fatalf("resolveLocalOssPath() error = %v", err)
|
|
}
|
|
if err = os.MkdirAll(filepath.Dir(localPath), 0o755); err != nil {
|
|
t.Fatalf("create directory: %v", err)
|
|
}
|
|
if err = os.WriteFile(localPath, []byte("inbound source"), 0o600); err != nil {
|
|
t.Fatalf("write source file: %v", err)
|
|
}
|
|
content, err := os.ReadFile(localPath)
|
|
if err != nil {
|
|
t.Fatalf("read source file: %v", err)
|
|
}
|
|
if string(content) != "inbound source" {
|
|
t.Fatalf("unexpected source content: %q", content)
|
|
}
|
|
}
|
|
|
|
func TestUploadRootCandidatesIncludeMountedUploadDirectory(t *testing.T) {
|
|
wd := t.TempDir()
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("get working directory: %v", err)
|
|
}
|
|
if err = os.Chdir(wd); err != nil {
|
|
t.Fatalf("change working directory: %v", err)
|
|
}
|
|
defer func() {
|
|
_ = os.Chdir(oldWd)
|
|
}()
|
|
|
|
candidates := uploadRootCandidates("resource/upload")
|
|
want := filepath.Join(wd, "upload")
|
|
for _, candidate := range candidates {
|
|
if filepath.Clean(candidate) == filepath.Clean(want) {
|
|
return
|
|
}
|
|
}
|
|
t.Fatalf("uploadRootCandidates() = %#v, want %q", candidates, want)
|
|
}
|
|
|
|
func TestResolveReadableLocalOssPathUsesMountedUploadDirectory(t *testing.T) {
|
|
wd := t.TempDir()
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("get working directory: %v", err)
|
|
}
|
|
if err = os.Chdir(wd); err != nil {
|
|
t.Fatalf("change working directory: %v", err)
|
|
}
|
|
defer func() {
|
|
_ = os.Chdir(oldWd)
|
|
}()
|
|
|
|
storedPath := "resource/upload/000000/user/file/20260813/sim-card-inbound/import/source.xlsx"
|
|
mountedPath := filepath.Join(wd, "upload", "000000", "user", "file", "20260813", "sim-card-inbound", "import", "source.xlsx")
|
|
if err = os.MkdirAll(filepath.Dir(mountedPath), 0o755); err != nil {
|
|
t.Fatalf("create mounted upload directory: %v", err)
|
|
}
|
|
if err = os.WriteFile(mountedPath, []byte("xlsx"), 0o600); err != nil {
|
|
t.Fatalf("write mounted upload file: %v", err)
|
|
}
|
|
|
|
gotPath, attempts, err := resolveReadableLocalOssPath("resource/upload", "000000", storedPath)
|
|
if err != nil {
|
|
t.Fatalf("resolveReadableLocalOssPath() error = %v, attempts = %#v", err, attempts)
|
|
}
|
|
if gotPath != mountedPath {
|
|
t.Fatalf("resolveReadableLocalOssPath() = %q, want %q, attempts = %#v", gotPath, mountedPath, attempts)
|
|
}
|
|
}
|
|
|
|
func TestBuildResultBatchInfoUsesGroupedResultFile(t *testing.T) {
|
|
task := &serviceinbound.Task{
|
|
Batch: serviceinbound.Batch{
|
|
BatchInfo: gjson.New(map[string]any{
|
|
"source": map[string]any{"inputText": "8986"},
|
|
}),
|
|
},
|
|
}
|
|
|
|
info := buildResultBatchInfo(task, "failed", 0, 0, "disk full")
|
|
if info.Get("resultFile.status").String() != "failed" {
|
|
t.Fatalf("unexpected result file status: %s", info.Get("resultFile.status").String())
|
|
}
|
|
if info.Get("resultFile.error").String() != "disk full" {
|
|
t.Fatalf("unexpected result file error: %s", info.Get("resultFile.error").String())
|
|
}
|
|
if info.Get("resultFileOssId").Int64() != 0 || info.Get("resultDownloadId").Int64() != 0 {
|
|
t.Fatalf("result file metadata should stay grouped: %s", info.MustToJsonString())
|
|
}
|
|
}
|
|
|
|
func TestParseAndBuildInboundOptionsPreserveUpstreamValidationFlag(t *testing.T) {
|
|
info := gjson.New(map[string]any{
|
|
"inboundOptions": map[string]any{
|
|
"chanKey": "chan-a",
|
|
"validateUpstreamBasicInfo": 1,
|
|
},
|
|
})
|
|
|
|
options := parseOptions(info)
|
|
if options.ValidateUpstreamBasicInfo != "1" {
|
|
t.Fatalf("ValidateUpstreamBasicInfo = %q, want 1", options.ValidateUpstreamBasicInfo)
|
|
}
|
|
|
|
task := &serviceinbound.Task{
|
|
Batch: serviceinbound.Batch{BatchInfo: info},
|
|
Options: options,
|
|
}
|
|
built := buildInboundBatchInfo(task)
|
|
if built.Get("inboundOptions.validateUpstreamBasicInfo").String() != "1" {
|
|
t.Fatalf("validate option not preserved: %s", built.MustToJsonString())
|
|
}
|
|
if built.Get("inboundOptions.chanKey").String() != "chan-a" {
|
|
t.Fatalf("chanKey not preserved: %s", built.MustToJsonString())
|
|
}
|
|
}
|
|
|
|
func TestParseSourcePrefersSourceOssId(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
sourceType int
|
|
wantImportID int64
|
|
wantInputID int64
|
|
}{
|
|
{name: "file source", sourceType: consts.BatchSourceTypeFile, wantImportID: 88, wantInputID: 22},
|
|
{name: "input source", sourceType: consts.BatchSourceTypeBatchInput, wantImportID: 11, wantInputID: 88},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
info := gjson.New(map[string]any{
|
|
"source": map[string]any{
|
|
"importFileOssId": int64(11),
|
|
"inputFileOssId": int64(22),
|
|
},
|
|
})
|
|
source := parseSource(info, 88, tt.sourceType)
|
|
if source.ImportFileOssID != tt.wantImportID {
|
|
t.Fatalf("ImportFileOssID = %d, want %d", source.ImportFileOssID, tt.wantImportID)
|
|
}
|
|
if source.InputFileOssID != tt.wantInputID {
|
|
t.Fatalf("InputFileOssID = %d, want %d", source.InputFileOssID, tt.wantInputID)
|
|
}
|
|
})
|
|
}
|
|
}
|