|
|
@@ -0,0 +1,176 @@
|
|
|
+//go:build linux
|
|
|
+
|
|
|
+package tests
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/binary"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+ "os/exec"
|
|
|
+ "path/filepath"
|
|
|
+ "strings"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "google.golang.org/grpc/codes"
|
|
|
+ "google.golang.org/grpc/status"
|
|
|
+ hthv1 "hth/internal/gen/hth/api/v1"
|
|
|
+ "hth/internal/grpcserver"
|
|
|
+ "hth/internal/protocol6400"
|
|
|
+ "hth/internal/testutil"
|
|
|
+)
|
|
|
+
|
|
|
+func thermalConfig(t *testing.T, profile string) (string, string, string) {
|
|
|
+ t.Helper()
|
|
|
+ root := filepath.Join(t.TempDir(), "runtime")
|
|
|
+ observe := filepath.Join(root, "observe.sock")
|
|
|
+ control := filepath.Join(root, "control.sock")
|
|
|
+ path := filepath.Join(t.TempDir(), "hthd.yaml")
|
|
|
+ raw := fmt.Sprintf(`api_version: hth.api.v1
|
|
|
+product: 6400-front-rear-v1
|
|
|
+device_id: prod2u-6400-0
|
|
|
+display_name: 6400 test
|
|
|
+backend: mock
|
|
|
+runtime_root: %s
|
|
|
+sockets:
|
|
|
+ observe: %s
|
|
|
+ control: %s
|
|
|
+ observe_perm: "0660"
|
|
|
+ control_perm: "0660"
|
|
|
+thermal:
|
|
|
+ firmware_profile: %s
|
|
|
+ transaction_timeout_ms: 100
|
|
|
+ retries: 1
|
|
|
+ mock_scenario: normal
|
|
|
+grpc:
|
|
|
+ reflection: false
|
|
|
+`, root, observe, control, profile)
|
|
|
+ if err := os.WriteFile(path, []byte(raw), 0o600); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ return path, observe, control
|
|
|
+}
|
|
|
+
|
|
|
+func startThermalMock(t *testing.T, profile string) (string, string) {
|
|
|
+ t.Helper()
|
|
|
+ config, observe, control := thermalConfig(t, profile)
|
|
|
+ startAppFromConfig(t, config)
|
|
|
+ waitUnix(t, observe, 2*time.Second)
|
|
|
+ waitUnix(t, control, 2*time.Second)
|
|
|
+ return observe, control
|
|
|
+}
|
|
|
+
|
|
|
+func TestThermal6400UDSProductionAPI(t *testing.T) {
|
|
|
+ observe, control := startThermalMock(t, "production")
|
|
|
+ obsConn, err := grpcserver.DialUnix(observe, time.Second)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ defer obsConn.Close()
|
|
|
+ ctlConn, err := grpcserver.DialUnix(control, time.Second)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ defer ctlConn.Close()
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
+ defer cancel()
|
|
|
+ ctl := hthv1.NewThermalControlServiceClient(ctlConn)
|
|
|
+ obs := hthv1.NewThermalObserveServiceClient(obsConn)
|
|
|
+ set, err := ctl.SetTarget(ctx, &hthv1.SetThermalTargetRequest{DeviceId: "prod2u-6400-0", TemperatureC: 65})
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if !set.GetCommitted() || set.GetContractSha256() != protocol6400.ContractSHA256 {
|
|
|
+ t.Fatalf("set result %+v", set)
|
|
|
+ }
|
|
|
+ snapshot, err := obs.CaptureSnapshot(ctx, &hthv1.GetThermalSnapshotRequest{DeviceId: "prod2u-6400-0"})
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if snapshot.GetSnapshot().GetCurrentTarget() != 65 || len(snapshot.GetSnapshot().GetRawPayload()) != protocol6400.SnapshotPayloadLen {
|
|
|
+ t.Fatalf("snapshot %+v", snapshot.GetSnapshot())
|
|
|
+ }
|
|
|
+ _, err = ctl.RunStimulus(ctx, &hthv1.RunThermalStimulusRequest{
|
|
|
+ DeviceId: "prod2u-6400-0", DurationSeconds: 1,
|
|
|
+ PtcDuty: make([]uint32, 24), FrontFanDuty: make([]uint32, 4), RearFanDuty: make([]uint32, 4),
|
|
|
+ })
|
|
|
+ if status.Code(err) != codes.Unimplemented {
|
|
|
+ t.Fatalf("production stimulus code=%s err=%v", status.Code(err), err)
|
|
|
+ }
|
|
|
+ if _, err := ctl.Stop(ctx, &hthv1.StopThermalRequest{DeviceId: "prod2u-6400-0"}); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestThermal6400CLIAndCapture(t *testing.T) {
|
|
|
+ observe, control := startThermalMock(t, "production")
|
|
|
+ hthctl := buildHostCmd(t, "hthctl", "./cmd/hthctl")
|
|
|
+ for _, args := range [][]string{
|
|
|
+ {"--observe-sock", observe, "--control-sock", control, "--device", "prod2u-6400-0", "--timeout", "2s", "--target", "65", "set-target"},
|
|
|
+ {"--observe-sock", observe, "--device", "prod2u-6400-0", "--timeout", "2s", "thermal-snapshot"},
|
|
|
+ {"--observe-sock", observe, "--control-sock", control, "--device", "prod2u-6400-0", "--timeout", "2s", "stop"},
|
|
|
+ } {
|
|
|
+ cmd := exec.Command(hthctl, args...)
|
|
|
+ if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
+ t.Fatalf("hthctl %v: %v\n%s", args, err, out)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ captureBin := buildHostCmd(t, "hth-capture", "./cmd/hth-capture")
|
|
|
+ outDir := filepath.Join(t.TempDir(), "capture")
|
|
|
+ cmd := exec.Command(captureBin, "--thermal", "--observe-sock", observe, "--device", "prod2u-6400-0", "--out", outDir, "--duration", "1s", "--period", "100ms")
|
|
|
+ if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
+ t.Fatalf("hth-capture: %v\n%s", err, out)
|
|
|
+ }
|
|
|
+ raw, err := os.ReadFile(filepath.Join(outDir, "raw-frames.bin"))
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if len(raw) < 4+protocol6400.SnapshotPayloadLen || binary.LittleEndian.Uint32(raw[:4]) != protocol6400.SnapshotPayloadLen {
|
|
|
+ t.Fatalf("raw capture len=%d prefix=%d", len(raw), binary.LittleEndian.Uint32(raw[:4]))
|
|
|
+ }
|
|
|
+ for _, name := range []string{"thermal-snapshots.jsonl", "run.json", "checksums.sha256"} {
|
|
|
+ if _, err := os.Stat(filepath.Join(outDir, name)); err != nil {
|
|
|
+ t.Fatalf("missing %s: %v", name, err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ runRaw, err := os.ReadFile(filepath.Join(outDir, "run.json"))
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ var runMeta map[string]any
|
|
|
+ if err := json.Unmarshal(runRaw, &runMeta); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if runMeta["hardware_validation"] != "NOT RUN" || runMeta["com_validation"] != "NOT RUN" || runMeta["real_thermal_performance"] != "NOT RUN" {
|
|
|
+ t.Fatalf("capture truth labels %+v", runMeta)
|
|
|
+ }
|
|
|
+ checksums, err := os.ReadFile(filepath.Join(outDir, "checksums.sha256"))
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if !strings.Contains(string(checksums), " run.json\n") {
|
|
|
+ t.Fatalf("run.json checksum missing: %s", checksums)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestThermal6400PythonSDKLiveUDS(t *testing.T) {
|
|
|
+ observe, control := startThermalMock(t, "identification")
|
|
|
+ code := strings.Join([]string{
|
|
|
+ "from hth_sdk.thermal import connect_thermal",
|
|
|
+ fmt.Sprintf("with connect_thermal(device_id='prod2u-6400-0', observe_socket=%q, control_socket=%q, firmware_profile='identification', timeout=2.0) as h:", observe, control),
|
|
|
+ " h.set_target(60)",
|
|
|
+ " s = h.get_snapshot()",
|
|
|
+ " assert s.decoded['current_target'] == 60",
|
|
|
+ " h.run_stimulus(1, [20] * 24, [30] * 4, [40] * 4)",
|
|
|
+ " s = h.capture_snapshot()",
|
|
|
+ " assert s.decoded['ptc_applied'][0] == 20",
|
|
|
+ " h.stop()",
|
|
|
+ }, "\n")
|
|
|
+ cmd := exec.Command("python3", "-c", code)
|
|
|
+ cmd.Env = append(os.Environ(), "PYTHONPATH="+filepath.Join(testutil.HostRoot(t), "sdk", "python"))
|
|
|
+ if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
+ t.Fatalf("Python thermal SDK: %v\n%s", err, out)
|
|
|
+ }
|
|
|
+}
|