1+ // Licensed to the Apache Software Foundation (ASF) under one
2+ // or more contributor license agreements. See the NOTICE file
3+ // distributed with this work for additional information
4+ // regarding copyright ownership. The ASF licenses this file
5+ // to you under the Apache License, Version 2.0 (the
6+ // "License"); you may not use this file except in compliance
7+ // with the License. You may obtain a copy of the License at
8+ //
9+ // http://www.apache.org/licenses/LICENSE-2.0
10+ //
11+ // Unless required by applicable law or agreed to in writing,
12+ // software distributed under the License is distributed on an
13+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ // KIND, either express or implied. See the License for the
15+ // specific language governing permissions and limitations
16+ // under the License.
17+ package com .cloud .hypervisor .xenserver .resource .wrapper ;
18+
19+ import static org .junit .Assert .assertFalse ;
20+ import static org .junit .Assert .assertNotNull ;
21+ import static org .junit .Assert .assertTrue ;
22+ import static org .junit .Assert .fail ;
23+ import static org .mockito .Mockito .times ;
24+ import static org .mockito .Mockito .verify ;
25+ import static org .mockito .Mockito .when ;
26+
27+ import java .util .HashMap ;
28+
29+ import org .apache .xmlrpc .XmlRpcException ;
30+ import org .junit .Test ;
31+ import org .junit .runner .RunWith ;
32+ import org .mockito .Mock ;
33+ import org .mockito .Mockito ;
34+ import org .powermock .modules .junit4 .PowerMockRunner ;
35+
36+ import com .cloud .agent .api .Answer ;
37+ import com .cloud .agent .api .GetGPUStatsCommand ;
38+ import com .cloud .agent .api .VgpuTypesInfo ;
39+ import com .cloud .hypervisor .xenserver .resource .XenServer620SP1Resource ;
40+ import com .cloud .utils .exception .CloudRuntimeException ;
41+ import com .xensource .xenapi .Connection ;
42+ import com .xensource .xenapi .Types .XenAPIException ;
43+
44+ @ RunWith (PowerMockRunner .class )
45+ public class XenServer620SP1WrapperTest {
46+
47+ @ Mock
48+ private XenServer620SP1Resource xenServer620SP1Resource ;
49+
50+ @ Test
51+ public void testGetGPUStatsCommand () {
52+ final String guuid = "246a5b75-05ed-4bbc-a171-2d1fe94a1b0e" ;
53+
54+ final Connection conn = Mockito .mock (Connection .class );
55+
56+ final GetGPUStatsCommand gpuStats = new GetGPUStatsCommand (guuid , "xen" );
57+
58+ final CitrixRequestWrapper wrapper = CitrixRequestWrapper .getInstance ();
59+ assertNotNull (wrapper );
60+
61+ when (xenServer620SP1Resource .getConnection ()).thenReturn (conn );
62+ try {
63+ when (xenServer620SP1Resource .getGPUGroupDetails (conn )).thenReturn (new HashMap <String , HashMap <String , VgpuTypesInfo >>());
64+ } catch (final XenAPIException e ) {
65+ fail (e .getMessage ());
66+ } catch (final XmlRpcException e ) {
67+ fail (e .getMessage ());
68+ }
69+
70+ final Answer answer = wrapper .execute (gpuStats , xenServer620SP1Resource );
71+ verify (xenServer620SP1Resource , times (1 )).getConnection ();
72+ try {
73+ verify (xenServer620SP1Resource , times (1 )).getGPUGroupDetails (conn );
74+ } catch (final XenAPIException e ) {
75+ fail (e .getMessage ());
76+ } catch (final XmlRpcException e ) {
77+ fail (e .getMessage ());
78+ }
79+
80+ assertTrue (answer .getResult ());
81+ }
82+
83+ @ Test
84+ public void testGetGPUStatsCommandFailure () {
85+ final String guuid = "246a5b75-05ed-4bbc-a171-2d1fe94a1b0e" ;
86+
87+ final Connection conn = Mockito .mock (Connection .class );
88+
89+ final GetGPUStatsCommand gpuStats = new GetGPUStatsCommand (guuid , "xen" );
90+
91+ final CitrixRequestWrapper wrapper = CitrixRequestWrapper .getInstance ();
92+ assertNotNull (wrapper );
93+
94+ when (xenServer620SP1Resource .getConnection ()).thenReturn (conn );
95+ try {
96+ when (xenServer620SP1Resource .getGPUGroupDetails (conn )).thenThrow (new CloudRuntimeException ("Failed!" ));
97+ } catch (final XenAPIException e ) {
98+ fail (e .getMessage ());
99+ } catch (final XmlRpcException e ) {
100+ fail (e .getMessage ());
101+ }
102+
103+ final Answer answer = wrapper .execute (gpuStats , xenServer620SP1Resource );
104+ verify (xenServer620SP1Resource , times (1 )).getConnection ();
105+ try {
106+ verify (xenServer620SP1Resource , times (1 )).getGPUGroupDetails (conn );
107+ } catch (final XenAPIException e ) {
108+ fail (e .getMessage ());
109+ } catch (final XmlRpcException e ) {
110+ fail (e .getMessage ());
111+ }
112+
113+ assertFalse (answer .getResult ());
114+ }
115+ }
0 commit comments