1919
2020package org .apache .cloudstack .diagnostics ;
2121
22+ import com .cloud .agent .AgentManager ;
23+ import com .cloud .exception .InvalidParameterValueException ;
24+ import com .cloud .network .router .RouterControlHelper ;
25+ import com .cloud .vm .VMInstanceVO ;
26+ import com .cloud .vm .VirtualMachine ;
27+ import com .cloud .vm .dao .VMInstanceDao ;
28+ import junit .framework .TestCase ;
29+ import org .apache .cloudstack .api .command .admin .diagnostics .ExecuteDiagnosticsCmd ;
30+ import org .apache .cloudstack .diangostics .DiagnosticsType ;
2231import org .junit .After ;
2332import org .junit .Before ;
2433import org .junit .Test ;
34+ import org .junit .runner .RunWith ;
35+ import org .mockito .InjectMocks ;
36+ import org .mockito .Mock ;
37+ import org .mockito .Mockito ;
38+ import org .mockito .runners .MockitoJUnitRunner ;
39+
40+ import java .util .Map ;
41+
42+ @ RunWith (MockitoJUnitRunner .class )
43+ public class DiagnosticsServiceImplTest extends TestCase {
44+
45+ @ Mock
46+ private AgentManager agentManager ;
47+ @ Mock
48+ private VMInstanceDao instanceDao ;
49+ @ Mock
50+ private RouterControlHelper routerControlHelper ;
51+ @ Mock
52+ private ExecuteDiagnosticsCmd diagnosticsCmd ;
53+ @ Mock
54+ private DiagnosticsCommand command ;
55+ @ Mock
56+ private VMInstanceVO instanceVO ;
57+
58+ @ InjectMocks
59+ private DiagnosticsServiceImpl diagnosticsService = new DiagnosticsServiceImpl ();
2560
26- public class DiagnosticsServiceImplTest {
2761
2862 @ Before
2963 public void setUp () throws Exception {
64+
3065 }
3166
3267 @ After
3368 public void tearDown () throws Exception {
69+ Mockito .reset (diagnosticsCmd );
70+ Mockito .reset (agentManager );
71+ Mockito .reset (instanceDao );
72+ Mockito .reset (instanceVO );
73+ }
74+
75+ @ Test
76+ public void testRunDiagnosticsCommandTrue () throws Exception {
77+
78+ Mockito .when (diagnosticsCmd .getType ()).thenReturn (DiagnosticsType .PING );
79+ Mockito .when (diagnosticsCmd .getAddress ()).thenReturn ("8.8.8.8" );
80+ Mockito .when (diagnosticsCmd .getId ()).thenReturn (1L );
81+ Mockito .when (instanceDao .findByIdTypes (Mockito .anyLong (), Mockito .any (VirtualMachine .Type .class ),
82+ Mockito .any (VirtualMachine .Type .class ), Mockito .any (VirtualMachine .Type .class ))).thenReturn (instanceVO );
83+
84+ Mockito .when (agentManager .send (Mockito .anyLong (), Mockito .any (DiagnosticsCommand .class ))).thenReturn (new DiagnosticsAnswer (command , true , "PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\n " +
85+ "64 bytes from 8.8.8.8: icmp_seq=1 ttl=125 time=7.88 ms\n " +
86+ "64 bytes from 8.8.8.8: icmp_seq=2 ttl=125 time=251 ms\n " +
87+ "64 bytes from 8.8.8.8: icmp_seq=3 ttl=125 time=64.9 ms\n " +
88+ "64 bytes from 8.8.8.8: icmp_seq=4 ttl=125 time=50.7 ms\n " +
89+ "64 bytes from 8.8.8.8: icmp_seq=5 ttl=125 time=67.9 ms\n " +
90+ "\n " +
91+ "--- 8.8.8.8 ping statistics ---\n " +
92+ "5 packets transmitted, 5 received, 0% packet loss, time 4003ms\n " +
93+ "rtt min/avg/max/mdev = 7.881/88.587/251.410/84.191 ms}\n " +
94+ "}\n " +
95+ "0\n " ));
96+
97+ Map <String , String > detailsMap = diagnosticsService .runDiagnosticsCommand (diagnosticsCmd );
98+
99+ String stdout = "PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\n " +
100+ "64 bytes from 8.8.8.8: icmp_seq=1 ttl=125 time=7.88 ms\n " +
101+ "64 bytes from 8.8.8.8: icmp_seq=2 ttl=125 time=251 ms\n " +
102+ "64 bytes from 8.8.8.8: icmp_seq=3 ttl=125 time=64.9 ms\n " +
103+ "64 bytes from 8.8.8.8: icmp_seq=4 ttl=125 time=50.7 ms\n " +
104+ "64 bytes from 8.8.8.8: icmp_seq=5 ttl=125 time=67.9 ms\n " +
105+ "\n " +
106+ "--- 8.8.8.8 ping statistics ---\n " +
107+ "5 packets transmitted, 5 received, 0% packet loss, time 4003ms\n " +
108+ "rtt min/avg/max/mdev = 7.881/88.587/251.410/84.191 ms" ;
109+
110+ assertEquals (4 , detailsMap .size ());
111+ assertEquals ("Mismatch between actual and expected STDERR" , "" , detailsMap .get ("STDERR" ));
112+ assertEquals ("Mismatch between actual and expected EXITCODE" , "0" , detailsMap .get ("EXITCODE" ));
113+ assertEquals ("Mismatch between actual and expected STDOUT" , stdout , detailsMap .get ("STDOUT" ));
34114 }
35115
36116 @ Test
37- public void runDiagnosticsCommand () {
117+ public void testRunDiagnosticsCommandFalse () throws Exception {
118+
119+ Mockito .when (diagnosticsCmd .getType ()).thenReturn (DiagnosticsType .PING );
120+ Mockito .when (diagnosticsCmd .getAddress ()).thenReturn ("8.8.8." );
121+ Mockito .when (diagnosticsCmd .getId ()).thenReturn (1L );
122+ Mockito .when (instanceDao .findByIdTypes (Mockito .anyLong (), Mockito .any (VirtualMachine .Type .class ),
123+ Mockito .any (VirtualMachine .Type .class ), Mockito .any (VirtualMachine .Type .class ))).thenReturn (instanceVO );
124+
125+ Mockito .when (agentManager .send (Mockito .anyLong (), Mockito .any (DiagnosticsCommand .class ))).thenReturn (new DiagnosticsAnswer (command , false , "}\n " +
126+ "ping: unknown host}\n " +
127+ "1\n " ));
128+
129+ Map <String , String > detailsMap = diagnosticsService .runDiagnosticsCommand (diagnosticsCmd );
130+
131+ assertEquals (4 , detailsMap .size ());
132+ assertEquals ("Mismatch between actual and expected STDERR" , "ping: unknown host" , detailsMap .get ("STDERR" ));
133+ assertTrue ("Mismatch between actual and expected EXITCODE" , !detailsMap .get ("EXITCODE" ).equalsIgnoreCase ("0" ));
134+ assertEquals ("Mismatch between actual and expected STDOUT" , "" , detailsMap .get ("STDOUT" ));
135+ }
136+
137+ @ Test (expected = InvalidParameterValueException .class )
138+ public void testRunDiagnosticsThrowsInvalidParamException () throws Exception {
139+ //Mockito.when(instanceDao.findByIdTypes(Mockito.anyLong())).thenReturn(null);
140+ Mockito .when (diagnosticsCmd .getType ()).thenReturn (DiagnosticsType .PING );
141+ Mockito .when (diagnosticsCmd .getAddress ()).thenReturn ("8.8.8." );
142+ Mockito .when (diagnosticsCmd .getId ()).thenReturn (1L );
143+ Mockito .when (instanceDao .findByIdTypes (Mockito .anyLong (), Mockito .any (VirtualMachine .Type .class ),
144+ Mockito .any (VirtualMachine .Type .class ), Mockito .any (VirtualMachine .Type .class ))).thenReturn (null );
145+
146+ Map <String , String > detailsMap = diagnosticsService .runDiagnosticsCommand (diagnosticsCmd );
38147 }
39148}
0 commit comments