forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkermanager.go
78 lines (71 loc) · 2.3 KB
/
workermanager.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
package mocktc
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"time"
"github.com/taskcluster/httpbackoff/v3"
"github.com/taskcluster/slugid-go/slugid"
tcclient "github.com/taskcluster/taskcluster/v47/clients/client-go"
"github.com/taskcluster/taskcluster/v47/clients/client-go/tcworkermanager"
)
type WorkerManager struct {
// workerPools["<workerPoolId>"]
workerPools map[string]*tcworkermanager.WorkerPoolFullDefinition
}
func NewWorkerManager(t *testing.T) *WorkerManager {
return &WorkerManager{
workerPools: map[string]*tcworkermanager.WorkerPoolFullDefinition{},
}
}
func (wm *WorkerManager) RegisterWorker(payload *tcworkermanager.RegisterWorkerRequest) (*tcworkermanager.RegisterWorkerResponse, error) {
d := json.NewDecoder(bytes.NewBuffer(payload.WorkerIdentityProof))
d.DisallowUnknownFields()
g := tcworkermanager.AwsProviderType{}
err := d.Decode(&g)
if err != nil {
return nil, &tcclient.APICallException{
CallSummary: &tcclient.CallSummary{
HTTPResponseBody: err.Error(),
},
RootCause: httpbackoff.BadHttpResponseCode{
HttpResponseCode: 400,
},
}
}
if g.Signature != "test-signature" {
return nil, &tcclient.APICallException{
CallSummary: &tcclient.CallSummary{
HTTPResponseBody: fmt.Sprintf("Got signature %q but was expecting %q", g.Signature, "test-signature"),
},
RootCause: httpbackoff.BadHttpResponseCode{
HttpResponseCode: 400,
},
}
}
return &tcworkermanager.RegisterWorkerResponse{
Credentials: tcworkermanager.Credentials{
ClientID: "fake-client-id",
Certificate: "fake-cert",
AccessToken: slugid.Nice(),
},
}, nil
}
func (wm *WorkerManager) WorkerPool(workerPoolId string) (*tcworkermanager.WorkerPoolFullDefinition, error) {
return wm.workerPools[workerPoolId], nil
}
func (wm *WorkerManager) CreateWorkerPool(workerPoolId string, payload *tcworkermanager.WorkerPoolDefinition) (*tcworkermanager.WorkerPoolFullDefinition, error) {
wpfd := &tcworkermanager.WorkerPoolFullDefinition{
Config: payload.Config,
Created: tcclient.Time(time.Now()),
Description: payload.Description,
EmailOnError: payload.EmailOnError,
LastModified: tcclient.Time(time.Now()),
Owner: payload.Owner,
ProviderID: payload.ProviderID,
WorkerPoolID: workerPoolId,
}
wm.workerPools[workerPoolId] = wpfd
return wpfd, nil
}