Skip to content

Commit 7325d75

Browse files
Added some tests
1 parent 76e0c16 commit 7325d75

13 files changed

Lines changed: 1463 additions & 226 deletions

api/v1alpha1/virtualcluster_types.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"encoding/json"
21+
2022
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
"sigs.k8s.io/yaml"
2223

2324
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2425
)
@@ -39,11 +40,15 @@ type VirtualClusterSpec struct {
3940
func (in VirtualCluster) GetValues() (map[string]interface{}, error) {
4041
var values map[string]interface{}
4142
if in.Spec.Values != nil {
42-
err := yaml.Unmarshal(in.Spec.Values.Raw, &values)
43+
err := json.Unmarshal(in.Spec.Values.Raw, &values)
4344
if err != nil {
4445
return nil, err
4546
}
4647
}
48+
// Initialize an empty map if values is nil
49+
if values == nil {
50+
values = make(map[string]interface{})
51+
}
4752
return values, nil
4853
}
4954

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
Copyright 2025.
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 v1alpha1
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
)
26+
27+
func TestVirtualCluster_GetValues(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
vc VirtualCluster
31+
wantValues map[string]interface{}
32+
wantErr bool
33+
}{
34+
{
35+
name: "valid values",
36+
vc: VirtualCluster{
37+
Spec: VirtualClusterSpec{
38+
Values: &apiextensionsv1.JSON{Raw: []byte(`{"vcluster": {"image": "rancher/k3s:v1.25.0-k3s1"}}`)},
39+
},
40+
},
41+
wantValues: map[string]interface{}{
42+
"vcluster": map[string]interface{}{
43+
"image": "rancher/k3s:v1.25.0-k3s1",
44+
},
45+
},
46+
wantErr: false,
47+
},
48+
{
49+
name: "complex values",
50+
vc: VirtualCluster{
51+
Spec: VirtualClusterSpec{
52+
Values: &apiextensionsv1.JSON{Raw: []byte(`{
53+
"vcluster": {
54+
"image": "rancher/k3s:v1.25.0-k3s1",
55+
"extraArgs": ["--disable=traefik"]
56+
},
57+
"service": {
58+
"type": "ClusterIP"
59+
},
60+
"persistence": {
61+
"enabled": true,
62+
"size": "10Gi"
63+
}
64+
}`)},
65+
},
66+
},
67+
wantValues: map[string]interface{}{
68+
"vcluster": map[string]interface{}{
69+
"image": "rancher/k3s:v1.25.0-k3s1",
70+
"extraArgs": []interface{}{"--disable=traefik"},
71+
},
72+
"service": map[string]interface{}{
73+
"type": "ClusterIP",
74+
},
75+
"persistence": map[string]interface{}{
76+
"enabled": true,
77+
"size": "10Gi",
78+
},
79+
},
80+
wantErr: false,
81+
},
82+
{
83+
name: "empty values",
84+
vc: VirtualCluster{
85+
Spec: VirtualClusterSpec{
86+
Values: &apiextensionsv1.JSON{Raw: []byte(`{}`)},
87+
},
88+
},
89+
wantValues: map[string]interface{}{},
90+
wantErr: false,
91+
},
92+
{
93+
name: "nil values",
94+
vc: VirtualCluster{
95+
Spec: VirtualClusterSpec{
96+
Values: nil,
97+
},
98+
},
99+
wantValues: map[string]interface{}{},
100+
wantErr: false,
101+
},
102+
{
103+
name: "invalid json",
104+
vc: VirtualCluster{
105+
Spec: VirtualClusterSpec{
106+
Values: &apiextensionsv1.JSON{Raw: []byte(`{invalid: json}`)},
107+
},
108+
},
109+
wantValues: nil,
110+
wantErr: true,
111+
},
112+
}
113+
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
gotValues, err := tt.vc.GetValues()
117+
if tt.wantErr {
118+
assert.Error(t, err)
119+
} else {
120+
assert.NoError(t, err)
121+
assert.Equal(t, tt.wantValues, gotValues)
122+
}
123+
})
124+
}
125+
}
126+
127+
func TestVirtualClusterPhases(t *testing.T) {
128+
// Test VirtualClusterPhase constants are as expected
129+
assert.Equal(t, VirtualClusterPhase("Pending"), VirtualClusterPending)
130+
assert.Equal(t, VirtualClusterPhase("Provisioning"), VirtualClusterProvisioning)
131+
assert.Equal(t, VirtualClusterPhase("Running"), VirtualClusterRunning)
132+
assert.Equal(t, VirtualClusterPhase("Failed"), VirtualClusterFailed)
133+
assert.Equal(t, VirtualClusterPhase("Deleting"), VirtualClusterDeleting)
134+
}
135+
136+
func TestVirtualCluster_DefaultValues(t *testing.T) {
137+
// Test default values for VirtualCluster
138+
vc := VirtualCluster{
139+
ObjectMeta: metav1.ObjectMeta{
140+
Name: "test-vcluster",
141+
Namespace: "default",
142+
},
143+
Spec: VirtualClusterSpec{
144+
Chart: HelmChart{
145+
// Default version should be used when not specified
146+
},
147+
Values: &apiextensionsv1.JSON{Raw: []byte(`{}`)},
148+
},
149+
}
150+
151+
// In a real implementation, you would call a defaulting webhook here
152+
// For testing purposes, we can check that the default value is v0.24.1
153+
// This is assuming you have implemented a defaulting webhook or function
154+
155+
// Simulate defaulting
156+
if vc.Spec.Chart.Version == "" {
157+
vc.Spec.Chart.Version = "v0.24.1"
158+
}
159+
160+
assert.Equal(t, "v0.24.1", vc.Spec.Chart.Version)
161+
}
162+
163+
func TestVirtualClusterStatusConditions(t *testing.T) {
164+
// Test adding and finding conditions in status
165+
vc := VirtualCluster{
166+
Status: VirtualClusterStatus{
167+
Conditions: []metav1.Condition{
168+
{
169+
Type: "Available",
170+
Status: metav1.ConditionTrue,
171+
Reason: "VirtualClusterAvailable",
172+
Message: "VirtualCluster is available",
173+
},
174+
},
175+
},
176+
}
177+
178+
// Test finding existing condition
179+
for _, cond := range vc.Status.Conditions {
180+
if cond.Type == "Available" {
181+
assert.Equal(t, metav1.ConditionTrue, cond.Status)
182+
assert.Equal(t, "VirtualClusterAvailable", cond.Reason)
183+
}
184+
}
185+
186+
// Add another condition
187+
vc.Status.Conditions = append(vc.Status.Conditions, metav1.Condition{
188+
Type: "Deployed",
189+
Status: metav1.ConditionTrue,
190+
Reason: "DeploymentComplete",
191+
Message: "VirtualCluster deployment completed",
192+
})
193+
194+
// Test condition count
195+
assert.Equal(t, 2, len(vc.Status.Conditions))
196+
}

