Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // 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 Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 5 | package test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "flag" |
| 9 | "os" |
| 10 | "runtime" |
Asim Shankar | 2028203 | 2014-07-07 22:48:00 -0700 | [diff] [blame] | 11 | "sync" |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 12 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 13 | "v.io/v23" |
| 14 | "v.io/v23/context" |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 15 | "v.io/v23/naming" |
| 16 | "v.io/v23/options" |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 17 | "v.io/x/ref/internal/logger" |
Cosmos Nicolaou | 036c30c | 2015-03-24 10:05:20 -0700 | [diff] [blame] | 18 | "v.io/x/ref/lib/flags" |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 19 | "v.io/x/ref/services/mounttable/mounttablelib" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 20 | "v.io/x/ref/test/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
Bogdan Caprita | fca013d | 2015-01-29 15:32:34 -0800 | [diff] [blame] | 23 | const ( |
Bogdan Caprita | fca013d | 2015-01-29 15:32:34 -0800 | [diff] [blame] | 24 | TestBlessing = "test-blessing" |
| 25 | ) |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 26 | |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 27 | var once sync.Once |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 28 | var IntegrationTestsEnabled bool |
| 29 | var IntegrationTestsDebugShellOnError bool |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 30 | |
| 31 | const IntegrationTestsFlag = "v23.tests" |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 32 | const IntegrationTestsDebugShellOnErrorFlag = "v23.tests.shell-on-fail" |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 33 | |
| 34 | func init() { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 35 | 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 Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 37 | } |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 38 | |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 39 | // Init sets up state for running tests: Adjusting GOMAXPROCS, |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 40 | // configuring the logging library, setting up the random number generator |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 41 | // etc. |
| 42 | // |
Cosmos Nicolaou | e3b1932 | 2015-06-18 16:05:08 -0700 | [diff] [blame] | 43 | // Doing so requires flags to be parsed, so this function explicitly parses |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 44 | // 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. |
| 46 | func Init() { |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 47 | 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 Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 52 | } |
Cosmos Nicolaou | 036c30c | 2015-03-24 10:05:20 -0700 | [diff] [blame] | 53 | flags.SetDefaultProtocol("tcp") |
| 54 | flags.SetDefaultHostPort("127.0.0.1:0") |
| 55 | flags.SetDefaultNamespaceRoot("/127.0.0.1:8101") |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 56 | // 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 Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 61 | logger.Manager(logger.Global()).ConfigureFromFlags() |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 62 | } |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 63 | once.Do(init) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 64 | } |
Ankur | fb9255a | 2015-01-21 15:29:38 -0800 | [diff] [blame] | 65 | |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 66 | // 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 Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 71 | func V23Init() (*context.T, v23.Shutdown) { |
Matt Rosencrantz | f7bee9e | 2015-06-14 18:49:20 -0700 | [diff] [blame] | 72 | moduleProcess := os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT") != "" |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 73 | return initWithParams(initParams{ |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 74 | CreatePrincipal: !moduleProcess, |
| 75 | CreateMounttable: !moduleProcess, |
| 76 | }) |
| 77 | } |
| 78 | |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 79 | // initParams contains parameters for tests that need to control what happens during |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 80 | // init carefully. |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 81 | type initParams struct { |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 82 | CreateMounttable bool // CreateMounttable creates a new mounttable. |
| 83 | CreatePrincipal bool // CreatePrincipal creates a new principal with self-signed blessing. |
| 84 | } |
| 85 | |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 86 | // initWithParams initializes the runtime and returns a new context and shutdown function. |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 87 | // Specific aspects of initialization can be controlled via the params struct. |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 88 | func initWithParams(params initParams) (*context.T, v23.Shutdown) { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 89 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 90 | if params.CreatePrincipal { |
| 91 | var err error |
| 92 | if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil { |
| 93 | panic(err) |
| 94 | } |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 95 | } |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 96 | ns := v23.GetNamespace(ctx) |
| 97 | ns.CacheCtl(naming.DisableCache(true)) |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 98 | if params.CreateMounttable { |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 99 | disp, err := mounttablelib.NewMountTableDispatcher(ctx, "", "", "mounttable") |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 100 | if err != nil { |
| 101 | panic(err) |
| 102 | } |
Matt Rosencrantz | 53ac585 | 2015-09-04 15:14:54 -0700 | [diff] [blame] | 103 | _, s, err := v23.WithNewDispatchingServer(ctx, "", disp, options.ServesMountTable(true)) |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 104 | if err != nil { |
| 105 | panic(err) |
| 106 | } |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 107 | ns.SetRoots(s.Status().Endpoints[0].Name()) |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 108 | } |
| 109 | return ctx, shutdown |
| 110 | } |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 111 | |
| 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 |
| 115 | func 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. |
| 121 | func V23InitAnon() (*context.T, v23.Shutdown) { |
| 122 | return initWithParams(initParams{ |
| 123 | CreatePrincipal: false, |
| 124 | CreateMounttable: false, |
| 125 | }) |
| 126 | } |