github.com/theishshah/operator-sdk@v0.6.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 name: memcacheds.cache.example.com 69 spec: 70 group: cache.example.com 71 names: 72 kind: Memcached 73 listKind: MemcachedList 74 plural: memcacheds 75 singular: memcached 76 scope: Namespaced 77 subresources: 78 status: {} 79 validation: 80 openAPIV3Schema: 81 properties: 82 apiVersion: 83 type: string 84 kind: 85 type: string 86 metadata: 87 type: object 88 spec: 89 properties: 90 size: 91 format: int32 92 type: integer 93 required: 94 - size 95 type: object 96 status: 97 properties: 98 nodes: 99 items: 100 type: string 101 type: array 102 required: 103 - nodes 104 type: object 105 version: v1alpha1 106 versions: 107 - name: v1alpha1 108 served: true 109 storage: true 110 ` 111 112 func TestCRDNonGoProject(t *testing.T) { 113 r, err := NewResource(appApiVersion, appKind) 114 if err != nil { 115 t.Fatal(err) 116 } 117 s, buf := setupScaffoldAndWriter() 118 err = s.Execute(appConfig, &CRD{Resource: r}) 119 if err != nil { 120 t.Fatalf("Failed to execute the scaffold: (%v)", err) 121 } 122 123 if crdNonGoExp != buf.String() { 124 diffs := diffutil.Diff(crdNonGoExp, buf.String()) 125 t.Fatalf("Expected vs actual differs.\n%v", diffs) 126 } 127 } 128 129 const crdNonGoExp = `apiVersion: apiextensions.k8s.io/v1beta1 130 kind: CustomResourceDefinition 131 metadata: 132 name: appservices.app.example.com 133 spec: 134 group: app.example.com 135 names: 136 kind: AppService 137 listKind: AppServiceList 138 plural: appservices 139 singular: appservice 140 scope: Namespaced 141 subresources: 142 status: {} 143 version: v1alpha1 144 versions: 145 - name: v1alpha1 146 served: true 147 storage: true 148 `