wangpengfei
2023-06-02 064c0874e5fd041c4641ef873d1bf72ac98a184d
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2016 The Kubernetes 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 disk
 
import (
    "io/ioutil"
    "os"
    "path/filepath"
    "testing"
    "time"
 
    openapi_v2 "github.com/googleapis/gnostic/openapiv2"
    "github.com/stretchr/testify/assert"
 
    "k8s.io/apimachinery/pkg/api/errors"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/runtime/schema"
    "k8s.io/apimachinery/pkg/version"
    "k8s.io/client-go/discovery"
    restclient "k8s.io/client-go/rest"
    "k8s.io/client-go/rest/fake"
)
 
func TestCachedDiscoveryClient_Fresh(t *testing.T) {
    assert := assert.New(t)
 
    d, err := ioutil.TempDir("", "")
    assert.NoError(err)
    defer os.RemoveAll(d)
 
    c := fakeDiscoveryClient{}
    cdc := newCachedDiscoveryClient(&c, d, 60*time.Second)
    assert.True(cdc.Fresh(), "should be fresh after creation")
 
    cdc.ServerGroups()
    assert.True(cdc.Fresh(), "should be fresh after groups call without cache")
    assert.Equal(c.groupCalls, 1)
 
    cdc.ServerGroups()
    assert.True(cdc.Fresh(), "should be fresh after another groups call")
    assert.Equal(c.groupCalls, 1)
 
    cdc.ServerResources()
    assert.True(cdc.Fresh(), "should be fresh after resources call")
    assert.Equal(c.resourceCalls, 1)
 
    cdc.ServerResources()
    assert.True(cdc.Fresh(), "should be fresh after another resources call")
    assert.Equal(c.resourceCalls, 1)
 
    cdc = newCachedDiscoveryClient(&c, d, 60*time.Second)
    cdc.ServerGroups()
    assert.False(cdc.Fresh(), "should NOT be fresh after recreation with existing groups cache")
    assert.Equal(c.groupCalls, 1)
 
    cdc.ServerResources()
    assert.False(cdc.Fresh(), "should NOT be fresh after recreation with existing resources cache")
    assert.Equal(c.resourceCalls, 1)
 
    cdc.Invalidate()
    assert.True(cdc.Fresh(), "should be fresh after cache invalidation")
 
    cdc.ServerResources()
    assert.True(cdc.Fresh(), "should ignore existing resources cache after invalidation")
    assert.Equal(c.resourceCalls, 2)
}
 
func TestNewCachedDiscoveryClient_TTL(t *testing.T) {
    assert := assert.New(t)
 
    d, err := ioutil.TempDir("", "")
    assert.NoError(err)
    defer os.RemoveAll(d)
 
    c := fakeDiscoveryClient{}
    cdc := newCachedDiscoveryClient(&c, d, 1*time.Nanosecond)
    cdc.ServerGroups()
    assert.Equal(c.groupCalls, 1)
 
    time.Sleep(1 * time.Second)
 
    cdc.ServerGroups()
    assert.Equal(c.groupCalls, 2)
}
 
func TestNewCachedDiscoveryClient_PathPerm(t *testing.T) {
    assert := assert.New(t)
 
    d, err := ioutil.TempDir("", "")
    assert.NoError(err)
    os.RemoveAll(d)
    defer os.RemoveAll(d)
 
    c := fakeDiscoveryClient{}
    cdc := newCachedDiscoveryClient(&c, d, 1*time.Nanosecond)
    cdc.ServerGroups()
 
    err = filepath.Walk(d, func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }
        if info.IsDir() {
            assert.Equal(os.FileMode(0750), info.Mode().Perm())
        } else {
            assert.Equal(os.FileMode(0660), info.Mode().Perm())
        }
        return nil
    })
    assert.NoError(err)
}
 
type fakeDiscoveryClient struct {
    groupCalls    int
    resourceCalls int
    versionCalls  int
    openAPICalls  int
 
    serverResourcesHandler func() ([]*metav1.APIResourceList, error)
}
 
var _ discovery.DiscoveryInterface = &fakeDiscoveryClient{}
 
func (c *fakeDiscoveryClient) RESTClient() restclient.Interface {
    return &fake.RESTClient{}
}
 
func (c *fakeDiscoveryClient) ServerGroups() (*metav1.APIGroupList, error) {
    c.groupCalls = c.groupCalls + 1
    return c.serverGroups()
}
 
func (c *fakeDiscoveryClient) serverGroups() (*metav1.APIGroupList, error) {
    return &metav1.APIGroupList{
        Groups: []metav1.APIGroup{
            {
                Name: "a",
                Versions: []metav1.GroupVersionForDiscovery{
                    {
                        GroupVersion: "a/v1",
                        Version:      "v1",
                    },
                },
                PreferredVersion: metav1.GroupVersionForDiscovery{
                    GroupVersion: "a/v1",
                    Version:      "v1",
                },
            },
        },
    }, nil
}
 
func (c *fakeDiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
    c.resourceCalls = c.resourceCalls + 1
    if groupVersion == "a/v1" {
        return &metav1.APIResourceList{APIResources: []metav1.APIResource{{Name: "widgets", Kind: "Widget"}}}, nil
    }
 
    return nil, errors.NewNotFound(schema.GroupResource{}, "")
}
 
// Deprecated: use ServerGroupsAndResources instead.
func (c *fakeDiscoveryClient) ServerResources() ([]*metav1.APIResourceList, error) {
    _, rs, err := c.ServerGroupsAndResources()
    return rs, err
}
 
func (c *fakeDiscoveryClient) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
    c.resourceCalls = c.resourceCalls + 1
 
    gs, _ := c.serverGroups()
    resultGroups := []*metav1.APIGroup{}
    for i := range gs.Groups {
        resultGroups = append(resultGroups, &gs.Groups[i])
    }
 
    if c.serverResourcesHandler != nil {
        rs, err := c.serverResourcesHandler()
        return resultGroups, rs, err
    }
    return resultGroups, []*metav1.APIResourceList{}, nil
}
 
func (c *fakeDiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
    c.resourceCalls = c.resourceCalls + 1
    return nil, nil
}
 
func (c *fakeDiscoveryClient) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
    c.resourceCalls = c.resourceCalls + 1
    return nil, nil
}
 
func (c *fakeDiscoveryClient) ServerVersion() (*version.Info, error) {
    c.versionCalls = c.versionCalls + 1
    return &version.Info{}, nil
}
 
func (c *fakeDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
    c.openAPICalls = c.openAPICalls + 1
    return &openapi_v2.Document{}, nil
}