-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
161 lines (140 loc) · 4.4 KB
/
integration_test.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Nuts node
* Copyright (C) 2022 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package main
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"nuts-foundation/nuts-monitor/client"
"nuts-foundation/nuts-monitor/client/diagnostics"
"nuts-foundation/nuts-monitor/client/network"
"nuts-foundation/nuts-monitor/config"
"nuts-foundation/nuts-monitor/test"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestStatusCodes tests if the returned errors from the API implementations are correctly translated to status codes
func TestStatusAndHealth(t *testing.T) {
ts := test.BasicTestNode(t)
os.Setenv("NUTS_NUTSNODEADDR", ts.URL())
defer os.Clearenv()
httpPort := startServer(t)
baseUrl := fmt.Sprintf("http://localhost:%d", httpPort)
type operation struct {
path string
body interface{}
}
t.Run("200s", func(t *testing.T) {
testCases := []operation{
{path: "/status"},
{path: "/health"},
}
for _, testCase := range testCases {
resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, testCase.path))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
})
}
func TestNetworkTopology(t *testing.T) {
ts := test.BasicTestNode(t)
os.Setenv("NUTS_NUTSNODEADDR", ts.URL())
defer os.Clearenv()
httpPort := startServer(t)
diagnosticsBytes, _ := json.Marshal(diagnostics.Diagnostics{
Network: diagnostics.Network{
Connections: struct {
ConnectedPeers []diagnostics.ConnectedPeer `json:"connected_peers"`
ConnectedPeersCount int `json:"connected_peers_count"`
PeerId string `json:"peer_id"`
}{
ConnectedPeersCount: 0,
PeerId: "us",
},
},
})
peers := []string{"us"}
peerDiagnosticsBytes, _ := json.Marshal(map[string]network.PeerDiagnostics{
"them": {
Peers: &peers,
},
})
ts.HandleFunc("/internal/network/v1/diagnostics/peers", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(peerDiagnosticsBytes)
})
ts.HandleFunc("/status/diagnostics", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(diagnosticsBytes)
})
baseUrl := fmt.Sprintf("http://localhost:%d", httpPort)
resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, "/web/network_topology"))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
topology := client.NetworkTopology{}
bytes, _ := io.ReadAll(resp.Body)
_ = json.Unmarshal(bytes, &topology)
assert.Equal(t, "us", topology.PeerID)
require.Len(t, topology.Peers, 2)
assert.Equal(t, "them", topology.Peers[1].PeerID)
assert.Equal(t, "us", topology.Peers[0].PeerID)
}
func startServer(t *testing.T) int {
cfg := config.LoadConfig()
e := newEchoServer(cfg, nil)
httpPort := test.FreeTCPPort()
go func() {
err := e.Start(fmt.Sprintf(":%d", httpPort))
if err != nil {
if err.Error() != "http: Server closed" {
t.Fatal(err)
}
}
}()
if !test.WaitFor(t, func() (bool, error) {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/status", httpPort))
return err == nil && resp.StatusCode == http.StatusOK, nil
}, time.Second*5, "Timeout while waiting for node to become available") {
t.Fatal("time-out")
}
t.Cleanup(func() {
err := e.Close()
if err != nil {
t.Fatal(err)
}
// wait for port to become free again
test.WaitFor(t, func() (bool, error) {
if a, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", httpPort)); err == nil {
if l, err := net.ListenTCP("tcp", a); err == nil {
l.Close()
return true, nil
}
}
return false, nil
}, 5*time.Second, "Timeout while waiting for node to shutdown")
})
return httpPort
}