|
| 1 | +package service_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + console "github.com/pluralsh/console/go/client" |
| 15 | + "github.com/pluralsh/deployment-operator/pkg/controller/service" |
| 16 | + "github.com/pluralsh/deployment-operator/pkg/test/mocks" |
| 17 | + "github.com/samber/lo" |
| 18 | + "github.com/stretchr/testify/mock" |
| 19 | + v1 "k8s.io/api/core/v1" |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | + "k8s.io/apimachinery/pkg/types" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("Reconciler", Ordered, func() { |
| 25 | + Context("When reconciling a resource", func() { |
| 26 | + const ( |
| 27 | + namespace = "default" |
| 28 | + serviceId = "1" |
| 29 | + serviceName = "test" |
| 30 | + clusterId = "1" |
| 31 | + clusterName = "cluster-test" |
| 32 | + operatorNamespace = "plrl-deploy-operator" |
| 33 | + ) |
| 34 | + consoleService := &console.GetServiceDeploymentForAgent_ServiceDeployment{ |
| 35 | + ID: serviceId, |
| 36 | + Name: serviceName, |
| 37 | + Namespace: namespace, |
| 38 | + Tarball: lo.ToPtr("http://localhost:8081/ext/v1/digests"), |
| 39 | + Configuration: []*console.GetServiceDeploymentForAgent_ServiceDeployment_Configuration{ |
| 40 | + { |
| 41 | + Name: "name", |
| 42 | + Value: serviceName, |
| 43 | + }, |
| 44 | + }, |
| 45 | + Cluster: &console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster{ |
| 46 | + ID: clusterId, |
| 47 | + Name: clusterName, |
| 48 | + }, |
| 49 | + Revision: &console.GetServiceDeploymentForAgent_ServiceDeployment_Revision{ |
| 50 | + ID: serviceId, |
| 51 | + }, |
| 52 | + } |
| 53 | + ctx := context.Background() |
| 54 | + tarPath := filepath.Join("..", "..", "..", "test", "tarball", "test.tar.gz") |
| 55 | + |
| 56 | + r := gin.Default() |
| 57 | + r.GET("/ext/v1/digests", func(c *gin.Context) { |
| 58 | + res, err := os.ReadFile(tarPath) |
| 59 | + Expect(err).NotTo(HaveOccurred()) |
| 60 | + c.String(http.StatusOK, string(res)) |
| 61 | + }) |
| 62 | + |
| 63 | + srv := &http.Server{ |
| 64 | + Addr: ":8081", |
| 65 | + Handler: r, |
| 66 | + } |
| 67 | + dir := "" |
| 68 | + BeforeEach(func() { |
| 69 | + var err error |
| 70 | + dir, err = os.MkdirTemp("", "test") |
| 71 | + Expect(err).NotTo(HaveOccurred()) |
| 72 | + Expect(kClient.Create(ctx, &v1.Namespace{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{ |
| 74 | + Name: operatorNamespace, |
| 75 | + }, |
| 76 | + })).NotTo(HaveOccurred()) |
| 77 | + // Initializing the server in a goroutine so that |
| 78 | + // it won't block the graceful shutdown handling below |
| 79 | + go func() { |
| 80 | + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 81 | + Expect(err).NotTo(HaveOccurred()) |
| 82 | + } |
| 83 | + }() |
| 84 | + }) |
| 85 | + AfterEach(func() { |
| 86 | + os.RemoveAll(dir) |
| 87 | + Expect(kClient.Delete(ctx, &v1.Namespace{ |
| 88 | + ObjectMeta: metav1.ObjectMeta{ |
| 89 | + Name: operatorNamespace, |
| 90 | + }, |
| 91 | + })).NotTo(HaveOccurred()) |
| 92 | + // The context is used to inform the server it has 5 seconds to finish |
| 93 | + // the request it is currently handling |
| 94 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 95 | + defer cancel() |
| 96 | + if err := srv.Shutdown(ctx); err != nil { |
| 97 | + log.Fatal("Server forced to shutdown: ", err) |
| 98 | + } |
| 99 | + |
| 100 | + log.Println("Server exiting") |
| 101 | + }) |
| 102 | + |
| 103 | + It("should create NewServiceReconciler and apply service", func() { |
| 104 | + fakeConsoleClient := mocks.NewClientMock(mocks.TestingT) |
| 105 | + fakeConsoleClient.On("GetCredentials").Return("", "") |
| 106 | + fakeConsoleClient.On("GetService", mock.Anything).Return(consoleService, nil) |
| 107 | + fakeConsoleClient.On("UpdateComponents", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) |
| 108 | + |
| 109 | + reconciler, err := service.NewServiceReconciler(fakeConsoleClient, cfg, time.Minute, time.Minute, namespace, "http://localhost:8080") |
| 110 | + Expect(err).NotTo(HaveOccurred()) |
| 111 | + _, err = reconciler.Reconcile(ctx, serviceId) |
| 112 | + Expect(err).NotTo(HaveOccurred()) |
| 113 | + |
| 114 | + Expect(kClient.Get(ctx, types.NamespacedName{Name: serviceName, Namespace: namespace}, &v1.Pod{})).NotTo(HaveOccurred()) |
| 115 | + }) |
| 116 | + |
| 117 | + }) |
| 118 | +}) |
0 commit comments