2015-11-05 21:57:57 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-05-26 05:27:44 +00:00
|
|
|
package nodecfg_test
|
2015-11-05 21:57:57 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-04 04:01:02 +00:00
|
|
|
"context"
|
2015-11-05 21:57:57 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-02-02 17:06:43 +00:00
|
|
|
"runtime"
|
2015-11-05 21:57:57 +00:00
|
|
|
"testing"
|
2022-05-26 05:27:44 +00:00
|
|
|
|
2022-11-20 03:41:30 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/datadir"
|
2022-05-26 05:27:44 +00:00
|
|
|
node2 "github.com/ledgerwatch/erigon/node"
|
|
|
|
"github.com/ledgerwatch/erigon/node/nodecfg"
|
2023-05-07 06:28:15 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2015-11-05 21:57:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Tests that datadirs can be successfully created, be them manually configured
|
|
|
|
// ones or automatically generated temporary ones.
|
2022-02-22 17:39:48 +00:00
|
|
|
func TestDataDirCreation(t *testing.T) {
|
2021-06-18 03:35:11 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("fix me on win please")
|
|
|
|
}
|
2015-11-05 21:57:57 +00:00
|
|
|
// Create a temporary data dir and check that it can be used by a node
|
2021-05-18 12:13:16 +00:00
|
|
|
dir := t.TempDir()
|
2023-10-04 04:01:02 +00:00
|
|
|
node, err := node2.New(context.Background(), &nodecfg.Config{Dirs: datadir.New(dir)}, log.New())
|
2019-02-07 10:40:36 +00:00
|
|
|
if err != nil {
|
2015-11-05 21:57:57 +00:00
|
|
|
t.Fatalf("failed to create stack with existing datadir: %v", err)
|
|
|
|
}
|
2019-02-07 10:40:36 +00:00
|
|
|
if err := node.Close(); err != nil {
|
|
|
|
t.Fatalf("failed to close node: %v", err)
|
|
|
|
}
|
2015-11-05 21:57:57 +00:00
|
|
|
// Generate a long non-existing datadir path and check that it gets created by a node
|
|
|
|
dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f")
|
2023-10-04 04:01:02 +00:00
|
|
|
node, err = node2.New(context.Background(), &nodecfg.Config{Dirs: datadir.New(dir)}, log.New())
|
2019-02-07 10:40:36 +00:00
|
|
|
if err != nil {
|
2015-11-05 21:57:57 +00:00
|
|
|
t.Fatalf("failed to create stack with creatable datadir: %v", err)
|
|
|
|
}
|
2019-02-07 10:40:36 +00:00
|
|
|
if err := node.Close(); err != nil {
|
|
|
|
t.Fatalf("failed to close node: %v", err)
|
|
|
|
}
|
2015-11-05 21:57:57 +00:00
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
t.Fatalf("freshly created datadir not accessible: %v", err)
|
|
|
|
}
|
|
|
|
// Verify that an impossible datadir fails creation
|
2022-04-23 14:43:00 +00:00
|
|
|
file, err := os.CreateTemp("", "")
|
2015-11-05 21:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temporary file: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:06:43 +00:00
|
|
|
// Tests that IPC paths are correctly resolved to valid endpoints of different
|
|
|
|
// platforms.
|
2016-02-09 11:24:42 +00:00
|
|
|
func TestIPCPathResolution(t *testing.T) {
|
2016-02-02 17:06:43 +00:00
|
|
|
var tests = []struct {
|
|
|
|
DataDir string
|
2016-02-09 11:24:42 +00:00
|
|
|
IPCPath string
|
2016-02-02 17:06:43 +00:00
|
|
|
Windows bool
|
|
|
|
Endpoint string
|
|
|
|
}{
|
|
|
|
{"", "", false, ""},
|
|
|
|
{"data", "", false, ""},
|
|
|
|
{"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
|
|
|
|
{"data", "geth.ipc", false, "data/geth.ipc"},
|
|
|
|
{"data", "./geth.ipc", false, "./geth.ipc"},
|
|
|
|
{"data", "/geth.ipc", false, "/geth.ipc"},
|
|
|
|
{"", "", true, ``},
|
|
|
|
{"data", "", true, ``},
|
|
|
|
{"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
|
|
|
|
{"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
|
|
|
|
{"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
// Only run when platform/test match
|
|
|
|
if (runtime.GOOS == "windows") == test.Windows {
|
2022-06-07 04:54:04 +00:00
|
|
|
if endpoint := (&nodecfg.Config{Dirs: datadir.New(test.DataDir), IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
|
2016-02-02 17:06:43 +00:00
|
|
|
t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|