This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathingest_test.go
98 lines (82 loc) · 1.97 KB
/
ingest_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
package main
import (
"context"
"math/rand"
"net/url"
"os"
"strconv"
"testing"
"github.com/packethost/packngo"
"github.com/packethost/pkg/log"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
)
func TestMain(m *testing.M) {
os.Setenv("PACKET_ENV", "test")
os.Setenv("PACKET_VERSION", "0")
os.Setenv("ROLLBAR_DISABLE", "1")
os.Setenv("ROLLBAR_TOKEN", "1")
logger, _ = log.Init("github.com/packethost/cacher")
setupMetrics()
os.Exit(m.Run())
}
func TestFetchFacility(t *testing.T) {
os.Setenv("CACHER_CONCURRENT_FETCHES", "1")
os.Unsetenv("CACHER_FETCH_PER_PAGE")
logger = log.Test(t, "github.com/packethost/cacher")
defer gock.Off()
facility := "testing" + strconv.Itoa(rand.Int())
pages := []map[string]interface{}{
{
"meta": map[string]interface{}{
"current_page": 1,
"last_page": 2,
"total": 100,
},
"Hardware": []map[string]interface{}{
{
"id": "1",
},
},
},
{
"meta": map[string]interface{}{
"current_page": 2,
"last_page": 2,
"total": 100,
},
"Hardware": []map[string]interface{}{
{
"id": "2",
},
},
},
}
gock.New("https://api.packet.net").
Get("staff/cacher/hardware").
MatchParam("facility", facility).
MatchParam("per_page", "1").
Reply(200).
JSON(pages[0])
for i, m := range pages {
gock.New("https://api.packet.net").
Get("staff/cacher/hardware").
MatchParam("facility", facility).
MatchParam("page", strconv.Itoa(i+1)).
Reply(200).
JSON(m)
}
ch := make(chan []map[string]interface{}, len(pages)*10)
u, err := url.Parse("https://api.packet.net")
assert := require.New(t)
assert.NoError(err)
client := packngo.NewClientWithAuth(os.Getenv("PACKET_CONSUMER_TOKEN"), os.Getenv("PACKET_API_AUTH_TOKEN"), nil)
err = fetchFacility(context.TODO(), client, u, facility, ch)
assert.NoError(err)
assert.Len(ch, len(pages))
assert.True(gock.IsDone())
for _, m := range pages {
p := <-ch
assert.Equal(m["Hardware"], p)
}
}