This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
/
fake.go
56 lines (49 loc) · 1.82 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
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package discovery
import (
openapi_v2 "github.com/google/gnostic/openapiv2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
fakediscovery "k8s.io/client-go/discovery/fake"
dynamicFake "k8s.io/client-go/dynamic/fake"
k8stesting "k8s.io/client-go/testing"
)
// NewFakeClusterQueryClient returns a fake ClusterQueryClient for use in tests.
func NewFakeClusterQueryClient(resources []*metav1.APIResourceList, scheme *runtime.Scheme, objs []runtime.Object) (*ClusterQueryClient, error) {
fakeDynamicClient := dynamicFake.NewSimpleDynamicClient(scheme, objs...)
fakeDiscoveryClient := &fakediscovery.FakeDiscovery{
Fake: &k8stesting.Fake{
Resources: resources,
}}
return NewClusterQueryClient(fakeDynamicClient, fakeDiscoveryClient)
}
// fakeWithSchema is the same as fakediscovery.FakeDiscovery except it has a
// different OPenAPISchema() method.
type fakeWithSchema struct {
*fakediscovery.FakeDiscovery
}
func (fws fakeWithSchema) OpenAPISchema() (*openapi_v2.Document, error) {
schema := `swagger: '2.0'
info:
title: 'example schema for test'
version: '1.3'
paths:
- '/test/path'
- '/another/path'
`
return openapi_v2.ParseDocument([]byte(schema))
}
// NewFakeClusterQueryClientWithSchema returns a fake ClusterQueryClient for use in tests.
func NewFakeClusterQueryClientWithSchema(_ []*metav1.APIResourceList, scheme *runtime.Scheme, objs []runtime.Object) (*ClusterQueryClient, error) {
fakeDynamicClient := dynamicFake.NewSimpleDynamicClient(scheme, objs...)
fakeDiscWithSchema := fakeWithSchema{
&fakediscovery.FakeDiscovery{
Fake: &k8stesting.Fake{
Resources: nil,
},
},
}
fakeDiscoveryClient := &fakeDiscWithSchema
return NewClusterQueryClient(fakeDynamicClient, fakeDiscoveryClient)
}