1+ /** 
2+  * Licensed to the Apache Software Foundation (ASF) under one 
3+  * or more contributor license agreements.  See the NOTICE file 
4+  * distributed with this work for additional information 
5+  * regarding copyright ownership.  The ASF licenses this file 
6+  * to you under the Apache License, Version 2.0 (the 
7+  * "License"); you may not use this file except in compliance 
8+  * with the License.  You may obtain a copy of the License at 
9+  * 
10+  *     http://www.apache.org/licenses/LICENSE-2.0 
11+  * 
12+  * Unless required by applicable law or agreed to in writing, software 
13+  * distributed under the License is distributed on an "AS IS" BASIS, 
14+  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15+  * See the License for the specific language governing permissions and 
16+  * limitations under the License. 
17+  */ 
18+ package  org .apache .hadoop .yarn .server .nodemanager .containermanager .container ;
19+ 
20+ import  com .google .common .collect .ImmutableList ;
21+ import  org .apache .commons .io .IOUtils ;
22+ import  org .apache .hadoop .yarn .server .nodemanager .api .deviceplugin .Device ;
23+ import  org .junit .Assert ;
24+ import  org .junit .BeforeClass ;
25+ import  org .junit .Test ;
26+ 
27+ import  java .io .ByteArrayOutputStream ;
28+ import  java .io .IOException ;
29+ import  java .io .ObjectOutputStream ;
30+ import  java .io .Serializable ;
31+ import  java .util .List ;
32+ 
33+ public  class  TestResourceMappings  {
34+ 
35+   private  static  final  ResourceMappings .AssignedResources  testResources  =
36+       new  ResourceMappings .AssignedResources ();
37+ 
38+   @ BeforeClass 
39+   public  static  void  setup () {
40+     testResources .updateAssignedResources (ImmutableList .of (
41+         Device .Builder .newInstance ()
42+             .setId (0 )
43+             .setDevPath ("/dev/hdwA0" )
44+             .setMajorNumber (256 )
45+             .setMinorNumber (0 )
46+             .setBusID ("0000:80:00.0" )
47+             .setHealthy (true )
48+             .build (),
49+         Device .Builder .newInstance ()
50+             .setId (1 )
51+             .setDevPath ("/dev/hdwA1" )
52+             .setMajorNumber (256 )
53+             .setMinorNumber (0 )
54+             .setBusID ("0000:80:00.1" )
55+             .setHealthy (true )
56+             .build ()
57+     ));
58+   }
59+ 
60+   @ Test 
61+   public  void  testSerializeAssignedResourcesWithSerializationUtils () {
62+     try  {
63+       byte [] serializedString  = testResources .toBytes ();
64+ 
65+       ResourceMappings .AssignedResources  deserialized  =
66+           ResourceMappings .AssignedResources .fromBytes (serializedString );
67+ 
68+       Assert .assertEquals (testResources .getAssignedResources (),
69+           deserialized .getAssignedResources ());
70+ 
71+     } catch  (IOException  e ) {
72+       e .printStackTrace ();
73+       Assert .fail (String .format ("Serialization of test AssignedResources "  +
74+           "failed with %s" , e .getMessage ()));
75+     }
76+   }
77+ 
78+   @ Test 
79+   public  void  testAssignedResourcesCanDeserializePreviouslySerializedValues () {
80+     try  {
81+       byte [] serializedString  = toBytes (testResources .getAssignedResources ());
82+ 
83+       ResourceMappings .AssignedResources  deserialized  =
84+           ResourceMappings .AssignedResources .fromBytes (serializedString );
85+ 
86+       Assert .assertEquals (testResources .getAssignedResources (),
87+           deserialized .getAssignedResources ());
88+ 
89+     } catch  (IOException  e ) {
90+       e .printStackTrace ();
91+       Assert .fail (String .format ("Deserialization of test AssignedResources "  +
92+           "failed with %s" , e .getMessage ()));
93+     }
94+   }
95+ 
96+   /** 
97+    * This was the legacy way to serialize resources. This is here for 
98+    * backward compatibility to ensure that after YARN-9128 we can still 
99+    * deserialize previously serialized resources. 
100+    * 
101+    * @param resources the list of resources 
102+    * @return byte array representation of the resource 
103+    * @throws IOException 
104+    */ 
105+   private  byte [] toBytes (List <Serializable > resources ) throws  IOException  {
106+     ObjectOutputStream  oos  = null ;
107+     byte [] bytes ;
108+     try  {
109+       ByteArrayOutputStream  bos  = new  ByteArrayOutputStream ();
110+       oos  = new  ObjectOutputStream (bos );
111+       oos .writeObject (resources );
112+       bytes  = bos .toByteArray ();
113+     } finally  {
114+       IOUtils .closeQuietly (oos );
115+     }
116+     return  bytes ;
117+   }
118+ }
0 commit comments