@@ -14,6 +14,10 @@ import (
1414
1515 . "github.com/onsi/ginkgo/v2"
1616 . "github.com/onsi/gomega"
17+ "google.golang.org/grpc/codes"
18+ "google.golang.org/grpc/status"
19+ corev1 "k8s.io/api/core/v1"
20+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1721
1822 "sigs.k8s.io/controller-runtime/pkg/client"
1923)
@@ -67,4 +71,172 @@ var _ = Describe("CreateVolume", func() {
6771 Expect (ironcoreVolume .Spec .VolumeClassRef .Name ).To (Equal (volumeClass .Name ))
6872 Expect (ironcoreVolume .Spec .Resources ).To (HaveLen (1 ))
6973 })
74+
75+ It ("should correctly create a volume from snapshot" , func (ctx SpecContext ) {
76+ By ("creating a volume snapshot" )
77+ volumeSnapshot := & storagev1alpha1.VolumeSnapshot {
78+ ObjectMeta : metav1.ObjectMeta {
79+ Namespace : ns .Name ,
80+ Name : "test-snapshot" ,
81+ },
82+ Spec : storagev1alpha1.VolumeSnapshotSpec {
83+ VolumeRef : & corev1.LocalObjectReference {Name : "source-volume" },
84+ },
85+ Status : storagev1alpha1.VolumeSnapshotStatus {
86+ State : storagev1alpha1 .VolumeSnapshotStateReady ,
87+ SnapshotID : "test-snapshot-id" ,
88+ },
89+ }
90+ Expect (k8sClient .Create (ctx , volumeSnapshot )).To (Succeed ())
91+
92+ By ("creating a volume with snapshot data source" )
93+ res , err := srv .CreateVolume (ctx , & iri.CreateVolumeRequest {
94+ Volume : & iri.Volume {
95+ Metadata : & irimeta.ObjectMetadata {
96+ Labels : map [string ]string {
97+ volumepoolletv1alpha1 .VolumeUIDLabel : "foobar" ,
98+ },
99+ },
100+ Spec : & iri.VolumeSpec {
101+ Class : volumeClass .Name ,
102+ Resources : & iri.VolumeResources {
103+ StorageBytes : 100 ,
104+ },
105+ VolumeDataSource : & iri.VolumeDataSource {
106+ SnapshotDataSource : & iri.SnapshotDataSource {
107+ SnapshotId : "test-snapshot-id" ,
108+ },
109+ },
110+ },
111+ },
112+ })
113+
114+ Expect (err ).NotTo (HaveOccurred ())
115+ Expect (res ).NotTo (BeNil ())
116+
117+ By ("getting the ironcore volume" )
118+ ironcoreVolume := & storagev1alpha1.Volume {}
119+ ironcoreVolumeKey := client.ObjectKey {Namespace : ns .Name , Name : res .Volume .Metadata .Id }
120+ Expect (k8sClient .Get (ctx , ironcoreVolumeKey , ironcoreVolume )).To (Succeed ())
121+
122+ By ("verifying the volume has the correct snapshot reference" )
123+ Expect (ironcoreVolume .Spec .VolumeDataSource .VolumeSnapshotRef ).NotTo (BeNil ())
124+ Expect (ironcoreVolume .Spec .VolumeDataSource .VolumeSnapshotRef .Name ).To (Equal ("test-snapshot" ))
125+ })
126+
127+ It ("should return error if snapshot is not found" , func (ctx SpecContext ) {
128+ By ("creating a volume with non-existent snapshot data source" )
129+ res , err := srv .CreateVolume (ctx , & iri.CreateVolumeRequest {
130+ Volume : & iri.Volume {
131+ Metadata : & irimeta.ObjectMetadata {
132+ Labels : map [string ]string {
133+ volumepoolletv1alpha1 .VolumeUIDLabel : "foobar" ,
134+ },
135+ },
136+ Spec : & iri.VolumeSpec {
137+ Class : volumeClass .Name ,
138+ Resources : & iri.VolumeResources {
139+ StorageBytes : 100 ,
140+ },
141+ VolumeDataSource : & iri.VolumeDataSource {
142+ SnapshotDataSource : & iri.SnapshotDataSource {
143+ SnapshotId : "non-existent-snapshot-id" ,
144+ },
145+ },
146+ },
147+ },
148+ })
149+
150+ Expect (err ).To (HaveOccurred ())
151+ Expect (res ).To (BeNil ())
152+ Expect (status .Code (err )).To (Equal (codes .NotFound ))
153+ })
154+
155+ It ("should return error if snapshot is in pending state" , func (ctx SpecContext ) {
156+ By ("creating a volume snapshot in pending state" )
157+ volumeSnapshot := & storagev1alpha1.VolumeSnapshot {
158+ ObjectMeta : metav1.ObjectMeta {
159+ Namespace : ns .Name ,
160+ Name : "pending-snapshot" ,
161+ },
162+ Spec : storagev1alpha1.VolumeSnapshotSpec {
163+ VolumeRef : & corev1.LocalObjectReference {Name : "source-volume" },
164+ },
165+ Status : storagev1alpha1.VolumeSnapshotStatus {
166+ State : storagev1alpha1 .VolumeSnapshotStatePending ,
167+ SnapshotID : "pending-snapshot-id" ,
168+ },
169+ }
170+ Expect (k8sClient .Create (ctx , volumeSnapshot )).To (Succeed ())
171+
172+ By ("creating a volume with pending snapshot data source" )
173+ res , err := srv .CreateVolume (ctx , & iri.CreateVolumeRequest {
174+ Volume : & iri.Volume {
175+ Metadata : & irimeta.ObjectMetadata {
176+ Labels : map [string ]string {
177+ volumepoolletv1alpha1 .VolumeUIDLabel : "foobar" ,
178+ },
179+ },
180+ Spec : & iri.VolumeSpec {
181+ Class : volumeClass .Name ,
182+ Resources : & iri.VolumeResources {
183+ StorageBytes : 100 ,
184+ },
185+ VolumeDataSource : & iri.VolumeDataSource {
186+ SnapshotDataSource : & iri.SnapshotDataSource {
187+ SnapshotId : "pending-snapshot-id" ,
188+ },
189+ },
190+ },
191+ },
192+ })
193+
194+ Expect (err ).To (HaveOccurred ())
195+ Expect (res ).To (BeNil ())
196+ Expect (status .Code (err )).To (Equal (codes .FailedPrecondition ))
197+ })
198+
199+ It ("should return error if snapshot is in failed state" , func (ctx SpecContext ) {
200+ By ("creating a volume snapshot in failed state" )
201+ volumeSnapshot := & storagev1alpha1.VolumeSnapshot {
202+ ObjectMeta : metav1.ObjectMeta {
203+ Namespace : ns .Name ,
204+ Name : "failed-snapshot" ,
205+ },
206+ Spec : storagev1alpha1.VolumeSnapshotSpec {
207+ VolumeRef : & corev1.LocalObjectReference {Name : "source-volume" },
208+ },
209+ Status : storagev1alpha1.VolumeSnapshotStatus {
210+ State : storagev1alpha1 .VolumeSnapshotStateFailed ,
211+ SnapshotID : "failed-snapshot-id" ,
212+ },
213+ }
214+ Expect (k8sClient .Create (ctx , volumeSnapshot )).To (Succeed ())
215+
216+ By ("creating a volume with failed snapshot data source" )
217+ res , err := srv .CreateVolume (ctx , & iri.CreateVolumeRequest {
218+ Volume : & iri.Volume {
219+ Metadata : & irimeta.ObjectMetadata {
220+ Labels : map [string ]string {
221+ volumepoolletv1alpha1 .VolumeUIDLabel : "foobar" ,
222+ },
223+ },
224+ Spec : & iri.VolumeSpec {
225+ Class : volumeClass .Name ,
226+ Resources : & iri.VolumeResources {
227+ StorageBytes : 100 ,
228+ },
229+ VolumeDataSource : & iri.VolumeDataSource {
230+ SnapshotDataSource : & iri.SnapshotDataSource {
231+ SnapshotId : "failed-snapshot-id" ,
232+ },
233+ },
234+ },
235+ },
236+ })
237+
238+ Expect (err ).To (HaveOccurred ())
239+ Expect (res ).To (BeNil ())
240+ Expect (status .Code (err )).To (Equal (codes .Internal ))
241+ })
70242})
0 commit comments