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

     1  package integrations
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	. "github.com/onsi/gomega"
     8  
     9  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/clients/applet_mgr"
    10  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/requires"
    11  	base "github.com/machinefi/w3bstream/pkg/depends/base/types"
    12  	"github.com/machinefi/w3bstream/pkg/enums"
    13  	"github.com/machinefi/w3bstream/pkg/types"
    14  )
    15  
    16  func TestTrafficLimitAPIs(t *testing.T) {
    17  	var (
    18  		client      = requires.AuthClient()
    19  		projectName = "test_traffic_limit_project"
    20  
    21  		projectID      types.SFID
    22  		trafficLimitID types.SFID
    23  	)
    24  
    25  	t.Logf("random a project name: %s, use this name create a project.", projectName)
    26  
    27  	{
    28  		req := &applet_mgr.CreateProject{}
    29  		req.CreateReq.Name = projectName
    30  
    31  		rsp, _, err := client.CreateProject(req)
    32  		if err != nil {
    33  			panic(err)
    34  		}
    35  		projectID = rsp.ProjectID
    36  	}
    37  
    38  	defer func() {
    39  		req := &applet_mgr.RemoveProject{ProjectName: projectName}
    40  		_, err := client.RemoveProject(req)
    41  		if err != nil {
    42  			panic(err)
    43  		}
    44  	}()
    45  
    46  	t.Run("TrafficLimit", func(t *testing.T) {
    47  		t.Run("#CreateTrafficLimit", func(t *testing.T) {
    48  			t.Run("#Success", func(t *testing.T) {
    49  
    50  				// create trafficLimit
    51  				{
    52  					req := &applet_mgr.CreateTrafficLimit{ProjectName: projectName}
    53  					req.CreateReq.Threshold = 2
    54  					req.CreateReq.Duration = base.Duration(10 * time.Minute)
    55  					req.CreateReq.ApiType = enums.TRAFFIC_LIMIT_TYPE__EVENT
    56  
    57  					rsp, _, err := client.CreateTrafficLimit(req)
    58  					NewWithT(t).Expect(err).To(BeNil())
    59  					trafficLimitID = rsp.TrafficLimitID
    60  				}
    61  
    62  				// get trafficLimit
    63  				{
    64  					req := &applet_mgr.GetTrafficLimit{TrafficLimitID: trafficLimitID}
    65  					rsp, _, err := client.GetTrafficLimit(req)
    66  					NewWithT(t).Expect(err).To(BeNil())
    67  					NewWithT(t).Expect(rsp.ProjectID).To(Equal(projectID))
    68  				}
    69  
    70  				// update trafficLimit
    71  				{
    72  					req := &applet_mgr.UpdateTrafficLimit{
    73  						ProjectName:    projectName,
    74  						TrafficLimitID: trafficLimitID,
    75  						UpdateReq: applet_mgr.GithubComMachinefiW3BstreamPkgModulesTrafficlimitUpdateReq{
    76  							Threshold: 100, Duration: base.Duration(time.Second),
    77  						},
    78  					}
    79  					req.UpdateReq.Threshold = 3
    80  					req.UpdateReq.Duration = base.Duration(3 * time.Minute)
    81  
    82  					_, _, err := client.UpdateTrafficLimit(req)
    83  					NewWithT(t).Expect(err).To(BeNil())
    84  				}
    85  
    86  				// remove trafficLimit
    87  				{
    88  					req := &applet_mgr.RemoveTrafficLimit{ProjectName: projectName, TrafficLimitID: trafficLimitID}
    89  					_, err := client.RemoveTrafficLimit(req)
    90  					NewWithT(t).Expect(err).To(BeNil())
    91  				}
    92  			})
    93  		})
    94  	})
    95  
    96  	t.Run("BatchTrafficLimit", func(t *testing.T) {
    97  		t.Run("#CreateTrafficLimits", func(t *testing.T) {
    98  			t.Run("#Success", func(t *testing.T) {
    99  
   100  				// prepare data
   101  				{
   102  					req := &applet_mgr.CreateTrafficLimit{ProjectName: projectName}
   103  					req.CreateReq.Threshold = 2
   104  					req.CreateReq.Duration = base.Duration(2 * time.Minute)
   105  					req.CreateReq.ApiType = enums.TRAFFIC_LIMIT_TYPE__EVENT
   106  
   107  					_, _, err := client.CreateTrafficLimit(req)
   108  					NewWithT(t).Expect(err).To(BeNil())
   109  
   110  					req = &applet_mgr.CreateTrafficLimit{ProjectName: projectName}
   111  					req.CreateReq.Threshold = 3
   112  					req.CreateReq.Duration = base.Duration(3 * time.Minute)
   113  					req.CreateReq.ApiType = enums.TRAFFIC_LIMIT_TYPE__BLOCKCHAIN
   114  
   115  					_, _, err = client.CreateTrafficLimit(req)
   116  					NewWithT(t).Expect(err).To(BeNil())
   117  				}
   118  
   119  				// get list trafficLimit
   120  				{
   121  					req := &applet_mgr.ListTrafficLimit{ProjectName: projectName}
   122  					rsp, _, err := client.ListTrafficLimit(req)
   123  					NewWithT(t).Expect(err).To(BeNil())
   124  					NewWithT(t).Expect(2).To(Equal(int(rsp.Total)))
   125  				}
   126  
   127  				// remove batch trafficLimit
   128  				{
   129  					req := &applet_mgr.BatchRemoveTrafficLimit{ProjectName: projectName}
   130  					_, err := client.BatchRemoveTrafficLimit(req)
   131  					NewWithT(t).Expect(err).To(BeNil())
   132  				}
   133  			})
   134  		})
   135  	})
   136  }