package config import ( "slices" "strings" "testing" ) const machineFile = ` listen: ":8080" agent_listen: ":7443" agent_insecure: true auth: enabled: true store: /var/lib/llmgw/llmgw.db default_rpm: 60 mirror: api: http://127.0.0.1:8082 update_check: 6h guard: default: [baseline] policies: baseline: input: injection: flag router: retries: 2 ` func TestAFileSplitsIntoTheMachinesAndTheClusters(t *testing.T) { file, err := Parse([]byte(machineFile)) if err != nil { t.Fatal(err) } local, cluster := Split(file) for _, k := range []string{"listen", "agent_listen", "agent_insecure"} { if _, ok := local[k]; !ok { t.Errorf("%s is not the machine's", k) } } if local["auth"].(map[string]any)["store"] == nil || cluster["auth"].(map[string]any)["store"] != nil { t.Errorf("auth.store went to the cluster: local %v cluster %v", local["auth"], cluster["auth"]) } if cluster["auth"].(map[string]any)["default_rpm"] != 60 { t.Errorf("auth's policy stayed with the machine: %v", cluster["auth"]) } if local["mirror"].(map[string]any)["api"] == nil || cluster["mirror"].(map[string]any)["update_check"] == nil { t.Errorf("mirror split wrong: local %v cluster %v", local["mirror"], cluster["mirror"]) } for _, k := range []string{"guard", "router"} { if _, ok := cluster[k]; !ok { t.Errorf("%s is not the cluster's", k) } } if err := CheckCluster(cluster); err != nil { t.Errorf("the cluster half is refused: %v", err) } if err := CheckCluster(file); err == nil || !strings.Contains(err.Error(), "auth.store") { t.Errorf("a cluster document with a machine's keys: %v", err) } } func TestTheClustersSettingsTakeThePlaceOfTheFiles(t *testing.T) { file, _ := Parse([]byte(machineFile)) cluster, _ := Parse([]byte("router:\n retries: 5\nauth:\n enabled: true\n default_rpm: 10\n")) eff := Layer(file, cluster) cfg, err := FromMap(eff) if err != nil { t.Fatal(err) } if cfg.Router.Retries != 5 || cfg.Auth.DefaultRPM != 10 { t.Errorf("cluster values not in force: retries %d rpm %d", cfg.Router.Retries, cfg.Auth.DefaultRPM) } if cfg.Auth.Store != "/var/lib/llmgw/llmgw.db" || cfg.Mirror.API != "http://127.0.0.1:8082" || cfg.Listen != ":8080" { t.Errorf("the machine's own keys lost: %+v", cfg.Auth) } if len(cfg.Guard.Policies) != 0 { t.Error("the file's guard applies though the cluster has settings without one") } if got := Ignored(file); !slices.Contains(got, "guard") || !slices.Contains(got, "auth.default_rpm") || slices.Contains(got, "auth.store") { t.Errorf("ignored: %v", got) } } func TestChangedNamesWhatMoved(t *testing.T) { a, _ := Parse([]byte(machineFile)) b, _ := Parse([]byte(strings.Replace(machineFile, "retries: 2", "retries: 3", 1))) if got := Changed(a, b); !slices.Equal(got, []string{"router"}) { t.Errorf("changed %v", got) } c, _ := Parse([]byte(strings.Replace(machineFile, "store: /var/lib/llmgw/llmgw.db", "store: /x.db", 1))) if got := Changed(a, c); !slices.Equal(got, []string{"auth.store"}) { t.Errorf("changed %v", got) } if got := Changed(a, a); len(got) != 0 { t.Errorf("unchanged: %v", got) } }