Skip to content

Commit 1cc616b

Browse files
committed
Add new testing stuff : existing users / groups
1 parent 1b1449f commit 1cc616b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

Diff for: Puppet/Testing.hs

+35-2
Original file line numberDiff line numberDiff line change
@@ -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

2627
import Prelude hiding (notElem,all)
2728
import Control.Lens
28-
import Data.Foldable hiding (forM_)
29+
import Data.Foldable hiding (forM_,mapM_)
2930
import Data.Maybe
3031
import Data.Monoid
3132
import Control.Monad.Error
3233
import Control.Monad.Reader
3334
import Control.Applicative
3435
import System.Posix.Files
36+
import qualified Data.HashSet as HS
3537
import qualified Data.Either.Strict as S
3638
import qualified Data.Text as T
3739
import qualified System.Log.Logger as LOG
@@ -79,6 +81,37 @@ describeCatalog nd pdir catlg test = H.describe (T.unpack nd) $ runReaderT test
7981
basicTest :: PSpec
8082
basicTest = 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+
82115
it :: HC.Example a => String -> PSpecM a -> PSpec
83116
it n tst = tst >>= lift . H.it n
84117

@@ -118,7 +151,7 @@ withFileContent :: String -- ^ Test description (the thing that goes after shoul
118151
withFileContent 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

123156
hTestFileSources :: PSpec
124157
hTestFileSources = do

Diff for: progs/PuppetResources.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ computeCatalogs testOnly queryfunc pdbapi printFunc (CommandLine _ showjson show
448448
_ -> do
449449
catalog <- filterCatalog rawcatalog
450450
exported <- filterCatalog rawexported
451-
r <- testCatalog tnodename puppetdir rawcatalog basicTest
451+
r <- testCatalog tnodename puppetdir rawcatalog (basicTest >> usersGroupsDefined)
452452
printFunc (pretty (HM.elems catalog))
453453
unless (HM.null exported) $ do
454454
printFunc (mempty <+> dullyellow "Exported:" <+> mempty)

0 commit comments

Comments
 (0)