github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/resources/daemonset_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  	appsv1 "k8s.io/api/apps/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 daemonsetSuite struct {
    20  	resourceSuite
    21  }
    22  
    23  var _ = gc.Suite(&daemonsetSuite{})
    24  
    25  func (s *daemonsetSuite) TestApply(c *gc.C) {
    26  	ds := &appsv1.DaemonSet{
    27  		ObjectMeta: metav1.ObjectMeta{
    28  			Name:      "ds1",
    29  			Namespace: "test",
    30  		},
    31  	}
    32  	// Create.
    33  	dsResource := resources.NewDaemonSet("ds1", "test", ds)
    34  	c.Assert(dsResource.Apply(context.TODO(), s.client), jc.ErrorIsNil)
    35  	result, err := s.client.AppsV1().DaemonSets("test").Get(context.TODO(), "ds1", metav1.GetOptions{})
    36  	c.Assert(err, jc.ErrorIsNil)
    37  	c.Assert(len(result.GetAnnotations()), gc.Equals, 0)
    38  
    39  	// Update.
    40  	ds.SetAnnotations(map[string]string{"a": "b"})
    41  	dsResource = resources.NewDaemonSet("ds1", "test", ds)
    42  	c.Assert(dsResource.Apply(context.TODO(), s.client), jc.ErrorIsNil)
    43  
    44  	result, err = s.client.AppsV1().DaemonSets("test").Get(context.TODO(), "ds1", metav1.GetOptions{})
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(result.GetName(), gc.Equals, `ds1`)
    47  	c.Assert(result.GetNamespace(), gc.Equals, `test`)
    48  	c.Assert(result.GetAnnotations(), gc.DeepEquals, map[string]string{"a": "b"})
    49  }
    50  
    51  func (s *daemonsetSuite) TestGet(c *gc.C) {
    52  	template := appsv1.DaemonSet{
    53  		ObjectMeta: metav1.ObjectMeta{
    54  			Name:      "ds1",
    55  			Namespace: "test",
    56  		},
    57  	}
    58  	ds1 := template
    59  	ds1.SetAnnotations(map[string]string{"a": "b"})
    60  	_, err := s.client.AppsV1().DaemonSets("test").Create(context.TODO(), &ds1, metav1.CreateOptions{})
    61  	c.Assert(err, jc.ErrorIsNil)
    62  
    63  	dsResource := resources.NewDaemonSet("ds1", "test", &template)
    64  	c.Assert(len(dsResource.GetAnnotations()), gc.Equals, 0)
    65  	err = dsResource.Get(context.TODO(), s.client)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(dsResource.GetName(), gc.Equals, `ds1`)
    68  	c.Assert(dsResource.GetNamespace(), gc.Equals, `test`)
    69  	c.Assert(dsResource.GetAnnotations(), gc.DeepEquals, map[string]string{"a": "b"})
    70  }
    71  
    72  func (s *daemonsetSuite) TestDelete(c *gc.C) {
    73  	ds := appsv1.DaemonSet{
    74  		ObjectMeta: metav1.ObjectMeta{
    75  			Name:      "ds1",
    76  			Namespace: "test",
    77  		},
    78  	}
    79  	_, err := s.client.AppsV1().DaemonSets("test").Create(context.TODO(), &ds, metav1.CreateOptions{})
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	result, err := s.client.AppsV1().DaemonSets("test").Get(context.TODO(), "ds1", metav1.GetOptions{})
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(result.GetName(), gc.Equals, `ds1`)
    85  
    86  	dsResource := resources.NewDaemonSet("ds1", "test", &ds)
    87  	err = dsResource.Delete(context.TODO(), s.client)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  
    90  	err = dsResource.Get(context.TODO(), s.client)
    91  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    92  
    93  	_, err = s.client.AppsV1().DaemonSets("test").Get(context.TODO(), "ds1", metav1.GetOptions{})
    94  	c.Assert(err, jc.Satisfies, k8serrors.IsNotFound)
    95  }