github.com/argoproj/argo-cd@v1.8.7/test/testdata.go (about) 1 package test 2 3 import ( 4 "context" 5 6 "github.com/argoproj/gitops-engine/pkg/utils/testing" 7 apiv1 "k8s.io/api/core/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 10 "k8s.io/apimachinery/pkg/labels" 11 "k8s.io/apimachinery/pkg/runtime" 12 13 "github.com/argoproj/argo-cd/common" 14 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 15 apps "github.com/argoproj/argo-cd/pkg/client/clientset/versioned/fake" 16 appclient "github.com/argoproj/argo-cd/pkg/client/clientset/versioned/typed/application/v1alpha1" 17 appinformer "github.com/argoproj/argo-cd/pkg/client/informers/externalversions" 18 applister "github.com/argoproj/argo-cd/pkg/client/listers/application/v1alpha1" 19 ) 20 21 const ( 22 FakeArgoCDNamespace = "fake-argocd-ns" 23 FakeDestNamespace = "fake-dest-ns" 24 FakeClusterURL = "https://fake-cluster:443" 25 ) 26 27 func NewControllerRevision() *unstructured.Unstructured { 28 return testing.Unstructured(` 29 kind: ControllerRevision 30 apiVersion: metacontroller.k8s.io/v1alpha1 31 metadata: 32 labels: 33 app: nginx 34 controller.kubernetes.io/hash: c7cd8d57f 35 name: web-c7cd8d57f 36 namespace: statefulset 37 revision: 2 38 `) 39 } 40 41 var DeploymentManifest = ` 42 { 43 "apiVersion": "apps/v1", 44 "kind": "Deployment", 45 "metadata": { 46 "name": "nginx-deployment", 47 "labels": { 48 "app": "nginx" 49 } 50 }, 51 "spec": { 52 "replicas": 3, 53 "selector": { 54 "matchLabels": { 55 "app": "nginx" 56 } 57 }, 58 "template": { 59 "metadata": { 60 "labels": { 61 "app": "nginx" 62 } 63 }, 64 "spec": { 65 "containers": [ 66 { 67 "name": "nginx", 68 "image": "nginx:1.15.4", 69 "ports": [ 70 { 71 "containerPort": 80 72 } 73 ] 74 } 75 ] 76 } 77 } 78 } 79 } 80 ` 81 82 func NewDeployment() *unstructured.Unstructured { 83 return testing.Unstructured(DeploymentManifest) 84 } 85 86 func NewFakeConfigMap() *apiv1.ConfigMap { 87 cm := apiv1.ConfigMap{ 88 TypeMeta: metav1.TypeMeta{ 89 Kind: "ConfigMap", 90 APIVersion: "v1", 91 }, 92 ObjectMeta: metav1.ObjectMeta{ 93 Name: common.ArgoCDConfigMapName, 94 Namespace: FakeArgoCDNamespace, 95 Labels: map[string]string{ 96 "app.kubernetes.io/part-of": "argocd", 97 }, 98 }, 99 Data: make(map[string]string), 100 } 101 return &cm 102 } 103 104 func NewFakeSecret(policy ...string) *apiv1.Secret { 105 secret := apiv1.Secret{ 106 TypeMeta: metav1.TypeMeta{ 107 Kind: "Secret", 108 APIVersion: "v1", 109 }, 110 ObjectMeta: metav1.ObjectMeta{ 111 Name: common.ArgoCDSecretName, 112 Namespace: FakeArgoCDNamespace, 113 }, 114 Data: map[string][]byte{ 115 "admin.password": []byte("test"), 116 "server.secretkey": []byte("test"), 117 }, 118 } 119 return &secret 120 } 121 122 type interfaceLister struct { 123 appProjects appclient.AppProjectInterface 124 } 125 126 func (l interfaceLister) List(selector labels.Selector) ([]*v1alpha1.AppProject, error) { 127 res, err := l.appProjects.List(context.Background(), metav1.ListOptions{LabelSelector: selector.String()}) 128 if err != nil { 129 return nil, err 130 } 131 items := make([]*v1alpha1.AppProject, len(res.Items)) 132 for i := range res.Items { 133 items[i] = &res.Items[i] 134 } 135 return items, nil 136 } 137 138 func (l interfaceLister) Get(name string) (*v1alpha1.AppProject, error) { 139 return l.appProjects.Get(context.Background(), name, metav1.GetOptions{}) 140 } 141 142 func NewFakeProjListerFromInterface(appProjects appclient.AppProjectInterface) applister.AppProjectNamespaceLister { 143 return &interfaceLister{appProjects: appProjects} 144 } 145 146 func NewFakeProjLister(objects ...runtime.Object) applister.AppProjectNamespaceLister { 147 fakeAppClientset := apps.NewSimpleClientset(objects...) 148 factory := appinformer.NewFilteredSharedInformerFactory(fakeAppClientset, 0, "", func(options *metav1.ListOptions) {}) 149 projInformer := factory.Argoproj().V1alpha1().AppProjects().Informer() 150 cancel := StartInformer(projInformer) 151 defer cancel() 152 return factory.Argoproj().V1alpha1().AppProjects().Lister().AppProjects(FakeArgoCDNamespace) 153 }