go.mod

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,70 @@ module github.com/prakashmishra1598/openvc
33
go 1.24
44

55
require (
6-
github.com/loft-sh/vcluster-config v0.0.0-20250408172738-9d5a6c680d53
76
github.com/onsi/ginkgo/v2 v2.22.1
87
github.com/onsi/gomega v1.36.2
8+
github.com/stretchr/testify v1.9.0
9+
github.com/xeipuuv/gojsonschema v1.2.0
10+
k8s.io/api v0.32.2
11+
k8s.io/apiextensions-apiserver v0.32.2
912
k8s.io/apimachinery v0.32.2
1013
k8s.io/client-go v0.32.2
1114
sigs.k8s.io/controller-runtime v0.20.1
15+
sigs.k8s.io/yaml v1.4.0
1216
)
1317

1418
require (
1519
cel.dev/expr v0.18.0 // indirect
16-
github.com/AlecAivazis/survey/v2 v2.3.7 // indirect
17-
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
18-
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
1920
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
2021
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
21-
github.com/bahlo/generic-list-go v0.2.0 // indirect
2222
github.com/beorn7/perks v1.0.1 // indirect
23-
github.com/blang/semver v3.5.1+incompatible // indirect
2423
github.com/blang/semver/v4 v4.0.0 // indirect
25-
github.com/buger/jsonparser v1.1.1 // indirect
2624
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
2725
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2826
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2927
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
28+
github.com/evanphx/json-patch v5.8.1+incompatible // indirect
3029
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
31-
github.com/fatih/color v1.18.0 // indirect
3230
github.com/felixge/httpsnoop v1.0.4 // indirect
3331
github.com/fsnotify/fsnotify v1.7.0 // indirect
3432
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
35-
github.com/ghodss/yaml v1.0.0 // indirect
3633
github.com/go-logr/logr v1.4.2 // indirect
3734
github.com/go-logr/stdr v1.2.2 // indirect
3835
github.com/go-logr/zapr v1.3.0 // indirect
3936
github.com/go-openapi/jsonpointer v0.21.0 // indirect
4037
github.com/go-openapi/jsonreference v0.21.0 // indirect
4138
github.com/go-openapi/swag v0.23.0 // indirect
4239
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
43-
github.com/gobuffalo/flect v1.0.3 // indirect
4440
github.com/gogo/protobuf v1.3.2 // indirect
4541
github.com/golang/protobuf v1.5.4 // indirect
4642
github.com/google/btree v1.1.3 // indirect
4743
github.com/google/cel-go v0.22.0 // indirect
4844
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
4945
github.com/google/go-cmp v0.7.0 // indirect
50-
github.com/google/go-github/v30 v30.1.0 // indirect
51-
github.com/google/go-querystring v1.1.0 // indirect
5246
github.com/google/gofuzz v1.2.0 // indirect
5347
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
5448
github.com/google/uuid v1.6.0 // indirect
5549
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
56-
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
5750
github.com/inconshreveable/mousetrap v1.1.0 // indirect
58-
github.com/invopop/jsonschema v0.12.0 // indirect
5951
github.com/josharian/intern v1.0.0 // indirect
6052
github.com/json-iterator/go v1.1.12 // indirect
61-
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect
62-
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
6353
github.com/klauspost/compress v1.17.10 // indirect
64-
github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac // indirect
65-
github.com/loft-sh/vcluster v0.23.0 // indirect
6654
github.com/mailru/easyjson v0.7.7 // indirect
67-
github.com/mattn/go-colorable v0.1.13 // indirect
68-
github.com/mattn/go-isatty v0.0.20 // indirect
69-
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
70-
github.com/moby/term v0.5.0 // indirect
7155
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7256
github.com/modern-go/reflect2 v1.0.2 // indirect
7357
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
7458
github.com/pkg/errors v0.9.1 // indirect
59+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
7560
github.com/prometheus/client_golang v1.20.4 // indirect
7661
github.com/prometheus/client_model v0.6.1 // indirect
7762
github.com/prometheus/common v0.60.0 // indirect
7863
github.com/prometheus/procfs v0.15.1 // indirect
79-
github.com/rhysd/go-github-selfupdate v1.2.3 // indirect
80-
github.com/sirupsen/logrus v1.9.3 // indirect
8164
github.com/spf13/cobra v1.9.1 // indirect
8265
github.com/spf13/pflag v1.0.6 // indirect
8366
github.com/stoewer/go-strcase v1.3.0 // indirect
84-
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
85-
github.com/ulikunitz/xz v0.5.11 // indirect
86-
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
8767
github.com/x448/float16 v0.8.4 // indirect
8868
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
8969
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
90-
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
9170
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
9271
go.opentelemetry.io/otel v1.30.0 // indirect
9372
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
@@ -98,9 +77,7 @@ require (
9877
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
9978
go.uber.org/multierr v1.11.0 // indirect
10079
go.uber.org/zap v1.27.0 // indirect
101-
golang.org/x/crypto v0.33.0 // indirect
10280
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
103-
golang.org/x/mod v0.24.0 // indirect
10481
golang.org/x/net v0.35.0 // indirect
10582
golang.org/x/oauth2 v0.23.0 // indirect
10683
golang.org/x/sync v0.11.0 // indirect
@@ -116,19 +93,13 @@ require (
11693
google.golang.org/protobuf v1.36.1 // indirect
11794
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
11895
gopkg.in/inf.v0 v0.9.1 // indirect
119-
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
120-
gopkg.in/yaml.v2 v2.4.0 // indirect
12196
gopkg.in/yaml.v3 v3.0.1 // indirect
122-
k8s.io/api v0.32.2 // indirect
123-
k8s.io/apiextensions-apiserver v0.32.2 // indirect
12497
k8s.io/apiserver v0.32.2 // indirect
12598
k8s.io/component-base v0.32.2 // indirect
12699
k8s.io/klog/v2 v2.130.1 // indirect
127100
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
128101
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
129102
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 // indirect
130-
sigs.k8s.io/controller-tools v0.17.3 // indirect
131103
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
132104
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
133-
sigs.k8s.io/yaml v1.4.0 // indirect
134105
)

0 commit comments

Comments
 (0)