|
| 1 | +package clusteraccess |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/mock" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | + "k8s.io/client-go/rest" |
| 12 | + ctrl "sigs.k8s.io/controller-runtime" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 14 | + |
| 15 | + "github.com/platform-mesh/golang-commons/logger" |
| 16 | + gatewayv1alpha1 "github.com/platform-mesh/kubernetes-graphql-gateway/common/apis/v1alpha1" |
| 17 | + workspacefile_mocks "github.com/platform-mesh/kubernetes-graphql-gateway/listener/pkg/workspacefile/mocks" |
| 18 | + "github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler" |
| 19 | +) |
| 20 | + |
| 21 | +func TestGenerateSchemaSubroutine_Process_InvalidResourceType(t *testing.T) { |
| 22 | + mockIO := workspacefile_mocks.NewMockIOHandler(t) |
| 23 | + log, _ := logger.New(logger.DefaultConfig()) |
| 24 | + |
| 25 | + r := &ClusterAccessReconciler{ |
| 26 | + ioHandler: mockIO, |
| 27 | + log: log, |
| 28 | + } |
| 29 | + s := &generateSchemaSubroutine{reconciler: r} |
| 30 | + |
| 31 | + _, opErr := s.Process(context.Background(), &metav1.PartialObjectMetadata{}) |
| 32 | + |
| 33 | + assert.NotNil(t, opErr) |
| 34 | +} |
| 35 | + |
| 36 | +func TestGenerateSchemaSubroutine_Process_MissingHostReturnsError(t *testing.T) { |
| 37 | + scheme := runtime.NewScheme() |
| 38 | + _ = gatewayv1alpha1.AddToScheme(scheme) |
| 39 | + |
| 40 | + ca := &gatewayv1alpha1.ClusterAccess{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: "test-cluster", |
| 43 | + Annotations: map[string]string{}, |
| 44 | + }, |
| 45 | + Spec: gatewayv1alpha1.ClusterAccessSpec{ |
| 46 | + // Host is intentionally empty to trigger validation error |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(ca).Build() |
| 51 | + |
| 52 | + mockIO := workspacefile_mocks.NewMockIOHandler(t) |
| 53 | + mockIO.EXPECT().Write(mock.Anything, mock.Anything).Maybe().Return(nil) |
| 54 | + mockIO.EXPECT().Delete(mock.Anything).Maybe().Return(nil) |
| 55 | + |
| 56 | + log, _ := logger.New(logger.DefaultConfig()) |
| 57 | + |
| 58 | + r := &ClusterAccessReconciler{ |
| 59 | + ioHandler: mockIO, |
| 60 | + log: log, |
| 61 | + opts: reconciler.ReconcilerOpts{ |
| 62 | + Client: fakeClient, |
| 63 | + Config: &rest.Config{Host: "https://unit-test.invalid"}, |
| 64 | + ManagerOpts: ctrl.Options{Scheme: scheme}, |
| 65 | + Scheme: scheme, |
| 66 | + }, |
| 67 | + } |
| 68 | + s := &generateSchemaSubroutine{reconciler: r} |
| 69 | + |
| 70 | + res, opErr := s.Process(context.Background(), ca) |
| 71 | + |
| 72 | + assert.NotNil(t, opErr) |
| 73 | + assert.Equal(t, ctrl.Result{}, res) |
| 74 | +} |
| 75 | + |
| 76 | +func TestGenerateSchemaSubroutine_Finalize_DeletesCurrentAndPreviousPaths(t *testing.T) { |
| 77 | + mockIO := workspacefile_mocks.NewMockIOHandler(t) |
| 78 | + log, _ := logger.New(logger.DefaultConfig()) |
| 79 | + |
| 80 | + // Expect deletion of both current and previous paths |
| 81 | + mockIO.EXPECT().Delete("current-path").Return(nil).Once() |
| 82 | + mockIO.EXPECT().Delete("previous-path").Return(nil).Once() |
| 83 | + |
| 84 | + r := &ClusterAccessReconciler{ |
| 85 | + ioHandler: mockIO, |
| 86 | + log: log, |
| 87 | + } |
| 88 | + s := &generateSchemaSubroutine{reconciler: r} |
| 89 | + |
| 90 | + ca := &gatewayv1alpha1.ClusterAccess{ |
| 91 | + ObjectMeta: metav1.ObjectMeta{ |
| 92 | + Name: "my-resource", |
| 93 | + }, |
| 94 | + Spec: gatewayv1alpha1.ClusterAccessSpec{ |
| 95 | + Path: "current-path", |
| 96 | + }, |
| 97 | + Status: gatewayv1alpha1.ClusterAccessStatus{ |
| 98 | + ObservedPath: "previous-path", |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + res, opErr := s.Finalize(context.Background(), ca) |
| 103 | + |
| 104 | + assert.Nil(t, opErr) |
| 105 | + assert.Equal(t, ctrl.Result{}, res) |
| 106 | +} |
| 107 | + |
| 108 | +func TestGenerateSchemaSubroutine_restMapperFromConfig_SucceedsWithMinimalConfig(t *testing.T) { |
| 109 | + mockIO := workspacefile_mocks.NewMockIOHandler(t) |
| 110 | + log, _ := logger.New(logger.DefaultConfig()) |
| 111 | + |
| 112 | + r := &ClusterAccessReconciler{ |
| 113 | + ioHandler: mockIO, |
| 114 | + log: log, |
| 115 | + } |
| 116 | + s := &generateSchemaSubroutine{reconciler: r} |
| 117 | + |
| 118 | + rm, err := s.restMapperFromConfig(&rest.Config{}) |
| 119 | + |
| 120 | + assert.NotNil(t, rm) |
| 121 | + assert.NoError(t, err) |
| 122 | +} |
0 commit comments