-
Notifications
You must be signed in to change notification settings - Fork 16
/
main_test.go
260 lines (228 loc) · 8.48 KB
/
main_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* 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 (
"context"
"errors"
"fmt"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"github.com/nuts-foundation/nuts-node/auth"
"github.com/nuts-foundation/nuts-node/cmd"
"github.com/nuts-foundation/nuts-node/core"
"github.com/nuts-foundation/nuts-node/crypto"
"github.com/nuts-foundation/nuts-node/events"
httpEngine "github.com/nuts-foundation/nuts-node/http"
"github.com/nuts-foundation/nuts-node/network"
"github.com/nuts-foundation/nuts-node/storage"
"github.com/nuts-foundation/nuts-node/test"
"github.com/nuts-foundation/nuts-node/test/pki"
v1 "github.com/nuts-foundation/nuts-node/vdr/api/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"os"
"path/filepath"
"syscall"
"testing"
"time"
)
// Test_ServerLifecycle tests the lifecycle of the Nuts node:
// - It starts the Nuts node
// - Waits for the /status endpoint to return HTTP 200, indicating it started properly
// - Sends SIGINT signal
// - Waits for the main function to return
// This test was introduced because the shutdown sequence was never called, due to kill signals not being handled.
func Test_ServerLifecycle(t *testing.T) {
testDirectory := t.TempDir()
runningCtx, nodeStoppedCallback := context.WithCancel(context.Background())
serverConfig, moduleConfig := getIntegrationTestConfig(t, testDirectory)
startCtx := startServer(testDirectory, nodeStoppedCallback, serverConfig, moduleConfig)
// Wait for the Nuts node to start
<-startCtx.Done()
if errors.Is(startCtx.Err(), context.Canceled) {
t.Log("Process successfully started, sending KILL signal")
stopNode(t, runningCtx)
} else {
t.Fatalf("Process didn't start before the time-out expired: %v", startCtx.Err())
}
}
// Test_LoadExistingDAG tests the lifecycle and persistence of the DAG:
// - It starts the Nuts node
// - It creates and then updates a DID document
// - It stops and then starts the Nuts node again
// - It checks whether it can read the DID document from the DAG
// This test was introduced because we repeatedly encountered a bug where a new DAG could be created and written to,
// but (DAG) verification failed when starting the node with an existing DAG.
// It also tests that file resources (that are locked) are properly freed by the shutdown sequence,
// because it uses the same files when restarting again (without exiting the main process).
func Test_LoadExistingDAG(t *testing.T) {
testDirectory := t.TempDir()
// Start Nuts node
runningCtx, nodeStoppedCallback := context.WithCancel(context.Background())
serverConfig, moduleConfig := getIntegrationTestConfig(t, testDirectory)
startCtx := startServer(testDirectory, nodeStoppedCallback, serverConfig, moduleConfig)
<-startCtx.Done()
if !errors.Is(startCtx.Err(), context.Canceled) {
t.Fatalf("Process didn't start before the time-out expired: %v", startCtx.Err())
}
defer stopNode(t, runningCtx)
// Create and update a DID document
vdrClient := createVDRClient(moduleConfig.HTTP.Internal.Address)
didDocument, err := vdrClient.Create(v1.DIDCreateRequest{})
require.NoError(t, err)
_, err = vdrClient.AddNewVerificationMethod(didDocument.ID.String())
require.NoError(t, err)
// Now stop node, and start it again
stopNode(t, runningCtx)
_, nodeStoppedCallback = context.WithCancel(context.Background())
// Make sure we get "fresh" ports since the OS might not immediately free closed sockets
serverConfig, moduleConfig = getIntegrationTestConfig(t, testDirectory)
startCtx = startServer(testDirectory, nodeStoppedCallback, serverConfig, moduleConfig)
<-startCtx.Done()
if !errors.Is(startCtx.Err(), context.Canceled) {
t.Fatalf("Process didn't start before the time-out expired: %v", startCtx.Err())
}
// Assert we can read the DID document
vdrClient = createVDRClient(moduleConfig.HTTP.Internal.Address)
doc, _, err := vdrClient.Get(didDocument.ID.String())
require.NoError(t, err)
assert.NotNil(t, doc)
}
func createVDRClient(address string) v1.HTTPClient {
vdrClient := v1.HTTPClient{
ClientConfig: core.ClientConfig{
Address: "http://" + address,
Timeout: 5 * time.Second,
},
}
return vdrClient
}
func stopNode(t *testing.T, ctx context.Context) {
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
<-ctx.Done()
t.Log("Nuts node shut down successfully.")
}
func startServer(testDirectory string, exitCallback func(), serverConfig core.ServerConfig, moduleConfig ModuleConfig) context.Context {
// Create YAML file of server config + additional configs. Write it to disk and pass it to the server.
koanfInstance := koanf.New(".")
yamlParser := yaml.Parser()
err := koanfInstance.Load(structs.ProviderWithDelim(serverConfig, "koanf", "."), nil)
if err != nil {
panic(err)
}
err = koanfInstance.Load(structs.ProviderWithDelim(moduleConfig, "koanf", "."), nil)
if err != nil {
panic(err)
}
if serverConfig.Strictmode {
type modCfg struct {
Storage storage.Config `koanf:"storage"`
}
storageConfig := modCfg{Storage: storage.DefaultConfig()}
storageConfig.Storage.SQL.ConnectionString = fmt.Sprintf("sqlite:file:%s/sqlite.db?_pragma=foreign_keys(1)&journal_mode(WAL)", testDirectory)
err = koanfInstance.Load(structs.ProviderWithDelim(storageConfig, "koanf", "."), nil)
if err != nil {
panic(err)
}
}
bytes, err := koanfInstance.Marshal(yamlParser)
if err != nil {
panic(err)
}
configFile := filepath.Join(testDirectory, "config.yaml")
err = os.WriteFile(configFile, bytes, 0644)
if err != nil {
panic(err)
}
os.Args = []string{"nuts", "server", "--configfile", configFile}
timeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
go func() {
// Wait for the Nuts node to start, until the given timeout. Check every 100ms
interval := 100 * time.Millisecond
attempts := int(timeout / interval)
address := fmt.Sprintf("http://%s/status", moduleConfig.HTTP.Internal.Address)
for i := 0; i < attempts; i++ {
if isHttpRunning(address) {
cancel()
break
}
time.Sleep(interval)
}
}()
go func() {
main()
exitCallback()
}()
return ctx
}
func isHttpRunning(address string) bool {
response, err := http.Get(address)
if err != nil {
println(err.Error())
return false
}
_, _ = io.ReadAll(response.Body)
return response.StatusCode == http.StatusOK
}
func getIntegrationTestConfig(t *testing.T, testDirectory string) (core.ServerConfig, ModuleConfig) {
system := cmd.CreateSystem(func() {
panic("test error")
})
for _, subCmd := range cmd.CreateCommand(system).Commands() {
if subCmd.Name() == "server" {
_ = system.Load(subCmd.Flags())
break
}
}
config := *system.Config
config.URL = "https://nuts.nl"
config.TLS.CertFile = pki.CertificateFile(t)
config.TLS.CertKeyFile = config.TLS.CertFile
config.TLS.TrustStoreFile = pki.TruststoreFile(t)
config.DIDMethods = []string{"nuts"}
config.Datadir = testDirectory
networkConfig := network.DefaultConfig()
networkConfig.GrpcAddr = fmt.Sprintf("localhost:%d", test.FreeTCPPort())
authConfig := auth.DefaultConfig()
authConfig.ContractValidators = []string{"dummy"} // disables IRMA
cryptoConfig := crypto.Config{Storage: "fs"}
eventsConfig := events.DefaultConfig()
eventsConfig.Nats.Port = test.FreeTCPPort()
eventsConfig.Nats.Hostname = "localhost"
httpConfig := httpEngine.DefaultConfig()
httpConfig.Internal.Address = fmt.Sprintf("localhost:%d", test.FreeTCPPort())
httpConfig.Public.Address = fmt.Sprintf("localhost:%d", test.FreeTCPPort())
return config, ModuleConfig{
Network: networkConfig,
Auth: authConfig,
Crypto: cryptoConfig,
Events: eventsConfig,
HTTP: httpConfig,
}
}
type ModuleConfig struct {
Network network.Config `koanf:"network"`
Auth auth.Config `koanf:"auth"`
Crypto crypto.Config `koanf:"crypto"`
Events events.Config `koanf:"events"`
HTTP httpEngine.Config `koanf:"http"`
}