67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"douyu_go/internal/types"
|
|
)
|
|
|
|
func TestMachineRemoteProfileIsInheritedByOtherInstances(t *testing.T) {
|
|
root := t.TempDir()
|
|
t.Setenv("DOUYU_DATA_DIR", root)
|
|
|
|
source := types.Config{
|
|
RemoteEnabled: true,
|
|
RemoteURL: " https://control.khhao.com ",
|
|
RemoteEnrollmentToken: " enroll-secret ",
|
|
RemoteMachineName: " PC-01 ",
|
|
}
|
|
if err := SaveMachineRemoteProfile(source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := os.Stat(filepath.Join(root, "remote", machineRemoteProfileFile))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode().Perm() != 0600 {
|
|
t.Fatalf("profile mode=%o, want 600", info.Mode().Perm())
|
|
}
|
|
|
|
profile, ok, err := LoadMachineRemoteProfile()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("machine remote profile should exist")
|
|
}
|
|
var inherited types.Config
|
|
ApplyMachineRemoteProfile(&inherited, profile)
|
|
if !inherited.RemoteEnabled ||
|
|
inherited.RemoteURL != "https://control.khhao.com" ||
|
|
inherited.RemoteEnrollmentToken != "enroll-secret" ||
|
|
inherited.RemoteMachineName != "PC-01" {
|
|
t.Fatalf("unexpected inherited remote config: %+v", inherited)
|
|
}
|
|
}
|
|
|
|
func TestMachineRemoteProfileCanDisableRemoteForNewInstances(t *testing.T) {
|
|
t.Setenv("DOUYU_DATA_DIR", t.TempDir())
|
|
if err := SaveMachineRemoteProfile(types.Config{
|
|
RemoteEnabled: false,
|
|
RemoteURL: "https://control.khhao.com",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
profile, ok, err := LoadMachineRemoteProfile()
|
|
if err != nil || !ok {
|
|
t.Fatalf("LoadMachineRemoteProfile() ok=%v err=%v", ok, err)
|
|
}
|
|
cfg := types.Config{RemoteEnabled: true}
|
|
ApplyMachineRemoteProfile(&cfg, profile)
|
|
if cfg.RemoteEnabled {
|
|
t.Fatal("disabled machine profile should disable remote for new instances")
|
|
}
|
|
}
|