1
+ /*
2
+ Copyright IBM Corp. 2016 All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ package state
18
+
19
+ import (
20
+ "testing"
21
+ "github.com/docker/docker/pkg/testutil/assert"
22
+ )
23
+
24
+ func TestNewNodeMetastate (t * testing.T ) {
25
+ metastate := NewNodeMetastate (0 )
26
+ assert .Equal (t , metastate .Height (), uint64 (0 ))
27
+ }
28
+
29
+ func TestNodeMetastateImpl_Update (t * testing.T ) {
30
+ metastate := NewNodeMetastate (0 )
31
+ assert .Equal (t , metastate .Height (), uint64 (0 ))
32
+ metastate .Update (10 )
33
+ assert .Equal (t , metastate .Height (), uint64 (10 ))
34
+ }
35
+
36
+ // Test node metastate encoding
37
+ func TestNodeMetastateImpl_Bytes (t * testing.T ) {
38
+ metastate := NewNodeMetastate (0 )
39
+ // Encode state into bytes and check there is no errors
40
+ _ , err := metastate .Bytes ()
41
+ assert .NilError (t , err )
42
+ }
43
+
44
+ // Check the deserialization of the meta stats structure
45
+ func TestNodeMetastate_FromBytes (t * testing.T ) {
46
+ metastate := NewNodeMetastate (0 )
47
+ // Serialize into bytes array
48
+ bytes , err := metastate .Bytes ()
49
+ assert .NilError (t , err )
50
+ if bytes == nil {
51
+ t .Fatal ("Was not able to serialize meta state into byte array." )
52
+ }
53
+
54
+ // Deserialize back and check, that state still have same
55
+ // height value
56
+ state , err := FromBytes (bytes )
57
+ assert .NilError (t , err )
58
+
59
+ assert .Equal (t , state .Height (), uint64 (0 ))
60
+
61
+ // Update state to the new height and serialize it again
62
+ state .Update (17 )
63
+ bytes , err = state .Bytes ()
64
+ assert .NilError (t , err )
65
+ if bytes == nil {
66
+ t .Fatal ("Was not able to serialize meta state into byte array." )
67
+ }
68
+
69
+ // Restore state from byte array and validate
70
+ // that stored height is still the same
71
+ updatedState , err := FromBytes (bytes )
72
+ assert .NilError (t , err )
73
+ assert .Equal (t , updatedState .Height (), uint64 (17 ))
74
+ }
0 commit comments