| new file mode 100644 |
| index 000000000..f69f2a4ad |
| --- /dev/null |
| +++ b/cmd/console/singletenant_test.go |
| @@ -0,0 +1,167 @@ |
| +package main |
| + |
| +import ( |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/console" |
| +) |
| + |
| +// The console reads CARLOS_DEPLOYMENT_SINGLE_TENANT exactly as the box does |
| +// (internal/host singleTenant, D6): "1" is on, "" and "0" are off, with no |
| +// trimming. Anything else stops the console at boot, so every value it runs |
| +// with is one every box reads the same way. |
| +func TestParseSingleTenant(t *testing.T) { |
| + for v, want := range map[string]bool{"": false, "0": false, "1": true} { |
| + got, err := parseSingleTenant(v) |
| + if err != nil || got != want { |
| + t.Errorf("parseSingleTenant(%q) = %v, %v; want %v", v, got, err, want) |
| + } |
| + } |
| + // The box reads only 1 (D6). A console that took "true" as on would |
| + // allow creates every box refuses. The box reads " 1 " and "1 " as off, |
| + // so a console that trimmed them would split from it the same way. |
| + for _, v := range []string{"true", "yes", "on", "2", "false", " 1 ", "1 ", " 1", "0 ", " ", "\t1", "1\n"} { |
| + if _, err := parseSingleTenant(v); err == nil { |
| + t.Errorf("parseSingleTenant(%q) accepted", v) |
| + } |
| + } |
| +} |
| + |
| +func TestApplySingleTenantReadsOnlyItsVariable(t *testing.T) { |
| + if singleTenantEnv != "CARLOS_DEPLOYMENT_SINGLE_TENANT" { |
| + t.Fatalf("singleTenantEnv = %q; the box reads CARLOS_DEPLOYMENT_SINGLE_TENANT", singleTenantEnv) |
| + } |
| + var cfg console.Config |
| + err := applySingleTenant(&cfg, func(k string) string { |
| + if k != singleTenantEnv { |
| + t.Fatalf("read %q", k) |
| + } |
| + return "1" |
| + }) |
| + if err != nil || !cfg.DeploymentSingleTenant { |
| + t.Fatalf("DeploymentSingleTenant = %v, %v", cfg.DeploymentSingleTenant, err) |
| + } |
| + |
| + // Off and unset leave the field false; a bad value errors and leaves |
| + // it false, so nothing downstream could read a half-applied setting. |
| + for _, v := range []string{"", "0", " 1 ", "true"} { |
| + cfg := console.Config{} |
| + err := applySingleTenant(&cfg, func(string) string { return v }) |
| + if cfg.DeploymentSingleTenant { |
| + t.Errorf("%q: DeploymentSingleTenant = true", v) |
| + } |
| + if bad := v != "" && v != "0"; bad != (err != nil) { |
| + t.Errorf("%q: err = %v", v, err) |
| + } |
| + } |
| +} |
| + |
| +// serve() is not callable from a test, so this parses main.go (the shape of |
| +// TestServeAppliesTheMovesSwitchFromTheEnvironment) and checks: serve() |
| +// calls applySingleTenant(&X, os.Getenv) exactly once, as the init of an |
| +// if whose err != nil branch hands that error to log.Fatal; the same X is |
| +// the config handed to console.NewServer; and the call comes first. |
| +func TestServeAppliesTheSingleTenantSetting(t *testing.T) { |
| + f, err := parser.ParseFile(token.NewFileSet(), "main.go", nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var serve *ast.FuncDecl |
| + for _, d := range f.Decls { |
| + if fn, ok := d.(*ast.FuncDecl); ok && fn.Name.Name == "serve" { |
| + serve = fn |
| + } |
| + } |
| + if serve == nil { |
| + t.Fatal("no serve() in main.go") |
| + } |
| + var applies, news []*ast.CallExpr |
| + var guarded *ast.CallExpr |
| + ast.Inspect(serve.Body, func(n ast.Node) bool { |
| + switch n := n.(type) { |
| + case *ast.IfStmt: |
| + if call := singleTenantGuard(n); call != nil { |
| + guarded = call |
| + } |
| + case *ast.CallExpr: |
| + switch fun := n.Fun.(type) { |
| + case *ast.Ident: |
| + if fun.Name == "applySingleTenant" { |
| + applies = append(applies, n) |
| + } |
| + case *ast.SelectorExpr: |
| + if isIdent(fun.X, "console") && fun.Sel.Name == "NewServer" { |
| + news = append(news, n) |
| + } |
| + } |
| + } |
| + return true |
| + }) |
| + if len(applies) != 1 || len(news) != 1 { |
| + t.Fatalf("serve() has %d applySingleTenant calls and %d console.NewServer calls, want one of each", len(applies), len(news)) |
| + } |
| + apply, ns := applies[0], news[0] |
| + if guarded != apply { |
| + t.Fatal("applySingleTenant is not called as `if err := applySingleTenant(...); err != nil { log.Fatal(err) }`; a bad value would not stop the console") |
| + } |
| + if len(apply.Args) != 2 || len(ns.Args) < 1 { |
| + t.Fatal("applySingleTenant wants (&cfg, os.Getenv) and console.NewServer wants (cfg, store)") |
| + } |
| + if sel, ok := apply.Args[1].(*ast.SelectorExpr); !ok || !isIdent(sel.X, "os") || sel.Sel.Name != "Getenv" { |
| + t.Fatal("applySingleTenant is not given os.Getenv; the setting would not read the real environment") |
| + } |
| + addr, ok := apply.Args[0].(*ast.UnaryExpr) |
| + if !ok || addr.Op != token.AND { |
| + t.Fatal("applySingleTenant's first argument is not &X") |
| + } |
| + x, ok := addr.X.(*ast.Ident) |
| + if !ok { |
| + t.Fatal("applySingleTenant's first argument is not the address of a plain variable") |
| + } |
| + if !isIdent(ns.Args[0], x.Name) { |
| + t.Fatalf("console.NewServer is not handed %s, the config applySingleTenant changed", x.Name) |
| + } |
| + if apply.Pos() >= ns.Pos() { |
| + t.Fatal("applySingleTenant runs after console.NewServer; the server would be built with the setting off") |
| + } |
| +} |
| + |
| +// singleTenantGuard returns the applySingleTenant call when s is |
| +// `if E := applySingleTenant(...); E != nil { ... log.Fatal(E) ... }`. |
| +func singleTenantGuard(s *ast.IfStmt) *ast.CallExpr { |
| + init, ok := s.Init.(*ast.AssignStmt) |
| + if !ok || len(init.Lhs) != 1 || len(init.Rhs) != 1 { |
| + return nil |
| + } |
| + errVar, ok := init.Lhs[0].(*ast.Ident) |
| + if !ok { |
| + return nil |
| + } |
| + call, ok := init.Rhs[0].(*ast.CallExpr) |
| + if !ok || !isIdent(call.Fun, "applySingleTenant") { |
| + return nil |
| + } |
| + cond, ok := s.Cond.(*ast.BinaryExpr) |
| + if !ok || cond.Op != token.NEQ || !isIdent(cond.X, errVar.Name) || !isIdent(cond.Y, "nil") { |
| + return nil |
| + } |
| + fatal := false |
| + ast.Inspect(s.Body, func(n ast.Node) bool { |
| + c, ok := n.(*ast.CallExpr) |
| + if !ok { |
| + return true |
| + } |
| + if sel, ok := c.Fun.(*ast.SelectorExpr); ok && isIdent(sel.X, "log") && sel.Sel.Name == "Fatal" && |
| + len(c.Args) == 1 && isIdent(c.Args[0], errVar.Name) { |
| + fatal = true |
| + } |
| + return true |
| + }) |
| + if !fatal { |
| + return nil |
| + } |
| + return call |
| +} |