Skip to content

Commit 498045b

Browse files
committed
api: unit test DefaultVersionConfig
Cover the Disabled-default branch added in the previous commit, plus the nil/Enabled state, multi-version, missing-default and empty-versions cases, and that the returned pointer aliases spec.Versions.
1 parent 617cf13 commit 498045b

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2026 Google LLC
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 v1alpha1
16+
17+
import (
18+
"testing"
19+
20+
"k8s.io/utils/ptr"
21+
)
22+
23+
func TestDefaultVersionConfig(t *testing.T) {
24+
version := func(name string, state *SandboxVersionState) SandboxVersionConfig {
25+
return SandboxVersionConfig{Name: name, State: state}
26+
}
27+
28+
tests := []struct {
29+
name string
30+
spec SandboxConfigSpec
31+
wantName string // "" means no default is expected
32+
}{{
33+
name: "default with nil state is returned",
34+
spec: SandboxConfigSpec{
35+
DefaultVersion: "v1",
36+
Versions: []SandboxVersionConfig{version("v1", nil)},
37+
},
38+
wantName: "v1",
39+
}, {
40+
name: "default explicitly Enabled is returned",
41+
spec: SandboxConfigSpec{
42+
DefaultVersion: "v1",
43+
Versions: []SandboxVersionConfig{version("v1", ptr.To(SandboxVersionStateEnabled))},
44+
},
45+
wantName: "v1",
46+
}, {
47+
name: "default picked out of several versions",
48+
spec: SandboxConfigSpec{
49+
DefaultVersion: "v2",
50+
Versions: []SandboxVersionConfig{
51+
version("v1", ptr.To(SandboxVersionStateDisabled)),
52+
version("v2", nil),
53+
version("v3", nil),
54+
},
55+
},
56+
wantName: "v2",
57+
}, {
58+
name: "Disabled default is treated as missing",
59+
spec: SandboxConfigSpec{
60+
DefaultVersion: "v1",
61+
Versions: []SandboxVersionConfig{
62+
version("v1", ptr.To(SandboxVersionStateDisabled)),
63+
version("v2", nil),
64+
},
65+
},
66+
}, {
67+
name: "defaultVersion names no entry",
68+
spec: SandboxConfigSpec{
69+
DefaultVersion: "v9",
70+
Versions: []SandboxVersionConfig{version("v1", nil)},
71+
},
72+
}, {
73+
name: "no versions",
74+
spec: SandboxConfigSpec{DefaultVersion: "v1"},
75+
}}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
got, ok := tt.spec.DefaultVersionConfig()
80+
if tt.wantName == "" {
81+
if ok || got != nil {
82+
t.Fatalf("DefaultVersionConfig() = %+v, %v; want nil, false", got, ok)
83+
}
84+
return
85+
}
86+
if !ok || got == nil {
87+
t.Fatalf("DefaultVersionConfig() = %+v, %v; want %q, true", got, ok, tt.wantName)
88+
}
89+
if got.Name != tt.wantName {
90+
t.Fatalf("DefaultVersionConfig().Name = %q; want %q", got.Name, tt.wantName)
91+
}
92+
// The pointer must alias the spec so callers see the real entry,
93+
// not a copy.
94+
if got != &tt.spec.Versions[indexOf(tt.spec.Versions, tt.wantName)] {
95+
t.Fatalf("DefaultVersionConfig() returned a copy, want pointer into spec.Versions")
96+
}
97+
})
98+
}
99+
}
100+
101+
func indexOf(versions []SandboxVersionConfig, name string) int {
102+
for i := range versions {
103+
if versions[i].Name == name {
104+
return i
105+
}
106+
}
107+
return -1
108+
}

0 commit comments

Comments
 (0)