14324: Remove context from interface, use Stop() instead
[arvados.git] / lib / dispatchcloud / test / lame_instance_set.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         "fmt"
9         "math/rand"
10         "sync"
11
12         "git.curoverse.com/arvados.git/lib/cloud"
13         "git.curoverse.com/arvados.git/sdk/go/arvados"
14         "golang.org/x/crypto/ssh"
15 )
16
17 // LameInstanceSet creates instances that boot but can't run
18 // containers.
19 type LameInstanceSet struct {
20         Hold chan bool // set to make(chan bool) to hold operations until Release is called
21
22         mtx       sync.Mutex
23         instances map[*lameInstance]bool
24 }
25
26 // Create returns a new instance.
27 func (p *LameInstanceSet) Create(instType arvados.InstanceType, imageID cloud.ImageID, tags cloud.InstanceTags, pubkey ssh.PublicKey) (cloud.Instance, error) {
28         inst := &lameInstance{
29                 p:            p,
30                 id:           cloud.InstanceID(fmt.Sprintf("lame-%x", rand.Uint64())),
31                 providerType: instType.ProviderType,
32         }
33         inst.SetTags(tags)
34         if p.Hold != nil {
35                 p.Hold <- true
36         }
37         p.mtx.Lock()
38         defer p.mtx.Unlock()
39         if p.instances == nil {
40                 p.instances = map[*lameInstance]bool{}
41         }
42         p.instances[inst] = true
43         return inst, nil
44 }
45
46 // Instances returns the instances that haven't been destroyed.
47 func (p *LameInstanceSet) Instances(cloud.InstanceTags) ([]cloud.Instance, error) {
48         p.mtx.Lock()
49         defer p.mtx.Unlock()
50         var instances []cloud.Instance
51         for i := range p.instances {
52                 instances = append(instances, i)
53         }
54         return instances, nil
55 }
56
57 // Stop is a no-op, but exists to satisfy cloud.InstanceSet.
58 func (p *LameInstanceSet) Stop() {
59 }
60
61 // Release n held calls. Blocks if n calls aren't already
62 // waiting. Blocks forever if Hold is nil.
63 func (p *LameInstanceSet) Release(n int) {
64         for i := 0; i < n; i++ {
65                 <-p.Hold
66         }
67 }
68
69 type lameInstance struct {
70         p            *LameInstanceSet
71         id           cloud.InstanceID
72         providerType string
73         tags         cloud.InstanceTags
74 }
75
76 func (inst *lameInstance) ID() cloud.InstanceID {
77         return inst.id
78 }
79
80 func (inst *lameInstance) String() string {
81         return fmt.Sprint(inst.id)
82 }
83
84 func (inst *lameInstance) ProviderType() string {
85         return inst.providerType
86 }
87
88 func (inst *lameInstance) Address() string {
89         return "0.0.0.0:1234"
90 }
91
92 func (inst *lameInstance) SetTags(tags cloud.InstanceTags) error {
93         inst.p.mtx.Lock()
94         defer inst.p.mtx.Unlock()
95         inst.tags = cloud.InstanceTags{}
96         for k, v := range tags {
97                 inst.tags[k] = v
98         }
99         return nil
100 }
101
102 func (inst *lameInstance) Destroy() error {
103         if inst.p.Hold != nil {
104                 inst.p.Hold <- true
105         }
106         inst.p.mtx.Lock()
107         defer inst.p.mtx.Unlock()
108         delete(inst.p.instances, inst)
109         return nil
110 }
111
112 func (inst *lameInstance) Tags() cloud.InstanceTags {
113         return inst.tags
114 }
115
116 func (inst *lameInstance) VerifyHostKey(ssh.PublicKey, *ssh.Client) error {
117         return nil
118 }