@@ -7,6 +7,7 @@ module Puppet.Testing
7
7
, module Puppet.Lens
8
8
, H. hspec
9
9
, basicTest
10
+ , usersGroupsDefined
10
11
, testingDaemon
11
12
, defaultDaemon
12
13
, testCatalog
@@ -25,13 +26,14 @@ module Puppet.Testing
25
26
26
27
import Prelude hiding (notElem ,all )
27
28
import Control.Lens
28
- import Data.Foldable hiding (forM_ )
29
+ import Data.Foldable hiding (forM_ , mapM_ )
29
30
import Data.Maybe
30
31
import Data.Monoid
31
32
import Control.Monad.Error
32
33
import Control.Monad.Reader
33
34
import Control.Applicative
34
35
import System.Posix.Files
36
+ import qualified Data.HashSet as HS
35
37
import qualified Data.Either.Strict as S
36
38
import qualified Data.Text as T
37
39
import qualified System.Log.Logger as LOG
@@ -79,6 +81,37 @@ describeCatalog nd pdir catlg test = H.describe (T.unpack nd) $ runReaderT test
79
81
basicTest :: PSpec
80
82
basicTest = hTestFileSources
81
83
84
+ -- | This tests that all users and groups used as resource parameters are
85
+ -- defined
86
+ usersGroupsDefined :: PSpec
87
+ usersGroupsDefined = do
88
+ c <- view lCatalog
89
+ let getResourceType t = c ^.. traverse . filtered (\ r -> r ^. rid . itype == t && r ^. rattributes . at " ensure" /= Just " absent" )
90
+ users = getResourceType " user"
91
+ groups = getResourceType " group"
92
+ knownUsers = HS. fromList $ map (view (rid . iname)) users ++ [" root" ," " ," syslog" ," mysql" ]
93
+ knownGroups = HS. fromList $ map (view (rid . iname)) groups ++ [" root" , " adm" , " syslog" , " mysql" , " nagios" ," " ]
94
+ checkResource lensU lensG = mapM_ (checkResource' lensU lensG)
95
+ checkResource' lensU lensG res = do
96
+ let d = " Resource " <> show (pretty res) <> " should have a valid "
97
+ case lensU of
98
+ Just lensU' -> do
99
+ let u = res ^. rattributes . lensU' . _PString
100
+ H. it (d <> " username (" ++ T. unpack u ++ " )" ) (HS. member u knownUsers)
101
+ Nothing -> return ()
102
+ case lensG of
103
+ Just lensG' -> do
104
+ let g = res ^. rattributes . lensG' . _PString
105
+ H. it (d <> " group (" ++ T. unpack g ++ " )" ) (HS. member g knownGroups)
106
+ Nothing -> return ()
107
+ lift $ do
108
+ checkResource (Just $ ix " owner" ) (Just $ ix " group" ) (getResourceType " file" )
109
+ checkResource (Just $ ix " user" ) (Just $ ix " group" ) (getResourceType " exec" )
110
+ checkResource (Just $ ix " user" ) Nothing (getResourceType " cron" )
111
+ checkResource (Just $ ix " user" ) Nothing (getResourceType " ssh_authorized_key" )
112
+ checkResource (Just $ ix " user" ) Nothing (getResourceType " ssh_authorized_key_secure" )
113
+ checkResource (Nothing ) (Just $ ix " gid" ) users
114
+
82
115
it :: HC. Example a => String -> PSpecM a -> PSpec
83
116
it n tst = tst >>= lift . H. it n
84
117
@@ -118,7 +151,7 @@ withFileContent :: String -- ^ Test description (the thing that goes after shoul
118
151
withFileContent desc fn action = withResource desc " file" fn $ \ r ->
119
152
case r ^? rattributes . ix " content" . _PString of
120
153
Just v -> action v
121
- Nothing -> H. expectationFailure " Contentnot found"
154
+ Nothing -> H. expectationFailure " Content not found"
122
155
123
156
hTestFileSources :: PSpec
124
157
hTestFileSources = do
0 commit comments