17
17
18
18
package com .cloud .vm .dao ;
19
19
20
- import com .cloud .utils .Pair ;
21
- import com .cloud .vm .VirtualMachine ;
20
+ import static com .cloud .vm .VirtualMachine .State .Running ;
21
+ import static com .cloud .vm .VirtualMachine .State .Stopped ;
22
+ import static com .cloud .vm .dao .VMInstanceDaoImpl .MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT ;
23
+ import static org .junit .Assert .assertFalse ;
24
+ import static org .junit .Assert .assertTrue ;
25
+ import static org .mockito .ArgumentMatchers .any ;
26
+ import static org .mockito .ArgumentMatchers .anyLong ;
27
+ import static org .mockito .Mockito .doReturn ;
28
+ import static org .mockito .Mockito .never ;
29
+ import static org .mockito .Mockito .times ;
30
+ import static org .mockito .Mockito .verify ;
31
+ import static org .mockito .Mockito .when ;
32
+
33
+ import java .util .Date ;
34
+
22
35
import org .joda .time .DateTime ;
23
36
import org .junit .Before ;
24
37
import org .junit .Test ;
25
- import org .junit .Assert ;
26
38
import org .mockito .Mock ;
27
-
28
- import static com .cloud .vm .VirtualMachine .State .Running ;
29
- import static com .cloud .vm .VirtualMachine .State .Stopped ;
30
-
31
- import static org .mockito .Mockito .when ;
32
- import com .cloud .vm .VMInstanceVO ;
33
39
import org .mockito .MockitoAnnotations ;
34
40
import org .mockito .Spy ;
35
41
42
+ import com .cloud .utils .Pair ;
43
+ import com .cloud .vm .VMInstanceVO ;
44
+ import com .cloud .vm .VirtualMachine ;
45
+
36
46
/**
37
47
* Created by sudharma_jain on 3/2/17.
38
48
*/
@@ -55,16 +65,130 @@ public void setUp() throws Exception {
55
65
}
56
66
57
67
@ Test
58
- public void testUpdateState () throws Exception {
68
+ public void testUpdateState () {
59
69
Long destHostId = null ;
60
- Pair <Long , Long > opaqueMock = new Pair <Long , Long >( new Long ( 1 ) , destHostId );
70
+ Pair <Long , Long > opaqueMock = new Pair <>( 1L , destHostId );
61
71
vmInstanceDao .updateState (Stopped , VirtualMachine .Event .FollowAgentPowerOffReport , Stopped , vm , opaqueMock );
62
72
}
63
73
64
74
@ Test
65
- public void testIfStateAndHostUnchanged () throws Exception {
66
- Assert .assertEquals (vmInstanceDao .ifStateUnchanged (Stopped , Stopped , null , null ), true );
67
- Assert .assertEquals (vmInstanceDao .ifStateUnchanged (Stopped , Running , null , null ), false );
75
+ public void testIfStateAndHostUnchanged () {
76
+ assertTrue (vmInstanceDao .ifStateUnchanged (Stopped , Stopped , null , null ));
77
+ assertFalse (vmInstanceDao .ifStateUnchanged (Stopped , Running , null , null ));
78
+ }
79
+
80
+ @ Test
81
+ public void testUpdatePowerStateDifferentPowerState () {
82
+ when (vm .getPowerStateUpdateTime ()).thenReturn (null );
83
+ when (vm .getPowerHostId ()).thenReturn (1L );
84
+ when (vm .getPowerState ()).thenReturn (VirtualMachine .PowerState .PowerOn );
85
+ doReturn (vm ).when (vmInstanceDao ).findById (anyLong ());
86
+ doReturn (true ).when (vmInstanceDao ).update (anyLong (), any ());
87
+
88
+ boolean result = vmInstanceDao .updatePowerState (1L , 1L , VirtualMachine .PowerState .PowerOff , new Date ());
89
+
90
+ verify (vm , times (1 )).setPowerState (VirtualMachine .PowerState .PowerOff );
91
+ verify (vm , times (1 )).setPowerHostId (1L );
92
+ verify (vm , times (1 )).setPowerStateUpdateCount (1 );
93
+ verify (vm , times (1 )).setPowerStateUpdateTime (any (Date .class ));
94
+
95
+ assertTrue (result );
96
+ }
97
+
98
+ @ Test
99
+ public void testUpdatePowerStateVmNotFound () {
100
+ when (vm .getPowerStateUpdateTime ()).thenReturn (null );
101
+ when (vm .getPowerHostId ()).thenReturn (1L );
102
+ when (vm .getPowerState ()).thenReturn (VirtualMachine .PowerState .PowerOn );
103
+ doReturn (null ).when (vmInstanceDao ).findById (anyLong ());
104
+
105
+ boolean result = vmInstanceDao .updatePowerState (1L , 1L , VirtualMachine .PowerState .PowerOff , new Date ());
106
+
107
+ verify (vm , never ()).setPowerState (any ());
108
+ verify (vm , never ()).setPowerHostId (anyLong ());
109
+ verify (vm , never ()).setPowerStateUpdateCount (any (Integer .class ));
110
+ verify (vm , never ()).setPowerStateUpdateTime (any (Date .class ));
111
+
112
+ assertFalse (result );
113
+ }
114
+
115
+ @ Test
116
+ public void testUpdatePowerStateNoChangeFirstUpdate () {
117
+ when (vm .getPowerStateUpdateTime ()).thenReturn (null );
118
+ when (vm .getPowerHostId ()).thenReturn (1L );
119
+ when (vm .getPowerState ()).thenReturn (VirtualMachine .PowerState .PowerOn );
120
+ when (vm .getState ()).thenReturn (Running );
121
+ when (vm .getPowerStateUpdateCount ()).thenReturn (1 );
122
+ doReturn (vm ).when (vmInstanceDao ).findById (anyLong ());
123
+ doReturn (true ).when (vmInstanceDao ).update (anyLong (), any ());
124
+
125
+ boolean result = vmInstanceDao .updatePowerState (1L , 1L , VirtualMachine .PowerState .PowerOn , new Date ());
126
+
127
+ verify (vm , never ()).setPowerState (any ());
128
+ verify (vm , never ()).setPowerHostId (anyLong ());
129
+ verify (vm , times (1 )).setPowerStateUpdateCount (2 );
130
+ verify (vm , times (1 )).setPowerStateUpdateTime (any (Date .class ));
131
+
132
+ assertTrue (result );
133
+ }
134
+
135
+ @ Test
136
+ public void testUpdatePowerStateNoChangeMaxUpdatesValidState () {
137
+ when (vm .getPowerStateUpdateTime ()).thenReturn (null );
138
+ when (vm .getPowerHostId ()).thenReturn (1L );
139
+ when (vm .getPowerState ()).thenReturn (VirtualMachine .PowerState .PowerOn );
140
+ when (vm .getPowerStateUpdateCount ()).thenReturn (MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT );
141
+ when (vm .getState ()).thenReturn (Running );
142
+ doReturn (vm ).when (vmInstanceDao ).findById (anyLong ());
143
+ doReturn (true ).when (vmInstanceDao ).update (anyLong (), any ());
144
+
145
+ boolean result = vmInstanceDao .updatePowerState (1L , 1L , VirtualMachine .PowerState .PowerOn , new Date ());
146
+
147
+ verify (vm , never ()).setPowerState (any ());
148
+ verify (vm , never ()).setPowerHostId (anyLong ());
149
+ verify (vm , never ()).setPowerStateUpdateCount (any (Integer .class ));
150
+ verify (vm , never ()).setPowerStateUpdateTime (any (Date .class ));
151
+
152
+ assertFalse (result );
68
153
}
69
154
155
+ @ Test
156
+ public void testUpdatePowerStateNoChangeMaxUpdatesInvalidStateVmStopped () {
157
+ when (vm .getPowerStateUpdateTime ()).thenReturn (null );
158
+ when (vm .getPowerHostId ()).thenReturn (1L );
159
+ when (vm .getPowerState ()).thenReturn (VirtualMachine .PowerState .PowerOn );
160
+ when (vm .getPowerStateUpdateCount ()).thenReturn (MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT );
161
+ when (vm .getState ()).thenReturn (Stopped );
162
+ doReturn (vm ).when (vmInstanceDao ).findById (anyLong ());
163
+ doReturn (true ).when (vmInstanceDao ).update (anyLong (), any ());
164
+
165
+ boolean result = vmInstanceDao .updatePowerState (1L , 1L , VirtualMachine .PowerState .PowerOn , new Date ());
166
+
167
+ verify (vm , times (1 )).setPowerState (any ());
168
+ verify (vm , times (1 )).setPowerHostId (anyLong ());
169
+ verify (vm , times (1 )).setPowerStateUpdateCount (1 );
170
+ verify (vm , times (1 )).setPowerStateUpdateTime (any (Date .class ));
171
+
172
+ assertTrue (result );
173
+ }
174
+
175
+ @ Test
176
+ public void testUpdatePowerStateNoChangeMaxUpdatesInvalidStateVmRunning () {
177
+ when (vm .getPowerStateUpdateTime ()).thenReturn (null );
178
+ when (vm .getPowerHostId ()).thenReturn (1L );
179
+ when (vm .getPowerState ()).thenReturn (VirtualMachine .PowerState .PowerOff );
180
+ when (vm .getPowerStateUpdateCount ()).thenReturn (MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT );
181
+ when (vm .getState ()).thenReturn (Running );
182
+ doReturn (vm ).when (vmInstanceDao ).findById (anyLong ());
183
+ doReturn (true ).when (vmInstanceDao ).update (anyLong (), any ());
184
+
185
+ boolean result = vmInstanceDao .updatePowerState (1L , 1L , VirtualMachine .PowerState .PowerOff , new Date ());
186
+
187
+ verify (vm , times (1 )).setPowerState (any ());
188
+ verify (vm , times (1 )).setPowerHostId (anyLong ());
189
+ verify (vm , times (1 )).setPowerStateUpdateCount (1 );
190
+ verify (vm , times (1 )).setPowerStateUpdateTime (any (Date .class ));
191
+
192
+ assertTrue (result );
193
+ }
70
194
}
0 commit comments