|
@@ -17,14 +17,15 @@ limitations under the License.
|
|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"testing"
|
|
|
|
|
|
appv1 "github.com/kakao/bluegreen/api/v1"
|
|
|
- v1 "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"
|
|
@@ -39,25 +40,31 @@ func init() {
|
|
|
utilruntime.Must(appv1.AddToScheme(testScheme))
|
|
|
}
|
|
|
|
|
|
-func TestBlueGreenReconciler_mutateBlueGreenService(t *testing.T) {
|
|
|
- t.Run("Should add proper labels and OwnerReferences", func(t *testing.T) {
|
|
|
- r := &BlueGreenReconciler{
|
|
|
- Client: fake.NewClientBuilder().WithScheme(testScheme).WithRuntimeObjects().Build(),
|
|
|
- Scheme: 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",
|
|
|
}
|
|
|
|
|
|
- const Name = "bluegreen-test"
|
|
|
- const Namespace = "test"
|
|
|
- svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: Namespace, Name: Name}}
|
|
|
- err := r.mutateBlueGreenService(
|
|
|
- &v1.BlueGreen{ObjectMeta: metav1.ObjectMeta{Namespace: Namespace, Name: Name, UID: "0000-0000"}},
|
|
|
- svc, "test", appv1.Blue)()
|
|
|
+ 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, "test", svc.Spec.Selector["app.kubernetes.io/name"])
|
|
|
+ assert.Equal(t, NN.Name, svc.Spec.Selector["app.kubernetes.io/name"])
|
|
|
/* more validation here */
|
|
|
})
|
|
|
}
|