123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package bluegreen
- 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 := &BlueGreenClient{Client: c}
- 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 */
- })
- }
|