summaryrefslogtreecommitdiff
path: root/tools/network/telemetryURIUpdateService_test.go
blob: 1014d28df36f011bef600ec7fdbb6fafef096c7a (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
// Copyright (C) 2019-2023 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 network

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

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

type telemetryURIUpdaterTest struct {
	telemetryURIUpdater
	readFromSRVResults map[string][]string
}

func (t *telemetryURIUpdaterTest) readFromSRV(protocol string, bootstrapID string) (addrs []string, err error) {
	if addr, ok := t.readFromSRVResults[protocol+bootstrapID]; ok {
		return addr, nil
	}
	fmt.Printf("no result for %s %s\n", protocol, bootstrapID)
	return nil, fmt.Errorf("no cached results")
}

func makeTelemetryURIUpdaterTest(genesisNetwork protocol.NetworkID) *telemetryURIUpdaterTest {
	t := &telemetryURIUpdaterTest{
		telemetryURIUpdater: telemetryURIUpdater{
			cfg:            config.GetDefaultLocal(),
			log:            logging.Base(),
			genesisNetwork: genesisNetwork,
		},
		readFromSRVResults: make(map[string][]string),
	}
	t.srvReader = t
	return t
}

func (t *telemetryURIUpdaterTest) add(protocol, bootstrap string, addrs []string) {
	t.readFromSRVResults[protocol+bootstrap] = addrs
}

func TestTelemetryURILookup(t *testing.T) {
	partitiontest.PartitionTest(t)

	// trivial success case.
	uriUpdater := makeTelemetryURIUpdaterTest(config.Devnet)
	uriUpdater.add("tcp", "devnet.algodev.network", []string{"myhost:4160"})
	uri := uriUpdater.lookupTelemetryURL()
	require.NotNil(t, uri)
	require.Equal(t, "http://myhost:4160", uri.String())

	// check https prefixing
	uriUpdater = makeTelemetryURIUpdaterTest(config.Devnet)
	uriUpdater.add("tcp", "devnet.algodev.network", []string{"https://myhost:4160"})
	uri = uriUpdater.lookupTelemetryURL()
	require.NotNil(t, uri)
	require.Equal(t, "https://myhost:4160", uri.String())

	// check https priority
	uriUpdater = makeTelemetryURIUpdaterTest(config.Devnet)
	uriUpdater.add("tcp", "devnet.algodev.network", []string{"myhost2:4160"})
	uriUpdater.add("tls", "devnet.algodev.network", []string{"myhost1:4160"})
	uri = uriUpdater.lookupTelemetryURL()
	require.NotNil(t, uri)
	require.Equal(t, "https://myhost1:4160", uri.String())

	// check fallback
	uriUpdater = makeTelemetryURIUpdaterTest(config.Devnet)
	uriUpdater.add("tcp", "default.algodev.network", []string{"fallbackhost:8123"})
	uri = uriUpdater.lookupTelemetryURL()
	require.NotNil(t, uri)
	require.Equal(t, "http://fallbackhost:8123", uri.String())
}