github.com/jmrodri/operator-sdk@v0.5.0/pkg/scaffold/crd_test.go (about) 1 // Copyright 2018 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package scaffold 16 17 import ( 18 "os" 19 "path/filepath" 20 "strings" 21 "testing" 22 23 "github.com/operator-framework/operator-sdk/internal/util/diffutil" 24 "github.com/operator-framework/operator-sdk/pkg/scaffold/input" 25 ) 26 27 func TestCRDGoProject(t *testing.T) { 28 r, err := NewResource("cache.example.com/v1alpha1", "Memcached") 29 if err != nil { 30 t.Fatal(err) 31 } 32 s, buf := setupScaffoldAndWriter() 33 absPath, err := os.Getwd() 34 if err != nil { 35 t.Fatal(err) 36 } 37 // Set the project and repo paths to {abs}/test/test-framework, which 38 // contains pkg/apis for the memcached-operator. 39 tfDir := filepath.Join("test", "test-framework") 40 pkgIdx := strings.Index(absPath, "pkg") 41 cfg := &input.Config{ 42 Repo: filepath.Join(absPath[strings.Index(absPath, "github.com"):pkgIdx], tfDir), 43 AbsProjectPath: filepath.Join(absPath[:pkgIdx], tfDir), 44 ProjectName: tfDir, 45 } 46 if err := os.Chdir(cfg.AbsProjectPath); err != nil { 47 t.Fatal(err) 48 } 49 defer func() { os.Chdir(absPath) }() 50 err = s.Execute(cfg, &CRD{ 51 Input: input.Input{Path: filepath.Join(tfDir, "cache_v1alpha1_memcached.yaml")}, 52 Resource: r, 53 IsOperatorGo: true, 54 }) 55 if err != nil { 56 t.Fatalf("Failed to execute the scaffold: (%v)", err) 57 } 58 59 if crdGoExp != buf.String() { 60 diffs := diffutil.Diff(crdGoExp, buf.String()) 61 t.Fatalf("Expected vs actual differs.\n%v", diffs) 62 } 63 } 64 65 const crdGoExp = `apiVersion: apiextensions.k8s.io/v1beta1 66 kind: CustomResourceDefinition 67 metadata: 68 creationTimestamp: null 69 name: memcacheds.cache.example.com 70 spec: 71 group: cache.example.com 72 names: 73 kind: Memcached 74 listKind: MemcachedList 75 plural: memcacheds 76 singular: memcached 77 scope: Namespaced 78 subresources: 79 status: {} 80 validation: 81 openAPIV3Schema: 82 properties: 83 apiVersion: 84 type: string 85 kind: 86 type: string 87 metadata: 88 type: object 89 spec: 90 properties: 91 size: 92 format: int32 93 type: integer 94 required: 95 - size 96 type: object 97 status: 98 properties: 99 nodes: 100 items: 101 type: string 102 type: array 103 required: 104 - nodes 105 type: object 106 version: v1alpha1 107 versions: 108 - name: v1alpha1 109 served: true 110 storage: true 111 ` 112 113 func TestCRDNonGoProject(t *testing.T) { 114 r, err := NewResource(appApiVersion, appKind) 115 if err != nil { 116 t.Fatal(err) 117 } 118 s, buf := setupScaffoldAndWriter() 119 err = s.Execute(appConfig, &CRD{Resource: r}) 120 if err != nil { 121 t.Fatalf("Failed to execute the scaffold: (%v)", err) 122 } 123 124 if crdNonGoExp != buf.String() { 125 diffs := diffutil.Diff(crdNonGoExp, buf.String()) 126 t.Fatalf("Expected vs actual differs.\n%v", diffs) 127 } 128 } 129 130 const crdNonGoExp = `apiVersion: apiextensions.k8s.io/v1beta1 131 kind: CustomResourceDefinition 132 metadata: 133 creationTimestamp: null 134 name: appservices.app.example.com 135 spec: 136 group: app.example.com 137 names: 138 kind: AppService 139 listKind: AppServiceList 140 plural: appservices 141 singular: appservice 142 scope: Namespaced 143 subresources: 144 status: {} 145 version: v1alpha1 146 versions: 147 - name: v1alpha1 148 served: true 149 storage: true 150 `