forked from knative/pkg
/
fake.go
83 lines (70 loc) · 2.11 KB
/
fake.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
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fake
import (
"fmt"
boskoscommon "k8s.io/test-infra/boskos/common"
"knative.dev/pkg/testutils/clustermanager/e2e-tests/boskos"
)
const (
fakeOwner = "fake-owner"
)
// FakeBoskosClient implements boskos.Operation
type FakeBoskosClient struct {
resources []*boskoscommon.Resource
}
func (c *FakeBoskosClient) getOwner(host *string) string {
if host == nil {
return fakeOwner
}
return *host
}
func (c *FakeBoskosClient) GetResources() []*boskoscommon.Resource {
return c.resources
}
// AcquireGKEProject fakes to be no op
func (c *FakeBoskosClient) AcquireGKEProject(resType string) (*boskoscommon.Resource, error) {
for _, res := range c.resources {
if res.State == boskoscommon.Free {
res.State = boskoscommon.Busy
res.Owner = c.getOwner(nil)
res.Type = resType
return res, nil
}
}
return nil, fmt.Errorf("no GKE project available")
}
// ReleaseGKEProject fakes to be no op
func (c *FakeBoskosClient) ReleaseGKEProject(name string) error {
owner := c.getOwner(nil)
for _, res := range c.resources {
if res.Name == name {
if res.Owner == owner {
res.Owner = ""
res.State = boskoscommon.Free
return nil
} else {
return fmt.Errorf("Got owner: '%s', expect owner: '%s'", res.Owner, owner)
}
}
}
return fmt.Errorf("resource doesn't exist yet: '%s'", name)
}
// NewGKEProject adds Boskos resources for testing purpose
func (c *FakeBoskosClient) NewGKEProject(name string) {
c.resources = append(c.resources, &boskoscommon.Resource{
Type: boskos.GKEProjectResource,
Name: name,
State: boskoscommon.Free,
})
}