2013-05-23 23:00:46 +00:00
|
|
|
{- git-annex fuzz generator
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-05-23 23:00:46 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-05-23 23:00:46 +00:00
|
|
|
-}
|
|
|
|
|
2019-12-05 18:36:43 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2013-05-23 23:00:46 +00:00
|
|
|
module Command.FuzzTest where
|
|
|
|
|
|
|
|
import Command
|
2016-01-20 20:36:33 +00:00
|
|
|
import qualified Annex
|
2013-05-23 23:00:46 +00:00
|
|
|
import qualified Git.Config
|
|
|
|
import Config
|
2020-03-06 15:57:15 +00:00
|
|
|
import Annex.Perms
|
2013-05-23 23:00:46 +00:00
|
|
|
import Utility.ThreadScheduler
|
2013-05-26 20:04:52 +00:00
|
|
|
import Utility.DiskFree
|
2019-12-05 18:36:43 +00:00
|
|
|
import Git.Types (fromConfigKey)
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
import Data.Time.Clock
|
|
|
|
import System.Random (getStdRandom, random, randomR)
|
|
|
|
import Test.QuickCheck
|
|
|
|
import Control.Concurrent
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = notBareRepo $
|
|
|
|
command "fuzztest" SectionTesting
|
|
|
|
"generates fuzz test files"
|
|
|
|
paramNothing (withParams seek)
|
2013-05-23 23:00:46 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2018-10-01 18:12:06 +00:00
|
|
|
seek = withNothing (commandAction start)
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
start :: CommandStart
|
|
|
|
start = do
|
|
|
|
guardTest
|
|
|
|
logf <- fromRepo gitAnnexFuzzTestLogFile
|
2020-09-15 20:22:44 +00:00
|
|
|
showStart "fuzztest" (toRawFilePath logf) (SeekInput [])
|
2018-01-02 21:17:10 +00:00
|
|
|
logh <- liftIO $ openFile logf WriteMode
|
2013-05-23 23:00:46 +00:00
|
|
|
void $ forever $ fuzz logh
|
|
|
|
stop
|
|
|
|
|
|
|
|
guardTest :: Annex ()
|
2019-12-20 19:01:34 +00:00
|
|
|
guardTest = unlessM (fromMaybe False . Git.Config.isTrueFalse' <$> getConfig key mempty) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup $ unlines
|
2013-05-23 23:00:46 +00:00
|
|
|
[ "Running fuzz tests *writes* to and *deletes* files in"
|
|
|
|
, "this repository, and pushes those changes to other"
|
|
|
|
, "repositories! This is a developer tool, not something"
|
|
|
|
, "to play with."
|
|
|
|
, ""
|
2019-12-05 18:36:43 +00:00
|
|
|
, "Refusing to run fuzz tests, since " ++ fromConfigKey key ++ " is not set!"
|
2013-05-23 23:00:46 +00:00
|
|
|
]
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
key = annexConfig "eat-my-repository"
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
fuzz :: Handle -> Annex ()
|
|
|
|
fuzz logh = do
|
2015-07-09 20:05:45 +00:00
|
|
|
fuzzer <- genFuzzAction
|
|
|
|
record logh $ flip Started fuzzer
|
|
|
|
result <- tryNonAsync $ runFuzzAction fuzzer
|
2013-05-25 19:52:28 +00:00
|
|
|
record logh $ flip Finished $
|
|
|
|
either (const False) (const True) result
|
|
|
|
|
|
|
|
record :: Handle -> (UTCTime -> TimeStampedFuzzAction) -> Annex ()
|
|
|
|
record h tmpl = liftIO $ do
|
|
|
|
now <- getCurrentTime
|
|
|
|
let s = show $ tmpl now
|
|
|
|
print s
|
|
|
|
hPrint h s
|
|
|
|
hFlush h
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
{- Delay for either a fraction of a second, or a few seconds, or up
|
2013-05-26 20:04:52 +00:00
|
|
|
- to 1 minute.
|
|
|
|
-
|
|
|
|
- The MinutesDelay is used as an opportunity to do housekeeping tasks.
|
|
|
|
-}
|
|
|
|
randomDelay :: Delay -> Annex ()
|
|
|
|
randomDelay TinyDelay = liftIO $
|
|
|
|
threadDelay =<< getStdRandom (randomR (10000, 1000000))
|
|
|
|
randomDelay SecondsDelay = liftIO $
|
|
|
|
threadDelaySeconds =<< Seconds <$> getStdRandom (randomR (1, 10))
|
|
|
|
randomDelay MinutesDelay = do
|
|
|
|
liftIO $ threadDelaySeconds =<< Seconds <$> getStdRandom (randomR (1, 60))
|
|
|
|
reserve <- annexDiskReserve <$> Annex.getGitConfig
|
|
|
|
free <- liftIO $ getDiskFree "."
|
|
|
|
case free of
|
|
|
|
Just have | have < reserve -> do
|
|
|
|
warning "Low disk space; fuzz test paused."
|
|
|
|
liftIO $ threadDelaySeconds (Seconds 60)
|
|
|
|
randomDelay MinutesDelay
|
|
|
|
_ -> noop
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
data Delay
|
|
|
|
= TinyDelay
|
|
|
|
| SecondsDelay
|
|
|
|
| MinutesDelay
|
|
|
|
deriving (Read, Show, Eq)
|
|
|
|
|
|
|
|
instance Arbitrary Delay where
|
|
|
|
arbitrary = elements [TinyDelay, SecondsDelay, MinutesDelay]
|
|
|
|
|
|
|
|
data FuzzFile = FuzzFile FilePath
|
|
|
|
deriving (Read, Show, Eq)
|
|
|
|
|
|
|
|
data FuzzDir = FuzzDir FilePath
|
|
|
|
deriving (Read, Show, Eq)
|
|
|
|
|
|
|
|
instance Arbitrary FuzzFile where
|
|
|
|
arbitrary = FuzzFile <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary FuzzDir where
|
|
|
|
arbitrary = FuzzDir <$> arbitrary
|
|
|
|
|
|
|
|
class ToFilePath a where
|
|
|
|
toFilePath :: a -> FilePath
|
|
|
|
|
|
|
|
instance ToFilePath FuzzFile where
|
|
|
|
toFilePath (FuzzFile f) = f
|
|
|
|
|
|
|
|
instance ToFilePath FuzzDir where
|
|
|
|
toFilePath (FuzzDir d) = d
|
|
|
|
|
|
|
|
isFuzzFile :: FilePath -> Bool
|
|
|
|
isFuzzFile f = "fuzzfile_" `isPrefixOf` takeFileName f
|
|
|
|
|
|
|
|
isFuzzDir :: FilePath -> Bool
|
|
|
|
isFuzzDir d = "fuzzdir_" `isPrefixOf` d
|
|
|
|
|
|
|
|
mkFuzzFile :: FilePath -> [FuzzDir] -> FuzzFile
|
|
|
|
mkFuzzFile file dirs = FuzzFile $ joinPath (map toFilePath dirs) </> ("fuzzfile_" ++ file)
|
|
|
|
|
|
|
|
mkFuzzDir :: Int -> FuzzDir
|
|
|
|
mkFuzzDir n = FuzzDir $ "fuzzdir_" ++ show n
|
|
|
|
|
2013-05-26 20:15:25 +00:00
|
|
|
{- File is placed inside a directory hierarchy up to 4 subdirectories deep. -}
|
2013-05-23 23:00:46 +00:00
|
|
|
genFuzzFile :: IO FuzzFile
|
|
|
|
genFuzzFile = do
|
2013-05-26 20:15:25 +00:00
|
|
|
n <- getStdRandom $ randomR (0, 4)
|
2013-05-23 23:00:46 +00:00
|
|
|
dirs <- replicateM n genFuzzDir
|
|
|
|
file <- show <$> (getStdRandom random :: IO Int)
|
|
|
|
return $ mkFuzzFile file dirs
|
|
|
|
|
2013-05-26 20:15:25 +00:00
|
|
|
{- Only 16 distinct subdirectories are used. When nested 4 deep, this
|
|
|
|
- yields 69904 total directories max, which is below the default Linux
|
|
|
|
- inotify limit of 81920. The goal is not to run the assistant out of
|
|
|
|
- inotify descriptors. -}
|
2013-05-23 23:00:46 +00:00
|
|
|
genFuzzDir :: IO FuzzDir
|
2013-05-26 20:15:25 +00:00
|
|
|
genFuzzDir = mkFuzzDir <$> (getStdRandom (randomR (1,16)) :: IO Int)
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
data TimeStampedFuzzAction
|
|
|
|
= Started UTCTime FuzzAction
|
|
|
|
| Finished UTCTime Bool
|
|
|
|
deriving (Read, Show)
|
|
|
|
|
|
|
|
data FuzzAction
|
|
|
|
= FuzzAdd FuzzFile
|
|
|
|
| FuzzDelete FuzzFile
|
|
|
|
| FuzzMove FuzzFile FuzzFile
|
|
|
|
| FuzzDeleteDir FuzzDir
|
|
|
|
| FuzzMoveDir FuzzDir FuzzDir
|
|
|
|
| FuzzPause Delay
|
|
|
|
deriving (Read, Show, Eq)
|
|
|
|
|
|
|
|
instance Arbitrary FuzzAction where
|
|
|
|
arbitrary = frequency
|
2013-05-25 21:18:37 +00:00
|
|
|
[ (50, FuzzAdd <$> arbitrary)
|
|
|
|
, (50, FuzzDelete <$> arbitrary)
|
2013-05-23 23:00:46 +00:00
|
|
|
, (10, FuzzMove <$> arbitrary <*> arbitrary)
|
|
|
|
, (10, FuzzDeleteDir <$> arbitrary)
|
|
|
|
, (10, FuzzMoveDir <$> arbitrary <*> arbitrary)
|
|
|
|
, (10, FuzzPause <$> arbitrary)
|
|
|
|
]
|
|
|
|
|
|
|
|
runFuzzAction :: FuzzAction -> Annex ()
|
2020-03-06 15:57:15 +00:00
|
|
|
runFuzzAction (FuzzAdd (FuzzFile f)) = do
|
|
|
|
createWorkTreeDirectory (parentDir f)
|
|
|
|
n <- liftIO (getStdRandom random :: IO Int)
|
|
|
|
liftIO $ writeFile f $ show n ++ "\n"
|
2013-05-23 23:00:46 +00:00
|
|
|
runFuzzAction (FuzzDelete (FuzzFile f)) = liftIO $ nukeFile f
|
|
|
|
runFuzzAction (FuzzMove (FuzzFile src) (FuzzFile dest)) = liftIO $
|
|
|
|
rename src dest
|
|
|
|
runFuzzAction (FuzzDeleteDir (FuzzDir d)) = liftIO $
|
|
|
|
removeDirectoryRecursive d
|
|
|
|
runFuzzAction (FuzzMoveDir (FuzzDir src) (FuzzDir dest)) = liftIO $
|
|
|
|
rename src dest
|
2013-05-26 20:04:52 +00:00
|
|
|
runFuzzAction (FuzzPause d) = randomDelay d
|
2013-05-23 23:00:46 +00:00
|
|
|
|
|
|
|
genFuzzAction :: Annex FuzzAction
|
2013-05-25 21:52:33 +00:00
|
|
|
genFuzzAction = do
|
|
|
|
tmpl <- liftIO $ Prelude.head <$> sample' (arbitrary :: Gen FuzzAction)
|
2013-05-23 23:00:46 +00:00
|
|
|
-- Fix up template action to make sense in the current repo tree.
|
|
|
|
case tmpl of
|
2013-05-25 21:52:33 +00:00
|
|
|
FuzzAdd _ -> do
|
|
|
|
f <- liftIO newFile
|
|
|
|
maybe genFuzzAction (return . FuzzAdd) f
|
|
|
|
FuzzDelete _ -> do
|
|
|
|
f <- liftIO $ existingFile 0 ""
|
|
|
|
maybe genFuzzAction (return . FuzzDelete) f
|
|
|
|
FuzzMove _ _ -> do
|
|
|
|
src <- liftIO $ existingFile 0 ""
|
|
|
|
dest <- liftIO newFile
|
|
|
|
case (src, dest) of
|
|
|
|
(Just s, Just d) -> return $ FuzzMove s d
|
|
|
|
_ -> genFuzzAction
|
2013-05-23 23:00:46 +00:00
|
|
|
FuzzMoveDir _ _ -> do
|
2013-05-25 21:52:33 +00:00
|
|
|
md <- liftIO existingDir
|
|
|
|
case md of
|
|
|
|
Nothing -> genFuzzAction
|
|
|
|
Just d -> do
|
2015-01-09 17:11:56 +00:00
|
|
|
newd <- liftIO $ newDir (parentDir $ toFilePath d)
|
2013-05-25 21:52:33 +00:00
|
|
|
maybe genFuzzAction (return . FuzzMoveDir d) newd
|
|
|
|
FuzzDeleteDir _ -> do
|
|
|
|
d <- liftIO existingDir
|
|
|
|
maybe genFuzzAction (return . FuzzDeleteDir) d
|
2013-05-23 23:00:46 +00:00
|
|
|
FuzzPause _ -> return tmpl
|
|
|
|
|
2013-05-25 21:52:33 +00:00
|
|
|
existingFile :: Int -> FilePath -> IO (Maybe FuzzFile)
|
|
|
|
existingFile 0 _ = return Nothing
|
2013-05-25 22:15:34 +00:00
|
|
|
existingFile n top = do
|
|
|
|
dir <- existingDirIncludingTop
|
|
|
|
contents <- catchDefaultIO [] (getDirectoryContents dir)
|
|
|
|
let files = filter isFuzzFile contents
|
|
|
|
if null files
|
|
|
|
then do
|
|
|
|
let dirs = filter isFuzzDir contents
|
|
|
|
if null dirs
|
|
|
|
then return Nothing
|
|
|
|
else do
|
|
|
|
i <- getStdRandom $ randomR (0, length dirs - 1)
|
|
|
|
existingFile (n - 1) (top </> dirs !! i)
|
|
|
|
else do
|
|
|
|
i <- getStdRandom $ randomR (0, length files - 1)
|
|
|
|
return $ Just $ FuzzFile $ top </> dir </> files !! i
|
|
|
|
|
|
|
|
existingDirIncludingTop :: IO FilePath
|
|
|
|
existingDirIncludingTop = do
|
2013-05-23 23:00:46 +00:00
|
|
|
dirs <- filter isFuzzDir <$> getDirectoryContents "."
|
|
|
|
if null dirs
|
2013-05-25 22:15:34 +00:00
|
|
|
then return "."
|
2013-05-23 23:00:46 +00:00
|
|
|
else do
|
|
|
|
n <- getStdRandom $ randomR (0, length dirs)
|
2013-05-25 22:15:34 +00:00
|
|
|
return $ ("." : dirs) !! n
|
|
|
|
|
|
|
|
existingDir :: IO (Maybe FuzzDir)
|
|
|
|
existingDir = do
|
|
|
|
d <- existingDirIncludingTop
|
|
|
|
return $ if isFuzzDir d
|
|
|
|
then Just $ FuzzDir d
|
|
|
|
else Nothing
|
2013-05-23 23:00:46 +00:00
|
|
|
|
2013-05-25 21:52:33 +00:00
|
|
|
newFile :: IO (Maybe FuzzFile)
|
2013-05-23 23:00:46 +00:00
|
|
|
newFile = go (100 :: Int)
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go 0 = return Nothing
|
2013-05-23 23:00:46 +00:00
|
|
|
go n = do
|
|
|
|
f <- genFuzzFile
|
|
|
|
ifM (doesnotexist (toFilePath f))
|
2013-05-25 21:52:33 +00:00
|
|
|
( return $ Just f
|
2013-05-23 23:00:46 +00:00
|
|
|
, go (n - 1)
|
|
|
|
)
|
|
|
|
|
2013-05-25 21:52:33 +00:00
|
|
|
newDir :: FilePath -> IO (Maybe FuzzDir)
|
2013-05-23 23:00:46 +00:00
|
|
|
newDir parent = go (100 :: Int)
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go 0 = return Nothing
|
2013-05-23 23:00:46 +00:00
|
|
|
go n = do
|
|
|
|
(FuzzDir d) <- genFuzzDir
|
|
|
|
ifM (doesnotexist (parent </> d))
|
2013-05-25 21:52:33 +00:00
|
|
|
( return $ Just $ FuzzDir d
|
2013-05-23 23:00:46 +00:00
|
|
|
, go (n - 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
doesnotexist :: FilePath -> IO Bool
|
|
|
|
doesnotexist f = isNothing <$> catchMaybeIO (getSymbolicLinkStatus f)
|