This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
redis_finder_test.go
177 lines (148 loc) · 4.04 KB
/
redis_finder_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
package mirrorcat_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/Azure/mirrorcat"
"github.com/go-redis/redis"
"github.com/spf13/viper"
)
func ExampleRedisRemoteRef_String() {
subject := mirrorcat.RemoteRef{
Repository: "https://github.com/Azure/mirrorcat",
Ref: "master",
}
marshaled := mirrorcat.RedisRemoteRef(subject).String()
fmt.Println(marshaled)
// Output: master:https://github.com/Azure/mirrorcat
}
func ExampleParseRedisRemoteRef() {
unmarshaled, err := mirrorcat.ParseRedisRemoteRef("master:https://github.com/Azure/mirrorcat")
if err != nil {
return
}
fmt.Println("Repository:", unmarshaled.Repository)
fmt.Println("Ref:", unmarshaled.Ref)
// Output:
// Repository: https://github.com/Azure/mirrorcat
// Ref: master
}
func TestParseRedisRemoteRef(t *testing.T) {
testCases := []struct {
string
want mirrorcat.RedisRemoteRef
}{
{":", mirrorcat.RedisRemoteRef{}},
{"left:right", mirrorcat.RedisRemoteRef{Ref: "left", Repository: "right"}},
{"branch:https://hostname:1234/folk?person=Pete Seeger", mirrorcat.RedisRemoteRef{Ref: "branch", Repository: "https://hostname:1234/folk?person=Pete Seeger"}},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
unmarshaled, err := mirrorcat.ParseRedisRemoteRef(tc.string)
if err != nil {
t.Log("unexpected err: ", err)
t.Fail()
}
if unmarshaled.Repository != tc.want.Repository {
t.Logf("got: %q want: %q", unmarshaled.Repository, tc.want.Repository)
t.Fail()
}
if unmarshaled.Ref != tc.want.Ref {
t.Logf("got: %q want: %q", unmarshaled.Ref, tc.want.Ref)
t.Fail()
}
})
}
}
func TestParseRedisRemoteRef_Invalid(t *testing.T) {
testCases := []string{
"",
"github.com/Azure/mirrorcat",
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
unmarshaled, err := mirrorcat.ParseRedisRemoteRef(tc)
if err == nil {
t.Log("expected a non-nil error for:", tc)
t.Log("got:", err)
t.Fail()
}
if unmarshaled.Repository != "" || unmarshaled.Ref != "" {
t.Log("expected the empty RedisRemoteRef for:", tc)
t.Fail()
}
})
}
}
func TestRedisFinder_FindMirrors(t *testing.T) {
viper.BindEnv("redis-connection", "MIRRORCAT_REDIS_CONNECTION")
viper.SetDefault("redis-connection", "redis://localhost:6379")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
t.Log("using redis connection:", viper.GetString("redis-connection"))
connectionOptions, err := redis.ParseURL(viper.GetString("redis-connection"))
if err != nil {
t.Fatal(err)
}
const testKey = "master:testRepo"
expected := map[mirrorcat.RemoteRef]struct{}{
mirrorcat.RemoteRef{Repository: "testRepo", Ref: "dev"}: struct{}{},
mirrorcat.RemoteRef{Repository: "otherRepo", Ref: "dev"}: struct{}{},
}
expectedArr := make([]interface{}, 0, len(expected))
for item := range expected {
expectedArr = append(expectedArr, mirrorcat.RedisRemoteRef(item).String())
}
client := redis.NewClient(connectionOptions)
_, err = client.SAdd(testKey, expectedArr...).Result()
defer func() {
_, err = client.Del(testKey).Result()
if err != nil {
t.Log("Unable to cleanup Redis Instance: ", err)
} else {
t.Log("Redis Instance cleaned up.")
}
}()
if err != nil {
t.Log("Unable to connect to Redis instance: ", err)
t.SkipNow()
}
subject := mirrorcat.RedisFinder(*client)
testRepo, err := mirrorcat.ParseRedisRemoteRef(testKey)
if err != nil {
t.Fatal(err)
}
results, errs := make(chan mirrorcat.RemoteRef), make(chan error, 1)
go func() {
select {
case errs <- subject.FindMirrors(ctx, mirrorcat.RemoteRef(testRepo), results):
case <-ctx.Done():
errs <- ctx.Err()
}
}()
loop:
for {
select {
case err = <-errs:
if err != nil {
t.Fatal(err)
}
case seen, ok := <-results:
if !ok {
break loop
}
_, ok = expected[seen]
if ok {
delete(expected, seen)
} else {
t.Log("unexpected result: ", seen)
t.Fail()
}
}
}
for unseen := range expected {
t.Log("didn't see expected value: ", unseen)
t.Fail()
}
}