Skip to content

Commit 2e57c66

Browse files
committed
removrt removing repo setup for testing
1 parent cf0b5b7 commit 2e57c66

File tree

15 files changed

+142
-55
lines changed

15 files changed

+142
-55
lines changed

cmd/cli/job/get_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (s *GetSuite) SetupTest() {
4444
}
4545

4646
func (s *GetSuite) TestGetSingleFileFromOutputBadChoice() {
47-
testutils.MustHaveIPFS(s.T())
47+
testutils.MustHaveIPFS(s.T(), s.Config)
4848
args := s.getDockerRunArgs([]string{
4949
"--wait",
5050
"--publisher", "ipfs",
@@ -62,7 +62,7 @@ func (s *GetSuite) TestGetSingleFileFromOutputBadChoice() {
6262
}
6363

6464
func (s *GetSuite) TestGetSingleFileFromOutput() {
65-
testutils.MustHaveIPFS(s.T())
65+
testutils.MustHaveIPFS(s.T(), s.Config)
6666
tempDir, cleanup := setupTempWorkingDir(s.T())
6767
defer cleanup()
6868

@@ -86,7 +86,7 @@ func (s *GetSuite) TestGetSingleFileFromOutput() {
8686
}
8787

8888
func (s *GetSuite) TestGetSingleNestedFileFromOutput() {
89-
testutils.MustHaveIPFS(s.T())
89+
testutils.MustHaveIPFS(s.T(), s.Config)
9090
tempDir, cleanup := setupTempWorkingDir(s.T())
9191
defer cleanup()
9292

cmd/cli/node/action_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
cmdtesting "github.com/bacalhau-project/bacalhau/cmd/testing"
1212
"github.com/bacalhau-project/bacalhau/pkg/logger"
13+
"github.com/bacalhau-project/bacalhau/pkg/setup"
1314
)
1415

1516
type NodeActionSuite struct {
@@ -22,6 +23,7 @@ func TestNodeActionSuite(t *testing.T) {
2223

2324
func (s *NodeActionSuite) SetupSuite() {
2425
logger.ConfigureTestLogging(s.T())
26+
setup.SetupBacalhauRepoForTesting(s.T())
2527
}
2628

2729
func (s *NodeActionSuite) TestListNodes() {

cmd/cli/serve/serve_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import (
1515
"github.com/stretchr/testify/suite"
1616
"golang.org/x/sync/errgroup"
1717

18-
"github.com/bacalhau-project/bacalhau/pkg/docker"
19-
"github.com/bacalhau-project/bacalhau/pkg/lib/marshaller"
20-
"github.com/bacalhau-project/bacalhau/pkg/lib/network"
18+
cfgtypes "github.com/bacalhau-project/bacalhau/pkg/config/types"
2119
"github.com/bacalhau-project/bacalhau/pkg/models"
2220
"github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels"
2321
clientv2 "github.com/bacalhau-project/bacalhau/pkg/publicapi/client/v2"
2422
apitest "github.com/bacalhau-project/bacalhau/pkg/publicapi/test"
23+
"github.com/bacalhau-project/bacalhau/pkg/setup"
24+
25+
"github.com/bacalhau-project/bacalhau/pkg/docker"
26+
"github.com/bacalhau-project/bacalhau/pkg/lib/marshaller"
27+
"github.com/bacalhau-project/bacalhau/pkg/lib/network"
2528

2629
cmd2 "github.com/bacalhau-project/bacalhau/cmd/cli"
2730
"github.com/bacalhau-project/bacalhau/pkg/logger"
@@ -40,7 +43,9 @@ type ServeSuite struct {
4043
out, err strings.Builder
4144

4245
ctx context.Context
46+
repoPath string
4347
protocol string
48+
config cfgtypes.Bacalhau
4449
}
4550

4651
func TestServeSuite(t *testing.T) {
@@ -49,7 +54,12 @@ func TestServeSuite(t *testing.T) {
4954

5055
func (s *ServeSuite) SetupTest() {
5156
logger.ConfigureTestLogging(s.T())
57+
fsRepo, c := setup.SetupBacalhauRepoForTesting(s.T())
58+
repoPath, err := fsRepo.Path()
59+
s.Require().NoError(err)
60+
s.repoPath = repoPath
5261
s.protocol = "http"
62+
s.config = c
5363

5464
var cancel context.CancelFunc
5565
s.ctx, cancel = context.WithTimeout(context.Background(), maxTestTime)
@@ -84,7 +94,7 @@ func (s *ServeSuite) serve(extraArgs ...string) (uint16, error) {
8494

8595
args := []string{
8696
"serve",
87-
"--data-dir", s.T().TempDir(),
97+
"--data-dir", s.repoPath,
8898
"--api-port", fmt.Sprint(port),
8999
}
90100
args = append(args, extraArgs...)

cmd/util/version_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/bacalhau-project/bacalhau/pkg/models"
1313

1414
"github.com/bacalhau-project/bacalhau/pkg/logger"
15+
"github.com/bacalhau-project/bacalhau/pkg/setup"
1516
"github.com/bacalhau-project/bacalhau/pkg/version"
1617
)
1718

@@ -32,6 +33,8 @@ func (s *UtilsSuite) SetupTest() {
3233
}
3334

3435
func (s *UtilsSuite) TestVersionCheck() {
36+
setup.SetupBacalhauRepoForTesting(s.T())
37+
3538
// OK: Normal operation
3639
err := EnsureValidVersion(context.TODO(), &models.BuildVersionInfo{
3740
GitVersion: "v1.2.3",

pkg/config/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ func New(opts ...Option) (*Config, error) {
144144
for _, flag := range flags {
145145
// only if the flag has been set do we want to bind to it, this allows multiple flags
146146
// to bind to the same config key.
147+
log.Warn().Msgf("Binding flag %+v to config key %q", flag, name)
148+
if flag == nil {
149+
log.Error().Msgf("flag %q is nil", name)
150+
}
147151
if flag.Changed {
148152
switch name {
149153
case "ipfs.connect.deprecated":

pkg/config/defaults.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -178,26 +178,15 @@ var testOverrides = types.Bacalhau{
178178

179179
// NewTestConfig returns a new configuration with the default values for testing.
180180
func NewTestConfig() (types.Bacalhau, error) {
181-
defaults, err := Default.MergeNew(testOverrides)
181+
cfg, err := Default.MergeNew(testOverrides)
182182
if err != nil {
183183
return types.Bacalhau{}, err
184184
}
185185

186186
// Set the data directory to a temporary directory
187-
defaults.DataDir = os.TempDir()
187+
cfg.DataDir = os.TempDir()
188188

189-
// now construct the config using the defaults
190-
// so far no value in doing this expect trying to exercise the production code path
191-
cfg, err := New(WithDefault(defaults))
192-
if err != nil {
193-
return types.Bacalhau{}, err
194-
}
195-
196-
var bacalhauConfig types.Bacalhau
197-
if err = cfg.Unmarshal(&bacalhauConfig); err != nil {
198-
return types.Bacalhau{}, err
199-
}
200-
return bacalhauConfig, nil
189+
return cfg, nil
201190
}
202191

203192
func NewTestConfigWithOverrides(overrides types.Bacalhau) (types.Bacalhau, error) {

pkg/downloader/download_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/bacalhau-project/bacalhau/pkg/models"
2020
s3helper "github.com/bacalhau-project/bacalhau/pkg/s3"
2121
s3test "github.com/bacalhau-project/bacalhau/pkg/s3/test"
22+
"github.com/bacalhau-project/bacalhau/pkg/setup"
2223
ipfssource "github.com/bacalhau-project/bacalhau/pkg/storage/ipfs"
2324
"github.com/bacalhau-project/bacalhau/pkg/system"
2425
testutils "github.com/bacalhau-project/bacalhau/pkg/test/utils"
@@ -43,12 +44,13 @@ type DownloaderSuite struct {
4344

4445
func (ds *DownloaderSuite) SetupSuite() {
4546
logger.ConfigureTestLogging(ds.T())
47+
_, cfg := setup.SetupBacalhauRepoForTesting(ds.T())
4648

47-
endpoint := testutils.MustHaveIPFS(ds.T())
48-
var err error
49-
ds.ipfsClient, err = ipfs.NewClient(context.Background(), endpoint)
50-
require.NoError(ds.T(), err)
51-
49+
if testutils.IsIPFSEnabled(cfg.ResultDownloaders.Types.IPFS.Endpoint) {
50+
var err error
51+
ds.ipfsClient, err = ipfs.NewClient(context.Background(), cfg.ResultDownloaders.Types.IPFS.Endpoint)
52+
require.NoError(ds.T(), err)
53+
}
5254
ds.HelperSuite.SetupSuite()
5355
ds.s3Signer = s3helper.NewResultSigner(s3helper.ResultSignerParams{
5456
ClientProvider: ds.ClientProvider,

pkg/downloader/ipfs/downloader_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/bacalhau-project/bacalhau/pkg/downloader"
1515
"github.com/bacalhau-project/bacalhau/pkg/ipfs"
1616
"github.com/bacalhau-project/bacalhau/pkg/models"
17+
"github.com/bacalhau-project/bacalhau/pkg/setup"
1718
ipfssource "github.com/bacalhau-project/bacalhau/pkg/storage/ipfs"
1819
testutils "github.com/bacalhau-project/bacalhau/pkg/test/utils"
1920
)
@@ -30,8 +31,10 @@ func TestIPFSDownload(t *testing.T) {
3031
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
3132
defer cancel()
3233

33-
endpoint := testutils.MustHaveIPFS(t)
34-
client, err := ipfs.NewClient(context.Background(), endpoint)
34+
_, cfg := setup.SetupBacalhauRepoForTesting(t)
35+
testutils.MustHaveIPFS(t, cfg)
36+
37+
client, err := ipfs.NewClient(context.Background(), cfg.ResultDownloaders.Types.IPFS.Endpoint)
3538
require.NoError(t, err)
3639

3740
text := randomText(t)

pkg/ipfs/client.go

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"github.com/bacalhau-project/bacalhau/pkg/util/multiaddresses"
2727
)
2828

29-
const IPFS_ENDPOINT_ENV = "BACALHAU_NODE_IPFS_CONNECT"
30-
3129
// Client is a front-end for an ipfs node's API endpoints
3230
type Client struct {
3331
API icore.CoreAPI

pkg/setup/setup.go

+61
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package setup
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
"time"
59

10+
"github.com/bacalhau-project/bacalhau/pkg/config"
611
"github.com/bacalhau-project/bacalhau/pkg/config/types"
712
"github.com/bacalhau-project/bacalhau/pkg/logger"
813
"github.com/bacalhau-project/bacalhau/pkg/repo/migrations"
@@ -51,3 +56,59 @@ func SetupBacalhauRepo(cfg types.Bacalhau) (*repo.FsRepo, error) {
5156
}
5257
return fsRepo, nil
5358
}
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+
}

pkg/storage/ipfs/storage_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/bacalhau-project/bacalhau/pkg/ipfs"
1414
"github.com/bacalhau-project/bacalhau/pkg/logger"
1515
"github.com/bacalhau-project/bacalhau/pkg/models"
16+
"github.com/bacalhau-project/bacalhau/pkg/setup"
1617
testutils "github.com/bacalhau-project/bacalhau/pkg/test/utils"
1718
)
1819

@@ -31,11 +32,11 @@ func TestStorageSuite(t *testing.T) {
3132

3233
func (s *StorageSuite) SetupSuite() {
3334
logger.ConfigureTestLogging(s.T())
34-
35-
endpoint := testutils.MustHaveIPFS(s.T())
35+
_, cfg := setup.SetupBacalhauRepoForTesting(s.T())
36+
testutils.MustHaveIPFS(s.T(), cfg)
3637

3738
var err error
38-
s.ipfsClient, err = ipfs.NewClient(context.Background(), endpoint)
39+
s.ipfsClient, err = ipfs.NewClient(context.Background(), cfg.ResultDownloaders.Types.IPFS.Endpoint)
3940
s.storage, err = NewStorage(*s.ipfsClient, 5*time.Second)
4041
s.Require().NoError(err)
4142
}

pkg/storage/parallel_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"github.com/stretchr/testify/require"
1313
"github.com/stretchr/testify/suite"
1414

15-
"github.com/bacalhau-project/bacalhau/pkg/config"
1615
"github.com/bacalhau-project/bacalhau/pkg/config/types"
1716
executor_util "github.com/bacalhau-project/bacalhau/pkg/executor/util"
1817
"github.com/bacalhau-project/bacalhau/pkg/ipfs"
1918
"github.com/bacalhau-project/bacalhau/pkg/lib/provider"
2019
_ "github.com/bacalhau-project/bacalhau/pkg/logger"
2120
"github.com/bacalhau-project/bacalhau/pkg/models"
21+
"github.com/bacalhau-project/bacalhau/pkg/setup"
2222
"github.com/bacalhau-project/bacalhau/pkg/storage"
2323
testutils "github.com/bacalhau-project/bacalhau/pkg/test/utils"
2424
)
@@ -34,19 +34,19 @@ func TestParallelStorageSuite(t *testing.T) {
3434
}
3535

3636
func (s *ParallelStorageSuite) SetupSuite() {
37-
var err error
38-
s.cfg, err = config.NewTestConfig()
39-
s.Require().NoError(err)
37+
_, cfg := setup.SetupBacalhauRepoForTesting(s.T())
38+
s.cfg = cfg
4039

41-
s.provider, err = executor_util.NewStandardStorageProvider(s.cfg)
40+
var err error
41+
s.provider, err = executor_util.NewStandardStorageProvider(cfg)
4242
s.Require().NoError(err)
4343
}
4444

4545
func (s *ParallelStorageSuite) TestIPFSCleanup() {
46-
endpoint := testutils.MustHaveIPFS(s.T())
46+
testutils.MustHaveIPFS(s.T(), s.cfg)
4747

4848
ctx := context.Background()
49-
client, err := ipfs.NewClient(ctx, endpoint)
49+
client, err := ipfs.NewClient(ctx, s.cfg.InputSources.Types.IPFS.Endpoint)
5050
require.NoError(s.T(), err)
5151

5252
cid, err := client.Put(ctx, "../../testdata/grep_file.txt")

pkg/test/ipfs/ipfs_host_storage_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212

1313
"github.com/stretchr/testify/suite"
1414

15-
"github.com/bacalhau-project/bacalhau/pkg/config"
1615
"github.com/bacalhau-project/bacalhau/pkg/config/types"
1716
"github.com/bacalhau-project/bacalhau/pkg/ipfs"
1817
"github.com/bacalhau-project/bacalhau/pkg/logger"
1918
"github.com/bacalhau-project/bacalhau/pkg/models"
19+
"github.com/bacalhau-project/bacalhau/pkg/setup"
2020
"github.com/bacalhau-project/bacalhau/pkg/storage"
2121
ipfs_storage "github.com/bacalhau-project/bacalhau/pkg/storage/ipfs"
2222
testutils "github.com/bacalhau-project/bacalhau/pkg/test/utils"
@@ -37,12 +37,11 @@ func TestIPFSHostStorageSuite(t *testing.T) {
3737
// Before each test
3838
func (suite *IPFSHostStorageSuite) SetupTest() {
3939
logger.ConfigureTestLogging(suite.T())
40-
var err error
41-
suite.Config, err = config.NewTestConfig()
42-
suite.Require().NoError(err)
40+
_, suite.Config = setup.SetupBacalhauRepoForTesting(suite.T())
41+
testutils.MustHaveIPFS(suite.T(), suite.Config)
4342

44-
endpoint := testutils.MustHaveIPFS(suite.T())
45-
suite.client, err = ipfs.NewClient(context.Background(), endpoint)
43+
var err error
44+
suite.client, err = ipfs.NewClient(context.Background(), suite.Config.InputSources.Types.IPFS.Endpoint)
4645
suite.Require().NoError(err)
4746

4847
}

pkg/test/teststack/stack.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ func Setup(
3030
cm.Cleanup(ctx)
3131
})
3232

33-
options := []devstack.ConfigOption{devstack.WithBasePath(t.TempDir())}
33+
// default options
34+
options := []devstack.ConfigOption{
35+
devstack.WithNumberOfHybridNodes(0),
36+
devstack.WithNumberOfRequesterOnlyNodes(0),
37+
devstack.WithNumberOfComputeOnlyNodes(0),
38+
devstack.WithBasePath(t.TempDir()),
39+
}
40+
41+
// append custom options
3442
options = append(options, opts...)
3543
stack, err := devstack.Setup(ctx, cm, options...)
3644
if err != nil {

0 commit comments

Comments
 (0)