12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /*
- Copyright 2022.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package controllers
- import (
- "context"
- "testing"
- appv1 "github.com/kakao/bluegreen/api/v1"
- "github.com/stretchr/testify/assert"
- corev1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/types"
- utilruntime "k8s.io/apimachinery/pkg/util/runtime"
- clientgoscheme "k8s.io/client-go/kubernetes/scheme"
- "sigs.k8s.io/controller-runtime/pkg/client/fake"
- )
- var (
- testScheme = runtime.NewScheme()
- )
- func init() {
- utilruntime.Must(clientgoscheme.AddToScheme(testScheme))
- utilruntime.Must(appv1.AddToScheme(testScheme))
- }
- func TestBlueGreenReconciler_CreateOrUpdateService(t *testing.T) {
- t.Run("Should Create a New Service", func(t *testing.T) {
- c := fake.NewClientBuilder().WithScheme(testScheme).Build()
- r := &BlueGreenReconciler{Client: c, Scheme: c.Scheme()}
- NN := types.NamespacedName{
- Namespace: "test-namespace",
- Name: "test-name",
- }
- svc := new(corev1.Service)
- assert.Error(t, c.Get(context.TODO(), NN, svc))
- err := r.CreateOrUpdateService(
- context.TODO(),
- &appv1.BlueGreen{
- ObjectMeta: metav1.ObjectMeta{Namespace: NN.Namespace, Name: NN.Name},
- Spec: appv1.BlueGreenSpec{RouteTo: appv1.Blue},
- })
- assert.NoError(t, err)
- assert.NoError(t, c.Get(context.TODO(), NN, svc))
- assert.Equal(t, 1, len(svc.ObjectMeta.OwnerReferences))
- assert.Equal(t, "app.demo.kakao.com", svc.Spec.Selector["app.kubernetes.io/managed-by"])
- assert.Equal(t, "Blue", svc.Spec.Selector["app.kubernetes.io/phase"])
- assert.Equal(t, NN.Name, svc.Spec.Selector["app.kubernetes.io/name"])
- /* more validation here */
- })
- }
|