blob: c62730949805262238b5d8f637eaad41c81ff6bb [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 Nicolaoua18a1eb2015-03-12 13:15:01 -070017
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070018 "v.io/x/ref/internal/logger"
Cosmos Nicolaou036c30c2015-03-24 10:05:20 -070019 "v.io/x/ref/lib/flags"
Matt Rosencrantz165e8902015-06-12 15:27:27 -070020 "v.io/x/ref/lib/xrpc"
21 "v.io/x/ref/services/mounttable/mounttablelib"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070022 "v.io/x/ref/test/testutil"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070023)
24
Bogdan Capritafca013d2015-01-29 15:32:34 -080025const (
Bogdan Capritafca013d2015-01-29 15:32:34 -080026 TestBlessing = "test-blessing"
27)
Jiri Simsa870ddd62014-06-27 14:56:58 -070028
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080029var once sync.Once
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080030var IntegrationTestsEnabled bool
31var IntegrationTestsDebugShellOnError bool
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080032
33const IntegrationTestsFlag = "v23.tests"
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080034const IntegrationTestsDebugShellOnErrorFlag = "v23.tests.shell-on-fail"
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080035
36func init() {
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080037 flag.BoolVar(&IntegrationTestsEnabled, IntegrationTestsFlag, false, "Run integration tests.")
38 flag.BoolVar(&IntegrationTestsDebugShellOnError, IntegrationTestsDebugShellOnErrorFlag, false, "Drop into a debug shell if an integration test fails.")
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080039}
Jiri Simsa870ddd62014-06-27 14:56:58 -070040
Asim Shankarc920db32014-10-16 19:18:21 -070041// Init sets up state for running tests: Adjusting GOMAXPROCS,
Cosmos Nicolaou19a50f62015-06-20 09:15:40 -070042// configuring the logging library, setting up the random number generator
Asim Shankarc920db32014-10-16 19:18:21 -070043// etc.
44//
Cosmos Nicolaoue3b19322015-06-18 16:05:08 -070045// Doing so requires flags to be parsed, so this function explicitly parses
Asim Shankarc920db32014-10-16 19:18:21 -070046// flags. Thus, it is NOT a good idea to call this from the init() function
47// of any module except "main" or _test.go files.
48func Init() {
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080049 init := func() {
50 if os.Getenv("GOMAXPROCS") == "" {
51 // Set the number of logical processors to the number of CPUs,
52 // if GOMAXPROCS is not set in the environment.
53 runtime.GOMAXPROCS(runtime.NumCPU())
Jiri Simsa870ddd62014-06-27 14:56:58 -070054 }
Cosmos Nicolaou036c30c2015-03-24 10:05:20 -070055 flags.SetDefaultProtocol("tcp")
56 flags.SetDefaultHostPort("127.0.0.1:0")
57 flags.SetDefaultNamespaceRoot("/127.0.0.1:8101")
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080058 // At this point all of the flags that we're going to use for
59 // tests must be defined.
60 // This will be the case if this is called from the init()
61 // function of a _test.go file.
62 flag.Parse()
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070063 logger.Manager(logger.Global()).ConfigureFromFlags()
Jiri Simsa870ddd62014-06-27 14:56:58 -070064 }
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080065 once.Do(init)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070066}
Ankurfb9255a2015-01-21 15:29:38 -080067
Matt Rosencrantz165e8902015-06-12 15:27:27 -070068// V23Init initializes the runtime and sets up some convenient infrastructure for tests:
69// - Sets a freshly created principal (with a single self-signed blessing) on it.
70// - Creates a mounttable and sets the namespace roots appropriately
71// Both steps are skipped if this function is invoked from a process run
72// using the modules package.
Todd Wang60052d82015-05-22 15:00:10 -070073func V23Init() (*context.T, v23.Shutdown) {
Matt Rosencrantzf7bee9e2015-06-14 18:49:20 -070074 moduleProcess := os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT") != ""
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070075 return initWithParams(initParams{
Matt Rosencrantz165e8902015-06-12 15:27:27 -070076 CreatePrincipal: !moduleProcess,
77 CreateMounttable: !moduleProcess,
78 })
79}
80
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070081// initParams contains parameters for tests that need to control what happens during
Matt Rosencrantz165e8902015-06-12 15:27:27 -070082// init carefully.
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070083type initParams struct {
Matt Rosencrantz165e8902015-06-12 15:27:27 -070084 CreateMounttable bool // CreateMounttable creates a new mounttable.
85 CreatePrincipal bool // CreatePrincipal creates a new principal with self-signed blessing.
86}
87
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070088// initWithParams initializes the runtime and returns a new context and shutdown function.
Matt Rosencrantz165e8902015-06-12 15:27:27 -070089// Specific aspects of initialization can be controlled via the params struct.
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -070090func initWithParams(params initParams) (*context.T, v23.Shutdown) {
Jiri Simsa6ac95222015-02-23 16:11:49 -080091 ctx, shutdown := v23.Init()
Matt Rosencrantz165e8902015-06-12 15:27:27 -070092 if params.CreatePrincipal {
93 var err error
94 if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil {
95 panic(err)
96 }
Ankurf416ac52015-01-29 13:58:24 -080097 }
Bogdan Caprita95bca142015-06-29 17:16:05 -070098 ns := v23.GetNamespace(ctx)
99 ns.CacheCtl(naming.DisableCache(true))
Matt Rosencrantz165e8902015-06-12 15:27:27 -0700100 if params.CreateMounttable {
Cosmos Nicolaou19a50f62015-06-20 09:15:40 -0700101 disp, err := mounttablelib.NewMountTableDispatcher(ctx, "", "", "mounttable")
Matt Rosencrantz165e8902015-06-12 15:27:27 -0700102 if err != nil {
103 panic(err)
104 }
105 s, err := xrpc.NewDispatchingServer(ctx, "", disp, options.ServesMountTable(true))
106 if err != nil {
107 panic(err)
108 }
Matt Rosencrantz165e8902015-06-12 15:27:27 -0700109 ns.SetRoots(s.Status().Endpoints[0].Name())
Ankurf416ac52015-01-29 13:58:24 -0800110 }
111 return ctx, shutdown
112}
Cosmos Nicolaou27dc65b2015-07-10 16:23:19 -0700113
114// TestContext returns a *contect.T suitable for use in tests with logging
115// configured to use loggler.Global(), but nothing else. In particular it does
116// not call v23.Init and hence any of the v23 functions that
117func TestContext() (*context.T, context.CancelFunc) {
118 ctx, cancel := context.RootContext()
119 return context.WithLogger(ctx, logger.Global()), cancel
120}
121
122// V23InitEmpty initializes a runtime but with no principal.
123func V23InitAnon() (*context.T, v23.Shutdown) {
124 return initWithParams(initParams{
125 CreatePrincipal: false,
126 CreateMounttable: false,
127 })
128}