Debian Codegraph @

test/hs/Test/Ganeti/Locking/Locks.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Tests for the lock data structure
5
6 -}
7
8 {-
9
10 Copyright (C) 2014 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Locking.Locks (testLocking_Locks) where
39
40 import Control.Applicative (liftA2)
41 import Control.Monad (liftM)
42 import System.Posix.Types (CPid)
43
44 import Test.QuickCheck
45 import Text.JSON
46
47 import Test.Ganeti.TestHelper
48 import Test.Ganeti.TestCommon
49 import Test.Ganeti.Types ()
50
51 import Ganeti.Locking.Locks
52 import Ganeti.Locking.Types
53
54 instance Arbitrary GanetiLocks where
55 arbitrary = oneof [ return BGL
56 , return ClusterLockSet
57 , return InstanceLockSet
58 , Instance <$> genFQDN
59 , return NodeGroupLockSet
60 , NodeGroup <$> genUUID
61 , return NodeResLockSet
62 , NodeRes <$> genUUID
63 , return NodeLockSet
64 , Node <$> genUUID
65 , return NetworkLockSet
66 , Network <$> genUUID
67 ]
68
69 -- | Verify that readJSON . showJSON = Ok
70 prop_ReadShow :: Property
71 prop_ReadShow = forAll (arbitrary :: Gen GanetiLocks) $ \a ->
72 readJSON (showJSON a) ==? Ok a
73
74 -- | Verify the implied locks are earlier in the lock order.
75 prop_ImpliedOrder :: Property
76 prop_ImpliedOrder =
77 forAll ((arbitrary :: Gen GanetiLocks)
78 `suchThat` (not . null . lockImplications)) $ \b ->
79 counterexample "Implied locks must be earlier in the lock order"
80 . flip all (lockImplications b) $ \a ->
81 a < b
82
83 -- | Verify the intervall property of the locks.
84 prop_ImpliedIntervall :: Property
85 prop_ImpliedIntervall =
86 forAll ((arbitrary :: Gen GanetiLocks)
87 `suchThat` (not . null . lockImplications)) $ \b ->
88 forAll (elements $ lockImplications b) $ \a ->
89 forAll (arbitrary `suchThat` liftA2 (&&) (a <) (<= b)) $ \x ->
90 counterexample ("Locks between a group and a member of the group"
91 ++ " must also belong to the group")
92 $ a `elem` lockImplications x
93
94 instance Arbitrary LockLevel where
95 arbitrary = elements [LevelCluster ..]
96
97 -- | Verify that readJSON . showJSON = Ok for lock levels
98 prop_ReadShowLevel :: Property
99 prop_ReadShowLevel = forAll (arbitrary :: Gen LockLevel) $ \a ->
100 readJSON (showJSON a) ==? Ok a
101
102 instance Arbitrary ClientType where
103 arbitrary = oneof [ ClientOther <$> arbitrary
104 , ClientJob <$> arbitrary
105 ]
106
107 -- | Verify that readJSON . showJSON = Ok for ClientType
108 prop_ReadShow_ClientType :: Property
109 prop_ReadShow_ClientType = forAll (arbitrary :: Gen ClientType) $ \a ->
110 readJSON (showJSON a) ==? Ok a
111
112 instance Arbitrary CPid where
113 arbitrary = liftM fromIntegral (arbitrary :: Gen Integer)
114
115 instance Arbitrary ClientId where
116 arbitrary = ClientId <$> arbitrary <*> arbitrary <*> arbitrary
117
118 -- | Verify that readJSON . showJSON = Ok for ClientId
119 prop_ReadShow_ClientId :: Property
120 prop_ReadShow_ClientId = forAll (arbitrary :: Gen ClientId) $ \a ->
121 readJSON (showJSON a) ==? Ok a
122
123 testSuite "Locking/Locks"
124 [ 'prop_ReadShow
125 , 'prop_ImpliedOrder
126 , 'prop_ImpliedIntervall
127 , 'prop_ReadShowLevel
128 , 'prop_ReadShow_ClientType
129 , 'prop_ReadShow_ClientId
130 ]