vitess.io/vitess@v0.16.2/go/vt/vtadmin/testutil/authztestgen/template.go (about) 1 /* 2 Copyright 2022 The Vitess 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 main 18 19 const _t = `// Code generated by testutil/authztestgen. DO NOT EDIT. 20 21 /* 22 Copyright 2022 The Vitess Authors. 23 24 Licensed under the Apache License, Version 2.0 (the "License"); 25 you may not use this file except in compliance with the License. 26 You may obtain a copy of the License at 27 28 http://www.apache.org/licenses/LICENSE-2.0 29 30 Unless required by applicable law or agreed to in writing, software 31 distributed under the License is distributed on an "AS IS" BASIS, 32 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 See the License for the specific language governing permissions and 34 limitations under the License. 35 */ 36 37 package {{ .Package }} 38 39 import ( 40 "context" 41 "fmt" 42 "testing" 43 "time" 44 45 "github.com/stretchr/testify/assert" 46 "github.com/stretchr/testify/require" 47 48 "vitess.io/vitess/go/vt/topo/topoproto" 49 "vitess.io/vitess/go/vt/vtadmin" 50 "vitess.io/vitess/go/vt/vtadmin/cluster" 51 "vitess.io/vitess/go/vt/vtadmin/rbac" 52 "vitess.io/vitess/go/vt/vtadmin/testutil" 53 "vitess.io/vitess/go/vt/vtadmin/vtctldclient/fakevtctldclient" 54 55 logutilpb "vitess.io/vitess/go/vt/proto/logutil" 56 mysqlctlpb "vitess.io/vitess/go/vt/proto/mysqlctl" 57 tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" 58 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 59 vschemapb "vitess.io/vitess/go/vt/proto/vschema" 60 vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" 61 vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" 62 ) 63 64 {{ range .Tests }} 65 func Test{{ .Method }}(t *testing.T) { 66 t.Parallel() 67 68 opts := vtadmin.Options{ 69 RBAC: &rbac.Config{ 70 Rules: []*struct{ 71 Resource string 72 Actions []string 73 Subjects []string 74 Clusters []string 75 }{ 76 {{- range .Rules }} 77 { 78 Resource: "{{ .Resource }}", 79 Actions: []string{ {{ range .Actions }}"{{ . }}",{{ end }} }, 80 Subjects: []string{ {{ range .Subjects }}"{{ . }}",{{ end }} }, 81 Clusters: []string{ {{ range .Clusters }}"{{ . }}",{{ end }} }, 82 }, 83 {{- end }} 84 }, 85 }, 86 } 87 err := opts.RBAC.Reify() 88 require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) 89 90 {{ if not .SerializeCases }} 91 api := vtadmin.NewAPI(testClusters(t), opts) 92 t.Cleanup(func() { 93 if err := api.Close(); err != nil { 94 t.Logf("api did not close cleanly: %s", err.Error()) 95 } 96 }) 97 {{ end }} 98 99 {{ with $test := . -}} 100 {{ range .Cases }} 101 t.Run("{{ .Name }}", func(t *testing.T) { 102 t.Parallel() 103 {{ if $test.SerializeCases }} 104 api := vtadmin.NewAPI(testClusters(t), opts) 105 t.Cleanup(func() { 106 if err := api.Close(); err != nil { 107 t.Logf("api did not close cleanly: %s", err.Error()) 108 } 109 }) 110 {{ end }} 111 112 {{ getActor .Actor }} 113 ctx := context.Background() 114 if actor != nil { 115 ctx = rbac.NewContext(ctx, actor) 116 } 117 {{ if .IncludeErrorVar }} 118 resp, err := api.{{ $test.Method }}(ctx, {{ $test.Request }}) 119 {{ else }} 120 resp, _ := api.{{ $test.Method }}(ctx, {{ $test.Request }}) 121 {{ end }} 122 {{- with $case := . -}} 123 {{ range .Assertions }} 124 {{- writeAssertion . $test $case }} 125 {{ end }} 126 {{- end -}} 127 }) 128 {{ end }} 129 {{- end -}} 130 } 131 {{ end }} 132 133 func testClusters(t testing.TB) []*cluster.Cluster { 134 configs := []testutil.TestClusterConfig{ 135 {{ range .Clusters -}} 136 {{- with $cluster := . -}} 137 { 138 Cluster: &vtadminpb.Cluster{ 139 Id: "{{ .ID }}", 140 Name: "{{ .Name }}", 141 }, 142 VtctldClient: &fakevtctldclient.VtctldClient{ 143 {{- range .FakeVtctldClientResults }} 144 {{ .FieldName }}: {{ .Type }}{ 145 {{ .Value }} 146 }, 147 {{- end }} 148 }, 149 Tablets: []*vtadminpb.Tablet{ 150 {{- range .DBTablets }} 151 { 152 Cluster: &vtadminpb.Cluster{Id: "{{ $cluster.ID }}", Name: "{{ $cluster.Name }}" }, 153 Tablet: &topodatapb.Tablet{ 154 Alias: &topodatapb.TabletAlias{ 155 Cell: "{{ .Tablet.Alias.Cell }}", 156 Uid: {{ .Tablet.Alias.Uid }}, 157 }, 158 Keyspace: "{{ .Tablet.Keyspace }}", 159 Type: topodatapb.TabletType_{{ .Tablet.Type }}, 160 }, 161 State: vtadminpb.Tablet_{{ .State }}, 162 }, 163 {{- end }} 164 }, 165 Config: &cluster.Config{ 166 TopoReadPoolConfig: &cluster.RPCPoolConfig{ 167 Size: 100, 168 WaitTimeout: time.Millisecond * 50, 169 }, 170 }, 171 }, 172 {{- end -}} 173 {{- end }} 174 } 175 176 return testutil.BuildClusters(t, configs...) 177 } 178 ` 179 180 const _doct = `{{ range .Methods -}} 181 {{ formatDocRow . }} 182 {{ end -}} 183 `