@@ -2,7 +2,12 @@ package setup
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
6
+ "path/filepath"
7
+ "testing"
8
+ "time"
5
9
10
+ "github.com/bacalhau-project/bacalhau/pkg/config"
6
11
"github.com/bacalhau-project/bacalhau/pkg/config/types"
7
12
"github.com/bacalhau-project/bacalhau/pkg/logger"
8
13
"github.com/bacalhau-project/bacalhau/pkg/repo/migrations"
@@ -51,3 +56,59 @@ func SetupBacalhauRepo(cfg types.Bacalhau) (*repo.FsRepo, error) {
51
56
}
52
57
return fsRepo , nil
53
58
}
59
+
60
+ func SetupBacalhauRepoForTesting (t testing.TB ) (* repo.FsRepo , types.Bacalhau ) {
61
+ // create a temporary dir to serve as bacalhau repo whose name includes the current time to avoid collisions with
62
+ /// other tests
63
+ tmpDir , err := os .MkdirTemp ("" , "" )
64
+ if err != nil {
65
+ t .Fatal (err )
66
+ }
67
+ path := filepath .Join (tmpDir , fmt .Sprint (time .Now ().UnixNano ()))
68
+
69
+ // disable update checks in testing.
70
+ t .Setenv (config .KeyAsEnvVar (types .UpdateConfigIntervalKey ), "0" )
71
+ // don't send analytics data during testing
72
+ t .Setenv (config .KeyAsEnvVar (types .DisableAnalyticsKey ), "true" )
73
+ cfgValues := map [string ]any {
74
+ types .DataDirKey : path ,
75
+ // callers of this method currently assume it creates an orchestrator node.
76
+ types .OrchestratorEnabledKey : true ,
77
+ }
78
+
79
+ // the BACALHAU_NODE_IPFS_CONNECT env var is only bound if it's corresponding flags are registered.
80
+ // This is because viper cannot bind its value to any existing keys via viper.AutomaticEnv() since the
81
+ // environment variable doesn't map to a key in the config, so we add special handling here until we move away
82
+ // from this flag to the dedicated flags like "BACALHAU_PUBLISHER_IPFS_ENDPOINT",
83
+ // "BACALHAU_INPUTSOURCES_IPFS_ENDPOINT", etc which have a direct mapping to the config key based on their name.
84
+ if connect := os .Getenv ("BACALHAU_NODE_IPFS_CONNECT" ); connect != "" {
85
+ cfgValues [types .PublishersTypesIPFSEndpointKey ] = connect
86
+ cfgValues [types .ResultDownloadersTypesIPFSEndpointKey ] = connect
87
+ cfgValues [types .InputSourcesTypesIPFSEndpointKey ] = connect
88
+ }
89
+
90
+ // init a config with this viper instance using the local configuration as default
91
+ c , err := config .New (config .WithValues (cfgValues ))
92
+ if err != nil {
93
+ t .Fatal (err )
94
+ }
95
+
96
+ t .Logf ("creating repo for testing at: %s" , path )
97
+ t .Cleanup (func () {
98
+ // This may fail to clean up due to nats store, log an error, don't fail testing
99
+ if err := os .RemoveAll (tmpDir ); err != nil {
100
+ t .Logf ("failed to clean up repo at: %s: %s" , path , err )
101
+ }
102
+ })
103
+ var cfg types.Bacalhau
104
+ if err := c .Unmarshal (& cfg ); err != nil {
105
+ t .Fatal (err )
106
+ }
107
+ // create the repo used for testing
108
+ fsRepo , err := SetupBacalhauRepo (cfg )
109
+ if err != nil {
110
+ t .Fatal (err )
111
+ }
112
+
113
+ return fsRepo , cfg
114
+ }
0 commit comments