Debian Codegraph @

pkg/generators/config_test.go

raw
1 /*
2 Copyright 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 generators
18
19 import (
20 "testing"
21
22 "k8s.io/gengo/v2/generator"
23 "k8s.io/gengo/v2/types"
24 "k8s.io/kube-openapi/cmd/openapi-gen/args"
25 )
26
27 func TestIsReadOnlyPkg(t *testing.T) {
28 tests := []struct {
29 name string
30 pkgPath string
31 readOnlyPkgs []string
32 want bool
33 }{
34 {
35 name: "nil readonly pkgs matches nothing",
36 pkgPath: "k8s.io/apimachinery/pkg/runtime",
37 readOnlyPkgs: nil,
38 want: false,
39 },
40 {
41 name: "empty readonly pkgs matches nothing",
42 pkgPath: "k8s.io/apimachinery/pkg/runtime",
43 readOnlyPkgs: []string{},
44 want: false,
45 },
46 {
47 name: "exact match",
48 pkgPath: "k8s.io/apimachinery/pkg/apis/meta/v1",
49 readOnlyPkgs: []string{"k8s.io/apimachinery/pkg/apis/meta/v1"},
50 want: true,
51 },
52 {
53 name: "no prefix match",
54 pkgPath: "k8s.io/apimachinery/pkg/apis/meta/v1",
55 readOnlyPkgs: []string{"k8s.io/apimachinery"},
56 want: false,
57 },
58 {
59 name: "non-readonly package",
60 pkgPath: "k8s.io/sample-apiserver/pkg/apis/wardle/v1beta1",
61 readOnlyPkgs: []string{"k8s.io/apimachinery/pkg/apis/meta/v1"},
62 want: false,
63 },
64 {
65 name: "multiple readonly pkgs",
66 pkgPath: "k8s.io/apimachinery/pkg/runtime",
67 readOnlyPkgs: []string{"k8s.io/apimachinery/pkg/apis/meta/v1", "k8s.io/apimachinery/pkg/runtime"},
68 want: true,
69 },
70 }
71
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 got := isReadOnlyPkg(tt.pkgPath, tt.readOnlyPkgs)
75 if got != tt.want {
76 t.Errorf("isReadOnlyPkg(%q, %v) = %v, want %v", tt.pkgPath, tt.readOnlyPkgs, got, tt.want)
77 }
78 })
79 }
80 }
81
82 func TestGetModelNameTargets_ReadOnlyPkgs(t *testing.T) {
83 localPkg := "k8s.io/sample-apiserver/pkg/apis/wardle/v1beta1"
84 depPkg := "k8s.io/apimachinery/pkg/apis/meta/v1"
85
86 // Build a minimal generator.Context with two input packages:
87 // one "local" package and one "dependency" package. Both have
88 // the +k8s:openapi-model-package tag and a public struct type,
89 // so without readonly pkgs both would produce targets.
90 universe := types.Universe{
91 localPkg: {
92 Path: localPkg,
93 Dir: "/fake/local/wardle/v1beta1",
94 Name: "v1beta1",
95 Comments: []string{
96 "+k8s:openapi-model-package=io.k8s.sample-apiserver.pkg.apis.wardle.v1beta1",
97 },
98 Types: map[string]*types.Type{
99 "Flunder": {
100 Name: types.Name{Package: localPkg, Name: "Flunder"},
101 Kind: types.Struct,
102 },
103 },
104 },
105 depPkg: {
106 Path: depPkg,
107 Dir: "/fake/gomodcache/k8s.io/apimachinery/pkg/apis/meta/v1",
108 Name: "v1",
109 Comments: []string{
110 "+k8s:openapi-model-package=io.k8s.apimachinery.pkg.apis.meta.v1",
111 },
112 Types: map[string]*types.Type{
113 "ObjectMeta": {
114 Name: types.Name{Package: depPkg, Name: "ObjectMeta"},
115 Kind: types.Struct,
116 },
117 },
118 },
119 }
120
121 ctx := &generator.Context{
122 Inputs: []string{localPkg, depPkg},
123 Universe: universe,
124 }
125
126 tests := []struct {
127 name string
128 readOnlyPkgs []string
129 wantTargetPkgs []string
130 }{
131 {
132 name: "no readonly pkgs generates for all packages",
133 readOnlyPkgs: nil,
134 wantTargetPkgs: []string{localPkg, depPkg},
135 },
136 {
137 name: "readonly dep excludes dependency",
138 readOnlyPkgs: []string{depPkg},
139 wantTargetPkgs: []string{localPkg},
140 },
141 {
142 name: "readonly local excludes local",
143 readOnlyPkgs: []string{localPkg},
144 wantTargetPkgs: []string{depPkg},
145 },
146 {
147 name: "both readonly excludes all",
148 readOnlyPkgs: []string{localPkg, depPkg},
149 wantTargetPkgs: nil,
150 },
151 }
152
153 for _, tt := range tests {
154 t.Run(tt.name, func(t *testing.T) {
155 a := &args.Args{
156 OutputModelNameFile: "zz_generated.model_name.go",
157 ReadOnlyPkgs: tt.readOnlyPkgs,
158 }
159
160 targets := GetModelNameTargets(ctx, a, nil)
161
162 gotPkgs := make([]string, 0, len(targets))
163 for _, tgt := range targets {
164 st, ok := tgt.(*generator.SimpleTarget)
165 if !ok {
166 t.Fatalf("unexpected target type %T", tgt)
167 }
168 gotPkgs = append(gotPkgs, st.PkgPath)
169 }
170
171 if len(gotPkgs) != len(tt.wantTargetPkgs) {
172 t.Fatalf("got %d targets %v, want %d targets %v", len(gotPkgs), gotPkgs, len(tt.wantTargetPkgs), tt.wantTargetPkgs)
173 }
174
175 for i, want := range tt.wantTargetPkgs {
176 if gotPkgs[i] != want {
177 t.Errorf("target[%d] = %q, want %q", i, gotPkgs[i], want)
178 }
179 }
180 })
181 }
182 }