get.porter.sh/porter@v1.3.0/pkg/config/helpers.go (about) 1 package config 2 3 import ( 4 "context" 5 "log" 6 "os" 7 "path/filepath" 8 "strconv" 9 "testing" 10 11 "get.porter.sh/porter/pkg" 12 "get.porter.sh/porter/pkg/portercontext" 13 "get.porter.sh/porter/pkg/tracing" 14 ) 15 16 type TestConfig struct { 17 *Config 18 TestContext *portercontext.TestContext 19 TestSpan tracing.RootTraceLogger 20 } 21 22 // NewTestConfig initializes a configuration suitable for testing: 23 // * buffered output, 24 // * in-memory file system, 25 // * does not automatically load config from ambient environment. 26 func NewTestConfig(t *testing.T) *TestConfig { 27 cxt := portercontext.NewTestContext(t) 28 cfg := NewFor(cxt.Context) 29 cfg.Data.Verbosity = "debug" 30 cfg.DataLoader = NoopDataLoader 31 tc := &TestConfig{ 32 Config: cfg, 33 TestContext: cxt, 34 } 35 tc.SetupUnitTest() 36 return tc 37 } 38 39 // SetupUnitTest initializes the unit test filesystem with the supporting files in the PORTER_HOME directory. 40 func (c *TestConfig) SetupUnitTest() { 41 // Set up the test porter home directory 42 home := filepath.FromSlash("/home/myuser/.porter") 43 c.SetHomeDir(home) 44 45 // Fake out the porter home directory 46 var err error 47 if _, err = c.FileSystem.Create(filepath.Join(home, "porter")); err != nil { 48 log.Fatal(err) 49 } 50 if _, err = c.FileSystem.Create(filepath.Join(home, "runtimes", "porter-runtime")); err != nil { 51 log.Fatal(err) 52 } 53 54 mixinsDir := filepath.Join(home, "mixins") 55 if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "exec/exec")); err != nil { 56 log.Fatal(err) 57 } 58 if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "exec/runtimes/exec-runtime")); err != nil { 59 log.Fatal(err) 60 } 61 if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/testmixin")); err != nil { 62 log.Fatal(err) 63 } 64 if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/runtimes/testmixin-runtime")); err != nil { 65 log.Fatal(err) 66 } 67 } 68 69 // SetupIntegrationTest initializes the filesystem with the supporting files in 70 // a temp PORTER_HOME directory. 71 func (c *TestConfig) SetupIntegrationTest() (ctx context.Context, testDir string, homeDir string) { 72 ctx = context.Background() 73 testDir, homeDir = c.TestContext.UseFilesystem() 74 c.SetHomeDir(homeDir) 75 76 // Use the compiled porter binary in the test home directory, 77 // and not the go test binary that is generated when we run integration tests. 78 // This way when Porter calls back to itself, e.g. for internal plugins, 79 // it is calling the normal porter binary. 80 c.SetPorterPath(filepath.Join(homeDir, "porter")) 81 82 // Copy bin dir contents to the home directory 83 c.TestContext.AddTestDirectory(c.TestContext.FindBinDir(), homeDir, pkg.FileModeDirectory) 84 85 // Check if telemetry should be enabled for the test 86 if telemetryEnabled, _ := strconv.ParseBool(os.Getenv("PORTER_TEST_TELEMETRY_ENABLED")); telemetryEnabled { 87 // Okay someone is listening, configure the tracer 88 c.Data.Telemetry.Enabled = true 89 c.Data.Telemetry.Insecure = true 90 c.Data.Telemetry.Protocol = "grpc" 91 c.ConfigureLogging(ctx, c.NewLogConfiguration()) 92 ctx, c.TestSpan = c.StartRootSpan(ctx, c.TestContext.T.Name()) 93 } 94 95 return ctx, testDir, homeDir 96 } 97 98 func (c *TestConfig) Close() { 99 if c.TestSpan != nil { 100 c.TestSpan.Close() 101 c.TestSpan = nil 102 } 103 c.TestContext.Close() 104 }