35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
|
|
package config
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLoadRequiresDeviceCredentials(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "iot.yaml")
|
||
|
|
data := []byte("MQTT:\n Address: 127.0.0.1:1883\nHTTP:\n Address: 127.0.0.1:12428\n InternalToken: test\n")
|
||
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := Load(path); err == nil {
|
||
|
|
t.Fatal("未配置设备凭证时应拒绝启动")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoadDefaultsListenerAndOverridesSingleDevicePassword(t *testing.T) {
|
||
|
|
t.Setenv("HEQI_IOT_MQTT_PASSWORD", "from-secret-store")
|
||
|
|
path := filepath.Join(t.TempDir(), "iot.yaml")
|
||
|
|
data := []byte("MQTT:\n Address: 127.0.0.1:1883\n Devices:\n - ClientID: detector-1\n DeviceID: '0000000000000001'\n Username: detector-1\n Password: placeholder\nHTTP:\n Address: 127.0.0.1:12428\n InternalToken: test\n")
|
||
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
cfg, err := Load(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if cfg.MQTT.ListenerID != "mqtt-tcp" || cfg.MQTT.Devices[0].Password != "from-secret-store" {
|
||
|
|
t.Fatalf("unexpected MQTT config: %+v", cfg.MQTT)
|
||
|
|
}
|
||
|
|
}
|