github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/typescaffold/scaffold_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package typescaffold_test 18 19 import ( 20 "bytes" 21 "testing" 22 23 "sigs.k8s.io/controller-tools/pkg/typescaffold" 24 ) 25 26 func TestScaffold(t *testing.T) { 27 tests := []struct { 28 name string 29 opts typescaffold.ScaffoldOptions 30 }{ 31 { 32 name: "kind only", 33 opts: typescaffold.ScaffoldOptions{ 34 Resource: typescaffold.Resource{ 35 Kind: "Foo", 36 }, 37 }, 38 }, 39 { 40 name: "kind and resource", 41 opts: typescaffold.ScaffoldOptions{ 42 Resource: typescaffold.Resource{ 43 Kind: "Foo", 44 Resource: "foos", 45 }, 46 }, 47 }, 48 { 49 name: "namespaced", 50 opts: typescaffold.ScaffoldOptions{ 51 Resource: typescaffold.Resource{ 52 Kind: "Foo", 53 Resource: "foos", 54 Namespaced: true, 55 }, 56 }, 57 }, 58 } 59 60 for _, test := range tests { 61 t.Run(test.name, func(t *testing.T) { 62 err := test.opts.Validate() 63 if err != nil { 64 t.Fatalf("unable to validate scaffold opts: %v", err) 65 } 66 var out bytes.Buffer 67 if err := test.opts.Scaffold(&out); err != nil { 68 t.Fatalf("unable to scaffold types: %v", err) 69 } 70 71 // TODO(directxman12): testing the direct output seems fragile 72 // there must be a better way. 73 }) 74 } 75 } 76 77 func TestInvalidScaffoldOpts(t *testing.T) { 78 tests := []struct { 79 name string 80 opts typescaffold.ScaffoldOptions 81 }{ 82 { 83 name: "bad kind", 84 opts: typescaffold.ScaffoldOptions{ 85 Resource: typescaffold.Resource{ 86 Kind: "Foo_bats", 87 }, 88 }, 89 }, 90 { 91 name: "no kind", 92 opts: typescaffold.ScaffoldOptions{}, 93 }, 94 } 95 96 for _, test := range tests { 97 t.Run(test.name, func(t *testing.T) { 98 err := test.opts.Validate() 99 if err == nil { 100 t.Fatalf("expected error -- those options were invalid") 101 } 102 }) 103 } 104 }