summaryrefslogtreecommitdiff
path: root/netdeploy/network_test.go
blob: e18217b7814007837549d350eb7c39de2c52409f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package netdeploy

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/gen"
	"github.com/algorand/go-algorand/test/partitiontest"
)

func TestSaveNetworkCfg(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	a := require.New(t)

	cfg := NetworkCfg{
		Name:      "testName",
		RelayDirs: []string{"testPND"},
		Template: NetworkTemplate{
			Genesis: gen.DefaultGenesis,
		},
	}

	tmpFolder := t.TempDir()
	cfgFile := filepath.Join(tmpFolder, configFileName)
	err := saveNetworkCfg(cfg, cfgFile)
	a.Nil(err)
	cfg1, err := loadNetworkCfg(cfgFile)
	a.NoError(err)
	a.Equal(cfg, cfg1)
}

func TestSaveConsensus(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	a := require.New(t)

	tmpFolder := t.TempDir()
	relayDir := filepath.Join(tmpFolder, "testRelayDir")
	err := os.MkdirAll(relayDir, 0744)
	a.NoError(err)
	nodeDir := filepath.Join(tmpFolder, "testNodeDir")
	err = os.MkdirAll(nodeDir, 0744)
	a.NoError(err)

	net := Network{
		cfg: NetworkCfg{
			Name:      "testName",
			RelayDirs: []string{relayDir},
			Template: NetworkTemplate{
				Genesis: gen.DefaultGenesis,
			},
		},
		nodeDirs: map[string]string{
			"node1": nodeDir,
		},
	}

	consensusRelayFilePath := filepath.Join(relayDir, config.ConfigurableConsensusProtocolsFilename)
	consensusNodeFilePath := filepath.Join(relayDir, config.ConfigurableConsensusProtocolsFilename)
	err = net.SetConsensus(tmpFolder, nil)
	a.NoError(err)
	_, err = os.Open(consensusRelayFilePath)
	a.True(os.IsNotExist(err), "%s should not have been created", config.ConfigurableConsensusProtocolsFilename)
	_, err = os.Open(consensusNodeFilePath)
	a.True(os.IsNotExist(err), "%s should not have been created", config.ConfigurableConsensusProtocolsFilename)

	err = net.SetConsensus(tmpFolder, config.Consensus)
	a.NoError(err)
	f, err := os.Open(consensusRelayFilePath)
	a.False(os.IsNotExist(err), "%s should have been created", config.ConfigurableConsensusProtocolsFilename)
	f.Close()
	f, err = os.Open(consensusNodeFilePath)
	a.False(os.IsNotExist(err), "%s should have been created", config.ConfigurableConsensusProtocolsFilename)
	f.Close()

	// now that the file exists, try to see if another call to SetConsensus would delete it.
	err = net.SetConsensus(tmpFolder, nil)
	a.NoError(err)
	_, err = os.Open(consensusRelayFilePath)
	a.True(os.IsNotExist(err), "%s should have been deleted", config.ConfigurableConsensusProtocolsFilename)
	_, err = os.Open(consensusNodeFilePath)
	a.True(os.IsNotExist(err), "%s should have been deleted", config.ConfigurableConsensusProtocolsFilename)
}