github.com/sacloud/iaas-api-go@v1.12.0/test/packet_filter_op_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/sacloud/iaas-api-go"
    21  	"github.com/sacloud/iaas-api-go/testutil"
    22  	"github.com/sacloud/iaas-api-go/types"
    23  )
    24  
    25  func TestPacketFilterOp_CRUD(t *testing.T) {
    26  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    27  		Parallel:           true,
    28  		IgnoreStartupWait:  true,
    29  		SetupAPICallerFunc: singletonAPICaller,
    30  		Create: &testutil.CRUDTestFunc{
    31  			Func: testPacketFilterCreate,
    32  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    33  				ExpectValue:  createPacketFilterExpected,
    34  				IgnoreFields: packetFilterIgnoreFields,
    35  			}),
    36  		},
    37  		Read: &testutil.CRUDTestFunc{
    38  			Func: testPacketFilterRead,
    39  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    40  				ExpectValue:  createPacketFilterExpected,
    41  				IgnoreFields: packetFilterIgnoreFields,
    42  			}),
    43  		},
    44  		Updates: []*testutil.CRUDTestFunc{
    45  			{
    46  				Func: testPacketFilterUpdate,
    47  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    48  					ExpectValue:  updatePacketFilterExpected,
    49  					IgnoreFields: packetFilterIgnoreFields,
    50  				}),
    51  			},
    52  			{
    53  				Func: testPacketFilterUpdateToMin,
    54  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    55  					ExpectValue:  updatePacketFilterToMinExpected,
    56  					IgnoreFields: packetFilterIgnoreFields,
    57  				}),
    58  			},
    59  		},
    60  		Delete: &testutil.CRUDTestDeleteFunc{
    61  			Func: testPacketFilterDelete,
    62  		},
    63  	})
    64  }
    65  
    66  var (
    67  	packetFilterIgnoreFields = []string{
    68  		"ID",
    69  		"CreatedAt",
    70  		"RequiredHostVersion",
    71  		"ExpressionHash",
    72  	}
    73  
    74  	createPacketFilterParam = &iaas.PacketFilterCreateRequest{
    75  		Name:        testutil.ResourceName("packet-filter"),
    76  		Description: "desc",
    77  		Expression: []*iaas.PacketFilterExpression{
    78  			{
    79  				Protocol:      types.Protocols.TCP,
    80  				SourceNetwork: types.PacketFilterNetwork("192.168.0.1"),
    81  				SourcePort:    types.PacketFilterPort("3000-3100"),
    82  				Action:        types.Actions.Allow,
    83  			},
    84  			{
    85  				Protocol: types.Protocols.IP,
    86  				Action:   types.Actions.Deny,
    87  			},
    88  		},
    89  	}
    90  	createPacketFilterExpected = &iaas.PacketFilter{
    91  		Name:        createPacketFilterParam.Name,
    92  		Description: createPacketFilterParam.Description,
    93  		Expression:  createPacketFilterParam.Expression,
    94  	}
    95  	updatePacketFilterParam = &iaas.PacketFilterUpdateRequest{
    96  		Name:        testutil.ResourceName("packet-filter-upd"),
    97  		Description: "desc-upd",
    98  		Expression: []*iaas.PacketFilterExpression{
    99  			{
   100  				Protocol:        types.Protocols.TCP,
   101  				SourceNetwork:   types.PacketFilterNetwork("192.168.0.2"),
   102  				DestinationPort: types.PacketFilterPort("4000-41000"),
   103  				Action:          types.Actions.Allow,
   104  			},
   105  			{
   106  				Protocol:        types.Protocols.UDP,
   107  				SourceNetwork:   types.PacketFilterNetwork("192.168.0.3"),
   108  				DestinationPort: types.PacketFilterPort("5000-5100"),
   109  				Action:          types.Actions.Allow,
   110  			},
   111  			{
   112  				Protocol: types.Protocols.IP,
   113  				Action:   types.Actions.Deny,
   114  			},
   115  		},
   116  	}
   117  	updatePacketFilterExpected = &iaas.PacketFilter{
   118  		Name:        updatePacketFilterParam.Name,
   119  		Description: updatePacketFilterParam.Description,
   120  		Expression:  updatePacketFilterParam.Expression,
   121  	}
   122  
   123  	updatePacketFilterToMinParam = &iaas.PacketFilterUpdateRequest{
   124  		Name: testutil.ResourceName("packet-filter-to-min"),
   125  	}
   126  	updatePacketFilterToMinExpected = &iaas.PacketFilter{
   127  		Name: updatePacketFilterToMinParam.Name,
   128  	}
   129  )
   130  
   131  func testPacketFilterCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   132  	client := iaas.NewPacketFilterOp(caller)
   133  	return client.Create(ctx, testZone, createPacketFilterParam)
   134  }
   135  
   136  func testPacketFilterRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   137  	client := iaas.NewPacketFilterOp(caller)
   138  	return client.Read(ctx, testZone, ctx.ID)
   139  }
   140  
   141  func testPacketFilterUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   142  	client := iaas.NewPacketFilterOp(caller)
   143  	return client.Update(ctx, testZone, ctx.ID, updatePacketFilterParam, "")
   144  }
   145  
   146  func testPacketFilterUpdateToMin(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   147  	client := iaas.NewPacketFilterOp(caller)
   148  	return client.Update(ctx, testZone, ctx.ID, updatePacketFilterToMinParam, "")
   149  }
   150  
   151  func testPacketFilterDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
   152  	client := iaas.NewPacketFilterOp(caller)
   153  	return client.Delete(ctx, testZone, ctx.ID)
   154  }