github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/stack/kubernetes/deploy_test.go (about) 1 package kubernetes 2 3 import ( 4 "errors" 5 "testing" 6 7 composev1alpha3 "github.com/docker/compose-on-kubernetes/api/client/clientset/typed/compose/v1alpha3" 8 composev1beta1 "github.com/docker/compose-on-kubernetes/api/client/clientset/typed/compose/v1beta1" 9 composev1beta2 "github.com/docker/compose-on-kubernetes/api/client/clientset/typed/compose/v1beta2" 10 "github.com/docker/compose-on-kubernetes/api/compose/v1alpha3" 11 "github.com/docker/compose-on-kubernetes/api/compose/v1beta1" 12 "github.com/docker/compose-on-kubernetes/api/compose/v1beta2" 13 "gotest.tools/v3/assert" 14 kerrors "k8s.io/apimachinery/pkg/api/errors" 15 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 16 types "k8s.io/apimachinery/pkg/types" 17 watch "k8s.io/apimachinery/pkg/watch" 18 "k8s.io/client-go/kubernetes/fake" 19 ) 20 21 func testStack() Stack { 22 return Stack{ 23 Name: "test", 24 Namespace: "test", 25 ComposeFile: `version: "3.3" 26 services: 27 test: 28 image: nginx 29 secrets: 30 test: 31 file: testdata/secret 32 configs: 33 test: 34 file: testdata/config 35 `, 36 Spec: &v1alpha3.StackSpec{ 37 Configs: map[string]v1alpha3.ConfigObjConfig{ 38 "test": {Name: "test", File: "testdata/config"}, 39 }, 40 Secrets: map[string]v1alpha3.SecretConfig{ 41 "test": {Name: "test", File: "testdata/secret"}, 42 }, 43 }, 44 } 45 } 46 47 func TestCreateChildResourcesV1Beta1(t *testing.T) { 48 k8sclientSet := fake.NewSimpleClientset() 49 stack := testStack() 50 configs := k8sclientSet.CoreV1().ConfigMaps("test") 51 secrets := k8sclientSet.CoreV1().Secrets("test") 52 assert.NilError(t, createResources( 53 stack, 54 &stackV1Beta1{stacks: &fakeV1beta1Client{}}, 55 configs, 56 secrets)) 57 c, err := configs.Get("test", metav1.GetOptions{}) 58 assert.NilError(t, err) 59 checkOwnerReferences(t, c.ObjectMeta, v1beta1.SchemeGroupVersion.String()) 60 s, err := secrets.Get("test", metav1.GetOptions{}) 61 assert.NilError(t, err) 62 checkOwnerReferences(t, s.ObjectMeta, v1beta1.SchemeGroupVersion.String()) 63 } 64 65 func checkOwnerReferences(t *testing.T, objMeta metav1.ObjectMeta, stackVersion string) { 66 t.Helper() 67 assert.Equal(t, len(objMeta.OwnerReferences), 1) 68 assert.Equal(t, objMeta.OwnerReferences[0].Name, "test") 69 assert.Equal(t, objMeta.OwnerReferences[0].Kind, "Stack") 70 assert.Equal(t, objMeta.OwnerReferences[0].APIVersion, stackVersion) 71 } 72 73 func TestCreateChildResourcesV1Beta2(t *testing.T) { 74 k8sclientSet := fake.NewSimpleClientset() 75 stack := testStack() 76 configs := k8sclientSet.CoreV1().ConfigMaps("test") 77 secrets := k8sclientSet.CoreV1().Secrets("test") 78 assert.NilError(t, createResources( 79 stack, 80 &stackV1Beta2{stacks: &fakeV1beta2Client{}}, 81 configs, 82 secrets)) 83 c, err := configs.Get("test", metav1.GetOptions{}) 84 assert.NilError(t, err) 85 checkOwnerReferences(t, c.ObjectMeta, v1beta2.SchemeGroupVersion.String()) 86 s, err := secrets.Get("test", metav1.GetOptions{}) 87 assert.NilError(t, err) 88 checkOwnerReferences(t, s.ObjectMeta, v1beta2.SchemeGroupVersion.String()) 89 } 90 91 func TestCreateChildResourcesV1Alpha3(t *testing.T) { 92 k8sclientSet := fake.NewSimpleClientset() 93 stack := testStack() 94 configs := k8sclientSet.CoreV1().ConfigMaps("test") 95 secrets := k8sclientSet.CoreV1().Secrets("test") 96 assert.NilError(t, createResources( 97 stack, 98 &stackV1Alpha3{stacks: &fakeV1alpha3Client{}}, 99 configs, 100 secrets)) 101 c, err := configs.Get("test", metav1.GetOptions{}) 102 assert.NilError(t, err) 103 checkOwnerReferences(t, c.ObjectMeta, v1alpha3.SchemeGroupVersion.String()) 104 s, err := secrets.Get("test", metav1.GetOptions{}) 105 assert.NilError(t, err) 106 checkOwnerReferences(t, s.ObjectMeta, v1alpha3.SchemeGroupVersion.String()) 107 } 108 109 func TestCreateChildResourcesWithStackCreationErrorV1Beta1(t *testing.T) { 110 k8sclientSet := fake.NewSimpleClientset() 111 stack := testStack() 112 configs := k8sclientSet.CoreV1().ConfigMaps("test") 113 secrets := k8sclientSet.CoreV1().Secrets("test") 114 err := createResources( 115 stack, 116 &stackV1Beta1{stacks: &fakeV1beta1Client{errorOnCreate: true}}, 117 configs, 118 secrets) 119 assert.Error(t, err, "some error") 120 _, err = configs.Get("test", metav1.GetOptions{}) 121 assert.Check(t, kerrors.IsNotFound(err)) 122 _, err = secrets.Get("test", metav1.GetOptions{}) 123 assert.Check(t, kerrors.IsNotFound(err)) 124 } 125 126 func TestCreateChildResourcesWithStackCreationErrorV1Beta2(t *testing.T) { 127 k8sclientSet := fake.NewSimpleClientset() 128 stack := testStack() 129 configs := k8sclientSet.CoreV1().ConfigMaps("test") 130 secrets := k8sclientSet.CoreV1().Secrets("test") 131 err := createResources( 132 stack, 133 &stackV1Beta2{stacks: &fakeV1beta2Client{errorOnCreate: true}}, 134 configs, 135 secrets) 136 assert.Error(t, err, "some error") 137 _, err = configs.Get("test", metav1.GetOptions{}) 138 assert.Check(t, kerrors.IsNotFound(err)) 139 _, err = secrets.Get("test", metav1.GetOptions{}) 140 assert.Check(t, kerrors.IsNotFound(err)) 141 } 142 143 func TestCreateChildResourcesWithStackCreationErrorV1Alpha3(t *testing.T) { 144 k8sclientSet := fake.NewSimpleClientset() 145 stack := testStack() 146 configs := k8sclientSet.CoreV1().ConfigMaps("test") 147 secrets := k8sclientSet.CoreV1().Secrets("test") 148 err := createResources( 149 stack, 150 &stackV1Alpha3{stacks: &fakeV1alpha3Client{errorOnCreate: true}}, 151 configs, 152 secrets) 153 assert.Error(t, err, "some error") 154 _, err = configs.Get("test", metav1.GetOptions{}) 155 assert.Check(t, kerrors.IsNotFound(err)) 156 _, err = secrets.Get("test", metav1.GetOptions{}) 157 assert.Check(t, kerrors.IsNotFound(err)) 158 } 159 160 type fakeV1beta1Client struct { 161 errorOnCreate bool 162 } 163 164 func (c *fakeV1beta1Client) Create(s *v1beta1.Stack) (*v1beta1.Stack, error) { 165 if c.errorOnCreate { 166 return nil, errors.New("some error") 167 } 168 return s, nil 169 } 170 171 func (c *fakeV1beta1Client) Update(*v1beta1.Stack) (*v1beta1.Stack, error) { 172 return nil, nil 173 } 174 175 func (c *fakeV1beta1Client) UpdateStatus(*v1beta1.Stack) (*v1beta1.Stack, error) { 176 return nil, nil 177 } 178 179 func (c *fakeV1beta1Client) Delete(name string, options *metav1.DeleteOptions) error { 180 return nil 181 } 182 183 func (c *fakeV1beta1Client) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error { 184 return nil 185 } 186 187 func (c *fakeV1beta1Client) Get(name string, options metav1.GetOptions) (*v1beta1.Stack, error) { 188 return nil, kerrors.NewNotFound(v1beta1.SchemeGroupVersion.WithResource("stacks").GroupResource(), name) 189 } 190 191 func (c *fakeV1beta1Client) List(opts metav1.ListOptions) (*v1beta1.StackList, error) { 192 return nil, nil 193 } 194 195 func (c *fakeV1beta1Client) Watch(opts metav1.ListOptions) (watch.Interface, error) { 196 return nil, nil 197 } 198 199 func (c *fakeV1beta1Client) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*v1beta1.Stack, error) { 200 return nil, nil 201 } 202 203 func (c *fakeV1beta1Client) WithSkipValidation() composev1beta1.StackInterface { 204 return c 205 } 206 207 type fakeV1beta2Client struct { 208 errorOnCreate bool 209 } 210 211 func (c *fakeV1beta2Client) Create(s *v1beta2.Stack) (*v1beta2.Stack, error) { 212 if c.errorOnCreate { 213 return nil, errors.New("some error") 214 } 215 return s, nil 216 } 217 218 func (c *fakeV1beta2Client) Update(*v1beta2.Stack) (*v1beta2.Stack, error) { 219 return nil, nil 220 } 221 222 func (c *fakeV1beta2Client) UpdateStatus(*v1beta2.Stack) (*v1beta2.Stack, error) { 223 return nil, nil 224 } 225 226 func (c *fakeV1beta2Client) Delete(name string, options *metav1.DeleteOptions) error { 227 return nil 228 } 229 230 func (c *fakeV1beta2Client) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error { 231 return nil 232 } 233 234 func (c *fakeV1beta2Client) Get(name string, options metav1.GetOptions) (*v1beta2.Stack, error) { 235 return nil, kerrors.NewNotFound(v1beta1.SchemeGroupVersion.WithResource("stacks").GroupResource(), name) 236 } 237 238 func (c *fakeV1beta2Client) List(opts metav1.ListOptions) (*v1beta2.StackList, error) { 239 return nil, nil 240 } 241 242 func (c *fakeV1beta2Client) Watch(opts metav1.ListOptions) (watch.Interface, error) { 243 return nil, nil 244 } 245 246 func (c *fakeV1beta2Client) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*v1beta2.Stack, error) { 247 return nil, nil 248 } 249 250 func (c *fakeV1beta2Client) WithSkipValidation() composev1beta2.StackInterface { 251 return c 252 } 253 254 type fakeV1alpha3Client struct { 255 errorOnCreate bool 256 } 257 258 func (c *fakeV1alpha3Client) Create(s *v1alpha3.Stack) (*v1alpha3.Stack, error) { 259 if c.errorOnCreate { 260 return nil, errors.New("some error") 261 } 262 return s, nil 263 } 264 265 func (c *fakeV1alpha3Client) Update(*v1alpha3.Stack) (*v1alpha3.Stack, error) { 266 return nil, nil 267 } 268 269 func (c *fakeV1alpha3Client) UpdateStatus(*v1alpha3.Stack) (*v1alpha3.Stack, error) { 270 return nil, nil 271 } 272 273 func (c *fakeV1alpha3Client) Delete(name string, options *metav1.DeleteOptions) error { 274 return nil 275 } 276 277 func (c *fakeV1alpha3Client) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error { 278 return nil 279 } 280 281 func (c *fakeV1alpha3Client) Get(name string, options metav1.GetOptions) (*v1alpha3.Stack, error) { 282 return nil, kerrors.NewNotFound(v1beta1.SchemeGroupVersion.WithResource("stacks").GroupResource(), name) 283 } 284 285 func (c *fakeV1alpha3Client) List(opts metav1.ListOptions) (*v1alpha3.StackList, error) { 286 return nil, nil 287 } 288 289 func (c *fakeV1alpha3Client) Watch(opts metav1.ListOptions) (watch.Interface, error) { 290 return nil, nil 291 } 292 293 func (c *fakeV1alpha3Client) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*v1alpha3.Stack, error) { 294 return nil, nil 295 } 296 297 func (c *fakeV1alpha3Client) WithSkipValidation() composev1alpha3.StackInterface { 298 return c 299 }