blob: f01b1dfb70566dfa797a1e849c0f3c6b306e785a [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"
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -070015
Jiri Simsa337af232015-02-27 14:36:46 -080016 "v.io/x/lib/vlog"
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -070017
Cosmos Nicolaou036c30c2015-03-24 10:05:20 -070018 "v.io/x/ref/lib/flags"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070019 "v.io/x/ref/test/testutil"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070020)
21
Bogdan Capritafca013d2015-01-29 15:32:34 -080022const (
Bogdan Capritafca013d2015-01-29 15:32:34 -080023 TestBlessing = "test-blessing"
24)
Jiri Simsa870ddd62014-06-27 14:56:58 -070025
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080026var once sync.Once
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080027var IntegrationTestsEnabled bool
28var IntegrationTestsDebugShellOnError bool
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080029
30const IntegrationTestsFlag = "v23.tests"
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080031const IntegrationTestsDebugShellOnErrorFlag = "v23.tests.shell-on-fail"
Cosmos Nicolaou234642b2015-02-04 18:30:52 -080032
33func init() {
Cosmos Nicolaoucc5a4a82015-02-07 23:09:28 -080034 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 Nicolaou234642b2015-02-04 18:30:52 -080036}
Jiri Simsa870ddd62014-06-27 14:56:58 -070037
Asim Shankarc920db32014-10-16 19:18:21 -070038// 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.
45func Init() {
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080046 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 Simsa870ddd62014-06-27 14:56:58 -070051 }
Cosmos Nicolaou036c30c2015-03-24 10:05:20 -070052 flags.SetDefaultProtocol("tcp")
53 flags.SetDefaultHostPort("127.0.0.1:0")
54 flags.SetDefaultNamespaceRoot("/127.0.0.1:8101")
Cosmos Nicolaoue3391532014-11-25 19:08:32 -080055 // 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 Nicolaoua18a1eb2015-03-12 13:15:01 -070061 testutil.InitRandGenerator()
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
Ankurf416ac52015-01-29 13:58:24 -080066// InitForTest initializes a new context.T and sets a freshly created principal
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -070067// (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 Simsa6ac95222015-02-23 16:11:49 -080070func InitForTest() (*context.T, v23.Shutdown) {
71 ctx, shutdown := v23.Init()
Asim Shankar59b8b692015-03-30 01:23:36 -070072 if len(os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT")) != 0 {
Ankurf416ac52015-01-29 13:58:24 -080073 return ctx, shutdown
74 }
75 var err error
Todd Wangad492042015-04-17 15:58:40 -070076 if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil {
Ankurf416ac52015-01-29 13:58:24 -080077 panic(err)
78 }
79 return ctx, shutdown
80}