1616// under the License.
1717package com .cloud .hypervisor .kvm .resource ;
1818
19+ import java .net .URI ;
20+ import java .net .URISyntaxException ;
21+
1922import org .junit .Assert ;
20- import org .junit .Before ;
2123import org .junit .Test ;
24+ import org .junit .runner .RunWith ;
25+ import org .mockito .InjectMocks ;
26+ import org .mockito .Mockito ;
27+ import org .mockito .Spy ;
28+ import org .mockito .junit .MockitoJUnitRunner ;
2229
2330import com .cloud .agent .api .to .NicTO ;
31+ import com .cloud .exception .InternalErrorException ;
2432import com .cloud .network .Networks ;
25- import org .junit .runner .RunWith ;
26- import org .mockito .junit .MockitoJUnitRunner ;
2733
2834@ RunWith (MockitoJUnitRunner .class )
2935public class BridgeVifDriverTest {
3036
31- private BridgeVifDriver driver ;
37+ private static final String BRIDGE_NAME = "cloudbr1" ;
3238
33- @ Before
34- public void setUp () throws Exception {
35- driver = new BridgeVifDriver ();
36- }
39+ @ Spy
40+ @ InjectMocks
41+ private BridgeVifDriver driver = new BridgeVifDriver ();
3742
3843 @ Test
3944 public void isBroadcastTypeVlanOrVxlan () {
@@ -58,4 +63,41 @@ public void isValidProtocolAndVnetId() {
5863 Assert .assertTrue (driver .isValidProtocolAndVnetId ("123" , "vlan" ));
5964 Assert .assertTrue (driver .isValidProtocolAndVnetId ("456" , "vxlan" ));
6065 }
66+
67+ @ Test
68+ public void createStorageVnetBridgeIfNeededReturnsStorageBrNameWhenBroadcastTypeIsNotStorageButValidValues () throws InternalErrorException {
69+ NicTO nic = new NicTO ();
70+ nic .setBroadcastType (Networks .BroadcastDomainType .Storage );
71+ int vlan = 123 ;
72+ String newBridge = "br-" + vlan ;
73+ nic .setBroadcastUri (Networks .BroadcastDomainType .Storage .toUri (vlan ));
74+ Mockito .doReturn (newBridge ).when (driver ).createVnetBr (Mockito .anyString (), Mockito .anyString (), Mockito .anyString ());
75+ String result = driver .createStorageVnetBridgeIfNeeded (nic , "trafficLabel" , BRIDGE_NAME );
76+ Assert .assertEquals (newBridge , result );
77+ }
78+
79+ @ Test
80+ public void createStorageVnetBridgeIfNeededReturnsStorageBrNameWhenBroadcastTypeIsNotStorage () throws InternalErrorException {
81+ NicTO nic = new NicTO ();
82+ nic .setBroadcastType (Networks .BroadcastDomainType .Vlan );
83+ String result = driver .createStorageVnetBridgeIfNeeded (nic , "trafficLabel" , BRIDGE_NAME );
84+ Assert .assertEquals (BRIDGE_NAME , result );
85+ }
86+
87+ @ Test
88+ public void createStorageVnetBridgeIfNeededReturnsStorageBrNameWhenBroadcastUriIsNull () throws InternalErrorException {
89+ NicTO nic = new NicTO ();
90+ nic .setBroadcastType (Networks .BroadcastDomainType .Storage );
91+ String result = driver .createStorageVnetBridgeIfNeeded (nic , "trafficLabel" , BRIDGE_NAME );
92+ Assert .assertEquals (BRIDGE_NAME , result );
93+ }
94+
95+ @ Test
96+ public void createStorageVnetBridgeIfNeededCreatesVnetBridgeWhenUntaggedVlan () throws InternalErrorException , URISyntaxException {
97+ NicTO nic = new NicTO ();
98+ nic .setBroadcastType (Networks .BroadcastDomainType .Storage );
99+ nic .setBroadcastUri (new URI (Networks .BroadcastDomainType .Storage .scheme () + "://untagged" ));
100+ String result = driver .createStorageVnetBridgeIfNeeded (nic , "trafficLabel" , BRIDGE_NAME );
101+ Assert .assertEquals (BRIDGE_NAME , result );
102+ }
61103}
0 commit comments