generated from cybozu-go/neco-template
/
fakeclient.go
116 lines (94 loc) · 2.76 KB
/
fakeclient.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
package github
import (
"context"
"errors"
"sync"
"time"
"github.com/google/go-github/v41/github"
)
type FakeClientFactory struct {
mu sync.Mutex
runners map[string][]*Runner
expiredAtDuration time.Duration
}
func NewFakeClientFactory() *FakeClientFactory {
return &FakeClientFactory{
runners: map[string][]*Runner{},
expiredAtDuration: 1 * time.Hour,
}
}
func genKey(owner, repo string) string {
if repo == "" {
return owner
}
return owner + "/" + repo
}
func (f *FakeClientFactory) New(_ *ClientCredential) (Client, error) {
return &FakeClient{parent: f}, nil
}
func (f *FakeClientFactory) createRegistrationToken(ctx context.Context, owner, repo string) (*github.RegistrationToken, error) {
f.mu.Lock()
defer f.mu.Unlock()
fakeToken := "faketoken"
return &github.RegistrationToken{
Token: &fakeToken,
ExpiresAt: &github.Timestamp{
Time: time.Now().Add(f.expiredAtDuration),
},
}, nil
}
// ListRunners returns dummy list.
func (f *FakeClientFactory) ListRunners(ctx context.Context, owner, repo string, labels []string) ([]*Runner, error) {
f.mu.Lock()
defer f.mu.Unlock()
key := genKey(owner, repo)
ret := []*Runner{}
runners := f.runners[key]
for _, r := range runners {
if r.hasLabels(labels) {
ret = append(ret, r)
}
}
return ret, nil
}
// RemoveRunner does not delete anything and returns success.
func (f *FakeClientFactory) RemoveRunner(ctx context.Context, owner, repo string, runnerID int64) error {
f.mu.Lock()
defer f.mu.Unlock()
key := genKey(owner, repo)
// skip existence and nil check below because this is mock
runners := f.runners[key]
for i, v := range runners {
if v.ID == runnerID {
f.runners[key] = append(runners[:i], runners[i+1:]...)
return nil
}
}
return errors.New("not exist")
}
func (f *FakeClientFactory) SetRunners(runners map[string][]*Runner) {
f.mu.Lock()
defer f.mu.Unlock()
f.runners = runners
}
func (f *FakeClientFactory) SetExpiredAtDuration(d time.Duration) {
f.mu.Lock()
defer f.mu.Unlock()
f.expiredAtDuration = d
}
// FakeClient is a fake client
type FakeClient struct {
parent *FakeClientFactory
}
// CreateRegistrationToken returns dummy token.
func (c *FakeClient) CreateRegistrationToken(ctx context.Context, owner, repo string) (*github.RegistrationToken, error) {
return c.parent.createRegistrationToken(ctx, owner, repo)
}
// ListRunners returns dummy list.
func (c *FakeClient) ListRunners(ctx context.Context, owner, repo string, labels []string) ([]*Runner, error) {
return c.parent.ListRunners(ctx, owner, repo, labels)
}
// RemoveRunner does not delete anything and returns success.
func (c *FakeClient) RemoveRunner(ctx context.Context, owner, repo string, runnerID int64) error {
return c.parent.RemoveRunner(ctx, owner, repo, runnerID)
}