github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/tests/integrations/cron_job_test.go (about)

     1  package integrations
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/onsi/gomega"
     7  
     8  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/clients/applet_mgr"
     9  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/requires"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
    11  	"github.com/machinefi/w3bstream/pkg/errors/status"
    12  	"github.com/machinefi/w3bstream/pkg/modules/cronjob"
    13  	"github.com/machinefi/w3bstream/pkg/types"
    14  )
    15  
    16  func TestCronJobAPIs(t *testing.T) {
    17  	var (
    18  		ctx         = requires.Context()
    19  		client      = requires.AuthClient()
    20  		projectName = "test_project_for_cron_job"
    21  		cronJobID   types.SFID
    22  		projectID   types.SFID
    23  	)
    24  
    25  	t.Logf("random a project name: %s", projectName)
    26  
    27  	t.Run("CronJob", func(t *testing.T) {
    28  		t.Run("#CreateCronJob", func(t *testing.T) {
    29  			t.Run("#Success", func(t *testing.T) {
    30  
    31  				// create project without user defined config(database/env)
    32  				{
    33  					req := &applet_mgr.CreateProject{}
    34  					req.CreateReq.Name = projectName
    35  
    36  					rsp, _, err := client.CreateProject(req)
    37  
    38  					NewWithT(t).Expect(err).To(BeNil())
    39  					NewWithT(t).Expect(rsp.Name).To(Equal(projectName))
    40  					projectID = rsp.ProjectID
    41  				}
    42  
    43  				// create cron job
    44  				{
    45  					req := applet_mgr.CreateCronJob{
    46  						ProjectID: projectID,
    47  					}
    48  					req.CreateReq.EventType = "default"
    49  					req.CreateReq.CronExpressions = "*/3 * * * *"
    50  					rsp, _, err := client.CreateCronJob(&req)
    51  
    52  					NewWithT(t).Expect(err).To(BeNil())
    53  					cronJobID = rsp.CronJobID
    54  				}
    55  
    56  				// check cron job is created
    57  				{
    58  					_, err := cronjob.GetBySFID(ctx, cronJobID)
    59  					NewWithT(t).Expect(err).To(BeNil())
    60  				}
    61  
    62  				// remove cron job
    63  				{
    64  					req := &applet_mgr.RemoveCronJob{
    65  						CronJobID: cronJobID,
    66  					}
    67  					_, err := client.RemoveCronJob(req)
    68  					NewWithT(t).Expect(err).To(BeNil())
    69  				}
    70  
    71  				// check cron job is removed
    72  				{
    73  					_, err := cronjob.GetBySFID(ctx, cronJobID)
    74  					requires.CheckError(t, err, status.CronJobNotFound)
    75  				}
    76  
    77  				// remove project
    78  				{
    79  					req := &applet_mgr.RemoveProject{ProjectName: projectName}
    80  					_, err := client.RemoveProject(req)
    81  					NewWithT(t).Expect(err).To(BeNil())
    82  				}
    83  			})
    84  			t.Run("#InvalidCronJobParam", func(t *testing.T) {
    85  
    86  				// create project without user defined config(database/env)
    87  				{
    88  					req := &applet_mgr.CreateProject{}
    89  					req.CreateReq.Name = projectName
    90  
    91  					rsp, _, err := client.CreateProject(req)
    92  
    93  					NewWithT(t).Expect(err).To(BeNil())
    94  					NewWithT(t).Expect(rsp.Name).To(Equal(projectName))
    95  					projectID = rsp.ProjectID
    96  				}
    97  
    98  				// cron expressions is empty
    99  				{
   100  					req := applet_mgr.CreateCronJob{
   101  						ProjectID: projectID,
   102  					}
   103  
   104  					_, _, err := client.CreateCronJob(&req)
   105  					requires.CheckError(t, err, &statusx.StatusErr{
   106  						Key: "badRequest",
   107  					})
   108  				}
   109  
   110  				// cron expressions is illegal
   111  				{
   112  					req := applet_mgr.CreateCronJob{
   113  						ProjectID: projectID,
   114  					}
   115  					req.CreateReq.CronExpressions = "*/3 * * * * *"
   116  
   117  					_, _, err := client.CreateCronJob(&req)
   118  					requires.CheckError(t, err, &statusx.StatusErr{
   119  						Key: "InvalidCronExpressions",
   120  					})
   121  				}
   122  
   123  				// remove project
   124  				{
   125  					req := &applet_mgr.RemoveProject{ProjectName: projectName}
   126  					_, err := client.RemoveProject(req)
   127  					NewWithT(t).Expect(err).To(BeNil())
   128  				}
   129  			})
   130  		})
   131  	})
   132  }