github.com/sacloud/iaas-api-go@v1.12.0/naked/vpc_router_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 naked 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 "github.com/sacloud/iaas-api-go/types" 22 "github.com/stretchr/testify/require" 23 ) 24 25 const vpcRouterMultipleInterfaceJSON = ` 26 [ 27 { 28 "IPAddress": "100.65.17.75", 29 "UserIPAddress": null, 30 "HostName": null, 31 "Switch": { 32 "ID": "112600699555", 33 "Name": "スイッチ", 34 "Scope": "shared", 35 "Subnet": { 36 "NetworkAddress": "100.65.0.0", 37 "NetworkMaskLen": 18, 38 "DefaultRoute": "100.65.0.1", 39 "Internet": { 40 "BandWidthMbps": 100 41 } 42 }, 43 "UserSubnet": { 44 "DefaultRoute": "100.65.0.1", 45 "NetworkMaskLen": 18 46 } 47 } 48 }, 49 null, 50 { 51 "IPAddress": null, 52 "UserIPAddress": null, 53 "HostName": null, 54 "Switch": { 55 "ID": "113100846288", 56 "Name": "name", 57 "Scope": "user", 58 "Subnet": null, 59 "UserSubnet": null 60 } 61 }, 62 null, 63 null, 64 null, 65 null, 66 null 67 ] 68 ` 69 70 func TestVPCRouterUnmarshalInterfaceJSON(t *testing.T) { 71 var ifs Interfaces 72 err := json.Unmarshal([]byte(vpcRouterMultipleInterfaceJSON), &ifs) 73 require.NoError(t, err) 74 require.Len(t, ifs, 2) 75 76 require.Equal(t, 0, ifs[0].Index) 77 require.Equal(t, 2, ifs[1].Index) 78 } 79 80 const ( 81 vpcRouterRemarkServersEmptyJSON = `[""]` 82 vpcRouterRemarkServersNotEmptyJSON = `[{"IPAddress":"192.168.0.1"}]` 83 ) 84 85 func TestVPCRouterRemarkServers_UnmarshalJSON(t *testing.T) { 86 var remarkServers ApplianceRemarkServers 87 err := json.Unmarshal([]byte(vpcRouterRemarkServersEmptyJSON), &remarkServers) 88 require.NoError(t, err) 89 require.Len(t, remarkServers, 0) 90 91 err = json.Unmarshal([]byte(vpcRouterRemarkServersNotEmptyJSON), &remarkServers) 92 require.NoError(t, err) 93 require.Len(t, remarkServers, 1) 94 } 95 96 const vpcRouterMultipleFirewallJSON = ` 97 { 98 "Config": [ 99 { 100 "Receive": [], 101 "Send": [] 102 }, 103 { 104 "Receive": [], 105 "Send": [] 106 }, 107 { 108 "Receive": [], 109 "Send": [ 110 { 111 "Protocol": "ip", 112 "SourceNetwork": null, 113 "SourcePort": null, 114 "DestinationNetwork": null, 115 "DestinationPort": null, 116 "Action": "deny", 117 "Logging": "False", 118 "Description": "" 119 } 120 ] 121 }, 122 { 123 "Receive": [], 124 "Send": [] 125 }, 126 { 127 "Receive": [], 128 "Send": [] 129 }, 130 { 131 "Receive": [], 132 "Send": [] 133 }, 134 { 135 "Receive": [], 136 "Send": [] 137 }, 138 { 139 "Receive": [], 140 "Send": [] 141 } 142 ], 143 "Enabled": "True" 144 } 145 ` 146 147 var vpcRouterFirewallMarshaled = `{"Config":[{"Receive":[],"Send":[]},{"Receive":[],"Send":[]},{"Receive":[],"Send":[{"Protocol":"ip","Action":"deny","Logging":"False","Description":""}]},{"Receive":[],"Send":[]},{"Receive":[],"Send":[]},{"Receive":[],"Send":[]},{"Receive":[],"Send":[]},{"Receive":[],"Send":[]}],"Enabled":"True"}` 148 149 func TestVPCRouterFirewall_UnmarshalJSON(t *testing.T) { 150 var firewallConfig VPCRouterFirewall 151 err := json.Unmarshal([]byte(vpcRouterMultipleFirewallJSON), &firewallConfig) 152 require.NoError(t, err) 153 for i, v := range firewallConfig.Config { 154 require.Equal(t, i, v.Index) 155 } 156 } 157 158 func TestVPCRouterFirewall_MarshalJSON(t *testing.T) { 159 firewallConfig := &VPCRouterFirewall{ 160 Config: VPCRouterFirewallConfigs{ 161 { 162 Send: []*VPCRouterFirewallRule{ 163 { 164 Protocol: "ip", 165 Action: types.Actions.Deny, 166 }, 167 }, 168 Index: 2, 169 }, 170 }, 171 Enabled: types.StringFlag(true), 172 } 173 174 data, err := json.Marshal(firewallConfig) 175 require.NoError(t, err) 176 require.Equal(t, vpcRouterFirewallMarshaled, string(data)) 177 }