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" |
Cosmos Nicolaou | a18a1eb | 2015-03-12 13:15:01 -0700 | [diff] [blame] | 15 | |
Jiri Simsa | 337af23 | 2015-02-27 14:36:46 -0800 | [diff] [blame] | 16 | "v.io/x/lib/vlog" |
Cosmos Nicolaou | a18a1eb | 2015-03-12 13:15:01 -0700 | [diff] [blame] | 17 | |
Cosmos Nicolaou | 036c30c | 2015-03-24 10:05:20 -0700 | [diff] [blame] | 18 | "v.io/x/ref/lib/flags" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 19 | "v.io/x/ref/test/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
Bogdan Caprita | fca013d | 2015-01-29 15:32:34 -0800 | [diff] [blame] | 22 | const ( |
Bogdan Caprita | fca013d | 2015-01-29 15:32:34 -0800 | [diff] [blame] | 23 | TestBlessing = "test-blessing" |
| 24 | ) |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 25 | |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 26 | var once sync.Once |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 27 | var IntegrationTestsEnabled bool |
| 28 | var IntegrationTestsDebugShellOnError bool |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 29 | |
| 30 | const IntegrationTestsFlag = "v23.tests" |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 31 | const IntegrationTestsDebugShellOnErrorFlag = "v23.tests.shell-on-fail" |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 32 | |
| 33 | func init() { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 34 | flag.BoolVar(&IntegrationTestsEnabled, IntegrationTestsFlag, false, "Run integration tests.") |
| 35 | 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] | 36 | } |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 37 | |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 38 | // Init sets up state for running tests: Adjusting GOMAXPROCS, |
| 39 | // configuring the vlog logging library, setting up the random number generator |
| 40 | // etc. |
| 41 | // |
| 42 | // Doing so requires flags to be parse, so this function explicitly parses |
| 43 | // flags. Thus, it is NOT a good idea to call this from the init() function |
| 44 | // of any module except "main" or _test.go files. |
| 45 | func Init() { |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 46 | init := func() { |
| 47 | if os.Getenv("GOMAXPROCS") == "" { |
| 48 | // Set the number of logical processors to the number of CPUs, |
| 49 | // if GOMAXPROCS is not set in the environment. |
| 50 | runtime.GOMAXPROCS(runtime.NumCPU()) |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 51 | } |
Cosmos Nicolaou | 036c30c | 2015-03-24 10:05:20 -0700 | [diff] [blame] | 52 | flags.SetDefaultProtocol("tcp") |
| 53 | flags.SetDefaultHostPort("127.0.0.1:0") |
| 54 | flags.SetDefaultNamespaceRoot("/127.0.0.1:8101") |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 55 | // At this point all of the flags that we're going to use for |
| 56 | // tests must be defined. |
| 57 | // This will be the case if this is called from the init() |
| 58 | // function of a _test.go file. |
| 59 | flag.Parse() |
| 60 | vlog.ConfigureLibraryLoggerFromFlags() |
Cosmos Nicolaou | a18a1eb | 2015-03-12 13:15:01 -0700 | [diff] [blame] | 61 | testutil.InitRandGenerator() |
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 | |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 66 | // InitForTest initializes a new context.T and sets a freshly created principal |
Cosmos Nicolaou | a18a1eb | 2015-03-12 13:15:01 -0700 | [diff] [blame] | 67 | // (with a single self-signed blessing) on it. The principal setting step is |
| 68 | // skipped if this function is invoked from a process run using the modules |
| 69 | // package. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 70 | func InitForTest() (*context.T, v23.Shutdown) { |
| 71 | ctx, shutdown := v23.Init() |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 72 | if len(os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT")) != 0 { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 73 | return ctx, shutdown |
| 74 | } |
| 75 | var err error |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 76 | if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 77 | panic(err) |
| 78 | } |
| 79 | return ctx, shutdown |
| 80 | } |