blob: ef625eb294110103162a66f401c870b1407ee829 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -07005package test
Jiri Simsa5293dcb2014-05-10 09:56:38 -07006
7import (
8 "flag"
9 "os"
10 "runtime"
Asim Shankar20282032014-07-07 22:48:00 -070011 "sync"
Ankurf416ac52015-01-29 13:58:24 -080012
Jiri Simsa6ac95222015-02-23 16:11:49 -080013 "v.io/v23"
14 "v.io/v23/context"
Matt Rosencrantz165e8902015-06-12 15:27:27 -070015 "v.io/v23/naming"
16 "v.io/v23/options"
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070017 "v.io/x/ref/internal/logger"
Cosmos Nicolaou036c30c2015-03-24 10:05:20 -070018 "v.io/x/ref/lib/flags"
Matt Rosencrantz165e8902015-06-12 15:27:27 -070019 "v.io/x/ref/services/mounttable/mounttablelib"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070020 "v.io/x/ref/test/testutil"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070021)
22
Bogdan Capritafca013d2015-01-29 15:32:34 -080023const (
Bogdan Capritafca013d2015-01-29 15:32:34 -080024 TestBlessing = "test-blessing"
25)
Jiri Simsa870ddd62014-06-27 14:56:58 -070026
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080027var once sync.Once
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080028var IntegrationTestsEnabled bool
29var IntegrationTestsDebugShellOnError bool
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080030
31const IntegrationTestsFlag = "v23.tests"
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080032const IntegrationTestsDebugShellOnErrorFlag = "v23.tests.shell-on-fail"
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080033
34func init() {
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080035 flag.BoolVar(&IntegrationTestsEnabled, IntegrationTestsFlag, false, "Run integration tests.")
36 flag.BoolVar(&IntegrationTestsDebugShellOnError, IntegrationTestsDebugShellOnErrorFlag, false, "Drop into a debug shell if an integration test fails.")
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080037}
Jiri Simsa870ddd62014-06-27 14:56:58 -070038
Asim Shankarc920db32014-10-16 19:18:21 -070039// Init sets up state for running tests: Adjusting GOMAXPROCS,
Cosmos Nicolaou19a50f62015-06-20 09:15:40 -070040// configuring the logging library, setting up the random number generator
Asim Shankarc920db32014-10-16 19:18:21 -070041// etc.
42//
Cosmos Nicolaoue3b19322015-06-18 16:05:08 -070043// Doing so requires flags to be parsed, so this function explicitly parses
Asim Shankarc920db32014-10-16 19:18:21 -070044// flags. Thus, it is NOT a good idea to call this from the init() function
45// of any module except "main" or _test.go files.
46func Init() {
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080047 init := func() {
48 if os.Getenv("GOMAXPROCS") == "" {
49 // Set the number of logical processors to the number of CPUs,
50 // if GOMAXPROCS is not set in the environment.
51 runtime.GOMAXPROCS(runtime.NumCPU())
Jiri Simsa870ddd62014-06-27 14:56:58 -070052 }
Cosmos Nicolaou036c30c2015-03-24 10:05:20 -070053 flags.SetDefaultProtocol("tcp")
54 flags.SetDefaultHostPort("127.0.0.1:0")
55 flags.SetDefaultNamespaceRoot("/127.0.0.1:8101")
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080056 // At this point all of the flags that we're going to use for
57 // tests must be defined.
58 // This will be the case if this is called from the init()
59 // function of a _test.go file.
60 flag.Parse()
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070061 logger.Manager(logger.Global()).ConfigureFromFlags()
Jiri Simsa870ddd62014-06-27 14:56:58 -070062 }
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080063 once.Do(init)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070064}
Ankurfb9255a2015-01-21 15:29:38 -080065
Matt Rosencrantz165e8902015-06-12 15:27:27 -070066// V23Init initializes the runtime and sets up some convenient infrastructure for tests:
67// - Sets a freshly created principal (with a single self-signed blessing) on it.
68// - Creates a mounttable and sets the namespace roots appropriately
69// Both steps are skipped if this function is invoked from a process run
70// using the modules package.
Todd Wang60052d82015-05-22 15:00:10 -070071func V23Init() (*context.T, v23.Shutdown) {
Matt Rosencrantzf7bee9e2015-06-14 18:49:20 -070072 moduleProcess := os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT") != ""
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070073 return initWithParams(initParams{
Matt Rosencrantz165e8902015-06-12 15:27:27 -070074 CreatePrincipal: !moduleProcess,
75 CreateMounttable: !moduleProcess,
76 })
77}
78
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070079// initParams contains parameters for tests that need to control what happens during
Matt Rosencrantz165e8902015-06-12 15:27:27 -070080// init carefully.
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070081type initParams struct {
Matt Rosencrantz165e8902015-06-12 15:27:27 -070082 CreateMounttable bool // CreateMounttable creates a new mounttable.
83 CreatePrincipal bool // CreatePrincipal creates a new principal with self-signed blessing.
84}
85
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070086// initWithParams initializes the runtime and returns a new context and shutdown function.
Matt Rosencrantz165e8902015-06-12 15:27:27 -070087// Specific aspects of initialization can be controlled via the params struct.
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070088func initWithParams(params initParams) (*context.T, v23.Shutdown) {
Jiri Simsa6ac95222015-02-23 16:11:49 -080089 ctx, shutdown := v23.Init()
Matt Rosencrantz165e8902015-06-12 15:27:27 -070090 if params.CreatePrincipal {
91 var err error
92 if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil {
93 panic(err)
94 }
Ankurf416ac52015-01-29 13:58:24 -080095 }
Bogdan Caprita95bca142015-06-29 17:16:05 -070096 ns := v23.GetNamespace(ctx)
97 ns.CacheCtl(naming.DisableCache(true))
Matt Rosencrantz165e8902015-06-12 15:27:27 -070098 if params.CreateMounttable {
Cosmos Nicolaou19a50f62015-06-20 09:15:40 -070099 disp, err := mounttablelib.NewMountTableDispatcher(ctx, "", "", "mounttable")
Matt Rosencrantz165e8902015-06-12 15:27:27 -0700100 if err != nil {
101 panic(err)
102 }
Matt Rosencrantz53ac5852015-09-04 15:14:54 -0700103 _, s, err := v23.WithNewDispatchingServer(ctx, "", disp, options.ServesMountTable(true))
Matt Rosencrantz165e8902015-06-12 15:27:27 -0700104 if err != nil {
105 panic(err)
106 }
Matt Rosencrantz165e8902015-06-12 15:27:27 -0700107 ns.SetRoots(s.Status().Endpoints[0].Name())
Ankurf416ac52015-01-29 13:58:24 -0800108 }
109 return ctx, shutdown
110}
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -0700111
112// TestContext returns a *contect.T suitable for use in tests with logging
113// configured to use loggler.Global(), but nothing else. In particular it does
114// not call v23.Init and hence any of the v23 functions that
115func TestContext() (*context.T, context.CancelFunc) {
116 ctx, cancel := context.RootContext()
117 return context.WithLogger(ctx, logger.Global()), cancel
118}
119
120// V23InitEmpty initializes a runtime but with no principal.
121func V23InitAnon() (*context.T, v23.Shutdown) {
122 return initWithParams(initParams{
123 CreatePrincipal: false,
124 CreateMounttable: false,
125 })
126}