client_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package bluegreen
  2. import (
  3. "context"
  4. "testing"
  5. appv1 "github.com/kakao/bluegreen/api/v1"
  6. "github.com/stretchr/testify/assert"
  7. corev1 "k8s.io/api/core/v1"
  8. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  9. "k8s.io/apimachinery/pkg/runtime"
  10. "k8s.io/apimachinery/pkg/types"
  11. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  12. clientgoscheme "k8s.io/client-go/kubernetes/scheme"
  13. "sigs.k8s.io/controller-runtime/pkg/client/fake"
  14. )
  15. var (
  16. testScheme = runtime.NewScheme()
  17. )
  18. func init() {
  19. utilruntime.Must(clientgoscheme.AddToScheme(testScheme))
  20. utilruntime.Must(appv1.AddToScheme(testScheme))
  21. }
  22. func TestBlueGreenReconciler_CreateOrUpdateService(t *testing.T) {
  23. t.Run("Should Create a New Service", func(t *testing.T) {
  24. c := fake.NewClientBuilder().WithScheme(testScheme).Build()
  25. r := &BlueGreenClient{Client: c}
  26. NN := types.NamespacedName{
  27. Namespace: "test-namespace",
  28. Name: "test-name",
  29. }
  30. svc := new(corev1.Service)
  31. assert.Error(t, c.Get(context.TODO(), NN, svc))
  32. err := r.CreateOrUpdateService(
  33. context.TODO(),
  34. &appv1.BlueGreen{
  35. ObjectMeta: metav1.ObjectMeta{Namespace: NN.Namespace, Name: NN.Name},
  36. Spec: appv1.BlueGreenSpec{RouteTo: appv1.Blue},
  37. })
  38. assert.NoError(t, err)
  39. assert.NoError(t, c.Get(context.TODO(), NN, svc))
  40. assert.Equal(t, 1, len(svc.ObjectMeta.OwnerReferences))
  41. assert.Equal(t, "app.demo.kakao.com", svc.Spec.Selector["app.kubernetes.io/managed-by"])
  42. assert.Equal(t, "Blue", svc.Spec.Selector["app.kubernetes.io/phase"])
  43. assert.Equal(t, NN.Name, svc.Spec.Selector["app.kubernetes.io/name"])
  44. /* more validation here */
  45. })
  46. }