@@ -7,6 +7,7 @@ package v2
77
88import (
99 "context"
10+ "encoding/json"
1011 "testing"
1112
1213 "github.com/stretchr/testify/require"
@@ -15,129 +16,33 @@ import (
1516 "github.com/microsoft/dcp/pkg/commonapi"
1617)
1718
19+ // Verifies that PhysicalContainerVolume.Validate requires a valid runtime volume name and labels.
1820func TestPhysicalContainerVolumeValidate (t * testing.T ) {
19- testCases := []struct {
21+ t .Parallel ()
22+ for _ , testCase := range []struct {
2023 name string
21- volume PhysicalContainerVolume
24+ spec PhysicalContainerVolumeSpec
2225 expectedError string
2326 }{
24- {
25- name : "valid created volume" ,
26- volume : PhysicalContainerVolume {
27- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
28- Spec : PhysicalContainerVolumeSpec {
29- Volume : & PhysicalContainerVolumeConfig {
30- VolumeName : "test-runtime-volume" ,
31- Labels : []commonapi.Label {{Key : "test-label" , Value : "test-value" }},
32- },
33- },
34- },
35- },
36- {
37- name : "valid tracked volume" ,
38- volume : PhysicalContainerVolume {
39- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
40- Spec : PhysicalContainerVolumeSpec {VolumeID : "test-runtime-volume" },
41- },
42- },
43- {
44- name : "valid replacement volume" ,
45- volume : PhysicalContainerVolume {
46- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
47- Spec : PhysicalContainerVolumeSpec {
48- Volume : & PhysicalContainerVolumeConfig {
49- VolumeName : "test-runtime-volume" ,
50- ReplaceExisting : true ,
51- },
52- },
53- },
54- },
55- {
56- name : "missing namespace" ,
57- volume : PhysicalContainerVolume {
58- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" },
59- Spec : PhysicalContainerVolumeSpec {
60- Volume : & PhysicalContainerVolumeConfig {VolumeName : "test-runtime-volume" },
61- },
62- },
63- expectedError : "metadata.namespace" ,
64- },
65- {
66- name : "missing volume source" ,
67- volume : PhysicalContainerVolume {
68- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
69- },
70- expectedError : "spec" ,
71- },
72- {
73- name : "volume ID and config" ,
74- volume : PhysicalContainerVolume {
75- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
76- Spec : PhysicalContainerVolumeSpec {
77- VolumeID : "test-runtime-volume" ,
78- Volume : & PhysicalContainerVolumeConfig {VolumeName : "another-runtime-volume" },
79- },
80- },
81- expectedError : "spec.volume" ,
82- },
83- {
84- name : "missing volume name" ,
85- volume : PhysicalContainerVolume {
86- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
87- Spec : PhysicalContainerVolumeSpec {
88- Volume : & PhysicalContainerVolumeConfig {},
89- },
90- },
91- expectedError : "spec.volume.volumeName" ,
92- },
93- {
94- name : "whitespace-only volume name" ,
95- volume : PhysicalContainerVolume {
96- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
97- Spec : PhysicalContainerVolumeSpec {
98- Volume : & PhysicalContainerVolumeConfig {VolumeName : " " },
99- },
100- },
101- expectedError : "spec.volume.volumeName" ,
102- },
103- {
104- name : "invalid volume name" ,
105- volume : PhysicalContainerVolume {
106- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
107- Spec : PhysicalContainerVolumeSpec {
108- Volume : & PhysicalContainerVolumeConfig {VolumeName : "invalid volume" },
109- },
110- },
111- expectedError : "volumeName must match regex" ,
112- },
113- {
114- name : "single-character volume name" ,
115- volume : PhysicalContainerVolume {
116- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
117- Spec : PhysicalContainerVolumeSpec {
118- Volume : & PhysicalContainerVolumeConfig {VolumeName : "v" },
119- },
120- },
121- expectedError : "volumeName must match regex" ,
122- },
123- {
124- name : "missing label key" ,
125- volume : PhysicalContainerVolume {
126- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
127- Spec : PhysicalContainerVolumeSpec {
128- Volume : & PhysicalContainerVolumeConfig {
129- VolumeName : "test-runtime-volume" ,
130- Labels : []commonapi.Label {{Value : "test-value" }},
131- },
132- },
133- },
134- expectedError : "spec.volume.labels[0].key" ,
135- },
136- }
137-
138- for _ , testCase := range testCases {
27+ {name : "retained" , spec : PhysicalContainerVolumeSpec {VolumeName : "test-runtime-volume" }},
28+ {name : "removable" , spec : PhysicalContainerVolumeSpec {VolumeName : "test-runtime-volume" , RemoveRuntimeVolumeOnDelete : true }},
29+ {name : "labelled" , spec : PhysicalContainerVolumeSpec {
30+ VolumeName : "test-runtime-volume" , Labels : []commonapi.Label {{Key : "test-label" , Value : "test-value" }},
31+ }},
32+ {name : "missing name" , expectedError : "spec.volumeName" },
33+ {name : "whitespace name" , spec : PhysicalContainerVolumeSpec {VolumeName : " " }, expectedError : "spec.volumeName" },
34+ {name : "invalid name" , spec : PhysicalContainerVolumeSpec {VolumeName : "invalid volume" }, expectedError : "spec.volumeName" },
35+ {name : "single character" , spec : PhysicalContainerVolumeSpec {VolumeName : "v" }, expectedError : "spec.volumeName" },
36+ {name : "missing label key" , spec : PhysicalContainerVolumeSpec {
37+ VolumeName : "test-runtime-volume" , Labels : []commonapi.Label {{Value : "test-value" }},
38+ }, expectedError : "spec.labels[0].key" },
39+ } {
13940 t .Run (testCase .name , func (t * testing.T ) {
140- errorList := testCase .volume .Validate (context .Background ())
41+ volume := & PhysicalContainerVolume {
42+ ObjectMeta : metav1.ObjectMeta {Name : "volume" , Namespace : "test-namespace" },
43+ Spec : testCase .spec ,
44+ }
45+ errorList := volume .Validate (context .Background ())
14146 if testCase .expectedError == "" {
14247 require .Empty (t , errorList )
14348 } else {
@@ -146,37 +51,78 @@ func TestPhysicalContainerVolumeValidate(t *testing.T) {
14651 }
14752 })
14853 }
54+ volume := & PhysicalContainerVolume {
55+ ObjectMeta : metav1.ObjectMeta {Name : "volume" },
56+ Spec : PhysicalContainerVolumeSpec {VolumeName : "runtime-volume" },
57+ }
58+ require .Contains (t , volume .Validate (context .Background ()).ToAggregate ().Error (), "metadata.namespace" )
14959}
15060
61+ // Verifies that PhysicalContainerVolume.ValidateUpdate keeps every top-level spec field immutable.
15162func TestPhysicalContainerVolumeValidateUpdateRejectsSpecChanges (t * testing.T ) {
152- oldVolume := & PhysicalContainerVolume {
153- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
154- Spec : PhysicalContainerVolumeSpec {
155- Volume : & PhysicalContainerVolumeConfig {VolumeName : "test-runtime-volume" },
156- },
63+ t .Parallel ()
64+ original := & PhysicalContainerVolume {
65+ ObjectMeta : metav1.ObjectMeta {Name : "volume" , Namespace : "test-namespace" },
66+ Spec : PhysicalContainerVolumeSpec {VolumeName : "runtime-volume" },
67+ }
68+ for _ , testCase := range []struct {
69+ name string
70+ change func (* PhysicalContainerVolume )
71+ }{
72+ {name : "name" , change : func (volume * PhysicalContainerVolume ) { volume .Spec .VolumeName = "another-volume" }},
73+ {name : "cleanup" , change : func (volume * PhysicalContainerVolume ) { volume .Spec .RemoveRuntimeVolumeOnDelete = true }},
74+ {name : "labels" , change : func (volume * PhysicalContainerVolume ) {
75+ volume .Spec .Labels = []commonapi.Label {{Key : "key" , Value : "value" }}
76+ }},
77+ } {
78+ t .Run (testCase .name , func (t * testing.T ) {
79+ updated := original .DeepCopy ()
80+ testCase .change (updated )
81+ updateErrors := updated .ValidateUpdate (context .Background (), original )
82+ require .Len (t , updateErrors , 1 )
83+ require .Equal (t , "spec" , updateErrors [0 ].Field )
84+ })
15785 }
158- newVolume := oldVolume .DeepCopy ()
159- newVolume .Spec .Volume .RemoveRuntimeVolumeOnDelete = true
160-
161- errorList := newVolume .ValidateUpdate (context .Background (), oldVolume )
162-
163- require .NotEmpty (t , errorList )
164- require .Contains (t , errorList .ToAggregate ().Error (), "spec" )
16586}
16687
88+ // Verifies that PhysicalContainerVolume.ValidateUpdate permits status updates during shutdown.
16789func TestPhysicalContainerVolumeValidateUpdateAllowsStatusUpdateDuringShutdown (t * testing.T ) {
16890 commonapi .ResourceCreationProhibited .Store (true )
16991 defer commonapi .ResourceCreationProhibited .Store (false )
92+ original := & PhysicalContainerVolume {
93+ ObjectMeta : metav1.ObjectMeta {Name : "volume" , Namespace : "test-namespace" },
94+ Spec : PhysicalContainerVolumeSpec {VolumeName : "runtime-volume" },
95+ }
96+ updated := original .DeepCopy ()
97+ updated .Status .Phase = PhysicalContainerVolumePhaseReady
98+ require .Error (t , updated .Validate (context .Background ()).ToAggregate ())
99+ require .Empty (t , updated .ValidateUpdate (context .Background (), original ))
100+ }
170101
171- oldVolume := & PhysicalContainerVolume {
172- ObjectMeta : metav1.ObjectMeta {Name : "test-volume" , Namespace : "test-namespace" },
102+ // Verifies that PhysicalContainerVolume serializes a flat spec, retains observed volumeID in status,
103+ // and deep copies isolate the configured labels.
104+ func TestPhysicalContainerVolumeJSONAndDeepCopy (t * testing.T ) {
105+ t .Parallel ()
106+ volume := & PhysicalContainerVolume {
173107 Spec : PhysicalContainerVolumeSpec {
174- Volume : & PhysicalContainerVolumeConfig {VolumeName : "test-runtime-volume" },
108+ VolumeName : "runtime-volume" , RemoveRuntimeVolumeOnDelete : true ,
109+ Labels : []commonapi.Label {{Key : "key" , Value : "value" }},
175110 },
111+ Status : PhysicalContainerVolumeStatus {VolumeID : "runtime-volume" },
176112 }
177- newVolume := oldVolume .DeepCopy ()
178- newVolume .Status .Phase = PhysicalContainerVolumePhaseReady
179-
180- require .Error (t , newVolume .Validate (context .Background ()).ToAggregate ())
181- require .Empty (t , newVolume .ValidateUpdate (context .Background (), oldVolume ))
113+ encoded , encodeErr := json .Marshal (volume )
114+ require .NoError (t , encodeErr )
115+ var document struct {
116+ Spec map [string ]json.RawMessage `json:"spec"`
117+ Status map [string ]json.RawMessage `json:"status"`
118+ }
119+ require .NoError (t , json .Unmarshal (encoded , & document ))
120+ require .Len (t , document .Spec , 3 )
121+ require .JSONEq (t , `"runtime-volume"` , string (document .Spec ["volumeName" ]))
122+ require .JSONEq (t , `true` , string (document .Spec ["removeRuntimeVolumeOnDelete" ]))
123+ require .JSONEq (t , `[{"key":"key","value":"value"}]` , string (document .Spec ["labels" ]))
124+ require .JSONEq (t , `"runtime-volume"` , string (document .Status ["volumeID" ]))
125+ cloned := volume .DeepCopy ()
126+ cloned .Spec .Labels [0 ].Value = "changed"
127+ require .Equal (t , "value" , volume .Spec .Labels [0 ].Value )
182128}
0 commit comments