github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/resources/persistentvolumeclaim_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package resources_test 5 6 import ( 7 "context" 8 "fmt" 9 10 "github.com/juju/errors" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 corev1 "k8s.io/api/core/v1" 14 k8serrors "k8s.io/apimachinery/pkg/api/errors" 15 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 16 17 "github.com/juju/juju/caas/kubernetes/provider/resources" 18 ) 19 20 type persistentVolumeClaimSuite struct { 21 resourceSuite 22 } 23 24 var _ = gc.Suite(&persistentVolumeClaimSuite{}) 25 26 func (s *persistentVolumeClaimSuite) TestApply(c *gc.C) { 27 ds := &corev1.PersistentVolumeClaim{ 28 ObjectMeta: metav1.ObjectMeta{ 29 Name: "ds1", 30 Namespace: "test", 31 }, 32 } 33 // Create. 34 dsResource := resources.NewPersistentVolumeClaim("ds1", "test", ds) 35 c.Assert(dsResource.Apply(context.TODO(), s.client), jc.ErrorIsNil) 36 result, err := s.client.CoreV1().PersistentVolumeClaims("test").Get(context.TODO(), "ds1", metav1.GetOptions{}) 37 c.Assert(err, jc.ErrorIsNil) 38 c.Assert(len(result.GetAnnotations()), gc.Equals, 0) 39 40 // Update. 41 ds.SetAnnotations(map[string]string{"a": "b"}) 42 dsResource = resources.NewPersistentVolumeClaim("ds1", "test", ds) 43 c.Assert(dsResource.Apply(context.TODO(), s.client), jc.ErrorIsNil) 44 45 result, err = s.client.CoreV1().PersistentVolumeClaims("test").Get(context.TODO(), "ds1", metav1.GetOptions{}) 46 c.Assert(err, jc.ErrorIsNil) 47 c.Assert(result.GetName(), gc.Equals, `ds1`) 48 c.Assert(result.GetNamespace(), gc.Equals, `test`) 49 c.Assert(result.GetAnnotations(), gc.DeepEquals, map[string]string{"a": "b"}) 50 } 51 52 func (s *persistentVolumeClaimSuite) TestGet(c *gc.C) { 53 template := corev1.PersistentVolumeClaim{ 54 ObjectMeta: metav1.ObjectMeta{ 55 Name: "ds1", 56 Namespace: "test", 57 }, 58 } 59 ds1 := template 60 ds1.SetAnnotations(map[string]string{"a": "b"}) 61 _, err := s.client.CoreV1().PersistentVolumeClaims("test").Create(context.TODO(), &ds1, metav1.CreateOptions{}) 62 c.Assert(err, jc.ErrorIsNil) 63 64 dsResource := resources.NewPersistentVolumeClaim("ds1", "test", &template) 65 c.Assert(len(dsResource.GetAnnotations()), gc.Equals, 0) 66 err = dsResource.Get(context.TODO(), s.client) 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(dsResource.GetName(), gc.Equals, `ds1`) 69 c.Assert(dsResource.GetNamespace(), gc.Equals, `test`) 70 c.Assert(dsResource.GetAnnotations(), gc.DeepEquals, map[string]string{"a": "b"}) 71 } 72 73 func (s *persistentVolumeClaimSuite) TestDelete(c *gc.C) { 74 ds := corev1.PersistentVolumeClaim{ 75 ObjectMeta: metav1.ObjectMeta{ 76 Name: "ds1", 77 Namespace: "test", 78 }, 79 } 80 _, err := s.client.CoreV1().PersistentVolumeClaims("test").Create(context.TODO(), &ds, metav1.CreateOptions{}) 81 c.Assert(err, jc.ErrorIsNil) 82 83 result, err := s.client.CoreV1().PersistentVolumeClaims("test").Get(context.TODO(), "ds1", metav1.GetOptions{}) 84 c.Assert(err, jc.ErrorIsNil) 85 c.Assert(result.GetName(), gc.Equals, `ds1`) 86 87 dsResource := resources.NewPersistentVolumeClaim("ds1", "test", &ds) 88 err = dsResource.Delete(context.TODO(), s.client) 89 c.Assert(err, jc.ErrorIsNil) 90 91 err = dsResource.Get(context.TODO(), s.client) 92 c.Assert(err, jc.Satisfies, errors.IsNotFound) 93 94 _, err = s.client.CoreV1().PersistentVolumeClaims("test").Get(context.TODO(), "ds1", metav1.GetOptions{}) 95 c.Assert(err, jc.Satisfies, k8serrors.IsNotFound) 96 } 97 98 func (s *persistentVolumeClaimSuite) TestList(c *gc.C) { 99 // Unfortunately with the K8s fake/testing API there doesn't seem to be a 100 // way to call List multiple times with "Continue" set. 101 102 // Create fake persistent volume claims, some of which have a label 103 for i := 0; i < 7; i++ { 104 pvc := corev1.PersistentVolumeClaim{ 105 ObjectMeta: metav1.ObjectMeta{ 106 Name: fmt.Sprintf("pvc%d", i), 107 Namespace: "test", 108 }, 109 } 110 if i%3 == 0 { 111 pvc.ObjectMeta.Labels = map[string]string{"modulo": "three"} 112 } 113 _, err := s.client.CoreV1().PersistentVolumeClaims("test").Create(context.Background(), &pvc, metav1.CreateOptions{}) 114 c.Assert(err, jc.ErrorIsNil) 115 } 116 117 // List PVCs filtered by the label 118 listed, err := resources.ListPersistentVolumeClaims(context.Background(), s.client, "test", metav1.ListOptions{ 119 LabelSelector: "modulo == three", 120 }) 121 c.Assert(err, jc.ErrorIsNil) 122 123 // Check that we fetch the right ones 124 c.Assert(len(listed), gc.Equals, 3) 125 for i, pvc := range listed { 126 c.Assert(pvc.Name, gc.Equals, fmt.Sprintf("pvc%d", i*3)) 127 c.Assert(pvc.Labels, gc.DeepEquals, map[string]string{"modulo": "three"}) 128 } 129 }