Add 'sdk/java-v2/' from commit '55f103e336ca9fb8bf1720d2ef4ee8dd4e221118'
[arvados.git] / lib / dispatchcloud / test / stub_driver.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package test
6
7 import (
8         "crypto/rand"
9         "encoding/json"
10         "errors"
11         "fmt"
12         "io"
13         "io/ioutil"
14         math_rand "math/rand"
15         "regexp"
16         "strings"
17         "sync"
18         "time"
19
20         "git.curoverse.com/arvados.git/lib/cloud"
21         "git.curoverse.com/arvados.git/sdk/go/arvados"
22         "github.com/sirupsen/logrus"
23         "golang.org/x/crypto/ssh"
24 )
25
26 // A StubDriver implements cloud.Driver by setting up local SSH
27 // servers that do fake command executions.
28 type StubDriver struct {
29         HostKey        ssh.Signer
30         AuthorizedKeys []ssh.PublicKey
31
32         // SetupVM, if set, is called upon creation of each new
33         // StubVM. This is the caller's opportunity to customize the
34         // VM's error rate and other behaviors.
35         SetupVM func(*StubVM)
36
37         // StubVM's fake crunch-run uses this Queue to read and update
38         // container state.
39         Queue *Queue
40
41         // Frequency of artificially introduced errors on calls to
42         // Destroy. 0=always succeed, 1=always fail.
43         ErrorRateDestroy float64
44
45         // If Create() or Instances() is called too frequently, return
46         // rate-limiting errors.
47         MinTimeBetweenCreateCalls    time.Duration
48         MinTimeBetweenInstancesCalls time.Duration
49
50         // If true, Create and Destroy calls block until Release() is
51         // called.
52         HoldCloudOps bool
53
54         instanceSets []*StubInstanceSet
55         holdCloudOps chan bool
56 }
57
58 // InstanceSet returns a new *StubInstanceSet.
59 func (sd *StubDriver) InstanceSet(params json.RawMessage, id cloud.InstanceSetID, logger logrus.FieldLogger) (cloud.InstanceSet, error) {
60         if sd.holdCloudOps == nil {
61                 sd.holdCloudOps = make(chan bool)
62         }
63         sis := StubInstanceSet{
64                 driver:  sd,
65                 logger:  logger,
66                 servers: map[cloud.InstanceID]*StubVM{},
67         }
68         sd.instanceSets = append(sd.instanceSets, &sis)
69
70         var err error
71         if params != nil {
72                 err = json.Unmarshal(params, &sis)
73         }
74         return &sis, err
75 }
76
77 // InstanceSets returns all instances that have been created by the
78 // driver. This can be used to test a component that uses the driver
79 // but doesn't expose the InstanceSets it has created.
80 func (sd *StubDriver) InstanceSets() []*StubInstanceSet {
81         return sd.instanceSets
82 }
83
84 // ReleaseCloudOps releases n pending Create/Destroy calls. If there
85 // are fewer than n blocked calls pending, it waits for the rest to
86 // arrive.
87 func (sd *StubDriver) ReleaseCloudOps(n int) {
88         for i := 0; i < n; i++ {
89                 <-sd.holdCloudOps
90         }
91 }
92
93 type StubInstanceSet struct {
94         driver  *StubDriver
95         logger  logrus.FieldLogger
96         servers map[cloud.InstanceID]*StubVM
97         mtx     sync.RWMutex
98         stopped bool
99
100         allowCreateCall    time.Time
101         allowInstancesCall time.Time
102 }
103
104 func (sis *StubInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, cmd cloud.InitCommand, authKey ssh.PublicKey) (cloud.Instance, error) {
105         if sis.driver.HoldCloudOps {
106                 sis.driver.holdCloudOps <- true
107         }
108         sis.mtx.Lock()
109         defer sis.mtx.Unlock()
110         if sis.stopped {
111                 return nil, errors.New("StubInstanceSet: Create called after Stop")
112         }
113         if sis.allowCreateCall.After(time.Now()) {
114                 return nil, RateLimitError{sis.allowCreateCall}
115         } else {
116                 sis.allowCreateCall = time.Now().Add(sis.driver.MinTimeBetweenCreateCalls)
117         }
118
119         ak := sis.driver.AuthorizedKeys
120         if authKey != nil {
121                 ak = append([]ssh.PublicKey{authKey}, ak...)
122         }
123         svm := &StubVM{
124                 sis:          sis,
125                 id:           cloud.InstanceID(fmt.Sprintf("stub-%s-%x", it.ProviderType, math_rand.Int63())),
126                 tags:         copyTags(tags),
127                 providerType: it.ProviderType,
128                 initCommand:  cmd,
129         }
130         svm.SSHService = SSHService{
131                 HostKey:        sis.driver.HostKey,
132                 AuthorizedUser: "root",
133                 AuthorizedKeys: ak,
134                 Exec:           svm.Exec,
135         }
136         if setup := sis.driver.SetupVM; setup != nil {
137                 setup(svm)
138         }
139         sis.servers[svm.id] = svm
140         return svm.Instance(), nil
141 }
142
143 func (sis *StubInstanceSet) Instances(cloud.InstanceTags) ([]cloud.Instance, error) {
144         sis.mtx.RLock()
145         defer sis.mtx.RUnlock()
146         if sis.allowInstancesCall.After(time.Now()) {
147                 return nil, RateLimitError{sis.allowInstancesCall}
148         } else {
149                 sis.allowInstancesCall = time.Now().Add(sis.driver.MinTimeBetweenInstancesCalls)
150         }
151         var r []cloud.Instance
152         for _, ss := range sis.servers {
153                 r = append(r, ss.Instance())
154         }
155         return r, nil
156 }
157
158 func (sis *StubInstanceSet) Stop() {
159         sis.mtx.Lock()
160         defer sis.mtx.Unlock()
161         if sis.stopped {
162                 panic("Stop called twice")
163         }
164         sis.stopped = true
165 }
166
167 type RateLimitError struct{ Retry time.Time }
168
169 func (e RateLimitError) Error() string            { return fmt.Sprintf("rate limited until %s", e.Retry) }
170 func (e RateLimitError) EarliestRetry() time.Time { return e.Retry }
171
172 // StubVM is a fake server that runs an SSH service. It represents a
173 // VM running in a fake cloud.
174 //
175 // Note this is distinct from a stubInstance, which is a snapshot of
176 // the VM's metadata. Like a VM in a real cloud, a StubVM keeps
177 // running (and might change IP addresses, shut down, etc.)  without
178 // updating any stubInstances that have been returned to callers.
179 type StubVM struct {
180         Boot                 time.Time
181         Broken               time.Time
182         CrunchRunMissing     bool
183         CrunchRunCrashRate   float64
184         CrunchRunDetachDelay time.Duration
185         ExecuteContainer     func(arvados.Container) int
186
187         sis          *StubInstanceSet
188         id           cloud.InstanceID
189         tags         cloud.InstanceTags
190         initCommand  cloud.InitCommand
191         providerType string
192         SSHService   SSHService
193         running      map[string]bool
194         sync.Mutex
195 }
196
197 func (svm *StubVM) Instance() stubInstance {
198         svm.Lock()
199         defer svm.Unlock()
200         return stubInstance{
201                 svm:  svm,
202                 addr: svm.SSHService.Address(),
203                 // We deliberately return a cached/stale copy of the
204                 // real tags here, so that (Instance)Tags() sometimes
205                 // returns old data after a call to
206                 // (Instance)SetTags().  This is permitted by the
207                 // driver interface, and this might help remind
208                 // callers that they need to tolerate it.
209                 tags: copyTags(svm.tags),
210         }
211 }
212
213 func (svm *StubVM) Exec(env map[string]string, command string, stdin io.Reader, stdout, stderr io.Writer) uint32 {
214         stdinData, err := ioutil.ReadAll(stdin)
215         if err != nil {
216                 fmt.Fprintf(stderr, "error reading stdin: %s\n", err)
217                 return 1
218         }
219         queue := svm.sis.driver.Queue
220         uuid := regexp.MustCompile(`.{5}-dz642-.{15}`).FindString(command)
221         if eta := svm.Boot.Sub(time.Now()); eta > 0 {
222                 fmt.Fprintf(stderr, "stub is booting, ETA %s\n", eta)
223                 return 1
224         }
225         if !svm.Broken.IsZero() && svm.Broken.Before(time.Now()) {
226                 fmt.Fprintf(stderr, "cannot fork\n")
227                 return 2
228         }
229         if svm.CrunchRunMissing && strings.Contains(command, "crunch-run") {
230                 fmt.Fprint(stderr, "crunch-run: command not found\n")
231                 return 1
232         }
233         if strings.HasPrefix(command, "crunch-run --detach --stdin-env ") {
234                 var stdinKV map[string]string
235                 err := json.Unmarshal(stdinData, &stdinKV)
236                 if err != nil {
237                         fmt.Fprintf(stderr, "unmarshal stdin: %s (stdin was: %q)\n", err, stdinData)
238                         return 1
239                 }
240                 for _, name := range []string{"ARVADOS_API_HOST", "ARVADOS_API_TOKEN"} {
241                         if stdinKV[name] == "" {
242                                 fmt.Fprintf(stderr, "%s env var missing from stdin %q\n", name, stdin)
243                                 return 1
244                         }
245                 }
246                 svm.Lock()
247                 if svm.running == nil {
248                         svm.running = map[string]bool{}
249                 }
250                 svm.running[uuid] = true
251                 svm.Unlock()
252                 time.Sleep(svm.CrunchRunDetachDelay)
253                 fmt.Fprintf(stderr, "starting %s\n", uuid)
254                 logger := svm.sis.logger.WithFields(logrus.Fields{
255                         "Instance":      svm.id,
256                         "ContainerUUID": uuid,
257                 })
258                 logger.Printf("[test] starting crunch-run stub")
259                 go func() {
260                         crashluck := math_rand.Float64()
261                         ctr, ok := queue.Get(uuid)
262                         if !ok {
263                                 logger.Print("[test] container not in queue")
264                                 return
265                         }
266                         if crashluck > svm.CrunchRunCrashRate/2 {
267                                 time.Sleep(time.Duration(math_rand.Float64()*20) * time.Millisecond)
268                                 ctr.State = arvados.ContainerStateRunning
269                                 queue.Notify(ctr)
270                         }
271
272                         time.Sleep(time.Duration(math_rand.Float64()*20) * time.Millisecond)
273                         svm.Lock()
274                         _, running := svm.running[uuid]
275                         svm.Unlock()
276                         if !running {
277                                 logger.Print("[test] container was killed")
278                                 return
279                         }
280                         if svm.ExecuteContainer != nil {
281                                 ctr.ExitCode = svm.ExecuteContainer(ctr)
282                         }
283                         // TODO: Check whether the stub instance has
284                         // been destroyed, and if so, don't call
285                         // queue.Notify. Then "container finished
286                         // twice" can be classified as a bug.
287                         if crashluck < svm.CrunchRunCrashRate {
288                                 logger.Print("[test] crashing crunch-run stub")
289                         } else {
290                                 ctr.State = arvados.ContainerStateComplete
291                                 queue.Notify(ctr)
292                         }
293                         logger.Print("[test] exiting crunch-run stub")
294                         svm.Lock()
295                         defer svm.Unlock()
296                         delete(svm.running, uuid)
297                 }()
298                 return 0
299         }
300         if command == "crunch-run --list" {
301                 svm.Lock()
302                 defer svm.Unlock()
303                 for uuid := range svm.running {
304                         fmt.Fprintf(stdout, "%s\n", uuid)
305                 }
306                 return 0
307         }
308         if strings.HasPrefix(command, "crunch-run --kill ") {
309                 svm.Lock()
310                 defer svm.Unlock()
311                 if svm.running[uuid] {
312                         delete(svm.running, uuid)
313                 } else {
314                         fmt.Fprintf(stderr, "%s: container is not running\n", uuid)
315                 }
316                 return 0
317         }
318         if command == "true" {
319                 return 0
320         }
321         fmt.Fprintf(stderr, "%q: command not found", command)
322         return 1
323 }
324
325 type stubInstance struct {
326         svm  *StubVM
327         addr string
328         tags cloud.InstanceTags
329 }
330
331 func (si stubInstance) ID() cloud.InstanceID {
332         return si.svm.id
333 }
334
335 func (si stubInstance) Address() string {
336         return si.addr
337 }
338
339 func (si stubInstance) RemoteUser() string {
340         return si.svm.SSHService.AuthorizedUser
341 }
342
343 func (si stubInstance) Destroy() error {
344         sis := si.svm.sis
345         if sis.driver.HoldCloudOps {
346                 sis.driver.holdCloudOps <- true
347         }
348         if math_rand.Float64() < si.svm.sis.driver.ErrorRateDestroy {
349                 return errors.New("instance could not be destroyed")
350         }
351         si.svm.SSHService.Close()
352         sis.mtx.Lock()
353         defer sis.mtx.Unlock()
354         delete(sis.servers, si.svm.id)
355         return nil
356 }
357
358 func (si stubInstance) ProviderType() string {
359         return si.svm.providerType
360 }
361
362 func (si stubInstance) SetTags(tags cloud.InstanceTags) error {
363         tags = copyTags(tags)
364         svm := si.svm
365         go func() {
366                 svm.Lock()
367                 defer svm.Unlock()
368                 svm.tags = tags
369         }()
370         return nil
371 }
372
373 func (si stubInstance) Tags() cloud.InstanceTags {
374         // Return a copy to ensure a caller can't change our saved
375         // tags just by writing to the returned map.
376         return copyTags(si.tags)
377 }
378
379 func (si stubInstance) String() string {
380         return string(si.svm.id)
381 }
382
383 func (si stubInstance) VerifyHostKey(key ssh.PublicKey, client *ssh.Client) error {
384         buf := make([]byte, 512)
385         _, err := io.ReadFull(rand.Reader, buf)
386         if err != nil {
387                 return err
388         }
389         sig, err := si.svm.sis.driver.HostKey.Sign(rand.Reader, buf)
390         if err != nil {
391                 return err
392         }
393         return key.Verify(buf, sig)
394 }
395
396 func copyTags(src cloud.InstanceTags) cloud.InstanceTags {
397         dst := cloud.InstanceTags{}
398         for k, v := range src {
399                 dst[k] = v
400         }
401         return dst
402 }