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