@@ -7,6 +7,7 @@ module Puppet.Testing
77 , module Puppet.Lens
88 , H. hspec
99 , basicTest
10+ , usersGroupsDefined
1011 , testingDaemon
1112 , defaultDaemon
1213 , testCatalog
@@ -25,13 +26,14 @@ module Puppet.Testing
2526
2627import Prelude hiding (notElem ,all )
2728import Control.Lens
28- import Data.Foldable hiding (forM_ )
29+ import Data.Foldable hiding (forM_ , mapM_ )
2930import Data.Maybe
3031import Data.Monoid
3132import Control.Monad.Error
3233import Control.Monad.Reader
3334import Control.Applicative
3435import System.Posix.Files
36+ import qualified Data.HashSet as HS
3537import qualified Data.Either.Strict as S
3638import qualified Data.Text as T
3739import qualified System.Log.Logger as LOG
@@ -79,6 +81,37 @@ describeCatalog nd pdir catlg test = H.describe (T.unpack nd) $ runReaderT test
7981basicTest :: PSpec
8082basicTest = hTestFileSources
8183
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+
82115it :: HC. Example a => String -> PSpecM a -> PSpec
83116it n tst = tst >>= lift . H. it n
84117
@@ -118,7 +151,7 @@ withFileContent :: String -- ^ Test description (the thing that goes after shoul
118151withFileContent desc fn action = withResource desc " file" fn $ \ r ->
119152 case r ^? rattributes . ix " content" . _PString of
120153 Just v -> action v
121- Nothing -> H. expectationFailure " Contentnot found"
154+ Nothing -> H. expectationFailure " Content not found"
122155
123156hTestFileSources :: PSpec
124157hTestFileSources = do
0 commit comments