github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/resources/persistentvolume_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  
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	corev1 "k8s.io/api/core/v1"
    13  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  
    16  	"github.com/juju/juju/caas/kubernetes/provider/resources"
    17  )
    18  
    19  type persistentVolumeSuite struct {
    20  	resourceSuite
    21  }
    22  
    23  var _ = gc.Suite(&persistentVolumeSuite{})
    24  
    25  func (s *persistentVolumeSuite) TestApply(c *gc.C) {
    26  	ds := &corev1.PersistentVolume{
    27  		ObjectMeta: metav1.ObjectMeta{
    28  			Name: "ds1",
    29  		},
    30  	}
    31  	// Create.
    32  	dsResource := resources.NewPersistentVolume("ds1", ds)
    33  	c.Assert(dsResource.Apply(context.TODO(), s.client), jc.ErrorIsNil)
    34  	result, err := s.client.CoreV1().PersistentVolumes().Get(context.TODO(), "ds1", metav1.GetOptions{})
    35  	c.Assert(err, jc.ErrorIsNil)
    36  	c.Assert(len(result.GetAnnotations()), gc.Equals, 0)
    37  
    38  	// Update.
    39  	ds.SetAnnotations(map[string]string{"a": "b"})
    40  	dsResource = resources.NewPersistentVolume("ds1", ds)
    41  	c.Assert(dsResource.Apply(context.TODO(), s.client), jc.ErrorIsNil)
    42  
    43  	result, err = s.client.CoreV1().PersistentVolumes().Get(context.TODO(), "ds1", metav1.GetOptions{})
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	c.Assert(result.GetName(), gc.Equals, `ds1`)
    46  	c.Assert(result.GetAnnotations(), gc.DeepEquals, map[string]string{"a": "b"})
    47  }
    48  
    49  func (s *persistentVolumeSuite) TestGet(c *gc.C) {
    50  	template := corev1.PersistentVolume{
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name: "ds1",
    53  		},
    54  	}
    55  	ds1 := template
    56  	ds1.SetAnnotations(map[string]string{"a": "b"})
    57  	_, err := s.client.CoreV1().PersistentVolumes().Create(context.TODO(), &ds1, metav1.CreateOptions{})
    58  	c.Assert(err, jc.ErrorIsNil)
    59  
    60  	dsResource := resources.NewPersistentVolume("ds1", &template)
    61  	c.Assert(len(dsResource.GetAnnotations()), gc.Equals, 0)
    62  	err = dsResource.Get(context.TODO(), s.client)
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	c.Assert(dsResource.GetName(), gc.Equals, `ds1`)
    65  	c.Assert(dsResource.GetAnnotations(), gc.DeepEquals, map[string]string{"a": "b"})
    66  }
    67  
    68  func (s *persistentVolumeSuite) TestDelete(c *gc.C) {
    69  	ds := corev1.PersistentVolume{
    70  		ObjectMeta: metav1.ObjectMeta{
    71  			Name: "ds1",
    72  		},
    73  	}
    74  	_, err := s.client.CoreV1().PersistentVolumes().Create(context.TODO(), &ds, metav1.CreateOptions{})
    75  	c.Assert(err, jc.ErrorIsNil)
    76  
    77  	result, err := s.client.CoreV1().PersistentVolumes().Get(context.TODO(), "ds1", metav1.GetOptions{})
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(result.GetName(), gc.Equals, `ds1`)
    80  
    81  	dsResource := resources.NewPersistentVolume("ds1", &ds)
    82  	err = dsResource.Delete(context.TODO(), s.client)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  
    85  	err = dsResource.Get(context.TODO(), s.client)
    86  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    87  
    88  	_, err = s.client.CoreV1().PersistentVolumes().Get(context.TODO(), "ds1", metav1.GetOptions{})
    89  	c.Assert(err, jc.Satisfies, k8serrors.IsNotFound)
    90  }