github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/services_test.go (about) 1 // Copyright 2022 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package provider 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 core "k8s.io/api/core/v1" 13 meta "k8s.io/apimachinery/pkg/apis/meta/v1" 14 "k8s.io/client-go/kubernetes/fake" 15 ) 16 17 type servicesSuite struct { 18 client *fake.Clientset 19 } 20 21 var _ = gc.Suite(&servicesSuite{}) 22 23 func (s *servicesSuite) SetUpTest(c *gc.C) { 24 s.client = fake.NewSimpleClientset() 25 } 26 27 func (s *servicesSuite) TestFindServiceForApplication(c *gc.C) { 28 _, err := s.client.CoreV1().Services("test").Create( 29 context.TODO(), 30 &core.Service{ 31 ObjectMeta: meta.ObjectMeta{ 32 Name: "wallyworld", 33 Labels: map[string]string{ 34 "app.kubernetes.io/name": "wallyworld", 35 "app.kubernetes.io/managed-by": "juju", 36 }, 37 }, 38 }, 39 meta.CreateOptions{}, 40 ) 41 42 c.Assert(err, jc.ErrorIsNil) 43 44 svc, err := findServiceForApplication( 45 context.TODO(), 46 s.client.CoreV1().Services("test"), 47 "wallyworld", 48 false, 49 ) 50 51 c.Assert(err, jc.ErrorIsNil) 52 c.Assert(svc.Name, gc.Equals, "wallyworld") 53 } 54 55 func (s *servicesSuite) TestFindServiceForApplicationWithEndpoints(c *gc.C) { 56 _, err := s.client.CoreV1().Services("test").Create( 57 context.TODO(), 58 &core.Service{ 59 ObjectMeta: meta.ObjectMeta{ 60 Name: "wallyworld", 61 Labels: map[string]string{ 62 "app.kubernetes.io/name": "wallyworld", 63 "app.kubernetes.io/managed-by": "juju", 64 }, 65 }, 66 }, 67 meta.CreateOptions{}, 68 ) 69 c.Assert(err, jc.ErrorIsNil) 70 71 _, err = s.client.CoreV1().Services("test").Create( 72 context.TODO(), 73 &core.Service{ 74 ObjectMeta: meta.ObjectMeta{ 75 Name: "wallyworld-endpoints", 76 Labels: map[string]string{ 77 "app.kubernetes.io/name": "wallyworld", 78 "app.kubernetes.io/managed-by": "juju", 79 }, 80 }, 81 }, 82 meta.CreateOptions{}, 83 ) 84 c.Assert(err, jc.ErrorIsNil) 85 86 svc, err := findServiceForApplication( 87 context.TODO(), 88 s.client.CoreV1().Services("test"), 89 "wallyworld", 90 false, 91 ) 92 93 c.Assert(err, jc.ErrorIsNil) 94 c.Assert(svc.Name, gc.Equals, "wallyworld") 95 } 96 97 func (s *servicesSuite) TestFindServiceForApplicationWithMultiple(c *gc.C) { 98 _, err := s.client.CoreV1().Services("test").Create( 99 context.TODO(), 100 &core.Service{ 101 ObjectMeta: meta.ObjectMeta{ 102 Name: "wallyworld", 103 Labels: map[string]string{ 104 "app.kubernetes.io/name": "wallyworld", 105 "app.kubernetes.io/managed-by": "juju", 106 }, 107 }, 108 }, 109 meta.CreateOptions{}, 110 ) 111 c.Assert(err, jc.ErrorIsNil) 112 113 _, err = s.client.CoreV1().Services("test").Create( 114 context.TODO(), 115 &core.Service{ 116 ObjectMeta: meta.ObjectMeta{ 117 Name: "wallyworld-v2", 118 Labels: map[string]string{ 119 "app.kubernetes.io/name": "wallyworld", 120 "app.kubernetes.io/managed-by": "juju", 121 }, 122 }, 123 }, 124 meta.CreateOptions{}, 125 ) 126 c.Assert(err, jc.ErrorIsNil) 127 128 _, err = findServiceForApplication( 129 context.TODO(), 130 s.client.CoreV1().Services("test"), 131 "wallyworld", 132 false, 133 ) 134 135 c.Assert(errors.Is(err, errors.NotValid), jc.IsTrue) 136 } 137 138 func (s *servicesSuite) TestFindServiceForApplicationMissing(c *gc.C) { 139 _, err := findServiceForApplication( 140 context.TODO(), 141 s.client.CoreV1().Services("test"), 142 "wallyworld", 143 false, 144 ) 145 146 c.Assert(errors.Is(err, errors.NotFound), jc.IsTrue) 147 }