-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
testdata.go
164 lines (148 loc) · 4.21 KB
/
testdata.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
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
package test
import (
"context"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis/v8"
"github.com/argoproj/gitops-engine/pkg/utils/testing"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
apps "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/fake"
appclient "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/typed/application/v1alpha1"
appinformer "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions"
applister "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
)
const (
FakeArgoCDNamespace = "fake-argocd-ns"
FakeDestNamespace = "fake-dest-ns"
FakeClusterURL = "https://fake-cluster:443"
)
func NewControllerRevision() *unstructured.Unstructured {
return testing.Unstructured(`
kind: ControllerRevision
apiVersion: metacontroller.k8s.io/v1alpha1
metadata:
labels:
app: nginx
controller.kubernetes.io/hash: c7cd8d57f
name: web-c7cd8d57f
namespace: statefulset
revision: 2
`)
}
var DeploymentManifest = `
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "nginx-deployment",
"labels": {
"app": "nginx"
}
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": {
"app": "nginx"
}
},
"template": {
"metadata": {
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx:1.15.4",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}
`
func NewDeployment() *unstructured.Unstructured {
return testing.Unstructured(DeploymentManifest)
}
func NewFakeConfigMap() *apiv1.ConfigMap {
cm := apiv1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: common.ArgoCDConfigMapName,
Namespace: FakeArgoCDNamespace,
Labels: map[string]string{
"app.kubernetes.io/part-of": "argocd",
},
},
Data: make(map[string]string),
}
return &cm
}
func NewFakeSecret(policy ...string) *apiv1.Secret {
secret := apiv1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: common.ArgoCDSecretName,
Namespace: FakeArgoCDNamespace,
},
Data: map[string][]byte{
"admin.password": []byte("test"),
"server.secretkey": []byte("test"),
},
}
return &secret
}
type interfaceLister struct {
appProjects appclient.AppProjectInterface
}
func (l interfaceLister) List(selector labels.Selector) ([]*v1alpha1.AppProject, error) {
res, err := l.appProjects.List(context.Background(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
items := make([]*v1alpha1.AppProject, len(res.Items))
for i := range res.Items {
items[i] = &res.Items[i]
}
return items, nil
}
func (l interfaceLister) Get(name string) (*v1alpha1.AppProject, error) {
return l.appProjects.Get(context.Background(), name, metav1.GetOptions{})
}
func NewFakeProjListerFromInterface(appProjects appclient.AppProjectInterface) applister.AppProjectNamespaceLister {
return &interfaceLister{appProjects: appProjects}
}
func NewFakeProjLister(objects ...runtime.Object) applister.AppProjectNamespaceLister {
fakeAppClientset := apps.NewSimpleClientset(objects...)
factory := appinformer.NewSharedInformerFactoryWithOptions(fakeAppClientset, 0, appinformer.WithNamespace(""), appinformer.WithTweakListOptions(func(options *metav1.ListOptions) {}))
projInformer := factory.Argoproj().V1alpha1().AppProjects().Informer()
cancel := StartInformer(projInformer)
defer cancel()
return factory.Argoproj().V1alpha1().AppProjects().Lister().AppProjects(FakeArgoCDNamespace)
}
func NewInMemoryRedis() (*redis.Client, func()) {
mr, err := miniredis.Run()
if err != nil {
panic(err)
}
return redis.NewClient(&redis.Options{Addr: mr.Addr()}), mr.Close
}