chore: 初始开源发布

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),属既有缺陷,本仓未引入亦未修复。
This commit is contained in:
MiFaZhan
2026-10-22 16:37:56 +08:00
commit 2bec257289
361 changed files with 71524 additions and 0 deletions
+192
View File
@@ -0,0 +1,192 @@
package lifecycle
import (
"context"
"errors"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
)
func TestExecuteValidationFailureSkipsUpstream(t *testing.T) {
operation, err := OperationByBatchType(consts.BatchTypeSimCardStop)
if err != nil {
t.Fatal(err)
}
task := &Task{
Batch: Batch{BatchNo: "B-1"},
Operation: operation,
Details: []*Detail{
{ID: 1, ICCID: "iccid-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
{ID: 2, ICCID: "iccid-2", Authorized: false, Card: &Card{ID: 12, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
},
}
repo := &fakeRepository{task: task}
upstream := &fakeUpstream{responseError: errors.New("must not be called")}
if err = NewExecutor(repo, upstream).Execute(context.Background(), "B-1"); err != nil {
t.Fatal(err)
}
if upstream.calls != 0 {
t.Fatalf("upstream calls = %d, want 0", upstream.calls)
}
if !repo.validationSaved || !repo.finalized {
t.Fatal("validation failure was not persisted and finalized")
}
if task.Details[0].Reason == "" || task.Details[1].Reason == "" {
t.Fatal("every detail must receive a validation failure reason")
}
}
func TestExecuteMapsMixedResponseAndPreservesOrderNumber(t *testing.T) {
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil {
t.Fatal(err)
}
task := &Task{
Batch: Batch{BatchNo: "B-2"},
Operation: operation,
Details: []*Detail{
{ID: 1, ICCID: "iccid-1", MSISDN: "msisdn-1", IMSI: "imsi-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", MSISDN: "msisdn-1", IMSI: "imsi-1", BossCardStatus: consts.SimBossCardStatusStopped}},
{ID: 2, ICCID: "iccid-2", MSISDN: "msisdn-2", IMSI: "imsi-2", Authorized: true, Card: &Card{ID: 12, ChanKey: "channel", MSISDN: "msisdn-2", IMSI: "imsi-2", BossCardStatus: consts.SimBossCardStatusStopped}},
},
}
repo := &fakeRepository{task: task}
upstream := &fakeUpstream{response: []model.SimBaseRes[model.SimCardStatusChangeOutputItem]{
{Code: 0, Message: "ok", SimKey: nil, ResData: model.SimCardStatusChangeOutputItem{OrderNum: "order-1"}},
{Code: 9001, Message: "denied", SimKey: &model.SimKey{ICCID: "iccid-2"}, ResData: model.SimCardStatusChangeOutputItem{OrderNum: "order-2"}},
}}
if err = NewExecutor(repo, upstream).Execute(context.Background(), "B-2"); err != nil {
t.Fatal(err)
}
if upstream.input.ReqData.OperType != consts.SimCardStatusOperTypeStart {
t.Fatalf("operType = %q", upstream.input.ReqData.OperType)
}
if len(repo.saved) != 2 || !repo.saved[0].Success || repo.saved[0].OrderNum != "order-1" {
t.Fatalf("first result = %+v", repo.saved)
}
if repo.saved[1].Success || repo.saved[1].Code != 9001 || repo.saved[1].Message != "denied" || repo.saved[1].OrderNum != "order-2" {
t.Fatalf("second result = %+v", repo.saved[1])
}
if !repo.finalized {
t.Fatal("batch was not finalized")
}
}
func TestOperationByBatchTypeOperType(t *testing.T) {
start, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil || start.OperType != "1" || start.TargetStatus != consts.SimBossCardStatusActivated {
t.Fatalf("start operation = %+v, err = %v", start, err)
}
stop, err := OperationByBatchType(consts.BatchTypeSimCardStop)
if err != nil || stop.OperType != "0" || stop.TargetStatus != consts.SimBossCardStatusStopped {
t.Fatalf("stop operation = %+v, err = %v", stop, err)
}
}
func TestValidateDetailPopulatesCardFieldsOnStatusFailure(t *testing.T) {
t.Skip("TODO: 恢复卡状态限制后启用此测试")
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil {
t.Fatal(err)
}
detail := &Detail{
ID: 1,
RowNo: 1,
ICCID: "iccid-1",
Card: &Card{
ID: 11,
ICCID: "iccid-1",
MSISDN: "msisdn-1",
IMSI: "imsi-1",
ChanKey: "channel",
BossCardStatus: consts.SimBossCardStatusActivated,
},
}
validateDetail(operation, detail)
if detail.Reason == "" {
t.Fatal("expected validation failure reason")
}
if detail.MSISDN != "msisdn-1" || detail.IMSI != "imsi-1" || detail.ChanKey != "channel" {
t.Fatalf("card fields not populated on status failure: %+v", detail)
}
if detail.StatusBefore != consts.SimBossCardStatusActivated {
t.Fatalf("StatusBefore = %d, want %d", detail.StatusBefore, consts.SimBossCardStatusActivated)
}
snapshot := detail.Snapshot()
if snapshot.Get("MSISDN").String() != "msisdn-1" || snapshot.Get("IMSI").String() != "imsi-1" || snapshot.Get("chanKey").String() != "channel" {
t.Fatalf("snapshot missing card fields: %s", snapshot.MustToJsonString())
}
}
func TestValidateDetailDetectsMSISDNMismatch(t *testing.T) {
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil {
t.Fatal(err)
}
detail := &Detail{
ID: 1,
ICCID: "iccid-1",
Authorized: true,
InputMSISDN: "msisdn-input",
Card: &Card{
ID: 11,
ICCID: "iccid-1",
MSISDN: "msisdn-card",
IMSI: "imsi-1",
ChanKey: "channel",
BossCardStatus: consts.SimBossCardStatusStopped,
},
}
validateDetail(operation, detail)
if detail.Reason != "MSISDN does not match the SIM card record" {
t.Fatalf("unexpected reason: %q", detail.Reason)
}
if detail.MSISDN != "msisdn-card" || detail.InputMSISDN != "msisdn-input" {
t.Fatalf("mismatch not preserved: MSISDN=%q InputMSISDN=%q", detail.MSISDN, detail.InputMSISDN)
}
snapshot := detail.Snapshot()
if snapshot.Get("inputMsisdn").String() != "msisdn-input" || snapshot.Get("MSISDN").String() != "msisdn-card" {
t.Fatalf("snapshot mismatch fields wrong: %s", snapshot.MustToJsonString())
}
}
type fakeRepository struct {
task *Task
validationSaved bool
finalized bool
saved []Outcome
}
func (r *fakeRepository) Load(context.Context, string) (*Task, error) { return r.task, nil }
func (r *fakeRepository) SaveValidationFailure(_ context.Context, task *Task) error {
r.validationSaved = true
return nil
}
func (r *fakeRepository) SaveResult(_ context.Context, _ *Task, detail *Detail, outcome Outcome) (Outcome, error) {
detail.ApplyOutcome(outcome)
r.saved = append(r.saved, outcome)
return outcome, nil
}
func (r *fakeRepository) Finalize(context.Context, *Task) error {
r.finalized = true
return nil
}
type fakeUpstream struct {
calls int
input *model.SimCardStatusChangeInput
response []model.SimBaseRes[model.SimCardStatusChangeOutputItem]
responseError error
}
func (u *fakeUpstream) ChangeSimStatus(_ context.Context, input *model.SimCardStatusChangeInput) ([]model.SimBaseRes[model.SimCardStatusChangeOutputItem], error) {
u.calls++
u.input = input
return u.response, u.responseError
}