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
|
package discovery
import (
"context"
"fmt"
"net"
"net/http"
"testing"
"time"
)
// setupFileServer sets up a file server with a directory
func setupFileServer(t *testing.T, directory string) (*http.Server) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("Failed to setup discovery file server")
}
s := &http.Server{Handler: http.FileServer(http.Dir(directory))}
go s.Serve(listener) //nolint:errcheck
// Override the global disco URL with the local file server
port := listener.Addr().(*net.TCPAddr).Port
DiscoURL = fmt.Sprintf("http://127.0.0.1:%d/", port)
return s
}
// TestServers tests whether or not we can obtain discovery servers
// It setups up a file server using the 'test_files' directory
func TestServers(t *testing.T) {
s := setupFileServer(t, "test_files")
d := &Discovery{}
// get servers
s1, err := d.Servers()
if err != nil {
t.Fatalf("Failed getting servers: %v", err)
}
// Shutdown the server
err = s.Shutdown(context.Background())
if err != nil {
t.Fatalf("Failed to shutdown server: %v", err)
}
// Test if we get the same cached copy
s2, err := d.Servers()
// We should not get an error as the timestamp is not expired
if err != nil {
t.Fatalf("Got a servers error after shutting down server: %v", err)
}
if s1 != s2 {
t.Fatalf("Servers copies not equal after shutting down file server")
}
// Force expired, 1 hour in the past
d.servers.Timestamp = time.Now().Add(-1 * time.Hour)
s3, err := d.Servers()
// Now we expect an error with the cached copy
if err == nil {
t.Fatalf("Got a servers nil error after shutting down file server and expired")
}
if s1 != s3 {
t.Fatalf("Servers copies not equal after shutting down file server and expired")
}
}
// TestOrganizations tests whether or not we can obtain discovery organizations
// It setups up a file server using the 'test_files' directory
func TestOrganizations(t *testing.T) {
s := setupFileServer(t, "test_files")
d := &Discovery{}
// get servers
s1, err := d.Organizations()
if err != nil {
t.Fatalf("Failed getting organizations: %v", err)
}
// Shutdown the server
err = s.Shutdown(context.Background())
if err != nil {
t.Fatalf("Failed to shutdown server: %v", err)
}
// Test if we get the same cached copy
// We should not get an error as the timestamp is not zero
s2, err := d.Organizations()
if err != nil {
t.Fatalf("Got an organizations error after shutting down file server: %v", err)
}
if s1 != s2 {
t.Fatalf("Organizations copies not equal after shutting down file server")
}
}
|