test_file.py 182 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the file state
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import errno
  8. import logging
  9. import os
  10. import re
  11. import sys
  12. import shutil
  13. import stat
  14. import tempfile
  15. import textwrap
  16. import filecmp
  17. log = logging.getLogger(__name__)
  18. # Import Salt Testing libs
  19. from tests.support.case import ModuleCase
  20. from tests.support.unit import skipIf
  21. from tests.support.paths import BASE_FILES, FILES, TMP, TMP_STATE_TREE
  22. from tests.support.helpers import (
  23. destructiveTest,
  24. skip_if_not_root,
  25. with_system_user_and_group,
  26. with_tempdir,
  27. with_tempfile,
  28. Webserver,
  29. destructiveTest,
  30. dedent,
  31. )
  32. from tests.support.mixins import SaltReturnAssertsMixin
  33. # Import Salt libs
  34. import salt.utils.data
  35. import salt.utils.files
  36. import salt.utils.json
  37. import salt.utils.path
  38. import salt.utils.platform
  39. import salt.utils.stringutils
  40. import salt.serializers.configparser
  41. from salt.utils.versions import LooseVersion as _LooseVersion
  42. HAS_PWD = True
  43. try:
  44. import pwd
  45. except ImportError:
  46. HAS_PWD = False
  47. HAS_GRP = True
  48. try:
  49. import grp
  50. except ImportError:
  51. HAS_GRP = False
  52. # Import 3rd-party libs
  53. from salt.ext import six
  54. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  55. IS_WINDOWS = salt.utils.platform.is_windows()
  56. BINARY_FILE = b'GIF89a\x01\x00\x01\x00\x80\x00\x00\x05\x04\x04\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'
  57. if IS_WINDOWS:
  58. FILEPILLAR = 'C:\\Windows\\Temp\\filepillar-python'
  59. FILEPILLARDEF = 'C:\\Windows\\Temp\\filepillar-defaultvalue'
  60. FILEPILLARGIT = 'C:\\Windows\\Temp\\filepillar-bar'
  61. else:
  62. FILEPILLAR = '/tmp/filepillar-python'
  63. FILEPILLARDEF = '/tmp/filepillar-defaultvalue'
  64. FILEPILLARGIT = '/tmp/filepillar-bar'
  65. def _test_managed_file_mode_keep_helper(testcase, local=False):
  66. '''
  67. DRY helper function to run the same test with a local or remote path
  68. '''
  69. name = os.path.join(TMP, 'scene33')
  70. grail_fs_path = os.path.join(BASE_FILES, 'grail', 'scene33')
  71. grail = 'salt://grail/scene33' if not local else grail_fs_path
  72. # Get the current mode so that we can put the file back the way we
  73. # found it when we're done.
  74. grail_fs_mode = int(testcase.run_function('file.get_mode', [grail_fs_path]), 8)
  75. initial_mode = 0o770
  76. new_mode_1 = 0o600
  77. new_mode_2 = 0o644
  78. # Set the initial mode, so we can be assured that when we set the mode
  79. # to "keep", we're actually changing the permissions of the file to the
  80. # new mode.
  81. ret = testcase.run_state(
  82. 'file.managed',
  83. name=name,
  84. mode=oct(initial_mode),
  85. source=grail,
  86. )
  87. if IS_WINDOWS:
  88. testcase.assertSaltFalseReturn(ret)
  89. return
  90. testcase.assertSaltTrueReturn(ret)
  91. try:
  92. # Update the mode on the fileserver (pass 1)
  93. os.chmod(grail_fs_path, new_mode_1)
  94. ret = testcase.run_state(
  95. 'file.managed',
  96. name=name,
  97. mode='keep',
  98. source=grail,
  99. )
  100. testcase.assertSaltTrueReturn(ret)
  101. managed_mode = stat.S_IMODE(os.stat(name).st_mode)
  102. testcase.assertEqual(oct(managed_mode), oct(new_mode_1))
  103. # Update the mode on the fileserver (pass 2)
  104. # This assures us that if the file in file_roots was originally set
  105. # to the same mode as new_mode_1, we definitely get an updated mode
  106. # this time.
  107. os.chmod(grail_fs_path, new_mode_2)
  108. ret = testcase.run_state(
  109. 'file.managed',
  110. name=name,
  111. mode='keep',
  112. source=grail,
  113. )
  114. testcase.assertSaltTrueReturn(ret)
  115. managed_mode = stat.S_IMODE(os.stat(name).st_mode)
  116. testcase.assertEqual(oct(managed_mode), oct(new_mode_2))
  117. except Exception:
  118. raise
  119. finally:
  120. # Set the mode of the file in the file_roots back to what it
  121. # originally was.
  122. os.chmod(grail_fs_path, grail_fs_mode)
  123. class FileTest(ModuleCase, SaltReturnAssertsMixin):
  124. '''
  125. Validate the file state
  126. '''
  127. def tearDown(self):
  128. '''
  129. remove files created in previous tests
  130. '''
  131. user = 'salt'
  132. if user in str(self.run_function('user.list_users')):
  133. self.run_function('user.delete', [user])
  134. for path in (FILEPILLAR, FILEPILLARDEF, FILEPILLARGIT):
  135. try:
  136. os.remove(path)
  137. except OSError as exc:
  138. if exc.errno != errno.ENOENT:
  139. log.error('Failed to remove %s: %s', path, exc)
  140. def test_symlink(self):
  141. '''
  142. file.symlink
  143. '''
  144. name = os.path.join(TMP, 'symlink')
  145. tgt = os.path.join(TMP, 'target')
  146. # Windows must have a source directory to link to
  147. if IS_WINDOWS and not os.path.isdir(tgt):
  148. os.mkdir(tgt)
  149. # Windows cannot create a symlink if it already exists
  150. if IS_WINDOWS and self.run_function('file.is_link', [name]):
  151. self.run_function('file.remove', [name])
  152. ret = self.run_state('file.symlink', name=name, target=tgt)
  153. self.assertSaltTrueReturn(ret)
  154. def test_test_symlink(self):
  155. '''
  156. file.symlink test interface
  157. '''
  158. name = os.path.join(TMP, 'symlink2')
  159. tgt = os.path.join(TMP, 'target')
  160. ret = self.run_state('file.symlink', test=True, name=name, target=tgt)
  161. self.assertSaltNoneReturn(ret)
  162. def test_absent_file(self):
  163. '''
  164. file.absent
  165. '''
  166. name = os.path.join(TMP, 'file_to_kill')
  167. with salt.utils.files.fopen(name, 'w+') as fp_:
  168. fp_.write('killme')
  169. ret = self.run_state('file.absent', name=name)
  170. self.assertSaltTrueReturn(ret)
  171. self.assertFalse(os.path.isfile(name))
  172. def test_absent_dir(self):
  173. '''
  174. file.absent
  175. '''
  176. name = os.path.join(TMP, 'dir_to_kill')
  177. if not os.path.isdir(name):
  178. # left behind... Don't fail because of this!
  179. os.makedirs(name)
  180. ret = self.run_state('file.absent', name=name)
  181. self.assertSaltTrueReturn(ret)
  182. self.assertFalse(os.path.isdir(name))
  183. def test_absent_link(self):
  184. '''
  185. file.absent
  186. '''
  187. name = os.path.join(TMP, 'link_to_kill')
  188. tgt = '{0}.tgt'.format(name)
  189. # Windows must have a source directory to link to
  190. if IS_WINDOWS and not os.path.isdir(tgt):
  191. os.mkdir(tgt)
  192. if not self.run_function('file.is_link', [name]):
  193. self.run_function('file.symlink', [tgt, name])
  194. ret = self.run_state('file.absent', name=name)
  195. try:
  196. self.assertSaltTrueReturn(ret)
  197. self.assertFalse(self.run_function('file.is_link', [name]))
  198. finally:
  199. if self.run_function('file.is_link', [name]):
  200. self.run_function('file.remove', [name])
  201. @with_tempfile()
  202. def test_test_absent(self, name):
  203. '''
  204. file.absent test interface
  205. '''
  206. with salt.utils.files.fopen(name, 'w+') as fp_:
  207. fp_.write('killme')
  208. ret = self.run_state('file.absent', test=True, name=name)
  209. self.assertSaltNoneReturn(ret)
  210. self.assertTrue(os.path.isfile(name))
  211. def test_managed(self):
  212. '''
  213. file.managed
  214. '''
  215. name = os.path.join(TMP, 'grail_scene33')
  216. ret = self.run_state(
  217. 'file.managed', name=name, source='salt://grail/scene33'
  218. )
  219. src = os.path.join(BASE_FILES, 'grail', 'scene33')
  220. with salt.utils.files.fopen(src, 'r') as fp_:
  221. master_data = fp_.read()
  222. with salt.utils.files.fopen(name, 'r') as fp_:
  223. minion_data = fp_.read()
  224. self.assertEqual(master_data, minion_data)
  225. self.assertSaltTrueReturn(ret)
  226. def test_managed_file_mode(self):
  227. '''
  228. file.managed, correct file permissions
  229. '''
  230. desired_mode = 504 # 0770 octal
  231. name = os.path.join(TMP, 'grail_scene33')
  232. ret = self.run_state(
  233. 'file.managed', name=name, mode='0770', source='salt://grail/scene33'
  234. )
  235. if IS_WINDOWS:
  236. expected = 'The \'mode\' option is not supported on Windows'
  237. self.assertEqual(ret[list(ret)[0]]['comment'], expected)
  238. self.assertSaltFalseReturn(ret)
  239. return
  240. resulting_mode = stat.S_IMODE(
  241. os.stat(name).st_mode
  242. )
  243. self.assertEqual(oct(desired_mode), oct(resulting_mode))
  244. self.assertSaltTrueReturn(ret)
  245. def test_managed_file_mode_keep(self):
  246. '''
  247. Test using "mode: keep" in a file.managed state
  248. '''
  249. _test_managed_file_mode_keep_helper(self, local=False)
  250. def test_managed_file_mode_keep_local_source(self):
  251. '''
  252. Test using "mode: keep" in a file.managed state, with a local file path
  253. as the source.
  254. '''
  255. _test_managed_file_mode_keep_helper(self, local=True)
  256. def test_managed_file_mode_file_exists_replace(self):
  257. '''
  258. file.managed, existing file with replace=True, change permissions
  259. '''
  260. initial_mode = 504 # 0770 octal
  261. desired_mode = 384 # 0600 octal
  262. name = os.path.join(TMP, 'grail_scene33')
  263. ret = self.run_state(
  264. 'file.managed', name=name, mode=oct(initial_mode), source='salt://grail/scene33'
  265. )
  266. if IS_WINDOWS:
  267. expected = 'The \'mode\' option is not supported on Windows'
  268. self.assertEqual(ret[list(ret)[0]]['comment'], expected)
  269. self.assertSaltFalseReturn(ret)
  270. return
  271. resulting_mode = stat.S_IMODE(
  272. os.stat(name).st_mode
  273. )
  274. self.assertEqual(oct(initial_mode), oct(resulting_mode))
  275. name = os.path.join(TMP, 'grail_scene33')
  276. ret = self.run_state(
  277. 'file.managed', name=name, replace=True, mode=oct(desired_mode), source='salt://grail/scene33'
  278. )
  279. resulting_mode = stat.S_IMODE(
  280. os.stat(name).st_mode
  281. )
  282. self.assertEqual(oct(desired_mode), oct(resulting_mode))
  283. self.assertSaltTrueReturn(ret)
  284. def test_managed_file_mode_file_exists_noreplace(self):
  285. '''
  286. file.managed, existing file with replace=False, change permissions
  287. '''
  288. initial_mode = 504 # 0770 octal
  289. desired_mode = 384 # 0600 octal
  290. name = os.path.join(TMP, 'grail_scene33')
  291. ret = self.run_state(
  292. 'file.managed', name=name, replace=True, mode=oct(initial_mode), source='salt://grail/scene33'
  293. )
  294. if IS_WINDOWS:
  295. expected = 'The \'mode\' option is not supported on Windows'
  296. self.assertEqual(ret[list(ret)[0]]['comment'], expected)
  297. self.assertSaltFalseReturn(ret)
  298. return
  299. ret = self.run_state(
  300. 'file.managed', name=name, replace=False, mode=oct(desired_mode), source='salt://grail/scene33'
  301. )
  302. resulting_mode = stat.S_IMODE(
  303. os.stat(name).st_mode
  304. )
  305. self.assertEqual(oct(desired_mode), oct(resulting_mode))
  306. self.assertSaltTrueReturn(ret)
  307. def test_managed_file_with_grains_data(self):
  308. '''
  309. Test to ensure we can render grains data into a managed
  310. file.
  311. '''
  312. grain_path = os.path.join(TMP, 'file-grain-test')
  313. state_file = 'file-grainget'
  314. self.run_function('state.sls', [state_file], pillar={'grain_path': grain_path})
  315. self.assertTrue(os.path.exists(grain_path))
  316. with salt.utils.files.fopen(grain_path, 'r') as fp_:
  317. file_contents = fp_.readlines()
  318. if IS_WINDOWS:
  319. match = '^minion\r\n'
  320. else:
  321. match = '^minion\n'
  322. self.assertTrue(re.match(match, file_contents[0]))
  323. def test_managed_file_with_pillar_sls(self):
  324. '''
  325. Test to ensure pillar data in sls file
  326. is rendered properly and file is created.
  327. '''
  328. state_name = 'file-pillarget'
  329. ret = self.run_function('state.sls', [state_name])
  330. self.assertSaltTrueReturn(ret)
  331. # Check to make sure the file was created
  332. check_file = self.run_function('file.file_exists', [FILEPILLAR])
  333. self.assertTrue(check_file)
  334. def test_managed_file_with_pillardefault_sls(self):
  335. '''
  336. Test to ensure when pillar data is not available
  337. in sls file with pillar.get it uses the default
  338. value.
  339. '''
  340. state_name = 'file-pillardefaultget'
  341. ret = self.run_function('state.sls', [state_name])
  342. self.assertSaltTrueReturn(ret)
  343. # Check to make sure the file was created
  344. check_file = self.run_function('file.file_exists', [FILEPILLARDEF])
  345. self.assertTrue(check_file)
  346. @skip_if_not_root
  347. def test_managed_dir_mode(self):
  348. '''
  349. Tests to ensure that file.managed creates directories with the
  350. permissions requested with the dir_mode argument
  351. '''
  352. desired_mode = 511 # 0777 in octal
  353. name = os.path.join(TMP, 'a', 'managed_dir_mode_test_file')
  354. desired_owner = 'nobody'
  355. ret = self.run_state(
  356. 'file.managed',
  357. name=name,
  358. source='salt://grail/scene33',
  359. mode=600,
  360. makedirs=True,
  361. user=desired_owner,
  362. dir_mode=oct(desired_mode) # 0777
  363. )
  364. if IS_WINDOWS:
  365. expected = 'The \'mode\' option is not supported on Windows'
  366. self.assertEqual(ret[list(ret)[0]]['comment'], expected)
  367. self.assertSaltFalseReturn(ret)
  368. return
  369. resulting_mode = stat.S_IMODE(
  370. os.stat(os.path.join(TMP, 'a')).st_mode
  371. )
  372. resulting_owner = pwd.getpwuid(os.stat(os.path.join(TMP, 'a')).st_uid).pw_name
  373. self.assertEqual(oct(desired_mode), oct(resulting_mode))
  374. self.assertSaltTrueReturn(ret)
  375. self.assertEqual(desired_owner, resulting_owner)
  376. def test_test_managed(self):
  377. '''
  378. file.managed test interface
  379. '''
  380. name = os.path.join(TMP, 'grail_not_not_scene33')
  381. ret = self.run_state(
  382. 'file.managed', test=True, name=name, source='salt://grail/scene33'
  383. )
  384. self.assertSaltNoneReturn(ret)
  385. self.assertFalse(os.path.isfile(name))
  386. def test_managed_show_changes_false(self):
  387. '''
  388. file.managed test interface
  389. '''
  390. name = os.path.join(TMP, 'grail_not_scene33')
  391. with salt.utils.files.fopen(name, 'wb') as fp_:
  392. fp_.write(b'test_managed_show_changes_false\n')
  393. ret = self.run_state(
  394. 'file.managed', name=name, source='salt://grail/scene33',
  395. show_changes=False
  396. )
  397. changes = next(six.itervalues(ret))['changes']
  398. self.assertEqual('<show_changes=False>', changes['diff'])
  399. def test_managed_show_changes_true(self):
  400. '''
  401. file.managed test interface
  402. '''
  403. name = os.path.join(TMP, 'grail_not_scene33')
  404. with salt.utils.files.fopen(name, 'wb') as fp_:
  405. fp_.write(b'test_managed_show_changes_false\n')
  406. ret = self.run_state(
  407. 'file.managed', name=name, source='salt://grail/scene33',
  408. )
  409. changes = next(six.itervalues(ret))['changes']
  410. self.assertIn('diff', changes)
  411. @skipIf(IS_WINDOWS, 'Don\'t know how to fix for Windows')
  412. def test_managed_escaped_file_path(self):
  413. '''
  414. file.managed test that 'salt://|' protects unusual characters in file path
  415. '''
  416. funny_file = salt.utils.files.mkstemp(prefix='?f!le? n@=3&', suffix='.file type')
  417. funny_file_name = os.path.split(funny_file)[1]
  418. funny_url = 'salt://|' + funny_file_name
  419. funny_url_path = os.path.join(BASE_FILES, funny_file_name)
  420. state_name = 'funny_file'
  421. state_file_name = state_name + '.sls'
  422. state_file = os.path.join(BASE_FILES, state_file_name)
  423. state_key = 'file_|-{0}_|-{0}_|-managed'.format(funny_file)
  424. self.addCleanup(os.remove, state_file)
  425. self.addCleanup(os.remove, funny_file)
  426. self.addCleanup(os.remove, funny_url_path)
  427. with salt.utils.files.fopen(funny_url_path, 'w'):
  428. pass
  429. with salt.utils.files.fopen(state_file, 'w') as fp_:
  430. fp_.write(textwrap.dedent('''\
  431. {0}:
  432. file.managed:
  433. - source: {1}
  434. - makedirs: True
  435. '''.format(funny_file, funny_url)))
  436. ret = self.run_function('state.sls', [state_name])
  437. self.assertTrue(ret[state_key]['result'])
  438. def test_managed_contents(self):
  439. '''
  440. test file.managed with contents that is a boolean, string, integer,
  441. float, list, and dictionary
  442. '''
  443. state_name = 'file-FileTest-test_managed_contents'
  444. state_filename = state_name + '.sls'
  445. state_file = os.path.join(BASE_FILES, state_filename)
  446. managed_files = {}
  447. state_keys = {}
  448. for typ in ('bool', 'str', 'int', 'float', 'list', 'dict'):
  449. managed_files[typ] = salt.utils.files.mkstemp()
  450. state_keys[typ] = 'file_|-{0} file_|-{1}_|-managed'.format(typ, managed_files[typ])
  451. try:
  452. with salt.utils.files.fopen(state_file, 'w') as fd_:
  453. fd_.write(textwrap.dedent('''\
  454. bool file:
  455. file.managed:
  456. - name: {bool}
  457. - contents: True
  458. str file:
  459. file.managed:
  460. - name: {str}
  461. - contents: Salt was here.
  462. int file:
  463. file.managed:
  464. - name: {int}
  465. - contents: 340282366920938463463374607431768211456
  466. float file:
  467. file.managed:
  468. - name: {float}
  469. - contents: 1.7518e-45 # gravitational coupling constant
  470. list file:
  471. file.managed:
  472. - name: {list}
  473. - contents: [1, 1, 2, 3, 5, 8, 13]
  474. dict file:
  475. file.managed:
  476. - name: {dict}
  477. - contents:
  478. C: charge
  479. P: parity
  480. T: time
  481. '''.format(**managed_files)))
  482. ret = self.run_function('state.sls', [state_name])
  483. for typ in state_keys:
  484. self.assertTrue(ret[state_keys[typ]]['result'])
  485. self.assertIn('diff', ret[state_keys[typ]]['changes'])
  486. finally:
  487. os.remove(state_file)
  488. for typ in managed_files:
  489. os.remove(managed_files[typ])
  490. @skip_if_not_root
  491. @skipIf(IS_WINDOWS, 'Windows does not support "mode" kwarg. Skipping.')
  492. @skipIf(not salt.utils.path.which('visudo'), 'sudo is missing')
  493. def test_managed_check_cmd(self):
  494. '''
  495. Test file.managed passing a basic check_cmd kwarg. See Issue #38111.
  496. '''
  497. r_group = 'root'
  498. if salt.utils.platform.is_darwin():
  499. r_group = 'wheel'
  500. try:
  501. ret = self.run_state(
  502. 'file.managed',
  503. name='/tmp/sudoers',
  504. user='root',
  505. group=r_group,
  506. mode=440,
  507. check_cmd='visudo -c -s -f'
  508. )
  509. self.assertSaltTrueReturn(ret)
  510. self.assertInSaltComment('Empty file', ret)
  511. self.assertEqual(ret['file_|-/tmp/sudoers_|-/tmp/sudoers_|-managed']['changes'],
  512. {'new': 'file /tmp/sudoers created', 'mode': '0440'})
  513. finally:
  514. # Clean Up File
  515. if os.path.exists('/tmp/sudoers'):
  516. os.remove('/tmp/sudoers')
  517. def test_managed_local_source_with_source_hash(self):
  518. '''
  519. Make sure that we enforce the source_hash even with local files
  520. '''
  521. name = os.path.join(TMP, 'local_source_with_source_hash')
  522. local_path = os.path.join(BASE_FILES, 'grail', 'scene33')
  523. actual_hash = '567fd840bf1548edc35c48eb66cdd78bfdfcccff'
  524. if IS_WINDOWS:
  525. # CRLF vs LF causes a different hash on windows
  526. actual_hash = 'f658a0ec121d9c17088795afcc6ff3c43cb9842a'
  527. # Reverse the actual hash
  528. bad_hash = actual_hash[::-1]
  529. def remove_file():
  530. try:
  531. os.remove(name)
  532. except OSError as exc:
  533. if exc.errno != errno.ENOENT:
  534. raise
  535. def do_test(clean=False):
  536. for proto in ('file://', ''):
  537. source = proto + local_path
  538. log.debug('Trying source %s', source)
  539. try:
  540. ret = self.run_state(
  541. 'file.managed',
  542. name=name,
  543. source=source,
  544. source_hash='sha1={0}'.format(bad_hash))
  545. self.assertSaltFalseReturn(ret)
  546. ret = ret[next(iter(ret))]
  547. # Shouldn't be any changes
  548. self.assertFalse(ret['changes'])
  549. # Check that we identified a hash mismatch
  550. self.assertIn(
  551. 'does not match actual checksum', ret['comment'])
  552. ret = self.run_state(
  553. 'file.managed',
  554. name=name,
  555. source=source,
  556. source_hash='sha1={0}'.format(actual_hash))
  557. self.assertSaltTrueReturn(ret)
  558. finally:
  559. if clean:
  560. remove_file()
  561. remove_file()
  562. log.debug('Trying with nonexistant destination file')
  563. do_test()
  564. log.debug('Trying with destination file already present')
  565. with salt.utils.files.fopen(name, 'w'):
  566. pass
  567. try:
  568. do_test(clean=False)
  569. finally:
  570. remove_file()
  571. def test_managed_local_source_does_not_exist(self):
  572. '''
  573. Make sure that we exit gracefully when a local source doesn't exist
  574. '''
  575. name = os.path.join(TMP, 'local_source_does_not_exist')
  576. local_path = os.path.join(BASE_FILES, 'grail', 'scene99')
  577. for proto in ('file://', ''):
  578. source = proto + local_path
  579. log.debug('Trying source %s', source)
  580. ret = self.run_state(
  581. 'file.managed',
  582. name=name,
  583. source=source)
  584. self.assertSaltFalseReturn(ret)
  585. ret = ret[next(iter(ret))]
  586. # Shouldn't be any changes
  587. self.assertFalse(ret['changes'])
  588. # Check that we identified a hash mismatch
  589. self.assertIn(
  590. 'does not exist', ret['comment'])
  591. def test_managed_unicode_jinja_with_tojson_filter(self):
  592. '''
  593. Using {{ varname }} with a list or dictionary which contains unicode
  594. types on Python 2 will result in Jinja rendering the "u" prefix on each
  595. string. This tests that using the "tojson" jinja filter will dump them
  596. to a format which can be successfully loaded by our YAML loader.
  597. The two lines that should end up being rendered are meant to test two
  598. issues that would trip up PyYAML if the "tojson" filter were not used:
  599. 1. A unicode string type would be loaded as a unicode literal with the
  600. leading "u" as well as the quotes, rather than simply being loaded
  601. as the proper unicode type which matches the content of the string
  602. literal. In other words, u'foo' would be loaded literally as
  603. u"u'foo'". This test includes actual non-ascii unicode in one of the
  604. strings to confirm that this also handles these international
  605. characters properly.
  606. 2. Any unicode string type (such as a URL) which contains a colon would
  607. cause a ScannerError in PyYAML, as it would be assumed to delimit a
  608. mapping node.
  609. Dumping the data structure to JSON using the "tojson" jinja filter
  610. should produce an inline data structure which is valid YAML and will be
  611. loaded properly by our YAML loader.
  612. '''
  613. test_file = os.path.join(TMP, 'test-tojson.txt')
  614. ret = self.run_function(
  615. 'state.apply',
  616. mods='tojson',
  617. pillar={'tojson-file': test_file})
  618. ret = ret[next(iter(ret))]
  619. assert ret['result'], ret
  620. with salt.utils.files.fopen(test_file, mode='rb') as fp_:
  621. managed = salt.utils.stringutils.to_unicode(fp_.read())
  622. expected = dedent('''\
  623. Die Webseite ist https://saltstack.com.
  624. Der Zucker ist süß.
  625. ''')
  626. assert managed == expected, '{0!r} != {1!r}'.format(managed, expected) # pylint: disable=repr-flag-used-in-string
  627. def test_managed_source_hash_indifferent_case(self):
  628. '''
  629. Test passing a source_hash as an uppercase hash.
  630. This is a regression test for Issue #38914 and Issue #48230 (test=true use).
  631. '''
  632. name = os.path.join(TMP, 'source_hash_indifferent_case')
  633. state_name = 'file_|-{0}_|' \
  634. '-{0}_|-managed'.format(name)
  635. local_path = os.path.join(BASE_FILES, 'hello_world.txt')
  636. actual_hash = 'c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31'
  637. if IS_WINDOWS:
  638. # CRLF vs LF causes a differnt hash on windows
  639. actual_hash = '92b772380a3f8e27a93e57e6deeca6c01da07f5aadce78bb2fbb20de10a66925'
  640. uppercase_hash = actual_hash.upper()
  641. try:
  642. # Lay down tmp file to test against
  643. self.run_state(
  644. 'file.managed',
  645. name=name,
  646. source=local_path,
  647. source_hash=actual_hash
  648. )
  649. # Test uppercase source_hash: should return True with no changes
  650. ret = self.run_state(
  651. 'file.managed',
  652. name=name,
  653. source=local_path,
  654. source_hash=uppercase_hash
  655. )
  656. assert ret[state_name]['result'] is True
  657. assert ret[state_name]['changes'] == {}
  658. # Test uppercase source_hash using test=true
  659. # Should return True with no changes
  660. ret = self.run_state(
  661. 'file.managed',
  662. name=name,
  663. source=local_path,
  664. source_hash=uppercase_hash,
  665. test=True
  666. )
  667. assert ret[state_name]['result'] is True
  668. assert ret[state_name]['changes'] == {}
  669. finally:
  670. # Clean Up File
  671. if os.path.exists(name):
  672. os.remove(name)
  673. @with_tempfile(create=False)
  674. def test_managed_latin1_diff(self, name):
  675. '''
  676. Tests that latin-1 file contents are represented properly in the diff
  677. '''
  678. # Lay down the initial file
  679. ret = self.run_state(
  680. 'file.managed',
  681. name=name,
  682. source='salt://issue-48777/old.html')
  683. ret = ret[next(iter(ret))]
  684. assert ret['result'] is True, ret
  685. # Replace it with the new file and check the diff
  686. ret = self.run_state(
  687. 'file.managed',
  688. name=name,
  689. source='salt://issue-48777/new.html')
  690. ret = ret[next(iter(ret))]
  691. assert ret['result'] is True, ret
  692. diff_lines = ret['changes']['diff'].split(os.linesep)
  693. assert '+räksmörgås' in diff_lines, diff_lines
  694. @with_tempfile()
  695. def test_managed_keep_source_false_salt(self, name):
  696. '''
  697. This test ensures that we properly clean the cached file if keep_source
  698. is set to False, for source files using a salt:// URL
  699. '''
  700. source = 'salt://grail/scene33'
  701. saltenv = 'base'
  702. # Run the state
  703. ret = self.run_state(
  704. 'file.managed',
  705. name=name,
  706. source=source,
  707. saltenv=saltenv,
  708. keep_source=False)
  709. ret = ret[next(iter(ret))]
  710. assert ret['result'] is True
  711. # Now make sure that the file is not cached
  712. result = self.run_function('cp.is_cached', [source, saltenv])
  713. assert result == '', 'File is still cached at {0}'.format(result)
  714. @with_tempfile(create=False)
  715. @with_tempfile(create=False)
  716. def test_file_managed_onchanges(self, file1, file2):
  717. '''
  718. Test file.managed state with onchanges
  719. '''
  720. pillar = {'file1': file1,
  721. 'file2': file2,
  722. 'source': 'salt://testfile',
  723. 'req': 'onchanges'}
  724. # Lay down the file used in the below SLS to ensure that when it is
  725. # run, there are no changes.
  726. self.run_state(
  727. 'file.managed',
  728. name=pillar['file2'],
  729. source=pillar['source'])
  730. ret = self.repack_state_returns(
  731. self.run_function(
  732. 'state.apply',
  733. mods='onchanges_prereq',
  734. pillar=pillar,
  735. test=True,
  736. )
  737. )
  738. # The file states should both exit with None
  739. assert ret['one']['result'] is None, ret['one']['result']
  740. assert ret['three']['result'] is True, ret['three']['result']
  741. # The first file state should have changes, since a new file was
  742. # created. The other one should not, since we already created that file
  743. # before applying the SLS file.
  744. assert ret['one']['changes']
  745. assert not ret['three']['changes'], ret['three']['changes']
  746. # The state watching 'one' should have been run due to changes
  747. assert ret['two']['comment'] == 'Success!', ret['two']['comment']
  748. # The state watching 'three' should not have been run
  749. assert ret['four']['comment'] == \
  750. 'State was not run because none of the onchanges reqs changed', \
  751. ret['four']['comment']
  752. @with_tempfile(create=False)
  753. @with_tempfile(create=False)
  754. def test_file_managed_prereq(self, file1, file2):
  755. '''
  756. Test file.managed state with prereq
  757. '''
  758. pillar = {'file1': file1,
  759. 'file2': file2,
  760. 'source': 'salt://testfile',
  761. 'req': 'prereq'}
  762. # Lay down the file used in the below SLS to ensure that when it is
  763. # run, there are no changes.
  764. self.run_state(
  765. 'file.managed',
  766. name=pillar['file2'],
  767. source=pillar['source'])
  768. ret = self.repack_state_returns(
  769. self.run_function(
  770. 'state.apply',
  771. mods='onchanges_prereq',
  772. pillar=pillar,
  773. test=True,
  774. )
  775. )
  776. # The file states should both exit with None
  777. assert ret['one']['result'] is None, ret['one']['result']
  778. assert ret['three']['result'] is True, ret['three']['result']
  779. # The first file state should have changes, since a new file was
  780. # created. The other one should not, since we already created that file
  781. # before applying the SLS file.
  782. assert ret['one']['changes']
  783. assert not ret['three']['changes'], ret['three']['changes']
  784. # The state watching 'one' should have been run due to changes
  785. assert ret['two']['comment'] == 'Success!', ret['two']['comment']
  786. # The state watching 'three' should not have been run
  787. assert ret['four']['comment'] == 'No changes detected', \
  788. ret['four']['comment']
  789. def test_directory(self):
  790. '''
  791. file.directory
  792. '''
  793. name = os.path.join(TMP, 'a_new_dir')
  794. ret = self.run_state('file.directory', name=name)
  795. self.assertSaltTrueReturn(ret)
  796. self.assertTrue(os.path.isdir(name))
  797. def test_directory_symlink_dry_run(self):
  798. '''
  799. Ensure that symlinks are followed when file.directory is run with
  800. test=True
  801. '''
  802. try:
  803. tmp_dir = os.path.join(TMP, 'pgdata')
  804. sym_dir = os.path.join(TMP, 'pg_data')
  805. if IS_WINDOWS:
  806. self.run_function('file.mkdir', [tmp_dir, 'Administrators'])
  807. else:
  808. os.mkdir(tmp_dir, 0o700)
  809. self.run_function('file.symlink', [tmp_dir, sym_dir])
  810. if IS_WINDOWS:
  811. ret = self.run_state(
  812. 'file.directory', test=True, name=sym_dir,
  813. follow_symlinks=True, win_owner='Administrators')
  814. else:
  815. ret = self.run_state(
  816. 'file.directory', test=True, name=sym_dir,
  817. follow_symlinks=True, mode=700)
  818. self.assertSaltTrueReturn(ret)
  819. finally:
  820. if os.path.isdir(tmp_dir):
  821. self.run_function('file.remove', [tmp_dir])
  822. if os.path.islink(sym_dir):
  823. self.run_function('file.remove', [sym_dir])
  824. @skip_if_not_root
  825. @skipIf(IS_WINDOWS, 'Mode not available in Windows')
  826. def test_directory_max_depth(self):
  827. '''
  828. file.directory
  829. Test the max_depth option by iteratively increasing the depth and
  830. checking that no changes deeper than max_depth have been attempted
  831. '''
  832. def _get_oct_mode(name):
  833. '''
  834. Return a string octal representation of the permissions for name
  835. '''
  836. return salt.utils.files.normalize_mode(oct(os.stat(name).st_mode & 0o777))
  837. top = os.path.join(TMP, 'top_dir')
  838. sub = os.path.join(top, 'sub_dir')
  839. subsub = os.path.join(sub, 'sub_sub_dir')
  840. dirs = [top, sub, subsub]
  841. initial_mode = '0111'
  842. changed_mode = '0555'
  843. initial_modes = {0: {sub: '0755',
  844. subsub: '0111'},
  845. 1: {sub: '0111',
  846. subsub: '0111'},
  847. 2: {sub: '0111',
  848. subsub: '0111'}}
  849. if not os.path.isdir(subsub):
  850. os.makedirs(subsub, int(initial_mode, 8))
  851. try:
  852. for depth in range(0, 3):
  853. ret = self.run_state('file.directory',
  854. name=top,
  855. max_depth=depth,
  856. dir_mode=changed_mode,
  857. recurse=['mode'])
  858. self.assertSaltTrueReturn(ret)
  859. for changed_dir in dirs[0:depth+1]:
  860. self.assertEqual(changed_mode,
  861. _get_oct_mode(changed_dir))
  862. for untouched_dir in dirs[depth+1:]:
  863. # Beginning in Python 3.7, os.makedirs no longer sets
  864. # the mode of intermediate directories to the mode that
  865. # is passed.
  866. if sys.version_info >= (3, 7):
  867. _mode = initial_modes[depth][untouched_dir]
  868. self.assertEqual(_mode,
  869. _get_oct_mode(untouched_dir))
  870. else:
  871. self.assertEqual(initial_mode,
  872. _get_oct_mode(untouched_dir))
  873. finally:
  874. shutil.rmtree(top)
  875. def test_test_directory(self):
  876. '''
  877. file.directory
  878. '''
  879. name = os.path.join(TMP, 'a_not_dir')
  880. ret = self.run_state('file.directory', test=True, name=name)
  881. self.assertSaltNoneReturn(ret)
  882. self.assertFalse(os.path.isdir(name))
  883. @with_tempdir()
  884. def test_directory_clean(self, base_dir):
  885. '''
  886. file.directory with clean=True
  887. '''
  888. name = os.path.join(base_dir, 'directory_clean_dir')
  889. os.mkdir(name)
  890. strayfile = os.path.join(name, 'strayfile')
  891. with salt.utils.files.fopen(strayfile, 'w'):
  892. pass
  893. straydir = os.path.join(name, 'straydir')
  894. if not os.path.isdir(straydir):
  895. os.makedirs(straydir)
  896. with salt.utils.files.fopen(os.path.join(straydir, 'strayfile2'), 'w'):
  897. pass
  898. ret = self.run_state('file.directory', name=name, clean=True)
  899. self.assertSaltTrueReturn(ret)
  900. self.assertFalse(os.path.exists(strayfile))
  901. self.assertFalse(os.path.exists(straydir))
  902. self.assertTrue(os.path.isdir(name))
  903. def test_directory_is_idempotent(self):
  904. '''
  905. Ensure the file.directory state produces no changes when rerun.
  906. '''
  907. name = os.path.join(TMP, 'a_dir_twice')
  908. if IS_WINDOWS:
  909. username = os.environ.get('USERNAME', 'Administrators')
  910. domain = os.environ.get('USERDOMAIN', '')
  911. fullname = '{0}\\{1}'.format(domain, username)
  912. ret = self.run_state('file.directory', name=name, win_owner=fullname)
  913. else:
  914. ret = self.run_state('file.directory', name=name)
  915. self.assertSaltTrueReturn(ret)
  916. if IS_WINDOWS:
  917. ret = self.run_state('file.directory', name=name, win_owner=username)
  918. else:
  919. ret = self.run_state('file.directory', name=name)
  920. self.assertSaltTrueReturn(ret)
  921. self.assertSaltStateChangesEqual(ret, {})
  922. @with_tempdir()
  923. def test_directory_clean_exclude(self, base_dir):
  924. '''
  925. file.directory with clean=True and exclude_pat set
  926. '''
  927. name = os.path.join(base_dir, 'directory_clean_dir')
  928. if not os.path.isdir(name):
  929. os.makedirs(name)
  930. strayfile = os.path.join(name, 'strayfile')
  931. with salt.utils.files.fopen(strayfile, 'w'):
  932. pass
  933. straydir = os.path.join(name, 'straydir')
  934. if not os.path.isdir(straydir):
  935. os.makedirs(straydir)
  936. strayfile2 = os.path.join(straydir, 'strayfile2')
  937. with salt.utils.files.fopen(strayfile2, 'w'):
  938. pass
  939. keepfile = os.path.join(straydir, 'keepfile')
  940. with salt.utils.files.fopen(keepfile, 'w'):
  941. pass
  942. exclude_pat = 'E@^straydir(|/keepfile)$'
  943. if IS_WINDOWS:
  944. exclude_pat = 'E@^straydir(|\\\\keepfile)$'
  945. ret = self.run_state('file.directory',
  946. name=name,
  947. clean=True,
  948. exclude_pat=exclude_pat)
  949. self.assertSaltTrueReturn(ret)
  950. self.assertFalse(os.path.exists(strayfile))
  951. self.assertFalse(os.path.exists(strayfile2))
  952. self.assertTrue(os.path.exists(keepfile))
  953. @skipIf(IS_WINDOWS, 'Skip on windows')
  954. @with_tempdir()
  955. def test_test_directory_clean_exclude(self, base_dir):
  956. '''
  957. file.directory with test=True, clean=True and exclude_pat set
  958. Skipped on windows because clean and exclude_pat not supported by
  959. salt.sates.file._check_directory_win
  960. '''
  961. name = os.path.join(base_dir, 'directory_clean_dir')
  962. os.mkdir(name)
  963. strayfile = os.path.join(name, 'strayfile')
  964. with salt.utils.files.fopen(strayfile, 'w'):
  965. pass
  966. straydir = os.path.join(name, 'straydir')
  967. if not os.path.isdir(straydir):
  968. os.makedirs(straydir)
  969. strayfile2 = os.path.join(straydir, 'strayfile2')
  970. with salt.utils.files.fopen(strayfile2, 'w'):
  971. pass
  972. keepfile = os.path.join(straydir, 'keepfile')
  973. with salt.utils.files.fopen(keepfile, 'w'):
  974. pass
  975. exclude_pat = 'E@^straydir(|/keepfile)$'
  976. if IS_WINDOWS:
  977. exclude_pat = 'E@^straydir(|\\\\keepfile)$'
  978. ret = self.run_state('file.directory',
  979. test=True,
  980. name=name,
  981. clean=True,
  982. exclude_pat=exclude_pat)
  983. comment = next(six.itervalues(ret))['comment']
  984. self.assertSaltNoneReturn(ret)
  985. self.assertTrue(os.path.exists(strayfile))
  986. self.assertTrue(os.path.exists(strayfile2))
  987. self.assertTrue(os.path.exists(keepfile))
  988. self.assertIn(strayfile, comment)
  989. self.assertIn(strayfile2, comment)
  990. self.assertNotIn(keepfile, comment)
  991. @with_tempdir()
  992. def test_directory_clean_require_in(self, name):
  993. '''
  994. file.directory test with clean=True and require_in file
  995. '''
  996. state_name = 'file-FileTest-test_directory_clean_require_in'
  997. state_filename = state_name + '.sls'
  998. state_file = os.path.join(BASE_FILES, state_filename)
  999. wrong_file = os.path.join(name, "wrong")
  1000. with salt.utils.files.fopen(wrong_file, "w") as fp:
  1001. fp.write("foo")
  1002. good_file = os.path.join(name, "bar")
  1003. with salt.utils.files.fopen(state_file, 'w') as fp:
  1004. self.addCleanup(lambda: os.remove(state_file))
  1005. fp.write(textwrap.dedent('''\
  1006. some_dir:
  1007. file.directory:
  1008. - name: {name}
  1009. - clean: true
  1010. {good_file}:
  1011. file.managed:
  1012. - require_in:
  1013. - file: some_dir
  1014. '''.format(name=name, good_file=good_file)))
  1015. ret = self.run_function('state.sls', [state_name])
  1016. self.assertTrue(os.path.exists(good_file))
  1017. self.assertFalse(os.path.exists(wrong_file))
  1018. @with_tempdir()
  1019. def test_directory_clean_require_in_with_id(self, name):
  1020. '''
  1021. file.directory test with clean=True and require_in file with an ID
  1022. different from the file name
  1023. '''
  1024. state_name = 'file-FileTest-test_directory_clean_require_in_with_id'
  1025. state_filename = state_name + '.sls'
  1026. state_file = os.path.join(BASE_FILES, state_filename)
  1027. wrong_file = os.path.join(name, "wrong")
  1028. with salt.utils.files.fopen(wrong_file, "w") as fp:
  1029. fp.write("foo")
  1030. good_file = os.path.join(name, "bar")
  1031. with salt.utils.files.fopen(state_file, 'w') as fp:
  1032. self.addCleanup(lambda: os.remove(state_file))
  1033. fp.write(textwrap.dedent('''\
  1034. some_dir:
  1035. file.directory:
  1036. - name: {name}
  1037. - clean: true
  1038. some_file:
  1039. file.managed:
  1040. - name: {good_file}
  1041. - require_in:
  1042. - file: some_dir
  1043. '''.format(name=name, good_file=good_file)))
  1044. ret = self.run_function('state.sls', [state_name])
  1045. self.assertTrue(os.path.exists(good_file))
  1046. self.assertFalse(os.path.exists(wrong_file))
  1047. @with_tempdir()
  1048. def test_directory_clean_require_with_name(self, name):
  1049. '''
  1050. file.directory test with clean=True and require with a file state
  1051. relatively to the state's name, not its ID.
  1052. '''
  1053. state_name = 'file-FileTest-test_directory_clean_require_in_with_id'
  1054. state_filename = state_name + '.sls'
  1055. state_file = os.path.join(BASE_FILES, state_filename)
  1056. wrong_file = os.path.join(name, "wrong")
  1057. with salt.utils.files.fopen(wrong_file, "w") as fp:
  1058. fp.write("foo")
  1059. good_file = os.path.join(name, "bar")
  1060. with salt.utils.files.fopen(state_file, 'w') as fp:
  1061. self.addCleanup(lambda: os.remove(state_file))
  1062. fp.write(textwrap.dedent('''\
  1063. some_dir:
  1064. file.directory:
  1065. - name: {name}
  1066. - clean: true
  1067. - require:
  1068. # This requirement refers to the name of the following
  1069. # state, not its ID.
  1070. - file: {good_file}
  1071. some_file:
  1072. file.managed:
  1073. - name: {good_file}
  1074. '''.format(name=name, good_file=good_file)))
  1075. ret = self.run_function('state.sls', [state_name])
  1076. self.assertTrue(os.path.exists(good_file))
  1077. self.assertFalse(os.path.exists(wrong_file))
  1078. def test_directory_broken_symlink(self):
  1079. '''
  1080. Ensure that file.directory works even if a directory
  1081. contains broken symbolic link
  1082. '''
  1083. try:
  1084. tmp_dir = os.path.join(TMP, 'foo')
  1085. null_file = '{0}/null'.format(tmp_dir)
  1086. broken_link = '{0}/broken'.format(tmp_dir)
  1087. if IS_WINDOWS:
  1088. self.run_function('file.mkdir', [tmp_dir, 'Administrators'])
  1089. else:
  1090. os.mkdir(tmp_dir, 0o700)
  1091. self.run_function('file.symlink', [null_file, broken_link])
  1092. if IS_WINDOWS:
  1093. ret = self.run_state(
  1094. 'file.directory', name=tmp_dir, recurse={'mode'},
  1095. follow_symlinks=True, win_owner='Administrators')
  1096. else:
  1097. ret = self.run_state(
  1098. 'file.directory', name=tmp_dir, recurse={'mode'},
  1099. file_mode=644, dir_mode=755)
  1100. self.assertSaltTrueReturn(ret)
  1101. finally:
  1102. if os.path.isdir(tmp_dir):
  1103. self.run_function('file.remove', [tmp_dir])
  1104. @with_tempdir(create=False)
  1105. def test_recurse(self, name):
  1106. '''
  1107. file.recurse
  1108. '''
  1109. ret = self.run_state('file.recurse', name=name, source='salt://grail')
  1110. self.assertSaltTrueReturn(ret)
  1111. self.assertTrue(os.path.isfile(os.path.join(name, '36', 'scene')))
  1112. @with_tempdir(create=False)
  1113. @with_tempdir(create=False)
  1114. def test_recurse_specific_env(self, dir1, dir2):
  1115. '''
  1116. file.recurse passing __env__
  1117. '''
  1118. ret = self.run_state('file.recurse',
  1119. name=dir1,
  1120. source='salt://holy',
  1121. __env__='prod')
  1122. self.assertSaltTrueReturn(ret)
  1123. self.assertTrue(os.path.isfile(os.path.join(dir1, '32', 'scene')))
  1124. ret = self.run_state('file.recurse',
  1125. name=dir2,
  1126. source='salt://holy',
  1127. saltenv='prod')
  1128. self.assertSaltTrueReturn(ret)
  1129. self.assertTrue(os.path.isfile(os.path.join(dir2, '32', 'scene')))
  1130. @with_tempdir(create=False)
  1131. @with_tempdir(create=False)
  1132. def test_recurse_specific_env_in_url(self, dir1, dir2):
  1133. '''
  1134. file.recurse passing __env__
  1135. '''
  1136. ret = self.run_state('file.recurse',
  1137. name=dir1,
  1138. source='salt://holy?saltenv=prod')
  1139. self.assertSaltTrueReturn(ret)
  1140. self.assertTrue(os.path.isfile(os.path.join(dir1, '32', 'scene')))
  1141. ret = self.run_state('file.recurse',
  1142. name=dir2,
  1143. source='salt://holy?saltenv=prod')
  1144. self.assertSaltTrueReturn(ret)
  1145. self.assertTrue(os.path.isfile(os.path.join(dir2, '32', 'scene')))
  1146. @with_tempdir(create=False)
  1147. def test_test_recurse(self, name):
  1148. '''
  1149. file.recurse test interface
  1150. '''
  1151. ret = self.run_state(
  1152. 'file.recurse', test=True, name=name, source='salt://grail',
  1153. )
  1154. self.assertSaltNoneReturn(ret)
  1155. self.assertFalse(os.path.isfile(os.path.join(name, '36', 'scene')))
  1156. self.assertFalse(os.path.exists(name))
  1157. @with_tempdir(create=False)
  1158. @with_tempdir(create=False)
  1159. def test_test_recurse_specific_env(self, dir1, dir2):
  1160. '''
  1161. file.recurse test interface
  1162. '''
  1163. ret = self.run_state('file.recurse',
  1164. test=True,
  1165. name=dir1,
  1166. source='salt://holy',
  1167. __env__='prod'
  1168. )
  1169. self.assertSaltNoneReturn(ret)
  1170. self.assertFalse(os.path.isfile(os.path.join(dir1, '32', 'scene')))
  1171. self.assertFalse(os.path.exists(dir1))
  1172. ret = self.run_state('file.recurse',
  1173. test=True,
  1174. name=dir2,
  1175. source='salt://holy',
  1176. saltenv='prod'
  1177. )
  1178. self.assertSaltNoneReturn(ret)
  1179. self.assertFalse(os.path.isfile(os.path.join(dir2, '32', 'scene')))
  1180. self.assertFalse(os.path.exists(dir2))
  1181. @with_tempdir(create=False)
  1182. def test_recurse_template(self, name):
  1183. '''
  1184. file.recurse with jinja template enabled
  1185. '''
  1186. _ts = 'TEMPLATE TEST STRING'
  1187. ret = self.run_state(
  1188. 'file.recurse', name=name, source='salt://grail',
  1189. template='jinja', defaults={'spam': _ts})
  1190. self.assertSaltTrueReturn(ret)
  1191. with salt.utils.files.fopen(os.path.join(name, 'scene33'), 'r') as fp_:
  1192. contents = fp_.read()
  1193. self.assertIn(_ts, contents)
  1194. @with_tempdir()
  1195. def test_recurse_clean(self, name):
  1196. '''
  1197. file.recurse with clean=True
  1198. '''
  1199. strayfile = os.path.join(name, 'strayfile')
  1200. with salt.utils.files.fopen(strayfile, 'w'):
  1201. pass
  1202. # Corner cases: replacing file with a directory and vice versa
  1203. with salt.utils.files.fopen(os.path.join(name, '36'), 'w'):
  1204. pass
  1205. os.makedirs(os.path.join(name, 'scene33'))
  1206. ret = self.run_state(
  1207. 'file.recurse', name=name, source='salt://grail', clean=True)
  1208. self.assertSaltTrueReturn(ret)
  1209. self.assertFalse(os.path.exists(strayfile))
  1210. self.assertTrue(os.path.isfile(os.path.join(name, '36', 'scene')))
  1211. self.assertTrue(os.path.isfile(os.path.join(name, 'scene33')))
  1212. @with_tempdir()
  1213. def test_recurse_clean_specific_env(self, name):
  1214. '''
  1215. file.recurse with clean=True and __env__=prod
  1216. '''
  1217. strayfile = os.path.join(name, 'strayfile')
  1218. with salt.utils.files.fopen(strayfile, 'w'):
  1219. pass
  1220. # Corner cases: replacing file with a directory and vice versa
  1221. with salt.utils.files.fopen(os.path.join(name, '32'), 'w'):
  1222. pass
  1223. os.makedirs(os.path.join(name, 'scene34'))
  1224. ret = self.run_state('file.recurse',
  1225. name=name,
  1226. source='salt://holy',
  1227. clean=True,
  1228. __env__='prod')
  1229. self.assertSaltTrueReturn(ret)
  1230. self.assertFalse(os.path.exists(strayfile))
  1231. self.assertTrue(os.path.isfile(os.path.join(name, '32', 'scene')))
  1232. self.assertTrue(os.path.isfile(os.path.join(name, 'scene34')))
  1233. @skipIf(IS_WINDOWS, 'Skip on windows')
  1234. @with_tempdir()
  1235. def test_recurse_issue_34945(self, base_dir):
  1236. '''
  1237. This tests the case where the source dir for the file.recurse state
  1238. does not contain any files (only subdirectories), and the dir_mode is
  1239. being managed. For a long time, this corner case resulted in the top
  1240. level of the destination directory being created with the wrong initial
  1241. permissions, a problem that would be corrected later on in the
  1242. file.recurse state via running state.directory. However, the
  1243. file.directory state only gets called when there are files to be
  1244. managed in that directory, and when the source directory contains only
  1245. subdirectories, the incorrectly-set initial perms would not be
  1246. repaired.
  1247. This was fixed in https://github.com/saltstack/salt/pull/35309
  1248. Skipped on windows because dir_mode is not supported.
  1249. '''
  1250. dir_mode = '2775'
  1251. issue_dir = 'issue-34945'
  1252. name = os.path.join(base_dir, issue_dir)
  1253. ret = self.run_state('file.recurse',
  1254. name=name,
  1255. source='salt://' + issue_dir,
  1256. dir_mode=dir_mode)
  1257. self.assertSaltTrueReturn(ret)
  1258. actual_dir_mode = oct(stat.S_IMODE(os.stat(name).st_mode))[-4:]
  1259. self.assertEqual(dir_mode, actual_dir_mode)
  1260. @with_tempdir(create=False)
  1261. def test_recurse_issue_40578(self, name):
  1262. '''
  1263. This ensures that the state doesn't raise an exception when it
  1264. encounters a file with a unicode filename in the process of invoking
  1265. file.source_list.
  1266. '''
  1267. ret = self.run_state('file.recurse',
  1268. name=name,
  1269. source='salt://соль')
  1270. self.assertSaltTrueReturn(ret)
  1271. if six.PY2 and IS_WINDOWS:
  1272. # Providing unicode to os.listdir so that we avoid having listdir
  1273. # try to decode the filenames using the systemencoding on windows
  1274. # python 2.
  1275. files = os.listdir(name.decode('utf-8'))
  1276. else:
  1277. files = salt.utils.data.decode(os.listdir(name), normalize=True)
  1278. self.assertEqual(
  1279. sorted(files),
  1280. sorted(['foo.txt', 'спам.txt', 'яйца.txt']),
  1281. )
  1282. @with_tempfile()
  1283. def test_replace(self, name):
  1284. '''
  1285. file.replace
  1286. '''
  1287. with salt.utils.files.fopen(name, 'w+') as fp_:
  1288. fp_.write('change_me')
  1289. ret = self.run_state('file.replace',
  1290. name=name, pattern='change', repl='salt', backup=False)
  1291. with salt.utils.files.fopen(name, 'r') as fp_:
  1292. self.assertIn('salt', fp_.read())
  1293. self.assertSaltTrueReturn(ret)
  1294. @with_tempdir()
  1295. def test_replace_issue_18612(self, base_dir):
  1296. '''
  1297. Test the (mis-)behaviour of file.replace as described in #18612:
  1298. Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
  1299. an infinitely growing file as 'file.replace' didn't check beforehand
  1300. whether the changes had already been done to the file
  1301. # Case description:
  1302. The tested file contains one commented line
  1303. The commented line should be uncommented in the end, nothing else should change
  1304. '''
  1305. test_name = 'test_replace_issue_18612'
  1306. path_test = os.path.join(base_dir, test_name)
  1307. with salt.utils.files.fopen(path_test, 'w+') as fp_test_:
  1308. fp_test_.write('# en_US.UTF-8')
  1309. ret = []
  1310. for x in range(0, 3):
  1311. ret.append(self.run_state('file.replace',
  1312. name=path_test, pattern='^# en_US.UTF-8$', repl='en_US.UTF-8', append_if_not_found=True))
  1313. # ensure, the number of lines didn't change, even after invoking 'file.replace' 3 times
  1314. with salt.utils.files.fopen(path_test, 'r') as fp_test_:
  1315. self.assertTrue((sum(1 for _ in fp_test_) == 1))
  1316. # ensure, the replacement succeeded
  1317. with salt.utils.files.fopen(path_test, 'r') as fp_test_:
  1318. self.assertTrue(fp_test_.read().startswith('en_US.UTF-8'))
  1319. # ensure, all runs of 'file.replace' reported success
  1320. for item in ret:
  1321. self.assertSaltTrueReturn(item)
  1322. @with_tempdir()
  1323. def test_replace_issue_18612_prepend(self, base_dir):
  1324. '''
  1325. Test the (mis-)behaviour of file.replace as described in #18612:
  1326. Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
  1327. an infinitely growing file as 'file.replace' didn't check beforehand
  1328. whether the changes had already been done to the file
  1329. # Case description:
  1330. The tested multifile contains multiple lines not matching the pattern or replacement in any way
  1331. The replacement pattern should be prepended to the file
  1332. '''
  1333. test_name = 'test_replace_issue_18612_prepend'
  1334. path_in = os.path.join(
  1335. FILES, 'file.replace', '{0}.in'.format(test_name)
  1336. )
  1337. path_out = os.path.join(
  1338. FILES, 'file.replace', '{0}.out'.format(test_name)
  1339. )
  1340. path_test = os.path.join(base_dir, test_name)
  1341. # create test file based on initial template
  1342. shutil.copyfile(path_in, path_test)
  1343. ret = []
  1344. for x in range(0, 3):
  1345. ret.append(self.run_state('file.replace',
  1346. name=path_test, pattern='^# en_US.UTF-8$', repl='en_US.UTF-8', prepend_if_not_found=True))
  1347. # ensure, the resulting file contains the expected lines
  1348. self.assertTrue(filecmp.cmp(path_test, path_out))
  1349. # ensure the initial file was properly backed up
  1350. self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
  1351. # ensure, all runs of 'file.replace' reported success
  1352. for item in ret:
  1353. self.assertSaltTrueReturn(item)
  1354. @with_tempdir()
  1355. def test_replace_issue_18612_append(self, base_dir):
  1356. '''
  1357. Test the (mis-)behaviour of file.replace as described in #18612:
  1358. Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
  1359. an infinitely growing file as 'file.replace' didn't check beforehand
  1360. whether the changes had already been done to the file
  1361. # Case description:
  1362. The tested multifile contains multiple lines not matching the pattern or replacement in any way
  1363. The replacement pattern should be appended to the file
  1364. '''
  1365. test_name = 'test_replace_issue_18612_append'
  1366. path_in = os.path.join(
  1367. FILES, 'file.replace', '{0}.in'.format(test_name)
  1368. )
  1369. path_out = os.path.join(
  1370. FILES, 'file.replace', '{0}.out'.format(test_name)
  1371. )
  1372. path_test = os.path.join(base_dir, test_name)
  1373. # create test file based on initial template
  1374. shutil.copyfile(path_in, path_test)
  1375. ret = []
  1376. for x in range(0, 3):
  1377. ret.append(self.run_state('file.replace',
  1378. name=path_test, pattern='^# en_US.UTF-8$', repl='en_US.UTF-8', append_if_not_found=True))
  1379. # ensure, the resulting file contains the expected lines
  1380. self.assertTrue(filecmp.cmp(path_test, path_out))
  1381. # ensure the initial file was properly backed up
  1382. self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
  1383. # ensure, all runs of 'file.replace' reported success
  1384. for item in ret:
  1385. self.assertSaltTrueReturn(item)
  1386. @with_tempdir()
  1387. def test_replace_issue_18612_append_not_found_content(self, base_dir):
  1388. '''
  1389. Test the (mis-)behaviour of file.replace as described in #18612:
  1390. Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
  1391. an infinitely growing file as 'file.replace' didn't check beforehand
  1392. whether the changes had already been done to the file
  1393. # Case description:
  1394. The tested multifile contains multiple lines not matching the pattern or replacement in any way
  1395. The 'not_found_content' value should be appended to the file
  1396. '''
  1397. test_name = 'test_replace_issue_18612_append_not_found_content'
  1398. path_in = os.path.join(
  1399. FILES, 'file.replace', '{0}.in'.format(test_name)
  1400. )
  1401. path_out = os.path.join(
  1402. FILES, 'file.replace', '{0}.out'.format(test_name)
  1403. )
  1404. path_test = os.path.join(base_dir, test_name)
  1405. # create test file based on initial template
  1406. shutil.copyfile(path_in, path_test)
  1407. ret = []
  1408. for x in range(0, 3):
  1409. ret.append(
  1410. self.run_state('file.replace',
  1411. name=path_test,
  1412. pattern='^# en_US.UTF-8$',
  1413. repl='en_US.UTF-8',
  1414. append_if_not_found=True,
  1415. not_found_content='THIS LINE WASN\'T FOUND! SO WE\'RE APPENDING IT HERE!'
  1416. ))
  1417. # ensure, the resulting file contains the expected lines
  1418. self.assertTrue(filecmp.cmp(path_test, path_out))
  1419. # ensure the initial file was properly backed up
  1420. self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
  1421. # ensure, all runs of 'file.replace' reported success
  1422. for item in ret:
  1423. self.assertSaltTrueReturn(item)
  1424. @with_tempdir()
  1425. def test_replace_issue_18612_change_mid_line_with_comment(self, base_dir):
  1426. '''
  1427. Test the (mis-)behaviour of file.replace as described in #18612:
  1428. Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
  1429. an infinitely growing file as 'file.replace' didn't check beforehand
  1430. whether the changes had already been done to the file
  1431. # Case description:
  1432. The tested file contains 5 key=value pairs
  1433. The commented key=value pair #foo=bar should be changed to foo=salt
  1434. The comment char (#) in front of foo=bar should be removed
  1435. '''
  1436. test_name = 'test_replace_issue_18612_change_mid_line_with_comment'
  1437. path_in = os.path.join(
  1438. FILES, 'file.replace', '{0}.in'.format(test_name)
  1439. )
  1440. path_out = os.path.join(
  1441. FILES, 'file.replace', '{0}.out'.format(test_name)
  1442. )
  1443. path_test = os.path.join(base_dir, test_name)
  1444. # create test file based on initial template
  1445. shutil.copyfile(path_in, path_test)
  1446. ret = []
  1447. for x in range(0, 3):
  1448. ret.append(self.run_state('file.replace',
  1449. name=path_test, pattern='^#foo=bar($|(?=\r\n))', repl='foo=salt', append_if_not_found=True))
  1450. # ensure, the resulting file contains the expected lines
  1451. self.assertTrue(filecmp.cmp(path_test, path_out))
  1452. # ensure the initial file was properly backed up
  1453. self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
  1454. # ensure, all 'file.replace' runs reported success
  1455. for item in ret:
  1456. self.assertSaltTrueReturn(item)
  1457. @with_tempdir()
  1458. def test_replace_issue_18841_no_changes(self, base_dir):
  1459. '''
  1460. Test the (mis-)behaviour of file.replace as described in #18841:
  1461. Using file.replace in a way which shouldn't modify the file at all
  1462. results in changed mtime of the original file and a backup file being created.
  1463. # Case description
  1464. The tested file contains multiple lines
  1465. The tested file contains a line already matching the replacement (no change needed)
  1466. The tested file's content shouldn't change at all
  1467. The tested file's mtime shouldn't change at all
  1468. No backup file should be created
  1469. '''
  1470. test_name = 'test_replace_issue_18841_no_changes'
  1471. path_in = os.path.join(
  1472. FILES, 'file.replace', '{0}.in'.format(test_name)
  1473. )
  1474. path_test = os.path.join(base_dir, test_name)
  1475. # create test file based on initial template
  1476. shutil.copyfile(path_in, path_test)
  1477. # get (m|a)time of file
  1478. fstats_orig = os.stat(path_test)
  1479. # define how far we predate the file
  1480. age = 5*24*60*60
  1481. # set (m|a)time of file 5 days into the past
  1482. os.utime(path_test, (fstats_orig.st_mtime-age, fstats_orig.st_atime-age))
  1483. ret = self.run_state('file.replace',
  1484. name=path_test,
  1485. pattern='^hello world$',
  1486. repl='goodbye world',
  1487. show_changes=True,
  1488. flags=['IGNORECASE'],
  1489. backup=False
  1490. )
  1491. # get (m|a)time of file
  1492. fstats_post = os.stat(path_test)
  1493. # ensure, the file content didn't change
  1494. self.assertTrue(filecmp.cmp(path_in, path_test))
  1495. # ensure no backup file was created
  1496. self.assertFalse(os.path.exists(path_test + '.bak'))
  1497. # ensure the file's mtime didn't change
  1498. self.assertTrue(fstats_post.st_mtime, fstats_orig.st_mtime-age)
  1499. # ensure, all 'file.replace' runs reported success
  1500. self.assertSaltTrueReturn(ret)
  1501. def test_serialize(self):
  1502. '''
  1503. Test to ensure that file.serialize returns a data structure that's
  1504. both serialized and formatted properly
  1505. '''
  1506. path_test = os.path.join(TMP, 'test_serialize')
  1507. ret = self.run_state('file.serialize',
  1508. name=path_test,
  1509. dataset={'name': 'naive',
  1510. 'description': 'A basic test',
  1511. 'a_list': ['first_element', 'second_element'],
  1512. 'finally': 'the last item'},
  1513. formatter='json')
  1514. with salt.utils.files.fopen(path_test, 'rb') as fp_:
  1515. serialized_file = salt.utils.stringutils.to_unicode(fp_.read())
  1516. # The JSON serializer uses LF even on OSes where os.path.sep is CRLF.
  1517. expected_file = '\n'.join([
  1518. '{',
  1519. ' "a_list": [',
  1520. ' "first_element",',
  1521. ' "second_element"',
  1522. ' ],',
  1523. ' "description": "A basic test",',
  1524. ' "finally": "the last item",',
  1525. ' "name": "naive"',
  1526. '}',
  1527. '',
  1528. ])
  1529. self.assertEqual(serialized_file, expected_file)
  1530. @with_tempfile(create=False)
  1531. def test_serializer_deserializer_opts(self, name):
  1532. '''
  1533. Test the serializer_opts and deserializer_opts options
  1534. '''
  1535. data1 = {'foo': {'bar': '%(x)s'}}
  1536. data2 = {'foo': {'abc': 123}}
  1537. merged = {'foo': {'y': 'not_used', 'x': 'baz', 'abc': 123, 'bar': u'baz'}}
  1538. ret = self.run_state(
  1539. 'file.serialize',
  1540. name=name,
  1541. dataset=data1,
  1542. formatter='configparser',
  1543. deserializer_opts=[{'defaults': {'y': 'not_used'}}])
  1544. ret = ret[next(iter(ret))]
  1545. assert ret['result'], ret
  1546. # We should have warned about deserializer_opts being used when
  1547. # merge_if_exists was not set to True.
  1548. assert 'warnings' in ret
  1549. # Run with merge_if_exists, as well as serializer and deserializer opts
  1550. # deserializer opts will be used for string interpolation of the %(x)s
  1551. # that was written to the file with data1 (i.e. bar should become baz)
  1552. ret = self.run_state(
  1553. 'file.serialize',
  1554. name=name,
  1555. dataset=data2,
  1556. formatter='configparser',
  1557. merge_if_exists=True,
  1558. serializer_opts=[{'defaults': {'y': 'not_used'}}],
  1559. deserializer_opts=[{'defaults': {'x': 'baz'}}])
  1560. ret = ret[next(iter(ret))]
  1561. assert ret['result'], ret
  1562. with salt.utils.files.fopen(name) as fp_:
  1563. serialized_data = salt.serializers.configparser.deserialize(fp_)
  1564. # If this test fails, this debug logging will help tell us how the
  1565. # serialized data differs from what was serialized.
  1566. log.debug('serialized_data = %r', serialized_data)
  1567. log.debug('merged = %r', merged)
  1568. # serializing with a default of 'y' will add y = not_used into foo
  1569. assert serialized_data['foo']['y'] == merged['foo']['y']
  1570. # deserializing with default of x = baz will perform interpolation on %(x)s
  1571. # and bar will then = baz
  1572. assert serialized_data['foo']['bar'] == merged['foo']['bar']
  1573. @with_tempdir()
  1574. def test_replace_issue_18841_omit_backup(self, base_dir):
  1575. '''
  1576. Test the (mis-)behaviour of file.replace as described in #18841:
  1577. Using file.replace in a way which shouldn't modify the file at all
  1578. results in changed mtime of the original file and a backup file being created.
  1579. # Case description
  1580. The tested file contains multiple lines
  1581. The tested file contains a line already matching the replacement (no change needed)
  1582. The tested file's content shouldn't change at all
  1583. The tested file's mtime shouldn't change at all
  1584. No backup file should be created, although backup=False isn't explicitly defined
  1585. '''
  1586. test_name = 'test_replace_issue_18841_omit_backup'
  1587. path_in = os.path.join(
  1588. FILES, 'file.replace', '{0}.in'.format(test_name)
  1589. )
  1590. path_test = os.path.join(base_dir, test_name)
  1591. # create test file based on initial template
  1592. shutil.copyfile(path_in, path_test)
  1593. # get (m|a)time of file
  1594. fstats_orig = os.stat(path_test)
  1595. # define how far we predate the file
  1596. age = 5*24*60*60
  1597. # set (m|a)time of file 5 days into the past
  1598. os.utime(path_test, (fstats_orig.st_mtime-age, fstats_orig.st_atime-age))
  1599. ret = self.run_state('file.replace',
  1600. name=path_test,
  1601. pattern='^hello world$',
  1602. repl='goodbye world',
  1603. show_changes=True,
  1604. flags=['IGNORECASE']
  1605. )
  1606. # get (m|a)time of file
  1607. fstats_post = os.stat(path_test)
  1608. # ensure, the file content didn't change
  1609. self.assertTrue(filecmp.cmp(path_in, path_test))
  1610. # ensure no backup file was created
  1611. self.assertFalse(os.path.exists(path_test + '.bak'))
  1612. # ensure the file's mtime didn't change
  1613. self.assertTrue(fstats_post.st_mtime, fstats_orig.st_mtime-age)
  1614. # ensure, all 'file.replace' runs reported success
  1615. self.assertSaltTrueReturn(ret)
  1616. @with_tempfile()
  1617. def test_comment(self, name):
  1618. '''
  1619. file.comment
  1620. '''
  1621. # write a line to file
  1622. with salt.utils.files.fopen(name, 'w+') as fp_:
  1623. fp_.write('comment_me')
  1624. # Look for changes with test=True: return should be "None" at the first run
  1625. ret = self.run_state('file.comment', test=True, name=name, regex='^comment')
  1626. self.assertSaltNoneReturn(ret)
  1627. # comment once
  1628. ret = self.run_state('file.comment', name=name, regex='^comment')
  1629. # result is positive
  1630. self.assertSaltTrueReturn(ret)
  1631. # line is commented
  1632. with salt.utils.files.fopen(name, 'r') as fp_:
  1633. self.assertTrue(fp_.read().startswith('#comment'))
  1634. # comment twice
  1635. ret = self.run_state('file.comment', name=name, regex='^comment')
  1636. # result is still positive
  1637. self.assertSaltTrueReturn(ret)
  1638. # line is still commented
  1639. with salt.utils.files.fopen(name, 'r') as fp_:
  1640. self.assertTrue(fp_.read().startswith('#comment'))
  1641. # Test previously commented file returns "True" now and not "None" with test=True
  1642. ret = self.run_state('file.comment', test=True, name=name, regex='^comment')
  1643. self.assertSaltTrueReturn(ret)
  1644. @with_tempfile()
  1645. def test_test_comment(self, name):
  1646. '''
  1647. file.comment test interface
  1648. '''
  1649. with salt.utils.files.fopen(name, 'w+') as fp_:
  1650. fp_.write('comment_me')
  1651. ret = self.run_state(
  1652. 'file.comment', test=True, name=name, regex='.*comment.*',
  1653. )
  1654. with salt.utils.files.fopen(name, 'r') as fp_:
  1655. self.assertNotIn('#comment', fp_.read())
  1656. self.assertSaltNoneReturn(ret)
  1657. @with_tempfile()
  1658. def test_uncomment(self, name):
  1659. '''
  1660. file.uncomment
  1661. '''
  1662. with salt.utils.files.fopen(name, 'w+') as fp_:
  1663. fp_.write('#comment_me')
  1664. ret = self.run_state('file.uncomment', name=name, regex='^comment')
  1665. with salt.utils.files.fopen(name, 'r') as fp_:
  1666. self.assertNotIn('#comment', fp_.read())
  1667. self.assertSaltTrueReturn(ret)
  1668. @with_tempfile()
  1669. def test_test_uncomment(self, name):
  1670. '''
  1671. file.comment test interface
  1672. '''
  1673. with salt.utils.files.fopen(name, 'w+') as fp_:
  1674. fp_.write('#comment_me')
  1675. ret = self.run_state(
  1676. 'file.uncomment', test=True, name=name, regex='^comment.*'
  1677. )
  1678. with salt.utils.files.fopen(name, 'r') as fp_:
  1679. self.assertIn('#comment', fp_.read())
  1680. self.assertSaltNoneReturn(ret)
  1681. @with_tempfile()
  1682. def test_append(self, name):
  1683. '''
  1684. file.append
  1685. '''
  1686. with salt.utils.files.fopen(name, 'w+') as fp_:
  1687. fp_.write('#salty!')
  1688. ret = self.run_state('file.append', name=name, text='cheese')
  1689. with salt.utils.files.fopen(name, 'r') as fp_:
  1690. self.assertIn('cheese', fp_.read())
  1691. self.assertSaltTrueReturn(ret)
  1692. @with_tempfile()
  1693. def test_test_append(self, name):
  1694. '''
  1695. file.append test interface
  1696. '''
  1697. with salt.utils.files.fopen(name, 'w+') as fp_:
  1698. fp_.write('#salty!')
  1699. ret = self.run_state(
  1700. 'file.append', test=True, name=name, text='cheese'
  1701. )
  1702. with salt.utils.files.fopen(name, 'r') as fp_:
  1703. self.assertNotIn('cheese', fp_.read())
  1704. self.assertSaltNoneReturn(ret)
  1705. @with_tempdir()
  1706. def test_append_issue_1864_makedirs(self, base_dir):
  1707. '''
  1708. file.append but create directories if needed as an option, and create
  1709. the file if it doesn't exist
  1710. '''
  1711. fname = 'append_issue_1864_makedirs'
  1712. name = os.path.join(base_dir, fname)
  1713. # Non existing file get's touched
  1714. ret = self.run_state(
  1715. 'file.append', name=name, text='cheese', makedirs=True
  1716. )
  1717. self.assertSaltTrueReturn(ret)
  1718. # Nested directory and file get's touched
  1719. name = os.path.join(base_dir, 'issue_1864', fname)
  1720. ret = self.run_state(
  1721. 'file.append', name=name, text='cheese', makedirs=True
  1722. )
  1723. self.assertSaltTrueReturn(ret)
  1724. # Parent directory exists but file does not and makedirs is False
  1725. name = os.path.join(base_dir, 'issue_1864', fname + '2')
  1726. ret = self.run_state(
  1727. 'file.append', name=name, text='cheese'
  1728. )
  1729. self.assertSaltTrueReturn(ret)
  1730. self.assertTrue(os.path.isfile(name))
  1731. @with_tempdir()
  1732. def test_prepend_issue_27401_makedirs(self, base_dir):
  1733. '''
  1734. file.prepend but create directories if needed as an option, and create
  1735. the file if it doesn't exist
  1736. '''
  1737. fname = 'prepend_issue_27401'
  1738. name = os.path.join(base_dir, fname)
  1739. # Non existing file get's touched
  1740. ret = self.run_state(
  1741. 'file.prepend', name=name, text='cheese', makedirs=True
  1742. )
  1743. self.assertSaltTrueReturn(ret)
  1744. # Nested directory and file get's touched
  1745. name = os.path.join(base_dir, 'issue_27401', fname)
  1746. ret = self.run_state(
  1747. 'file.prepend', name=name, text='cheese', makedirs=True
  1748. )
  1749. self.assertSaltTrueReturn(ret)
  1750. # Parent directory exists but file does not and makedirs is False
  1751. name = os.path.join(base_dir, 'issue_27401', fname + '2')
  1752. ret = self.run_state(
  1753. 'file.prepend', name=name, text='cheese'
  1754. )
  1755. self.assertSaltTrueReturn(ret)
  1756. self.assertTrue(os.path.isfile(name))
  1757. @with_tempfile()
  1758. def test_touch(self, name):
  1759. '''
  1760. file.touch
  1761. '''
  1762. ret = self.run_state('file.touch', name=name)
  1763. self.assertTrue(os.path.isfile(name))
  1764. self.assertSaltTrueReturn(ret)
  1765. @with_tempfile(create=False)
  1766. def test_test_touch(self, name):
  1767. '''
  1768. file.touch test interface
  1769. '''
  1770. ret = self.run_state('file.touch', test=True, name=name)
  1771. self.assertFalse(os.path.isfile(name))
  1772. self.assertSaltNoneReturn(ret)
  1773. @with_tempdir()
  1774. def test_touch_directory(self, base_dir):
  1775. '''
  1776. file.touch a directory
  1777. '''
  1778. name = os.path.join(base_dir, 'touch_test_dir')
  1779. os.mkdir(name)
  1780. ret = self.run_state('file.touch', name=name)
  1781. self.assertSaltTrueReturn(ret)
  1782. self.assertTrue(os.path.isdir(name))
  1783. @with_tempdir()
  1784. def test_issue_2227_file_append(self, base_dir):
  1785. '''
  1786. Text to append includes a percent symbol
  1787. '''
  1788. # let's make use of existing state to create a file with contents to
  1789. # test against
  1790. tmp_file_append = os.path.join(base_dir, 'test.append')
  1791. self.run_state('file.touch', name=tmp_file_append)
  1792. self.run_state(
  1793. 'file.append',
  1794. name=tmp_file_append,
  1795. source='salt://testappend/firstif')
  1796. self.run_state(
  1797. 'file.append',
  1798. name=tmp_file_append,
  1799. source='salt://testappend/secondif')
  1800. # Now our real test
  1801. try:
  1802. ret = self.run_state(
  1803. 'file.append',
  1804. name=tmp_file_append,
  1805. text="HISTTIMEFORMAT='%F %T '")
  1806. self.assertSaltTrueReturn(ret)
  1807. with salt.utils.files.fopen(tmp_file_append, 'r') as fp_:
  1808. contents_pre = fp_.read()
  1809. # It should not append text again
  1810. ret = self.run_state(
  1811. 'file.append',
  1812. name=tmp_file_append,
  1813. text="HISTTIMEFORMAT='%F %T '")
  1814. self.assertSaltTrueReturn(ret)
  1815. with salt.utils.files.fopen(tmp_file_append, 'r') as fp_:
  1816. contents_post = fp_.read()
  1817. self.assertEqual(contents_pre, contents_post)
  1818. except AssertionError:
  1819. if os.path.exists(tmp_file_append):
  1820. shutil.copy(tmp_file_append, tmp_file_append + '.bak')
  1821. raise
  1822. @with_tempdir()
  1823. def test_issue_2401_file_comment(self, base_dir):
  1824. # Get a path to the temporary file
  1825. tmp_file = os.path.join(base_dir, 'issue-2041-comment.txt')
  1826. # Write some data to it
  1827. with salt.utils.files.fopen(tmp_file, 'w') as fp_:
  1828. fp_.write('hello\nworld\n')
  1829. # create the sls template
  1830. template_lines = [
  1831. '{0}:'.format(tmp_file),
  1832. ' file.comment:',
  1833. ' - regex: ^world'
  1834. ]
  1835. template = '\n'.join(template_lines)
  1836. try:
  1837. ret = self.run_function(
  1838. 'state.template_str', [template], timeout=120
  1839. )
  1840. self.assertSaltTrueReturn(ret)
  1841. self.assertNotInSaltComment('Pattern already commented', ret)
  1842. self.assertInSaltComment('Commented lines successfully', ret)
  1843. # This next time, it is already commented.
  1844. ret = self.run_function(
  1845. 'state.template_str', [template], timeout=120
  1846. )
  1847. self.assertSaltTrueReturn(ret)
  1848. self.assertInSaltComment('Pattern already commented', ret)
  1849. except AssertionError:
  1850. shutil.copy(tmp_file, tmp_file + '.bak')
  1851. raise
  1852. @with_tempdir()
  1853. def test_issue_2379_file_append(self, base_dir):
  1854. # Get a path to the temporary file
  1855. tmp_file = os.path.join(base_dir, 'issue-2379-file-append.txt')
  1856. # Write some data to it
  1857. with salt.utils.files.fopen(tmp_file, 'w') as fp_:
  1858. fp_.write(
  1859. 'hello\nworld\n' # Some junk
  1860. '#PermitRootLogin yes\n' # Commented text
  1861. '# PermitRootLogin yes\n' # Commented text with space
  1862. )
  1863. # create the sls template
  1864. template_lines = [
  1865. '{0}:'.format(tmp_file),
  1866. ' file.append:',
  1867. ' - text: PermitRootLogin yes'
  1868. ]
  1869. template = '\n'.join(template_lines)
  1870. try:
  1871. ret = self.run_function('state.template_str', [template])
  1872. self.assertSaltTrueReturn(ret)
  1873. self.assertInSaltComment('Appended 1 lines', ret)
  1874. except AssertionError:
  1875. shutil.copy(tmp_file, tmp_file + '.bak')
  1876. raise
  1877. @skipIf(IS_WINDOWS, 'Mode not available in Windows')
  1878. @with_tempdir(create=False)
  1879. @with_tempdir(create=False)
  1880. def test_issue_2726_mode_kwarg(self, dir1, dir2):
  1881. # Let's test for the wrong usage approach
  1882. bad_mode_kwarg_testfile = os.path.join(
  1883. dir1, 'bad_mode_kwarg', 'testfile'
  1884. )
  1885. bad_template = [
  1886. '{0}:'.format(bad_mode_kwarg_testfile),
  1887. ' file.recurse:',
  1888. ' - source: salt://testfile',
  1889. ' - mode: 644'
  1890. ]
  1891. ret = self.run_function(
  1892. 'state.template_str', [os.linesep.join(bad_template)]
  1893. )
  1894. self.assertSaltFalseReturn(ret)
  1895. self.assertInSaltComment(
  1896. '\'mode\' is not allowed in \'file.recurse\'. Please use '
  1897. '\'file_mode\' and \'dir_mode\'.',
  1898. ret
  1899. )
  1900. self.assertNotInSaltComment(
  1901. 'TypeError: managed() got multiple values for keyword '
  1902. 'argument \'mode\'',
  1903. ret
  1904. )
  1905. # Now, the correct usage approach
  1906. good_mode_kwargs_testfile = os.path.join(
  1907. dir2, 'good_mode_kwargs', 'testappend'
  1908. )
  1909. good_template = [
  1910. '{0}:'.format(good_mode_kwargs_testfile),
  1911. ' file.recurse:',
  1912. ' - source: salt://testappend',
  1913. ' - dir_mode: 744',
  1914. ' - file_mode: 644',
  1915. ]
  1916. ret = self.run_function(
  1917. 'state.template_str', [os.linesep.join(good_template)]
  1918. )
  1919. self.assertSaltTrueReturn(ret)
  1920. @with_tempdir()
  1921. def test_issue_8343_accumulated_require_in(self, base_dir):
  1922. template_path = os.path.join(TMP_STATE_TREE, 'issue-8343.sls')
  1923. testcase_filedest = os.path.join(base_dir, 'issue-8343.txt')
  1924. if os.path.exists(template_path):
  1925. os.remove(template_path)
  1926. if os.path.exists(testcase_filedest):
  1927. os.remove(testcase_filedest)
  1928. sls_template = [
  1929. '{0}:',
  1930. ' file.managed:',
  1931. ' - contents: |',
  1932. ' #',
  1933. '',
  1934. 'prepend-foo-accumulator-from-pillar:',
  1935. ' file.accumulated:',
  1936. ' - require_in:',
  1937. ' - file: prepend-foo-management',
  1938. ' - filename: {0}',
  1939. ' - text: |',
  1940. ' foo',
  1941. '',
  1942. 'append-foo-accumulator-from-pillar:',
  1943. ' file.accumulated:',
  1944. ' - require_in:',
  1945. ' - file: append-foo-management',
  1946. ' - filename: {0}',
  1947. ' - text: |',
  1948. ' bar',
  1949. '',
  1950. 'prepend-foo-management:',
  1951. ' file.blockreplace:',
  1952. ' - name: {0}',
  1953. ' - marker_start: "#-- start salt managed zonestart -- PLEASE, DO NOT EDIT"',
  1954. ' - marker_end: "#-- end salt managed zonestart --"',
  1955. " - content: ''",
  1956. ' - prepend_if_not_found: True',
  1957. " - backup: '.bak'",
  1958. ' - show_changes: True',
  1959. '',
  1960. 'append-foo-management:',
  1961. ' file.blockreplace:',
  1962. ' - name: {0}',
  1963. ' - marker_start: "#-- start salt managed zoneend -- PLEASE, DO NOT EDIT"',
  1964. ' - marker_end: "#-- end salt managed zoneend --"',
  1965. " - content: ''",
  1966. ' - append_if_not_found: True',
  1967. " - backup: '.bak2'",
  1968. ' - show_changes: True',
  1969. '']
  1970. with salt.utils.files.fopen(template_path, 'w') as fp_:
  1971. fp_.write(
  1972. os.linesep.join(sls_template).format(testcase_filedest))
  1973. ret = self.run_function('state.sls', mods='issue-8343')
  1974. for name, step in six.iteritems(ret):
  1975. self.assertSaltTrueReturn({name: step})
  1976. with salt.utils.files.fopen(testcase_filedest) as fp_:
  1977. contents = fp_.read().split(os.linesep)
  1978. expected = [
  1979. '#-- start salt managed zonestart -- PLEASE, DO NOT EDIT',
  1980. 'foo',
  1981. '#-- end salt managed zonestart --',
  1982. '#',
  1983. '#-- start salt managed zoneend -- PLEASE, DO NOT EDIT',
  1984. 'bar',
  1985. '#-- end salt managed zoneend --',
  1986. '']
  1987. self.assertEqual([salt.utils.stringutils.to_str(line) for line in expected], contents)
  1988. @with_tempdir()
  1989. def test_issue_11003_immutable_lazy_proxy_sum(self, base_dir):
  1990. # causes the Import-Module ServerManager error on Windows
  1991. template_path = os.path.join(TMP_STATE_TREE, 'issue-11003.sls')
  1992. testcase_filedest = os.path.join(base_dir, 'issue-11003.txt')
  1993. sls_template = [
  1994. 'a{0}:',
  1995. ' file.absent:',
  1996. ' - name: {0}',
  1997. '',
  1998. '{0}:',
  1999. ' file.managed:',
  2000. ' - contents: |',
  2001. ' #',
  2002. '',
  2003. 'test-acc1:',
  2004. ' file.accumulated:',
  2005. ' - require_in:',
  2006. ' - file: final',
  2007. ' - filename: {0}',
  2008. ' - text: |',
  2009. ' bar',
  2010. '',
  2011. 'test-acc2:',
  2012. ' file.accumulated:',
  2013. ' - watch_in:',
  2014. ' - file: final',
  2015. ' - filename: {0}',
  2016. ' - text: |',
  2017. ' baz',
  2018. '',
  2019. 'final:',
  2020. ' file.blockreplace:',
  2021. ' - name: {0}',
  2022. ' - marker_start: "#-- start managed zone PLEASE, DO NOT EDIT"',
  2023. ' - marker_end: "#-- end managed zone"',
  2024. ' - content: \'\'',
  2025. ' - append_if_not_found: True',
  2026. ' - show_changes: True'
  2027. ]
  2028. with salt.utils.files.fopen(template_path, 'w') as fp_:
  2029. fp_.write(os.linesep.join(sls_template).format(testcase_filedest))
  2030. ret = self.run_function('state.sls', mods='issue-11003')
  2031. for name, step in six.iteritems(ret):
  2032. self.assertSaltTrueReturn({name: step})
  2033. with salt.utils.files.fopen(testcase_filedest) as fp_:
  2034. contents = fp_.read().split(os.linesep)
  2035. begin = contents.index(
  2036. '#-- start managed zone PLEASE, DO NOT EDIT') + 1
  2037. end = contents.index('#-- end managed zone')
  2038. block_contents = contents[begin:end]
  2039. for item in ('', 'bar', 'baz'):
  2040. block_contents.remove(item)
  2041. self.assertEqual(block_contents, [])
  2042. @with_tempdir()
  2043. def test_issue_8947_utf8_sls(self, base_dir):
  2044. '''
  2045. Test some file operation with utf-8 characters on the sls
  2046. This is more generic than just a file test. Feel free to move
  2047. '''
  2048. self.maxDiff = None
  2049. korean_1 = '한국어 시험'
  2050. korean_2 = '첫 번째 행'
  2051. korean_3 = '마지막 행'
  2052. test_file = os.path.join(base_dir, '{0}.txt'.format(korean_1))
  2053. test_file_encoded = test_file
  2054. template_path = os.path.join(TMP_STATE_TREE, 'issue-8947.sls')
  2055. # create the sls template
  2056. template = textwrap.dedent('''\
  2057. some-utf8-file-create:
  2058. file.managed:
  2059. - name: {test_file}
  2060. - contents: {korean_1}
  2061. - makedirs: True
  2062. - replace: True
  2063. - show_diff: True
  2064. some-utf8-file-create2:
  2065. file.managed:
  2066. - name: {test_file}
  2067. - contents: |
  2068. {korean_2}
  2069. {korean_1}
  2070. {korean_3}
  2071. - replace: True
  2072. - show_diff: True
  2073. '''.format(**locals()))
  2074. if not salt.utils.platform.is_windows():
  2075. template += textwrap.dedent('''\
  2076. some-utf8-file-content-test:
  2077. cmd.run:
  2078. - name: 'cat "{test_file}"'
  2079. - require:
  2080. - file: some-utf8-file-create2
  2081. '''.format(**locals()))
  2082. # Save template file
  2083. with salt.utils.files.fopen(template_path, 'wb') as fp_:
  2084. fp_.write(salt.utils.stringutils.to_bytes(template))
  2085. try:
  2086. result = self.run_function('state.sls', mods='issue-8947')
  2087. if not isinstance(result, dict):
  2088. raise AssertionError(
  2089. ('Something went really wrong while testing this sls:'
  2090. ' {0}').format(repr(result))
  2091. )
  2092. # difflib produces different output on python 2.6 than on >=2.7
  2093. if sys.version_info < (2, 7):
  2094. diff = '--- \n+++ \n@@ -1,1 +1,3 @@\n'
  2095. else:
  2096. diff = '--- \n+++ \n@@ -1 +1,3 @@\n'
  2097. diff += (
  2098. '+첫 번째 행{0}'
  2099. ' 한국어 시험{0}'
  2100. '+마지막 행{0}'
  2101. ).format(os.linesep)
  2102. ret = {x.split('_|-')[1]: y for x, y in six.iteritems(result)}
  2103. # Confirm initial creation of file
  2104. self.assertEqual(
  2105. ret['some-utf8-file-create']['comment'],
  2106. 'File {0} updated'.format(test_file_encoded)
  2107. )
  2108. self.assertEqual(
  2109. ret['some-utf8-file-create']['changes'],
  2110. {'diff': 'New file'}
  2111. )
  2112. # Confirm file was modified and that the diff was as expected
  2113. self.assertEqual(
  2114. ret['some-utf8-file-create2']['comment'],
  2115. 'File {0} updated'.format(test_file_encoded)
  2116. )
  2117. self.assertEqual(
  2118. ret['some-utf8-file-create2']['changes'],
  2119. {'diff': diff}
  2120. )
  2121. if salt.utils.platform.is_windows():
  2122. import subprocess
  2123. import win32api
  2124. p = subprocess.Popen(
  2125. salt.utils.stringutils.to_str(
  2126. 'type {}'.format(win32api.GetShortPathName(test_file))),
  2127. shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  2128. p.poll()
  2129. out = p.stdout.read()
  2130. self.assertEqual(
  2131. out.decode('utf-8'),
  2132. os.linesep.join((korean_2, korean_1, korean_3)) + os.linesep
  2133. )
  2134. else:
  2135. self.assertEqual(
  2136. ret['some-utf8-file-content-test']['comment'],
  2137. 'Command "cat "{0}"" run'.format(
  2138. test_file_encoded
  2139. )
  2140. )
  2141. self.assertEqual(
  2142. ret['some-utf8-file-content-test']['changes']['stdout'],
  2143. '\n'.join((korean_2, korean_1, korean_3))
  2144. )
  2145. finally:
  2146. try:
  2147. os.remove(template_path)
  2148. except OSError:
  2149. pass
  2150. @skip_if_not_root
  2151. @skipIf(not HAS_PWD, "pwd not available. Skipping test")
  2152. @skipIf(not HAS_GRP, "grp not available. Skipping test")
  2153. @with_system_user_and_group('user12209', 'group12209',
  2154. on_existing='delete', delete=True)
  2155. @with_tempdir()
  2156. def test_issue_12209_follow_symlinks(self, tempdir, user, group):
  2157. '''
  2158. Ensure that symlinks are properly chowned when recursing (following
  2159. symlinks)
  2160. '''
  2161. # Make the directories for this test
  2162. onedir = os.path.join(tempdir, 'one')
  2163. twodir = os.path.join(tempdir, 'two')
  2164. os.mkdir(onedir)
  2165. os.symlink(onedir, twodir)
  2166. # Run the state
  2167. ret = self.run_state(
  2168. 'file.directory', name=tempdir, follow_symlinks=True,
  2169. user=user, group=group, recurse=['user', 'group']
  2170. )
  2171. self.assertSaltTrueReturn(ret)
  2172. # Double-check, in case state mis-reported a True result. Since we are
  2173. # following symlinks, we expect twodir to still be owned by root, but
  2174. # onedir should be owned by the 'issue12209' user.
  2175. onestats = os.stat(onedir)
  2176. twostats = os.lstat(twodir)
  2177. self.assertEqual(pwd.getpwuid(onestats.st_uid).pw_name, user)
  2178. self.assertEqual(pwd.getpwuid(twostats.st_uid).pw_name, 'root')
  2179. self.assertEqual(grp.getgrgid(onestats.st_gid).gr_name, group)
  2180. if salt.utils.path.which('id'):
  2181. root_group = self.run_function('user.primary_group', ['root'])
  2182. self.assertEqual(grp.getgrgid(twostats.st_gid).gr_name, root_group)
  2183. @skip_if_not_root
  2184. @skipIf(not HAS_PWD, "pwd not available. Skipping test")
  2185. @skipIf(not HAS_GRP, "grp not available. Skipping test")
  2186. @with_system_user_and_group('user12209', 'group12209',
  2187. on_existing='delete', delete=True)
  2188. @with_tempdir()
  2189. def test_issue_12209_no_follow_symlinks(self, tempdir, user, group):
  2190. '''
  2191. Ensure that symlinks are properly chowned when recursing (not following
  2192. symlinks)
  2193. '''
  2194. # Make the directories for this test
  2195. onedir = os.path.join(tempdir, 'one')
  2196. twodir = os.path.join(tempdir, 'two')
  2197. os.mkdir(onedir)
  2198. os.symlink(onedir, twodir)
  2199. # Run the state
  2200. ret = self.run_state(
  2201. 'file.directory', name=tempdir, follow_symlinks=False,
  2202. user=user, group=group, recurse=['user', 'group']
  2203. )
  2204. self.assertSaltTrueReturn(ret)
  2205. # Double-check, in case state mis-reported a True result. Since we
  2206. # are not following symlinks, we expect twodir to now be owned by
  2207. # the 'issue12209' user, just link onedir.
  2208. onestats = os.stat(onedir)
  2209. twostats = os.lstat(twodir)
  2210. self.assertEqual(pwd.getpwuid(onestats.st_uid).pw_name, user)
  2211. self.assertEqual(pwd.getpwuid(twostats.st_uid).pw_name, user)
  2212. self.assertEqual(grp.getgrgid(onestats.st_gid).gr_name, group)
  2213. self.assertEqual(grp.getgrgid(twostats.st_gid).gr_name, group)
  2214. @with_tempfile(create=False)
  2215. @with_tempfile()
  2216. def test_template_local_file(self, source, dest):
  2217. '''
  2218. Test a file.managed state with a local file as the source. Test both
  2219. with the file:// protocol designation prepended, and without it.
  2220. '''
  2221. with salt.utils.files.fopen(source, 'w') as fp_:
  2222. fp_.write('{{ foo }}\n')
  2223. for prefix in ('file://', ''):
  2224. ret = self.run_state(
  2225. 'file.managed',
  2226. name=dest,
  2227. source=prefix + source,
  2228. template='jinja',
  2229. context={'foo': 'Hello world!'}
  2230. )
  2231. self.assertSaltTrueReturn(ret)
  2232. @with_tempfile()
  2233. def test_template_local_file_noclobber(self, source):
  2234. '''
  2235. Test the case where a source file is in the minion's local filesystem,
  2236. and the source path is the same as the destination path.
  2237. '''
  2238. with salt.utils.files.fopen(source, 'w') as fp_:
  2239. fp_.write('{{ foo }}\n')
  2240. ret = self.run_state(
  2241. 'file.managed',
  2242. name=source,
  2243. source=source,
  2244. template='jinja',
  2245. context={'foo': 'Hello world!'}
  2246. )
  2247. self.assertSaltFalseReturn(ret)
  2248. self.assertIn(
  2249. ('Source file cannot be the same as destination'),
  2250. ret[next(iter(ret))]['comment'],
  2251. )
  2252. @with_tempfile(create=False)
  2253. @with_tempfile(create=False)
  2254. def test_issue_25250_force_copy_deletes(self, source, dest):
  2255. '''
  2256. ensure force option in copy state does not delete target file
  2257. '''
  2258. shutil.copyfile(os.path.join(FILES, 'hosts'), source)
  2259. shutil.copyfile(os.path.join(FILES, 'file/base/cheese'), dest)
  2260. self.run_state('file.copy', name=dest, source=source, force=True)
  2261. self.assertTrue(os.path.exists(dest))
  2262. self.assertTrue(filecmp.cmp(source, dest))
  2263. os.remove(source)
  2264. os.remove(dest)
  2265. @destructiveTest
  2266. @with_tempfile()
  2267. def test_file_copy_make_dirs(self, source):
  2268. '''
  2269. ensure make_dirs creates correct user perms
  2270. '''
  2271. shutil.copyfile(os.path.join(FILES, 'hosts'), source)
  2272. dest = os.path.join(TMP, 'dir1', 'dir2', 'copied_file.txt')
  2273. user = 'salt'
  2274. mode = '0644'
  2275. self.run_function('user.add', [user])
  2276. ret = self.run_state('file.copy', name=dest, source=source, user=user,
  2277. makedirs=True, mode=mode)
  2278. file_checks = [dest, os.path.join(TMP, 'dir1'), os.path.join(TMP, 'dir1', 'dir2')]
  2279. for check in file_checks:
  2280. user_check = self.run_function('file.get_user', [check])
  2281. mode_check = self.run_function('file.get_mode', [check])
  2282. assert user_check == user
  2283. assert salt.utils.files.normalize_mode(mode_check) == mode
  2284. def test_contents_pillar_with_pillar_list(self):
  2285. '''
  2286. This tests for any regressions for this issue:
  2287. https://github.com/saltstack/salt/issues/30934
  2288. '''
  2289. state_file = 'file_contents_pillar'
  2290. ret = self.run_function('state.sls', mods=state_file)
  2291. self.assertSaltTrueReturn(ret)
  2292. @skip_if_not_root
  2293. @skipIf(not HAS_PWD, "pwd not available. Skipping test")
  2294. @skipIf(not HAS_GRP, "grp not available. Skipping test")
  2295. @with_system_user_and_group('test_setuid_user', 'test_setuid_group',
  2296. on_existing='delete', delete=True)
  2297. def test_owner_after_setuid(self, user, group):
  2298. '''
  2299. Test to check file user/group after setting setuid or setgid.
  2300. Because Python os.chown() does reset the setuid/setgid to 0.
  2301. https://github.com/saltstack/salt/pull/45257
  2302. '''
  2303. # Desired configuration.
  2304. desired = {
  2305. 'file': os.path.join(TMP, 'file_with_setuid'),
  2306. 'user': user,
  2307. 'group': group,
  2308. 'mode': '4750'
  2309. }
  2310. # Run the state.
  2311. ret = self.run_state(
  2312. 'file.managed', name=desired['file'],
  2313. user=desired['user'], group=desired['group'], mode=desired['mode']
  2314. )
  2315. # Check result.
  2316. file_stat = os.stat(desired['file'])
  2317. result = {
  2318. 'user': pwd.getpwuid(file_stat.st_uid).pw_name,
  2319. 'group': grp.getgrgid(file_stat.st_gid).gr_name,
  2320. 'mode': oct(stat.S_IMODE(file_stat.st_mode))
  2321. }
  2322. self.assertSaltTrueReturn(ret)
  2323. self.assertEqual(desired['user'], result['user'])
  2324. self.assertEqual(desired['group'], result['group'])
  2325. self.assertEqual(desired['mode'], result['mode'].lstrip('0Oo'))
  2326. def test_binary_contents(self):
  2327. '''
  2328. This tests to ensure that binary contents do not cause a traceback.
  2329. '''
  2330. name = os.path.join(TMP, '1px.gif')
  2331. try:
  2332. ret = self.run_state(
  2333. 'file.managed',
  2334. name=name,
  2335. contents=BINARY_FILE)
  2336. self.assertSaltTrueReturn(ret)
  2337. finally:
  2338. try:
  2339. os.remove(name)
  2340. except OSError:
  2341. pass
  2342. @skip_if_not_root
  2343. @skipIf(not HAS_PWD, "pwd not available. Skipping test")
  2344. @skipIf(not HAS_GRP, "grp not available. Skipping test")
  2345. @with_system_user_and_group('user12209', 'group12209',
  2346. on_existing='delete', delete=True)
  2347. @with_tempdir()
  2348. def test_issue_48336_file_managed_mode_setuid(self, tempdir, user, group):
  2349. '''
  2350. Ensure that mode is correct with changing of ownership and group
  2351. symlinks)
  2352. '''
  2353. tempfile = os.path.join(tempdir, 'temp_file_issue_48336')
  2354. # Run the state
  2355. ret = self.run_state(
  2356. 'file.managed', name=tempfile,
  2357. user=user, group=group, mode='4750',
  2358. )
  2359. self.assertSaltTrueReturn(ret)
  2360. # Check that the owner and group are correct, and
  2361. # the mode is what we expect
  2362. temp_file_stats = os.stat(tempfile)
  2363. # Normalize the mode
  2364. temp_file_mode = six.text_type(oct(stat.S_IMODE(temp_file_stats.st_mode)))
  2365. temp_file_mode = salt.utils.files.normalize_mode(temp_file_mode)
  2366. self.assertEqual(temp_file_mode, '4750')
  2367. self.assertEqual(pwd.getpwuid(temp_file_stats.st_uid).pw_name, user)
  2368. self.assertEqual(grp.getgrgid(temp_file_stats.st_gid).gr_name, group)
  2369. @with_tempdir()
  2370. def test_issue_48557(self, tempdir):
  2371. tempfile = os.path.join(tempdir, 'temp_file_issue_48557')
  2372. with salt.utils.files.fopen(tempfile, 'wb') as fp:
  2373. fp.write(os.linesep.join([
  2374. 'test1',
  2375. 'test2',
  2376. 'test3',
  2377. '',
  2378. ]).encode('utf-8'))
  2379. ret = self.run_state('file.line',
  2380. name=tempfile,
  2381. after='test2',
  2382. mode='insert',
  2383. content='test4')
  2384. self.assertSaltTrueReturn(ret)
  2385. with salt.utils.files.fopen(tempfile, 'rb') as fp:
  2386. content = fp.read()
  2387. self.assertEqual(content, os.linesep.join([
  2388. 'test1',
  2389. 'test2',
  2390. 'test4',
  2391. 'test3',
  2392. '',
  2393. ]).encode('utf-8'))
  2394. @with_tempfile()
  2395. def test_issue_50221(self, name):
  2396. expected = 'abc{0}{0}{0}'.format(os.linesep)
  2397. ret = self.run_function(
  2398. 'pillar.get',
  2399. ['issue-50221']
  2400. )
  2401. assert ret == expected
  2402. ret = self.run_function(
  2403. 'state.apply',
  2404. ['issue-50221'],
  2405. pillar={
  2406. 'name': name
  2407. },
  2408. )
  2409. self.assertSaltTrueReturn(ret)
  2410. with salt.utils.files.fopen(name, 'r') as fp:
  2411. contents = fp.read()
  2412. assert contents == expected
  2413. def test_managed_file_issue_51208(self):
  2414. '''
  2415. Test to ensure we can handle a file with escaped double-quotes
  2416. '''
  2417. name = os.path.join(TMP, 'issue_51208.txt')
  2418. ret = self.run_state(
  2419. 'file.managed', name=name, source='salt://issue-51208/vimrc.stub'
  2420. )
  2421. src = os.path.join(BASE_FILES, 'issue-51208', 'vimrc.stub')
  2422. with salt.utils.files.fopen(src, 'r') as fp_:
  2423. master_data = fp_.read()
  2424. with salt.utils.files.fopen(name, 'r') as fp_:
  2425. minion_data = fp_.read()
  2426. self.assertEqual(master_data, minion_data)
  2427. self.assertSaltTrueReturn(ret)
  2428. class BlockreplaceTest(ModuleCase, SaltReturnAssertsMixin):
  2429. marker_start = '# start'
  2430. marker_end = '# end'
  2431. content = dedent(six.text_type('''\
  2432. Line 1 of block
  2433. Line 2 of block
  2434. '''))
  2435. without_block = dedent(six.text_type('''\
  2436. Hello world!
  2437. # comment here
  2438. '''))
  2439. with_non_matching_block = dedent(six.text_type('''\
  2440. Hello world!
  2441. # start
  2442. No match here
  2443. # end
  2444. # comment here
  2445. '''))
  2446. with_non_matching_block_and_marker_end_not_after_newline = dedent(six.text_type('''\
  2447. Hello world!
  2448. # start
  2449. No match here# end
  2450. # comment here
  2451. '''))
  2452. with_matching_block = dedent(six.text_type('''\
  2453. Hello world!
  2454. # start
  2455. Line 1 of block
  2456. Line 2 of block
  2457. # end
  2458. # comment here
  2459. '''))
  2460. with_matching_block_and_extra_newline = dedent(six.text_type('''\
  2461. Hello world!
  2462. # start
  2463. Line 1 of block
  2464. Line 2 of block
  2465. # end
  2466. # comment here
  2467. '''))
  2468. with_matching_block_and_marker_end_not_after_newline = dedent(six.text_type('''\
  2469. Hello world!
  2470. # start
  2471. Line 1 of block
  2472. Line 2 of block# end
  2473. # comment here
  2474. '''))
  2475. content_explicit_posix_newlines = ('Line 1 of block\n'
  2476. 'Line 2 of block\n')
  2477. content_explicit_windows_newlines = ('Line 1 of block\r\n'
  2478. 'Line 2 of block\r\n')
  2479. without_block_explicit_posix_newlines = ('Hello world!\n\n'
  2480. '# comment here\n')
  2481. without_block_explicit_windows_newlines = ('Hello world!\r\n\r\n'
  2482. '# comment here\r\n')
  2483. with_block_prepended_explicit_posix_newlines = ('# start\n'
  2484. 'Line 1 of block\n'
  2485. 'Line 2 of block\n'
  2486. '# end\n'
  2487. 'Hello world!\n\n'
  2488. '# comment here\n')
  2489. with_block_prepended_explicit_windows_newlines = ('# start\r\n'
  2490. 'Line 1 of block\r\n'
  2491. 'Line 2 of block\r\n'
  2492. '# end\r\n'
  2493. 'Hello world!\r\n\r\n'
  2494. '# comment here\r\n')
  2495. with_block_appended_explicit_posix_newlines = ('Hello world!\n\n'
  2496. '# comment here\n'
  2497. '# start\n'
  2498. 'Line 1 of block\n'
  2499. 'Line 2 of block\n'
  2500. '# end\n')
  2501. with_block_appended_explicit_windows_newlines = ('Hello world!\r\n\r\n'
  2502. '# comment here\r\n'
  2503. '# start\r\n'
  2504. 'Line 1 of block\r\n'
  2505. 'Line 2 of block\r\n'
  2506. '# end\r\n')
  2507. @staticmethod
  2508. def _write(dest, content):
  2509. with salt.utils.files.fopen(dest, 'wb') as fp_:
  2510. fp_.write(salt.utils.stringutils.to_bytes(content))
  2511. @staticmethod
  2512. def _read(src):
  2513. with salt.utils.files.fopen(src, 'rb') as fp_:
  2514. return salt.utils.stringutils.to_unicode(fp_.read())
  2515. @with_tempfile()
  2516. def test_prepend(self, name):
  2517. '''
  2518. Test blockreplace when prepend_if_not_found=True and block doesn't
  2519. exist in file.
  2520. '''
  2521. expected = self.marker_start + os.linesep + self.content + \
  2522. self.marker_end + os.linesep + self.without_block
  2523. # Pass 1: content ends in newline
  2524. self._write(name, self.without_block)
  2525. ret = self.run_state('file.blockreplace',
  2526. name=name,
  2527. content=self.content,
  2528. marker_start=self.marker_start,
  2529. marker_end=self.marker_end,
  2530. prepend_if_not_found=True)
  2531. self.assertSaltTrueReturn(ret)
  2532. self.assertTrue(ret[next(iter(ret))]['changes'])
  2533. self.assertEqual(self._read(name), expected)
  2534. # Pass 1a: Re-run state, no changes should be made
  2535. ret = self.run_state('file.blockreplace',
  2536. name=name,
  2537. content=self.content,
  2538. marker_start=self.marker_start,
  2539. marker_end=self.marker_end,
  2540. prepend_if_not_found=True)
  2541. self.assertSaltTrueReturn(ret)
  2542. self.assertFalse(ret[next(iter(ret))]['changes'])
  2543. self.assertEqual(self._read(name), expected)
  2544. # Pass 2: content does not end in newline
  2545. self._write(name, self.without_block)
  2546. ret = self.run_state('file.blockreplace',
  2547. name=name,
  2548. content=self.content.rstrip('\r\n'),
  2549. marker_start=self.marker_start,
  2550. marker_end=self.marker_end,
  2551. prepend_if_not_found=True)
  2552. self.assertSaltTrueReturn(ret)
  2553. self.assertTrue(ret[next(iter(ret))]['changes'])
  2554. self.assertEqual(self._read(name), expected)
  2555. # Pass 2a: Re-run state, no changes should be made
  2556. ret = self.run_state('file.blockreplace',
  2557. name=name,
  2558. content=self.content.rstrip('\r\n'),
  2559. marker_start=self.marker_start,
  2560. marker_end=self.marker_end,
  2561. prepend_if_not_found=True)
  2562. self.assertSaltTrueReturn(ret)
  2563. self.assertFalse(ret[next(iter(ret))]['changes'])
  2564. self.assertEqual(self._read(name), expected)
  2565. @with_tempfile()
  2566. def test_prepend_append_newline(self, name):
  2567. '''
  2568. Test blockreplace when prepend_if_not_found=True and block doesn't
  2569. exist in file. Test with append_newline explicitly set to True.
  2570. '''
  2571. # Pass 1: content ends in newline
  2572. expected = self.marker_start + os.linesep + self.content + \
  2573. os.linesep + self.marker_end + os.linesep + self.without_block
  2574. self._write(name, self.without_block)
  2575. ret = self.run_state('file.blockreplace',
  2576. name=name,
  2577. content=self.content,
  2578. marker_start=self.marker_start,
  2579. marker_end=self.marker_end,
  2580. prepend_if_not_found=True,
  2581. append_newline=True)
  2582. self.assertSaltTrueReturn(ret)
  2583. self.assertTrue(ret[next(iter(ret))]['changes'])
  2584. self.assertEqual(self._read(name), expected)
  2585. # Pass 1a: Re-run state, no changes should be made
  2586. ret = self.run_state('file.blockreplace',
  2587. name=name,
  2588. content=self.content,
  2589. marker_start=self.marker_start,
  2590. marker_end=self.marker_end,
  2591. prepend_if_not_found=True,
  2592. append_newline=True)
  2593. self.assertSaltTrueReturn(ret)
  2594. self.assertFalse(ret[next(iter(ret))]['changes'])
  2595. self.assertEqual(self._read(name), expected)
  2596. # Pass 2: content does not end in newline
  2597. expected = self.marker_start + os.linesep + self.content + \
  2598. self.marker_end + os.linesep + self.without_block
  2599. self._write(name, self.without_block)
  2600. ret = self.run_state('file.blockreplace',
  2601. name=name,
  2602. content=self.content.rstrip('\r\n'),
  2603. marker_start=self.marker_start,
  2604. marker_end=self.marker_end,
  2605. prepend_if_not_found=True,
  2606. append_newline=True)
  2607. self.assertSaltTrueReturn(ret)
  2608. self.assertTrue(ret[next(iter(ret))]['changes'])
  2609. self.assertEqual(self._read(name), expected)
  2610. # Pass 2a: Re-run state, no changes should be made
  2611. ret = self.run_state('file.blockreplace',
  2612. name=name,
  2613. content=self.content.rstrip('\r\n'),
  2614. marker_start=self.marker_start,
  2615. marker_end=self.marker_end,
  2616. prepend_if_not_found=True,
  2617. append_newline=True)
  2618. self.assertSaltTrueReturn(ret)
  2619. self.assertFalse(ret[next(iter(ret))]['changes'])
  2620. self.assertEqual(self._read(name), expected)
  2621. @with_tempfile()
  2622. def test_prepend_no_append_newline(self, name):
  2623. '''
  2624. Test blockreplace when prepend_if_not_found=True and block doesn't
  2625. exist in file. Test with append_newline explicitly set to False.
  2626. '''
  2627. # Pass 1: content ends in newline
  2628. expected = self.marker_start + os.linesep + self.content + \
  2629. self.marker_end + os.linesep + self.without_block
  2630. self._write(name, self.without_block)
  2631. ret = self.run_state('file.blockreplace',
  2632. name=name,
  2633. content=self.content,
  2634. marker_start=self.marker_start,
  2635. marker_end=self.marker_end,
  2636. prepend_if_not_found=True,
  2637. append_newline=False)
  2638. self.assertSaltTrueReturn(ret)
  2639. self.assertTrue(ret[next(iter(ret))]['changes'])
  2640. self.assertEqual(self._read(name), expected)
  2641. # Pass 1a: Re-run state, no changes should be made
  2642. ret = self.run_state('file.blockreplace',
  2643. name=name,
  2644. content=self.content,
  2645. marker_start=self.marker_start,
  2646. marker_end=self.marker_end,
  2647. prepend_if_not_found=True,
  2648. append_newline=False)
  2649. self.assertSaltTrueReturn(ret)
  2650. self.assertFalse(ret[next(iter(ret))]['changes'])
  2651. self.assertEqual(self._read(name), expected)
  2652. # Pass 2: content does not end in newline
  2653. expected = self.marker_start + os.linesep + \
  2654. self.content.rstrip('\r\n') + self.marker_end + os.linesep + \
  2655. self.without_block
  2656. self._write(name, self.without_block)
  2657. ret = self.run_state('file.blockreplace',
  2658. name=name,
  2659. content=self.content.rstrip('\r\n'),
  2660. marker_start=self.marker_start,
  2661. marker_end=self.marker_end,
  2662. prepend_if_not_found=True,
  2663. append_newline=False)
  2664. self.assertSaltTrueReturn(ret)
  2665. self.assertTrue(ret[next(iter(ret))]['changes'])
  2666. self.assertEqual(self._read(name), expected)
  2667. # Pass 2a: Re-run state, no changes should be made
  2668. ret = self.run_state('file.blockreplace',
  2669. name=name,
  2670. content=self.content.rstrip('\r\n'),
  2671. marker_start=self.marker_start,
  2672. marker_end=self.marker_end,
  2673. prepend_if_not_found=True,
  2674. append_newline=False)
  2675. self.assertSaltTrueReturn(ret)
  2676. self.assertFalse(ret[next(iter(ret))]['changes'])
  2677. self.assertEqual(self._read(name), expected)
  2678. @with_tempfile()
  2679. def test_append(self, name):
  2680. '''
  2681. Test blockreplace when append_if_not_found=True and block doesn't
  2682. exist in file.
  2683. '''
  2684. expected = self.without_block + self.marker_start + os.linesep + \
  2685. self.content + self.marker_end + os.linesep
  2686. # Pass 1: content ends in newline
  2687. self._write(name, self.without_block)
  2688. ret = self.run_state('file.blockreplace',
  2689. name=name,
  2690. content=self.content,
  2691. marker_start=self.marker_start,
  2692. marker_end=self.marker_end,
  2693. append_if_not_found=True)
  2694. self.assertSaltTrueReturn(ret)
  2695. self.assertTrue(ret[next(iter(ret))]['changes'])
  2696. self.assertEqual(self._read(name), expected)
  2697. # Pass 1a: Re-run state, no changes should be made
  2698. ret = self.run_state('file.blockreplace',
  2699. name=name,
  2700. content=self.content,
  2701. marker_start=self.marker_start,
  2702. marker_end=self.marker_end,
  2703. append_if_not_found=True)
  2704. self.assertSaltTrueReturn(ret)
  2705. self.assertFalse(ret[next(iter(ret))]['changes'])
  2706. self.assertEqual(self._read(name), expected)
  2707. # Pass 2: content does not end in newline
  2708. self._write(name, self.without_block)
  2709. ret = self.run_state('file.blockreplace',
  2710. name=name,
  2711. content=self.content.rstrip('\r\n'),
  2712. marker_start=self.marker_start,
  2713. marker_end=self.marker_end,
  2714. append_if_not_found=True)
  2715. self.assertSaltTrueReturn(ret)
  2716. self.assertTrue(ret[next(iter(ret))]['changes'])
  2717. self.assertEqual(self._read(name), expected)
  2718. # Pass 2a: Re-run state, no changes should be made
  2719. ret = self.run_state('file.blockreplace',
  2720. name=name,
  2721. content=self.content.rstrip('\r\n'),
  2722. marker_start=self.marker_start,
  2723. marker_end=self.marker_end,
  2724. append_if_not_found=True)
  2725. self.assertSaltTrueReturn(ret)
  2726. self.assertFalse(ret[next(iter(ret))]['changes'])
  2727. self.assertEqual(self._read(name), expected)
  2728. @with_tempfile()
  2729. def test_append_append_newline(self, name):
  2730. '''
  2731. Test blockreplace when append_if_not_found=True and block doesn't
  2732. exist in file. Test with append_newline explicitly set to True.
  2733. '''
  2734. # Pass 1: content ends in newline
  2735. expected = self.without_block + self.marker_start + os.linesep + \
  2736. self.content + os.linesep + self.marker_end + os.linesep
  2737. self._write(name, self.without_block)
  2738. ret = self.run_state('file.blockreplace',
  2739. name=name,
  2740. content=self.content,
  2741. marker_start=self.marker_start,
  2742. marker_end=self.marker_end,
  2743. append_if_not_found=True,
  2744. append_newline=True)
  2745. self.assertSaltTrueReturn(ret)
  2746. self.assertTrue(ret[next(iter(ret))]['changes'])
  2747. self.assertEqual(self._read(name), expected)
  2748. # Pass 1a: Re-run state, no changes should be made
  2749. ret = self.run_state('file.blockreplace',
  2750. name=name,
  2751. content=self.content,
  2752. marker_start=self.marker_start,
  2753. marker_end=self.marker_end,
  2754. append_if_not_found=True,
  2755. append_newline=True)
  2756. self.assertSaltTrueReturn(ret)
  2757. self.assertFalse(ret[next(iter(ret))]['changes'])
  2758. self.assertEqual(self._read(name), expected)
  2759. # Pass 2: content does not end in newline
  2760. expected = self.without_block + self.marker_start + os.linesep + \
  2761. self.content + self.marker_end + os.linesep
  2762. self._write(name, self.without_block)
  2763. ret = self.run_state('file.blockreplace',
  2764. name=name,
  2765. content=self.content.rstrip('\r\n'),
  2766. marker_start=self.marker_start,
  2767. marker_end=self.marker_end,
  2768. append_if_not_found=True,
  2769. append_newline=True)
  2770. self.assertSaltTrueReturn(ret)
  2771. self.assertTrue(ret[next(iter(ret))]['changes'])
  2772. self.assertEqual(self._read(name), expected)
  2773. # Pass 2a: Re-run state, no changes should be made
  2774. ret = self.run_state('file.blockreplace',
  2775. name=name,
  2776. content=self.content.rstrip('\r\n'),
  2777. marker_start=self.marker_start,
  2778. marker_end=self.marker_end,
  2779. append_if_not_found=True,
  2780. append_newline=True)
  2781. self.assertSaltTrueReturn(ret)
  2782. self.assertFalse(ret[next(iter(ret))]['changes'])
  2783. self.assertEqual(self._read(name), expected)
  2784. @with_tempfile()
  2785. def test_append_no_append_newline(self, name):
  2786. '''
  2787. Test blockreplace when append_if_not_found=True and block doesn't
  2788. exist in file. Test with append_newline explicitly set to False.
  2789. '''
  2790. # Pass 1: content ends in newline
  2791. expected = self.without_block + self.marker_start + os.linesep + \
  2792. self.content + self.marker_end + os.linesep
  2793. self._write(name, self.without_block)
  2794. ret = self.run_state('file.blockreplace',
  2795. name=name,
  2796. content=self.content,
  2797. marker_start=self.marker_start,
  2798. marker_end=self.marker_end,
  2799. append_if_not_found=True,
  2800. append_newline=False)
  2801. self.assertSaltTrueReturn(ret)
  2802. self.assertTrue(ret[next(iter(ret))]['changes'])
  2803. self.assertEqual(self._read(name), expected)
  2804. # Pass 1a: Re-run state, no changes should be made
  2805. ret = self.run_state('file.blockreplace',
  2806. name=name,
  2807. content=self.content,
  2808. marker_start=self.marker_start,
  2809. marker_end=self.marker_end,
  2810. append_if_not_found=True,
  2811. append_newline=False)
  2812. self.assertSaltTrueReturn(ret)
  2813. self.assertFalse(ret[next(iter(ret))]['changes'])
  2814. self.assertEqual(self._read(name), expected)
  2815. # Pass 2: content does not end in newline
  2816. expected = self.without_block + self.marker_start + os.linesep + \
  2817. self.content.rstrip('\r\n') + self.marker_end + os.linesep
  2818. self._write(name, self.without_block)
  2819. ret = self.run_state('file.blockreplace',
  2820. name=name,
  2821. content=self.content.rstrip('\r\n'),
  2822. marker_start=self.marker_start,
  2823. marker_end=self.marker_end,
  2824. append_if_not_found=True,
  2825. append_newline=False)
  2826. self.assertSaltTrueReturn(ret)
  2827. self.assertTrue(ret[next(iter(ret))]['changes'])
  2828. self.assertEqual(self._read(name), expected)
  2829. # Pass 2a: Re-run state, no changes should be made
  2830. ret = self.run_state('file.blockreplace',
  2831. name=name,
  2832. content=self.content.rstrip('\r\n'),
  2833. marker_start=self.marker_start,
  2834. marker_end=self.marker_end,
  2835. append_if_not_found=True,
  2836. append_newline=False)
  2837. self.assertSaltTrueReturn(ret)
  2838. self.assertFalse(ret[next(iter(ret))]['changes'])
  2839. self.assertEqual(self._read(name), expected)
  2840. @with_tempfile()
  2841. def test_prepend_auto_line_separator(self, name):
  2842. '''
  2843. This tests the line separator auto-detection when prepending the block
  2844. '''
  2845. # POSIX newlines to Windows newlines
  2846. self._write(name, self.without_block_explicit_windows_newlines)
  2847. ret = self.run_state('file.blockreplace',
  2848. name=name,
  2849. content=self.content_explicit_posix_newlines,
  2850. marker_start=self.marker_start,
  2851. marker_end=self.marker_end,
  2852. prepend_if_not_found=True)
  2853. self.assertSaltTrueReturn(ret)
  2854. self.assertTrue(ret[next(iter(ret))]['changes'])
  2855. self.assertEqual(
  2856. self._read(name),
  2857. self.with_block_prepended_explicit_windows_newlines)
  2858. # Re-run state, no changes should be made
  2859. ret = self.run_state('file.blockreplace',
  2860. name=name,
  2861. content=self.content_explicit_posix_newlines,
  2862. marker_start=self.marker_start,
  2863. marker_end=self.marker_end,
  2864. prepend_if_not_found=True)
  2865. self.assertSaltTrueReturn(ret)
  2866. self.assertFalse(ret[next(iter(ret))]['changes'])
  2867. self.assertEqual(
  2868. self._read(name),
  2869. self.with_block_prepended_explicit_windows_newlines)
  2870. # Windows newlines to POSIX newlines
  2871. self._write(name, self.without_block_explicit_posix_newlines)
  2872. ret = self.run_state('file.blockreplace',
  2873. name=name,
  2874. content=self.content_explicit_windows_newlines,
  2875. marker_start=self.marker_start,
  2876. marker_end=self.marker_end,
  2877. prepend_if_not_found=True)
  2878. self.assertSaltTrueReturn(ret)
  2879. self.assertTrue(ret[next(iter(ret))]['changes'])
  2880. self.assertEqual(
  2881. self._read(name),
  2882. self.with_block_prepended_explicit_posix_newlines)
  2883. # Re-run state, no changes should be made
  2884. ret = self.run_state('file.blockreplace',
  2885. name=name,
  2886. content=self.content_explicit_windows_newlines,
  2887. marker_start=self.marker_start,
  2888. marker_end=self.marker_end,
  2889. prepend_if_not_found=True)
  2890. self.assertSaltTrueReturn(ret)
  2891. self.assertFalse(ret[next(iter(ret))]['changes'])
  2892. self.assertEqual(
  2893. self._read(name),
  2894. self.with_block_prepended_explicit_posix_newlines)
  2895. @with_tempfile()
  2896. def test_append_auto_line_separator(self, name):
  2897. '''
  2898. This tests the line separator auto-detection when appending the block
  2899. '''
  2900. # POSIX newlines to Windows newlines
  2901. self._write(name, self.without_block_explicit_windows_newlines)
  2902. ret = self.run_state('file.blockreplace',
  2903. name=name,
  2904. content=self.content_explicit_posix_newlines,
  2905. marker_start=self.marker_start,
  2906. marker_end=self.marker_end,
  2907. append_if_not_found=True)
  2908. self.assertSaltTrueReturn(ret)
  2909. self.assertTrue(ret[next(iter(ret))]['changes'])
  2910. self.assertEqual(
  2911. self._read(name),
  2912. self.with_block_appended_explicit_windows_newlines)
  2913. # Re-run state, no changes should be made
  2914. ret = self.run_state('file.blockreplace',
  2915. name=name,
  2916. content=self.content_explicit_posix_newlines,
  2917. marker_start=self.marker_start,
  2918. marker_end=self.marker_end,
  2919. append_if_not_found=True)
  2920. self.assertSaltTrueReturn(ret)
  2921. self.assertFalse(ret[next(iter(ret))]['changes'])
  2922. self.assertEqual(
  2923. self._read(name),
  2924. self.with_block_appended_explicit_windows_newlines)
  2925. # Windows newlines to POSIX newlines
  2926. self._write(name, self.without_block_explicit_posix_newlines)
  2927. ret = self.run_state('file.blockreplace',
  2928. name=name,
  2929. content=self.content_explicit_windows_newlines,
  2930. marker_start=self.marker_start,
  2931. marker_end=self.marker_end,
  2932. append_if_not_found=True)
  2933. self.assertSaltTrueReturn(ret)
  2934. self.assertTrue(ret[next(iter(ret))]['changes'])
  2935. self.assertEqual(
  2936. self._read(name),
  2937. self.with_block_appended_explicit_posix_newlines)
  2938. # Re-run state, no changes should be made
  2939. ret = self.run_state('file.blockreplace',
  2940. name=name,
  2941. content=self.content_explicit_windows_newlines,
  2942. marker_start=self.marker_start,
  2943. marker_end=self.marker_end,
  2944. append_if_not_found=True)
  2945. self.assertSaltTrueReturn(ret)
  2946. self.assertFalse(ret[next(iter(ret))]['changes'])
  2947. self.assertEqual(
  2948. self._read(name),
  2949. self.with_block_appended_explicit_posix_newlines)
  2950. @with_tempfile()
  2951. def test_non_matching_block(self, name):
  2952. '''
  2953. Test blockreplace when block exists but its contents are not a
  2954. match.
  2955. '''
  2956. # Pass 1: content ends in newline
  2957. self._write(name, self.with_non_matching_block)
  2958. ret = self.run_state('file.blockreplace',
  2959. name=name,
  2960. content=self.content,
  2961. marker_start=self.marker_start,
  2962. marker_end=self.marker_end)
  2963. self.assertSaltTrueReturn(ret)
  2964. self.assertTrue(ret[next(iter(ret))]['changes'])
  2965. self.assertEqual(self._read(name), self.with_matching_block)
  2966. # Pass 1a: Re-run state, no changes should be made
  2967. ret = self.run_state('file.blockreplace',
  2968. name=name,
  2969. content=self.content,
  2970. marker_start=self.marker_start,
  2971. marker_end=self.marker_end)
  2972. self.assertSaltTrueReturn(ret)
  2973. self.assertFalse(ret[next(iter(ret))]['changes'])
  2974. self.assertEqual(self._read(name), self.with_matching_block)
  2975. # Pass 2: content does not end in newline
  2976. self._write(name, self.with_non_matching_block)
  2977. ret = self.run_state('file.blockreplace',
  2978. name=name,
  2979. content=self.content.rstrip('\r\n'),
  2980. marker_start=self.marker_start,
  2981. marker_end=self.marker_end)
  2982. self.assertSaltTrueReturn(ret)
  2983. self.assertTrue(ret[next(iter(ret))]['changes'])
  2984. self.assertEqual(self._read(name), self.with_matching_block)
  2985. # Pass 2a: Re-run state, no changes should be made
  2986. ret = self.run_state('file.blockreplace',
  2987. name=name,
  2988. content=self.content.rstrip('\r\n'),
  2989. marker_start=self.marker_start,
  2990. marker_end=self.marker_end)
  2991. self.assertSaltTrueReturn(ret)
  2992. self.assertFalse(ret[next(iter(ret))]['changes'])
  2993. self.assertEqual(self._read(name), self.with_matching_block)
  2994. @with_tempfile()
  2995. def test_non_matching_block_append_newline(self, name):
  2996. '''
  2997. Test blockreplace when block exists but its contents are not a
  2998. match. Test with append_newline explicitly set to True.
  2999. '''
  3000. # Pass 1: content ends in newline
  3001. self._write(name, self.with_non_matching_block)
  3002. ret = self.run_state('file.blockreplace',
  3003. name=name,
  3004. content=self.content,
  3005. marker_start=self.marker_start,
  3006. marker_end=self.marker_end,
  3007. append_newline=True)
  3008. self.assertSaltTrueReturn(ret)
  3009. self.assertTrue(ret[next(iter(ret))]['changes'])
  3010. self.assertEqual(
  3011. self._read(name),
  3012. self.with_matching_block_and_extra_newline)
  3013. # Pass 1a: Re-run state, no changes should be made
  3014. ret = self.run_state('file.blockreplace',
  3015. name=name,
  3016. content=self.content,
  3017. marker_start=self.marker_start,
  3018. marker_end=self.marker_end,
  3019. append_newline=True)
  3020. self.assertSaltTrueReturn(ret)
  3021. self.assertFalse(ret[next(iter(ret))]['changes'])
  3022. self.assertEqual(
  3023. self._read(name),
  3024. self.with_matching_block_and_extra_newline)
  3025. # Pass 2: content does not end in newline
  3026. self._write(name, self.with_non_matching_block)
  3027. ret = self.run_state('file.blockreplace',
  3028. name=name,
  3029. content=self.content.rstrip('\r\n'),
  3030. marker_start=self.marker_start,
  3031. marker_end=self.marker_end,
  3032. append_newline=True)
  3033. self.assertSaltTrueReturn(ret)
  3034. self.assertTrue(ret[next(iter(ret))]['changes'])
  3035. self.assertEqual(self._read(name), self.with_matching_block)
  3036. # Pass 2a: Re-run state, no changes should be made
  3037. ret = self.run_state('file.blockreplace',
  3038. name=name,
  3039. content=self.content.rstrip('\r\n'),
  3040. marker_start=self.marker_start,
  3041. marker_end=self.marker_end,
  3042. append_newline=True)
  3043. self.assertSaltTrueReturn(ret)
  3044. self.assertFalse(ret[next(iter(ret))]['changes'])
  3045. self.assertEqual(self._read(name), self.with_matching_block)
  3046. @with_tempfile()
  3047. def test_non_matching_block_no_append_newline(self, name):
  3048. '''
  3049. Test blockreplace when block exists but its contents are not a
  3050. match. Test with append_newline explicitly set to False.
  3051. '''
  3052. # Pass 1: content ends in newline
  3053. self._write(name, self.with_non_matching_block)
  3054. ret = self.run_state('file.blockreplace',
  3055. name=name,
  3056. content=self.content,
  3057. marker_start=self.marker_start,
  3058. marker_end=self.marker_end,
  3059. append_newline=False)
  3060. self.assertSaltTrueReturn(ret)
  3061. self.assertTrue(ret[next(iter(ret))]['changes'])
  3062. self.assertEqual(self._read(name), self.with_matching_block)
  3063. # Pass 1a: Re-run state, no changes should be made
  3064. ret = self.run_state('file.blockreplace',
  3065. name=name,
  3066. content=self.content,
  3067. marker_start=self.marker_start,
  3068. marker_end=self.marker_end,
  3069. append_newline=False)
  3070. self.assertSaltTrueReturn(ret)
  3071. self.assertFalse(ret[next(iter(ret))]['changes'])
  3072. self.assertEqual(self._read(name), self.with_matching_block)
  3073. # Pass 2: content does not end in newline
  3074. self._write(name, self.with_non_matching_block)
  3075. ret = self.run_state('file.blockreplace',
  3076. name=name,
  3077. content=self.content.rstrip('\r\n'),
  3078. marker_start=self.marker_start,
  3079. marker_end=self.marker_end,
  3080. append_newline=False)
  3081. self.assertSaltTrueReturn(ret)
  3082. self.assertTrue(ret[next(iter(ret))]['changes'])
  3083. self.assertEqual(
  3084. self._read(name),
  3085. self.with_matching_block_and_marker_end_not_after_newline)
  3086. # Pass 2a: Re-run state, no changes should be made
  3087. ret = self.run_state('file.blockreplace',
  3088. name=name,
  3089. content=self.content.rstrip('\r\n'),
  3090. marker_start=self.marker_start,
  3091. marker_end=self.marker_end,
  3092. append_newline=False)
  3093. self.assertSaltTrueReturn(ret)
  3094. self.assertFalse(ret[next(iter(ret))]['changes'])
  3095. self.assertEqual(
  3096. self._read(name),
  3097. self.with_matching_block_and_marker_end_not_after_newline)
  3098. @with_tempfile()
  3099. def test_non_matching_block_and_marker_not_after_newline(self, name):
  3100. '''
  3101. Test blockreplace when block exists but its contents are not a
  3102. match, and the marker_end is not directly preceded by a newline.
  3103. '''
  3104. # Pass 1: content ends in newline
  3105. self._write(
  3106. name,
  3107. self.with_non_matching_block_and_marker_end_not_after_newline)
  3108. ret = self.run_state('file.blockreplace',
  3109. name=name,
  3110. content=self.content,
  3111. marker_start=self.marker_start,
  3112. marker_end=self.marker_end)
  3113. self.assertSaltTrueReturn(ret)
  3114. self.assertTrue(ret[next(iter(ret))]['changes'])
  3115. self.assertEqual(self._read(name), self.with_matching_block)
  3116. # Pass 1a: Re-run state, no changes should be made
  3117. ret = self.run_state('file.blockreplace',
  3118. name=name,
  3119. content=self.content,
  3120. marker_start=self.marker_start,
  3121. marker_end=self.marker_end)
  3122. self.assertSaltTrueReturn(ret)
  3123. self.assertFalse(ret[next(iter(ret))]['changes'])
  3124. self.assertEqual(self._read(name), self.with_matching_block)
  3125. # Pass 2: content does not end in newline
  3126. self._write(
  3127. name,
  3128. self.with_non_matching_block_and_marker_end_not_after_newline)
  3129. ret = self.run_state('file.blockreplace',
  3130. name=name,
  3131. content=self.content.rstrip('\r\n'),
  3132. marker_start=self.marker_start,
  3133. marker_end=self.marker_end)
  3134. self.assertSaltTrueReturn(ret)
  3135. self.assertTrue(ret[next(iter(ret))]['changes'])
  3136. self.assertEqual(self._read(name), self.with_matching_block)
  3137. # Pass 2a: Re-run state, no changes should be made
  3138. ret = self.run_state('file.blockreplace',
  3139. name=name,
  3140. content=self.content.rstrip('\r\n'),
  3141. marker_start=self.marker_start,
  3142. marker_end=self.marker_end)
  3143. self.assertSaltTrueReturn(ret)
  3144. self.assertFalse(ret[next(iter(ret))]['changes'])
  3145. self.assertEqual(self._read(name), self.with_matching_block)
  3146. @with_tempfile()
  3147. def test_non_matching_block_and_marker_not_after_newline_append_newline(self, name):
  3148. '''
  3149. Test blockreplace when block exists but its contents are not a match,
  3150. and the marker_end is not directly preceded by a newline. Test with
  3151. append_newline explicitly set to True.
  3152. '''
  3153. # Pass 1: content ends in newline
  3154. self._write(
  3155. name,
  3156. self.with_non_matching_block_and_marker_end_not_after_newline)
  3157. ret = self.run_state('file.blockreplace',
  3158. name=name,
  3159. content=self.content,
  3160. marker_start=self.marker_start,
  3161. marker_end=self.marker_end,
  3162. append_newline=True)
  3163. self.assertSaltTrueReturn(ret)
  3164. self.assertTrue(ret[next(iter(ret))]['changes'])
  3165. self.assertEqual(
  3166. self._read(name),
  3167. self.with_matching_block_and_extra_newline)
  3168. # Pass 1a: Re-run state, no changes should be made
  3169. ret = self.run_state('file.blockreplace',
  3170. name=name,
  3171. content=self.content,
  3172. marker_start=self.marker_start,
  3173. marker_end=self.marker_end,
  3174. append_newline=True)
  3175. self.assertSaltTrueReturn(ret)
  3176. self.assertFalse(ret[next(iter(ret))]['changes'])
  3177. self.assertEqual(
  3178. self._read(name),
  3179. self.with_matching_block_and_extra_newline)
  3180. # Pass 2: content does not end in newline
  3181. self._write(
  3182. name,
  3183. self.with_non_matching_block_and_marker_end_not_after_newline)
  3184. ret = self.run_state('file.blockreplace',
  3185. name=name,
  3186. content=self.content.rstrip('\r\n'),
  3187. marker_start=self.marker_start,
  3188. marker_end=self.marker_end,
  3189. append_newline=True)
  3190. self.assertSaltTrueReturn(ret)
  3191. self.assertTrue(ret[next(iter(ret))]['changes'])
  3192. self.assertEqual(self._read(name), self.with_matching_block)
  3193. # Pass 2a: Re-run state, no changes should be made
  3194. ret = self.run_state('file.blockreplace',
  3195. name=name,
  3196. content=self.content.rstrip('\r\n'),
  3197. marker_start=self.marker_start,
  3198. marker_end=self.marker_end,
  3199. append_newline=True)
  3200. self.assertSaltTrueReturn(ret)
  3201. self.assertFalse(ret[next(iter(ret))]['changes'])
  3202. self.assertEqual(self._read(name), self.with_matching_block)
  3203. @with_tempfile()
  3204. def test_non_matching_block_and_marker_not_after_newline_no_append_newline(self, name):
  3205. '''
  3206. Test blockreplace when block exists but its contents are not a match,
  3207. and the marker_end is not directly preceded by a newline. Test with
  3208. append_newline explicitly set to False.
  3209. '''
  3210. # Pass 1: content ends in newline
  3211. self._write(
  3212. name,
  3213. self.with_non_matching_block_and_marker_end_not_after_newline)
  3214. ret = self.run_state('file.blockreplace',
  3215. name=name,
  3216. content=self.content,
  3217. marker_start=self.marker_start,
  3218. marker_end=self.marker_end,
  3219. append_newline=False)
  3220. self.assertSaltTrueReturn(ret)
  3221. self.assertTrue(ret[next(iter(ret))]['changes'])
  3222. self.assertEqual(self._read(name), self.with_matching_block)
  3223. # Pass 1a: Re-run state, no changes should be made
  3224. ret = self.run_state('file.blockreplace',
  3225. name=name,
  3226. content=self.content,
  3227. marker_start=self.marker_start,
  3228. marker_end=self.marker_end,
  3229. append_newline=False)
  3230. self.assertSaltTrueReturn(ret)
  3231. self.assertFalse(ret[next(iter(ret))]['changes'])
  3232. self.assertEqual(self._read(name), self.with_matching_block)
  3233. # Pass 2: content does not end in newline
  3234. self._write(
  3235. name,
  3236. self.with_non_matching_block_and_marker_end_not_after_newline)
  3237. ret = self.run_state('file.blockreplace',
  3238. name=name,
  3239. content=self.content.rstrip('\r\n'),
  3240. marker_start=self.marker_start,
  3241. marker_end=self.marker_end,
  3242. append_newline=False)
  3243. self.assertSaltTrueReturn(ret)
  3244. self.assertTrue(ret[next(iter(ret))]['changes'])
  3245. self.assertEqual(
  3246. self._read(name),
  3247. self.with_matching_block_and_marker_end_not_after_newline)
  3248. # Pass 2a: Re-run state, no changes should be made
  3249. ret = self.run_state('file.blockreplace',
  3250. name=name,
  3251. content=self.content.rstrip('\r\n'),
  3252. marker_start=self.marker_start,
  3253. marker_end=self.marker_end,
  3254. append_newline=False)
  3255. self.assertSaltTrueReturn(ret)
  3256. self.assertFalse(ret[next(iter(ret))]['changes'])
  3257. self.assertEqual(
  3258. self._read(name),
  3259. self.with_matching_block_and_marker_end_not_after_newline)
  3260. @with_tempfile()
  3261. def test_matching_block(self, name):
  3262. '''
  3263. Test blockreplace when block exists and its contents are a match. No
  3264. changes should be made.
  3265. '''
  3266. # Pass 1: content ends in newline
  3267. self._write(name, self.with_matching_block)
  3268. ret = self.run_state('file.blockreplace',
  3269. name=name,
  3270. content=self.content,
  3271. marker_start=self.marker_start,
  3272. marker_end=self.marker_end)
  3273. self.assertSaltTrueReturn(ret)
  3274. self.assertFalse(ret[next(iter(ret))]['changes'])
  3275. self.assertEqual(self._read(name), self.with_matching_block)
  3276. # Pass 1a: Re-run state, no changes should be made
  3277. ret = self.run_state('file.blockreplace',
  3278. name=name,
  3279. content=self.content,
  3280. marker_start=self.marker_start,
  3281. marker_end=self.marker_end)
  3282. self.assertSaltTrueReturn(ret)
  3283. self.assertFalse(ret[next(iter(ret))]['changes'])
  3284. self.assertEqual(self._read(name), self.with_matching_block)
  3285. # Pass 2: content does not end in newline
  3286. self._write(name, self.with_matching_block)
  3287. ret = self.run_state('file.blockreplace',
  3288. name=name,
  3289. content=self.content.rstrip('\r\n'),
  3290. marker_start=self.marker_start,
  3291. marker_end=self.marker_end)
  3292. self.assertSaltTrueReturn(ret)
  3293. self.assertFalse(ret[next(iter(ret))]['changes'])
  3294. self.assertEqual(self._read(name), self.with_matching_block)
  3295. # Pass 2a: Re-run state, no changes should be made
  3296. ret = self.run_state('file.blockreplace',
  3297. name=name,
  3298. content=self.content.rstrip('\r\n'),
  3299. marker_start=self.marker_start,
  3300. marker_end=self.marker_end)
  3301. self.assertSaltTrueReturn(ret)
  3302. self.assertFalse(ret[next(iter(ret))]['changes'])
  3303. self.assertEqual(self._read(name), self.with_matching_block)
  3304. @with_tempfile()
  3305. def test_matching_block_append_newline(self, name):
  3306. '''
  3307. Test blockreplace when block exists and its contents are a match. Test
  3308. with append_newline explicitly set to True. This will result in an
  3309. extra newline when the content ends in a newline, and will not when the
  3310. content does not end in a newline.
  3311. '''
  3312. # Pass 1: content ends in newline
  3313. self._write(name, self.with_matching_block)
  3314. ret = self.run_state('file.blockreplace',
  3315. name=name,
  3316. content=self.content,
  3317. marker_start=self.marker_start,
  3318. marker_end=self.marker_end,
  3319. append_newline=True)
  3320. self.assertSaltTrueReturn(ret)
  3321. self.assertTrue(ret[next(iter(ret))]['changes'])
  3322. self.assertEqual(
  3323. self._read(name),
  3324. self.with_matching_block_and_extra_newline)
  3325. # Pass 1a: Re-run state, no changes should be made
  3326. ret = self.run_state('file.blockreplace',
  3327. name=name,
  3328. content=self.content,
  3329. marker_start=self.marker_start,
  3330. marker_end=self.marker_end,
  3331. append_newline=True)
  3332. self.assertSaltTrueReturn(ret)
  3333. self.assertFalse(ret[next(iter(ret))]['changes'])
  3334. self.assertEqual(
  3335. self._read(name),
  3336. self.with_matching_block_and_extra_newline)
  3337. # Pass 2: content does not end in newline
  3338. self._write(name, self.with_matching_block)
  3339. ret = self.run_state('file.blockreplace',
  3340. name=name,
  3341. content=self.content.rstrip('\r\n'),
  3342. marker_start=self.marker_start,
  3343. marker_end=self.marker_end,
  3344. append_newline=True)
  3345. self.assertSaltTrueReturn(ret)
  3346. self.assertFalse(ret[next(iter(ret))]['changes'])
  3347. self.assertEqual(self._read(name), self.with_matching_block)
  3348. # Pass 2a: Re-run state, no changes should be made
  3349. ret = self.run_state('file.blockreplace',
  3350. name=name,
  3351. content=self.content.rstrip('\r\n'),
  3352. marker_start=self.marker_start,
  3353. marker_end=self.marker_end,
  3354. append_newline=True)
  3355. self.assertSaltTrueReturn(ret)
  3356. self.assertFalse(ret[next(iter(ret))]['changes'])
  3357. self.assertEqual(self._read(name), self.with_matching_block)
  3358. @with_tempfile()
  3359. def test_matching_block_no_append_newline(self, name):
  3360. '''
  3361. Test blockreplace when block exists and its contents are a match. Test
  3362. with append_newline explicitly set to False. This will result in the
  3363. marker_end not being directly preceded by a newline when the content
  3364. does not end in a newline.
  3365. '''
  3366. # Pass 1: content ends in newline
  3367. self._write(name, self.with_matching_block)
  3368. ret = self.run_state('file.blockreplace',
  3369. name=name,
  3370. content=self.content,
  3371. marker_start=self.marker_start,
  3372. marker_end=self.marker_end,
  3373. append_newline=False)
  3374. self.assertSaltTrueReturn(ret)
  3375. self.assertFalse(ret[next(iter(ret))]['changes'])
  3376. self.assertEqual(self._read(name), self.with_matching_block)
  3377. # Pass 1a: Re-run state, no changes should be made
  3378. ret = self.run_state('file.blockreplace',
  3379. name=name,
  3380. content=self.content,
  3381. marker_start=self.marker_start,
  3382. marker_end=self.marker_end,
  3383. append_newline=False)
  3384. self.assertSaltTrueReturn(ret)
  3385. self.assertFalse(ret[next(iter(ret))]['changes'])
  3386. self.assertEqual(self._read(name), self.with_matching_block)
  3387. # Pass 2: content does not end in newline
  3388. self._write(name, self.with_matching_block)
  3389. ret = self.run_state('file.blockreplace',
  3390. name=name,
  3391. content=self.content.rstrip('\r\n'),
  3392. marker_start=self.marker_start,
  3393. marker_end=self.marker_end,
  3394. append_newline=False)
  3395. self.assertSaltTrueReturn(ret)
  3396. self.assertTrue(ret[next(iter(ret))]['changes'])
  3397. self.assertEqual(
  3398. self._read(name),
  3399. self.with_matching_block_and_marker_end_not_after_newline)
  3400. # Pass 2a: Re-run state, no changes should be made
  3401. ret = self.run_state('file.blockreplace',
  3402. name=name,
  3403. content=self.content.rstrip('\r\n'),
  3404. marker_start=self.marker_start,
  3405. marker_end=self.marker_end,
  3406. append_newline=False)
  3407. self.assertSaltTrueReturn(ret)
  3408. self.assertFalse(ret[next(iter(ret))]['changes'])
  3409. self.assertEqual(
  3410. self._read(name),
  3411. self.with_matching_block_and_marker_end_not_after_newline)
  3412. @with_tempfile()
  3413. def test_matching_block_and_marker_not_after_newline(self, name):
  3414. '''
  3415. Test blockreplace when block exists and its contents are a match, but
  3416. the marker_end is not directly preceded by a newline.
  3417. '''
  3418. # Pass 1: content ends in newline
  3419. self._write(
  3420. name,
  3421. self.with_matching_block_and_marker_end_not_after_newline)
  3422. ret = self.run_state('file.blockreplace',
  3423. name=name,
  3424. content=self.content,
  3425. marker_start=self.marker_start,
  3426. marker_end=self.marker_end)
  3427. self.assertSaltTrueReturn(ret)
  3428. self.assertTrue(ret[next(iter(ret))]['changes'])
  3429. self.assertEqual(self._read(name), self.with_matching_block)
  3430. # Pass 1a: Re-run state, no changes should be made
  3431. ret = self.run_state('file.blockreplace',
  3432. name=name,
  3433. content=self.content,
  3434. marker_start=self.marker_start,
  3435. marker_end=self.marker_end)
  3436. self.assertSaltTrueReturn(ret)
  3437. self.assertFalse(ret[next(iter(ret))]['changes'])
  3438. self.assertEqual(self._read(name), self.with_matching_block)
  3439. # Pass 2: content does not end in newline
  3440. self._write(
  3441. name,
  3442. self.with_matching_block_and_marker_end_not_after_newline)
  3443. ret = self.run_state('file.blockreplace',
  3444. name=name,
  3445. content=self.content.rstrip('\r\n'),
  3446. marker_start=self.marker_start,
  3447. marker_end=self.marker_end)
  3448. self.assertSaltTrueReturn(ret)
  3449. self.assertTrue(ret[next(iter(ret))]['changes'])
  3450. self.assertEqual(self._read(name), self.with_matching_block)
  3451. # Pass 2a: Re-run state, no changes should be made
  3452. ret = self.run_state('file.blockreplace',
  3453. name=name,
  3454. content=self.content.rstrip('\r\n'),
  3455. marker_start=self.marker_start,
  3456. marker_end=self.marker_end)
  3457. self.assertSaltTrueReturn(ret)
  3458. self.assertFalse(ret[next(iter(ret))]['changes'])
  3459. self.assertEqual(self._read(name), self.with_matching_block)
  3460. @with_tempfile()
  3461. def test_matching_block_and_marker_not_after_newline_append_newline(self, name):
  3462. '''
  3463. Test blockreplace when block exists and its contents are a match, but
  3464. the marker_end is not directly preceded by a newline. Test with
  3465. append_newline explicitly set to True. This will result in an extra
  3466. newline when the content ends in a newline, and will not when the
  3467. content does not end in a newline.
  3468. '''
  3469. # Pass 1: content ends in newline
  3470. self._write(
  3471. name,
  3472. self.with_matching_block_and_marker_end_not_after_newline)
  3473. ret = self.run_state('file.blockreplace',
  3474. name=name,
  3475. content=self.content,
  3476. marker_start=self.marker_start,
  3477. marker_end=self.marker_end,
  3478. append_newline=True)
  3479. self.assertSaltTrueReturn(ret)
  3480. self.assertTrue(ret[next(iter(ret))]['changes'])
  3481. self.assertEqual(
  3482. self._read(name),
  3483. self.with_matching_block_and_extra_newline)
  3484. # Pass 1a: Re-run state, no changes should be made
  3485. ret = self.run_state('file.blockreplace',
  3486. name=name,
  3487. content=self.content,
  3488. marker_start=self.marker_start,
  3489. marker_end=self.marker_end,
  3490. append_newline=True)
  3491. self.assertSaltTrueReturn(ret)
  3492. self.assertFalse(ret[next(iter(ret))]['changes'])
  3493. self.assertEqual(
  3494. self._read(name),
  3495. self.with_matching_block_and_extra_newline)
  3496. # Pass 2: content does not end in newline
  3497. self._write(
  3498. name,
  3499. self.with_matching_block_and_marker_end_not_after_newline)
  3500. ret = self.run_state('file.blockreplace',
  3501. name=name,
  3502. content=self.content.rstrip('\r\n'),
  3503. marker_start=self.marker_start,
  3504. marker_end=self.marker_end,
  3505. append_newline=True)
  3506. self.assertSaltTrueReturn(ret)
  3507. self.assertTrue(ret[next(iter(ret))]['changes'])
  3508. self.assertEqual(self._read(name), self.with_matching_block)
  3509. # Pass 2a: Re-run state, no changes should be made
  3510. ret = self.run_state('file.blockreplace',
  3511. name=name,
  3512. content=self.content.rstrip('\r\n'),
  3513. marker_start=self.marker_start,
  3514. marker_end=self.marker_end,
  3515. append_newline=True)
  3516. self.assertSaltTrueReturn(ret)
  3517. self.assertFalse(ret[next(iter(ret))]['changes'])
  3518. self.assertEqual(self._read(name), self.with_matching_block)
  3519. @with_tempfile()
  3520. def test_matching_block_and_marker_not_after_newline_no_append_newline(self, name):
  3521. '''
  3522. Test blockreplace when block exists and its contents are a match, but
  3523. the marker_end is not directly preceded by a newline. Test with
  3524. append_newline explicitly set to False.
  3525. '''
  3526. # Pass 1: content ends in newline
  3527. self._write(
  3528. name,
  3529. self.with_matching_block_and_marker_end_not_after_newline)
  3530. ret = self.run_state('file.blockreplace',
  3531. name=name,
  3532. content=self.content,
  3533. marker_start=self.marker_start,
  3534. marker_end=self.marker_end,
  3535. append_newline=False)
  3536. self.assertSaltTrueReturn(ret)
  3537. self.assertTrue(ret[next(iter(ret))]['changes'])
  3538. self.assertEqual(self._read(name), self.with_matching_block)
  3539. # Pass 1a: Re-run state, no changes should be made
  3540. ret = self.run_state('file.blockreplace',
  3541. name=name,
  3542. content=self.content,
  3543. marker_start=self.marker_start,
  3544. marker_end=self.marker_end,
  3545. append_newline=False)
  3546. self.assertSaltTrueReturn(ret)
  3547. self.assertFalse(ret[next(iter(ret))]['changes'])
  3548. self.assertEqual(self._read(name), self.with_matching_block)
  3549. # Pass 2: content does not end in newline
  3550. self._write(
  3551. name,
  3552. self.with_matching_block_and_marker_end_not_after_newline)
  3553. ret = self.run_state('file.blockreplace',
  3554. name=name,
  3555. content=self.content.rstrip('\r\n'),
  3556. marker_start=self.marker_start,
  3557. marker_end=self.marker_end,
  3558. append_newline=False)
  3559. self.assertSaltTrueReturn(ret)
  3560. self.assertFalse(ret[next(iter(ret))]['changes'])
  3561. self.assertEqual(
  3562. self._read(name),
  3563. self.with_matching_block_and_marker_end_not_after_newline)
  3564. # Pass 2a: Re-run state, no changes should be made
  3565. ret = self.run_state('file.blockreplace',
  3566. name=name,
  3567. content=self.content.rstrip('\r\n'),
  3568. marker_start=self.marker_start,
  3569. marker_end=self.marker_end,
  3570. append_newline=False)
  3571. self.assertSaltTrueReturn(ret)
  3572. self.assertFalse(ret[next(iter(ret))]['changes'])
  3573. self.assertEqual(
  3574. self._read(name),
  3575. self.with_matching_block_and_marker_end_not_after_newline)
  3576. @with_tempfile()
  3577. def test_issue_49043(self, name):
  3578. ret = self.run_function(
  3579. 'state.sls',
  3580. mods='issue-49043',
  3581. pillar={'name': name},
  3582. )
  3583. log.error("ret = %s", repr(ret))
  3584. diff = '--- \n+++ \n@@ -0,0 +1,3 @@\n'
  3585. diff += dedent('''\
  3586. +#-- start managed zone --
  3587. +äöü
  3588. +#-- end managed zone --
  3589. ''')
  3590. job = 'file_|-somefile-blockreplace_|-{}_|-blockreplace'.format(name)
  3591. self.assertEqual(
  3592. ret[job]['changes']['diff'],
  3593. diff)
  3594. class RemoteFileTest(ModuleCase, SaltReturnAssertsMixin):
  3595. '''
  3596. Uses a local tornado webserver to test http(s) file.managed states with and
  3597. without skip_verify
  3598. '''
  3599. @classmethod
  3600. def setUpClass(cls):
  3601. cls.webserver = Webserver()
  3602. cls.webserver.start()
  3603. cls.source = cls.webserver.url('grail/scene33')
  3604. if IS_WINDOWS:
  3605. # CRLF vs LF causes a different hash on windows
  3606. cls.source_hash = '21438b3d5fd2c0028bcab92f7824dc69'
  3607. else:
  3608. cls.source_hash = 'd2feb3beb323c79fc7a0f44f1408b4a3'
  3609. @classmethod
  3610. def tearDownClass(cls):
  3611. cls.webserver.stop()
  3612. @with_tempfile(create=False)
  3613. def setUp(self, name): # pylint: disable=arguments-differ
  3614. self.name = name
  3615. def tearDown(self):
  3616. try:
  3617. os.remove(self.name)
  3618. except OSError as exc:
  3619. if exc.errno != errno.ENOENT:
  3620. raise exc
  3621. def run_state(self, *args, **kwargs):
  3622. ret = super(RemoteFileTest, self).run_state(*args, **kwargs)
  3623. log.debug('ret = %s', ret)
  3624. return ret
  3625. def test_file_managed_http_source_no_hash(self):
  3626. '''
  3627. Test a remote file with no hash
  3628. '''
  3629. ret = self.run_state('file.managed',
  3630. name=self.name,
  3631. source=self.source,
  3632. skip_verify=False)
  3633. # This should fail because no hash was provided
  3634. self.assertSaltFalseReturn(ret)
  3635. def test_file_managed_http_source(self):
  3636. '''
  3637. Test a remote file with no hash
  3638. '''
  3639. ret = self.run_state('file.managed',
  3640. name=self.name,
  3641. source=self.source,
  3642. source_hash=self.source_hash,
  3643. skip_verify=False)
  3644. self.assertSaltTrueReturn(ret)
  3645. def test_file_managed_http_source_skip_verify(self):
  3646. '''
  3647. Test a remote file using skip_verify
  3648. '''
  3649. ret = self.run_state('file.managed',
  3650. name=self.name,
  3651. source=self.source,
  3652. skip_verify=True)
  3653. self.assertSaltTrueReturn(ret)
  3654. def test_file_managed_keep_source_false_http(self):
  3655. '''
  3656. This test ensures that we properly clean the cached file if keep_source
  3657. is set to False, for source files using an http:// URL
  3658. '''
  3659. # Run the state
  3660. ret = self.run_state('file.managed',
  3661. name=self.name,
  3662. source=self.source,
  3663. source_hash=self.source_hash,
  3664. keep_source=False)
  3665. ret = ret[next(iter(ret))]
  3666. assert ret['result'] is True
  3667. # Now make sure that the file is not cached
  3668. result = self.run_function('cp.is_cached', [self.source])
  3669. assert result == '', 'File is still cached at {0}'.format(result)
  3670. @skipIf(not salt.utils.path.which('patch'), 'patch is not installed')
  3671. class PatchTest(ModuleCase, SaltReturnAssertsMixin):
  3672. def _check_patch_version(self, min_version):
  3673. '''
  3674. patch version check
  3675. '''
  3676. version = self.run_function('cmd.run', ['patch --version']).splitlines()[0]
  3677. version = version.split()[1]
  3678. if _LooseVersion(version) < _LooseVersion(min_version):
  3679. self.skipTest('Minimum patch version required: {0}. '
  3680. 'Patch version installed: {1}'.format(min_version, version))
  3681. @classmethod
  3682. def setUpClass(cls):
  3683. cls.webserver = Webserver()
  3684. cls.webserver.start()
  3685. cls.numbers_patch_name = 'numbers.patch'
  3686. cls.math_patch_name = 'math.patch'
  3687. cls.all_patch_name = 'all.patch'
  3688. cls.numbers_patch_template_name = cls.numbers_patch_name + '.jinja'
  3689. cls.math_patch_template_name = cls.math_patch_name + '.jinja'
  3690. cls.all_patch_template_name = cls.all_patch_name + '.jinja'
  3691. cls.numbers_patch_path = 'patches/' + cls.numbers_patch_name
  3692. cls.math_patch_path = 'patches/' + cls.math_patch_name
  3693. cls.all_patch_path = 'patches/' + cls.all_patch_name
  3694. cls.numbers_patch_template_path = \
  3695. 'patches/' + cls.numbers_patch_template_name
  3696. cls.math_patch_template_path = \
  3697. 'patches/' + cls.math_patch_template_name
  3698. cls.all_patch_template_path = \
  3699. 'patches/' + cls.all_patch_template_name
  3700. cls.numbers_patch = 'salt://' + cls.numbers_patch_path
  3701. cls.math_patch = 'salt://' + cls.math_patch_path
  3702. cls.all_patch = 'salt://' + cls.all_patch_path
  3703. cls.numbers_patch_template = 'salt://' + cls.numbers_patch_template_path
  3704. cls.math_patch_template = 'salt://' + cls.math_patch_template_path
  3705. cls.all_patch_template = 'salt://' + cls.all_patch_template_path
  3706. cls.numbers_patch_http = cls.webserver.url(cls.numbers_patch_path)
  3707. cls.math_patch_http = cls.webserver.url(cls.math_patch_path)
  3708. cls.all_patch_http = cls.webserver.url(cls.all_patch_path)
  3709. cls.numbers_patch_template_http = \
  3710. cls.webserver.url(cls.numbers_patch_template_path)
  3711. cls.math_patch_template_http = \
  3712. cls.webserver.url(cls.math_patch_template_path)
  3713. cls.all_patch_template_http = \
  3714. cls.webserver.url(cls.all_patch_template_path)
  3715. patches_dir = os.path.join(FILES, 'file', 'base', 'patches')
  3716. cls.numbers_patch_hash = salt.utils.hashutils.get_hash(
  3717. os.path.join(patches_dir, cls.numbers_patch_name)
  3718. )
  3719. cls.math_patch_hash = salt.utils.hashutils.get_hash(
  3720. os.path.join(patches_dir, cls.math_patch_name)
  3721. )
  3722. cls.all_patch_hash = salt.utils.hashutils.get_hash(
  3723. os.path.join(patches_dir, cls.all_patch_name)
  3724. )
  3725. cls.numbers_patch_template_hash = salt.utils.hashutils.get_hash(
  3726. os.path.join(patches_dir, cls.numbers_patch_template_name)
  3727. )
  3728. cls.math_patch_template_hash = salt.utils.hashutils.get_hash(
  3729. os.path.join(patches_dir, cls.math_patch_template_name)
  3730. )
  3731. cls.all_patch_template_hash = salt.utils.hashutils.get_hash(
  3732. os.path.join(patches_dir, cls.all_patch_template_name)
  3733. )
  3734. cls.context = {'two': 'two', 'ten': 10}
  3735. @classmethod
  3736. def tearDownClass(cls):
  3737. cls.webserver.stop()
  3738. def setUp(self):
  3739. '''
  3740. Create a new unpatched set of files
  3741. '''
  3742. self.base_dir = tempfile.mkdtemp(dir=TMP)
  3743. os.makedirs(os.path.join(self.base_dir, 'foo', 'bar'))
  3744. self.numbers_file = os.path.join(self.base_dir, 'foo', 'numbers.txt')
  3745. self.math_file = os.path.join(self.base_dir, 'foo', 'bar', 'math.txt')
  3746. with salt.utils.files.fopen(self.numbers_file, 'w') as fp_:
  3747. fp_.write(textwrap.dedent('''\
  3748. one
  3749. two
  3750. three
  3751. 1
  3752. 2
  3753. 3
  3754. '''))
  3755. with salt.utils.files.fopen(self.math_file, 'w') as fp_:
  3756. fp_.write(textwrap.dedent('''\
  3757. Five plus five is ten
  3758. Four squared is sixteen
  3759. '''))
  3760. self.addCleanup(shutil.rmtree, self.base_dir, ignore_errors=True)
  3761. def test_patch_single_file(self):
  3762. '''
  3763. Test file.patch using a patch applied to a single file
  3764. '''
  3765. ret = self.run_state(
  3766. 'file.patch',
  3767. name=self.numbers_file,
  3768. source=self.numbers_patch,
  3769. )
  3770. self.assertSaltTrueReturn(ret)
  3771. ret = ret[next(iter(ret))]
  3772. self.assertEqual(ret['comment'], 'Patch successfully applied')
  3773. # Re-run the state, should succeed and there should be a message about
  3774. # a partially-applied hunk.
  3775. ret = self.run_state(
  3776. 'file.patch',
  3777. name=self.numbers_file,
  3778. source=self.numbers_patch,
  3779. )
  3780. self.assertSaltTrueReturn(ret)
  3781. ret = ret[next(iter(ret))]
  3782. self.assertEqual(ret['comment'], 'Patch was already applied')
  3783. self.assertEqual(ret['changes'], {})
  3784. def test_patch_directory(self):
  3785. '''
  3786. Test file.patch using a patch applied to a directory, with changes
  3787. spanning multiple files.
  3788. '''
  3789. self._check_patch_version('2.6')
  3790. ret = self.run_state(
  3791. 'file.patch',
  3792. name=self.base_dir,
  3793. source=self.all_patch,
  3794. strip=1,
  3795. )
  3796. self.assertSaltTrueReturn(ret)
  3797. ret = ret[next(iter(ret))]
  3798. self.assertEqual(ret['comment'], 'Patch successfully applied')
  3799. # Re-run the state, should succeed and there should be a message about
  3800. # a partially-applied hunk.
  3801. ret = self.run_state(
  3802. 'file.patch',
  3803. name=self.base_dir,
  3804. source=self.all_patch,
  3805. strip=1,
  3806. )
  3807. self.assertSaltTrueReturn(ret)
  3808. ret = ret[next(iter(ret))]
  3809. self.assertEqual(ret['comment'], 'Patch was already applied')
  3810. self.assertEqual(ret['changes'], {})
  3811. def test_patch_strip_parsing(self):
  3812. '''
  3813. Test that we successfuly parse -p/--strip when included in the options
  3814. '''
  3815. self._check_patch_version('2.6')
  3816. # Run the state using -p1
  3817. ret = self.run_state(
  3818. 'file.patch',
  3819. name=self.base_dir,
  3820. source=self.all_patch,
  3821. options='-p1',
  3822. )
  3823. self.assertSaltTrueReturn(ret)
  3824. ret = ret[next(iter(ret))]
  3825. self.assertEqual(ret['comment'], 'Patch successfully applied')
  3826. # Re-run the state using --strip=1
  3827. ret = self.run_state(
  3828. 'file.patch',
  3829. name=self.base_dir,
  3830. source=self.all_patch,
  3831. options='--strip=1',
  3832. )
  3833. self.assertSaltTrueReturn(ret)
  3834. ret = ret[next(iter(ret))]
  3835. self.assertEqual(ret['comment'], 'Patch was already applied')
  3836. self.assertEqual(ret['changes'], {})
  3837. # Re-run the state using --strip 1
  3838. ret = self.run_state(
  3839. 'file.patch',
  3840. name=self.base_dir,
  3841. source=self.all_patch,
  3842. options='--strip 1',
  3843. )
  3844. self.assertSaltTrueReturn(ret)
  3845. ret = ret[next(iter(ret))]
  3846. self.assertEqual(ret['comment'], 'Patch was already applied')
  3847. self.assertEqual(ret['changes'], {})
  3848. def test_patch_saltenv(self):
  3849. '''
  3850. Test that we attempt to download the patch from a non-base saltenv
  3851. '''
  3852. # This state will fail because we don't have a patch file in that
  3853. # environment, but that is OK, we just want to test that we're looking
  3854. # in an environment other than base.
  3855. ret = self.run_state(
  3856. 'file.patch',
  3857. name=self.math_file,
  3858. source=self.math_patch,
  3859. saltenv='prod',
  3860. )
  3861. self.assertSaltFalseReturn(ret)
  3862. ret = ret[next(iter(ret))]
  3863. self.assertEqual(
  3864. ret['comment'],
  3865. "Source file {0} not found in saltenv 'prod'".format(self.math_patch)
  3866. )
  3867. def test_patch_single_file_failure(self):
  3868. '''
  3869. Test file.patch using a patch applied to a single file. This tests a
  3870. failed patch.
  3871. '''
  3872. # Empty the file to ensure that the patch doesn't apply cleanly
  3873. with salt.utils.files.fopen(self.numbers_file, 'w'):
  3874. pass
  3875. ret = self.run_state(
  3876. 'file.patch',
  3877. name=self.numbers_file,
  3878. source=self.numbers_patch,
  3879. )
  3880. self.assertSaltFalseReturn(ret)
  3881. ret = ret[next(iter(ret))]
  3882. self.assertIn('Patch would not apply cleanly', ret['comment'])
  3883. # Test the reject_file option and ensure that the rejects are written
  3884. # to the path specified.
  3885. reject_file = salt.utils.files.mkstemp()
  3886. ret = self.run_state(
  3887. 'file.patch',
  3888. name=self.numbers_file,
  3889. source=self.numbers_patch,
  3890. reject_file=reject_file,
  3891. strip=1,
  3892. )
  3893. self.assertSaltFalseReturn(ret)
  3894. ret = ret[next(iter(ret))]
  3895. self.assertIn('Patch would not apply cleanly', ret['comment'])
  3896. self.assertIn(
  3897. 'saving rejects to file {0}'.format(reject_file),
  3898. ret['comment']
  3899. )
  3900. def test_patch_directory_failure(self):
  3901. '''
  3902. Test file.patch using a patch applied to a directory, with changes
  3903. spanning multiple files.
  3904. '''
  3905. # Empty the file to ensure that the patch doesn't apply
  3906. with salt.utils.files.fopen(self.math_file, 'w'):
  3907. pass
  3908. ret = self.run_state(
  3909. 'file.patch',
  3910. name=self.base_dir,
  3911. source=self.all_patch,
  3912. strip=1,
  3913. )
  3914. self.assertSaltFalseReturn(ret)
  3915. ret = ret[next(iter(ret))]
  3916. self.assertIn('Patch would not apply cleanly', ret['comment'])
  3917. # Test the reject_file option and ensure that the rejects are written
  3918. # to the path specified.
  3919. reject_file = salt.utils.files.mkstemp()
  3920. ret = self.run_state(
  3921. 'file.patch',
  3922. name=self.base_dir,
  3923. source=self.all_patch,
  3924. reject_file=reject_file,
  3925. strip=1,
  3926. )
  3927. self.assertSaltFalseReturn(ret)
  3928. ret = ret[next(iter(ret))]
  3929. self.assertIn('Patch would not apply cleanly', ret['comment'])
  3930. self.assertIn(
  3931. 'saving rejects to file {0}'.format(reject_file),
  3932. ret['comment']
  3933. )
  3934. def test_patch_single_file_remote_source(self):
  3935. '''
  3936. Test file.patch using a patch applied to a single file, with the patch
  3937. coming from a remote source.
  3938. '''
  3939. # Try without a source_hash and without skip_verify=True, this should
  3940. # fail with a message about the source_hash
  3941. ret = self.run_state(
  3942. 'file.patch',
  3943. name=self.math_file,
  3944. source=self.math_patch_http,
  3945. )
  3946. self.assertSaltFalseReturn(ret)
  3947. ret = ret[next(iter(ret))]
  3948. self.assertIn('Unable to verify upstream hash', ret['comment'])
  3949. # Re-run the state with a source hash, it should now succeed
  3950. ret = self.run_state(
  3951. 'file.patch',
  3952. name=self.math_file,
  3953. source=self.math_patch_http,
  3954. source_hash=self.math_patch_hash,
  3955. )
  3956. self.assertSaltTrueReturn(ret)
  3957. ret = ret[next(iter(ret))]
  3958. self.assertEqual(ret['comment'], 'Patch successfully applied')
  3959. # Re-run again, this time with no hash and skip_verify=True to test
  3960. # skipping hash verification
  3961. ret = self.run_state(
  3962. 'file.patch',
  3963. name=self.math_file,
  3964. source=self.math_patch_http,
  3965. skip_verify=True,
  3966. )
  3967. self.assertSaltTrueReturn(ret)
  3968. ret = ret[next(iter(ret))]
  3969. self.assertEqual(ret['comment'], 'Patch was already applied')
  3970. self.assertEqual(ret['changes'], {})
  3971. def test_patch_directory_remote_source(self):
  3972. '''
  3973. Test file.patch using a patch applied to a directory, with changes
  3974. spanning multiple files, and the patch file coming from a remote
  3975. source.
  3976. '''
  3977. self._check_patch_version('2.6')
  3978. # Try without a source_hash and without skip_verify=True, this should
  3979. # fail with a message about the source_hash
  3980. ret = self.run_state(
  3981. 'file.patch',
  3982. name=self.base_dir,
  3983. source=self.all_patch_http,
  3984. strip=1,
  3985. )
  3986. self.assertSaltFalseReturn(ret)
  3987. ret = ret[next(iter(ret))]
  3988. self.assertIn('Unable to verify upstream hash', ret['comment'])
  3989. # Re-run the state with a source hash, it should now succeed
  3990. ret = self.run_state(
  3991. 'file.patch',
  3992. name=self.base_dir,
  3993. source=self.all_patch_http,
  3994. source_hash=self.all_patch_hash,
  3995. strip=1,
  3996. )
  3997. self.assertSaltTrueReturn(ret)
  3998. ret = ret[next(iter(ret))]
  3999. self.assertEqual(ret['comment'], 'Patch successfully applied')
  4000. # Re-run again, this time with no hash and skip_verify=True to test
  4001. # skipping hash verification
  4002. ret = self.run_state(
  4003. 'file.patch',
  4004. name=self.base_dir,
  4005. source=self.all_patch_http,
  4006. strip=1,
  4007. skip_verify=True,
  4008. )
  4009. self.assertSaltTrueReturn(ret)
  4010. ret = ret[next(iter(ret))]
  4011. self.assertEqual(ret['comment'], 'Patch was already applied')
  4012. self.assertEqual(ret['changes'], {})
  4013. def test_patch_single_file_template(self):
  4014. '''
  4015. Test file.patch using a patch applied to a single file, with jinja
  4016. templating applied to the patch file.
  4017. '''
  4018. ret = self.run_state(
  4019. 'file.patch',
  4020. name=self.numbers_file,
  4021. source=self.numbers_patch_template,
  4022. template='jinja',
  4023. context=self.context,
  4024. )
  4025. self.assertSaltTrueReturn(ret)
  4026. ret = ret[next(iter(ret))]
  4027. self.assertEqual(ret['comment'], 'Patch successfully applied')
  4028. # Re-run the state, should succeed and there should be a message about
  4029. # a partially-applied hunk.
  4030. ret = self.run_state(
  4031. 'file.patch',
  4032. name=self.numbers_file,
  4033. source=self.numbers_patch_template,
  4034. template='jinja',
  4035. context=self.context,
  4036. )
  4037. self.assertSaltTrueReturn(ret)
  4038. ret = ret[next(iter(ret))]
  4039. self.assertEqual(ret['comment'], 'Patch was already applied')
  4040. self.assertEqual(ret['changes'], {})
  4041. def test_patch_directory_template(self):
  4042. '''
  4043. Test file.patch using a patch applied to a directory, with changes
  4044. spanning multiple files, and with jinja templating applied to the patch
  4045. file.
  4046. '''
  4047. self._check_patch_version('2.6')
  4048. ret = self.run_state(
  4049. 'file.patch',
  4050. name=self.base_dir,
  4051. source=self.all_patch_template,
  4052. template='jinja',
  4053. context=self.context,
  4054. strip=1,
  4055. )
  4056. self.assertSaltTrueReturn(ret)
  4057. ret = ret[next(iter(ret))]
  4058. self.assertEqual(ret['comment'], 'Patch successfully applied')
  4059. # Re-run the state, should succeed and there should be a message about
  4060. # a partially-applied hunk.
  4061. ret = self.run_state(
  4062. 'file.patch',
  4063. name=self.base_dir,
  4064. source=self.all_patch_template,
  4065. template='jinja',
  4066. context=self.context,
  4067. strip=1,
  4068. )
  4069. self.assertSaltTrueReturn(ret)
  4070. ret = ret[next(iter(ret))]
  4071. self.assertEqual(ret['comment'], 'Patch was already applied')
  4072. self.assertEqual(ret['changes'], {})
  4073. def test_patch_single_file_remote_source_template(self):
  4074. '''
  4075. Test file.patch using a patch applied to a single file, with the patch
  4076. coming from a remote source.
  4077. '''
  4078. # Try without a source_hash and without skip_verify=True, this should
  4079. # fail with a message about the source_hash
  4080. ret = self.run_state(
  4081. 'file.patch',
  4082. name=self.math_file,
  4083. source=self.math_patch_template_http,
  4084. template='jinja',
  4085. context=self.context,
  4086. )
  4087. self.assertSaltFalseReturn(ret)
  4088. ret = ret[next(iter(ret))]
  4089. self.assertIn('Unable to verify upstream hash', ret['comment'])
  4090. # Re-run the state with a source hash, it should now succeed
  4091. ret = self.run_state(
  4092. 'file.patch',
  4093. name=self.math_file,
  4094. source=self.math_patch_template_http,
  4095. source_hash=self.math_patch_template_hash,
  4096. template='jinja',
  4097. context=self.context,
  4098. )
  4099. self.assertSaltTrueReturn(ret)
  4100. ret = ret[next(iter(ret))]
  4101. self.assertEqual(ret['comment'], 'Patch successfully applied')
  4102. # Re-run again, this time with no hash and skip_verify=True to test
  4103. # skipping hash verification
  4104. ret = self.run_state(
  4105. 'file.patch',
  4106. name=self.math_file,
  4107. source=self.math_patch_template_http,
  4108. template='jinja',
  4109. context=self.context,
  4110. skip_verify=True,
  4111. )
  4112. self.assertSaltTrueReturn(ret)
  4113. ret = ret[next(iter(ret))]
  4114. self.assertEqual(ret['comment'], 'Patch was already applied')
  4115. self.assertEqual(ret['changes'], {})
  4116. def test_patch_directory_remote_source_template(self):
  4117. '''
  4118. Test file.patch using a patch applied to a directory, with changes
  4119. spanning multiple files, and the patch file coming from a remote
  4120. source.
  4121. '''
  4122. self._check_patch_version('2.6')
  4123. # Try without a source_hash and without skip_verify=True, this should
  4124. # fail with a message about the source_hash
  4125. ret = self.run_state(
  4126. 'file.patch',
  4127. name=self.base_dir,
  4128. source=self.all_patch_template_http,
  4129. template='jinja',
  4130. context=self.context,
  4131. strip=1,
  4132. )
  4133. self.assertSaltFalseReturn(ret)
  4134. ret = ret[next(iter(ret))]
  4135. self.assertIn('Unable to verify upstream hash', ret['comment'])
  4136. # Re-run the state with a source hash, it should now succeed
  4137. ret = self.run_state(
  4138. 'file.patch',
  4139. name=self.base_dir,
  4140. source=self.all_patch_template_http,
  4141. source_hash=self.all_patch_template_hash,
  4142. template='jinja',
  4143. context=self.context,
  4144. strip=1,
  4145. )
  4146. self.assertSaltTrueReturn(ret)
  4147. ret = ret[next(iter(ret))]
  4148. self.assertEqual(ret['comment'], 'Patch successfully applied')
  4149. # Re-run again, this time with no hash and skip_verify=True to test
  4150. # skipping hash verification
  4151. ret = self.run_state(
  4152. 'file.patch',
  4153. name=self.base_dir,
  4154. source=self.all_patch_template_http,
  4155. template='jinja',
  4156. context=self.context,
  4157. strip=1,
  4158. skip_verify=True,
  4159. )
  4160. self.assertSaltTrueReturn(ret)
  4161. ret = ret[next(iter(ret))]
  4162. self.assertEqual(ret['comment'], 'Patch was already applied')
  4163. self.assertEqual(ret['changes'], {})
  4164. def test_patch_test_mode(self):
  4165. '''
  4166. Test file.patch using test=True
  4167. '''
  4168. # Try without a source_hash and without skip_verify=True, this should
  4169. # fail with a message about the source_hash
  4170. ret = self.run_state(
  4171. 'file.patch',
  4172. name=self.numbers_file,
  4173. source=self.numbers_patch,
  4174. test=True,
  4175. )
  4176. self.assertSaltNoneReturn(ret)
  4177. ret = ret[next(iter(ret))]
  4178. self.assertEqual(ret['comment'], 'The patch would be applied')
  4179. self.assertTrue(ret['changes'])
  4180. # Apply the patch for real. We'll then be able to test below that we
  4181. # exit with a True rather than a None result if test=True is used on an
  4182. # already-applied patch.
  4183. ret = self.run_state(
  4184. 'file.patch',
  4185. name=self.numbers_file,
  4186. source=self.numbers_patch,
  4187. )
  4188. self.assertSaltTrueReturn(ret)
  4189. ret = ret[next(iter(ret))]
  4190. self.assertEqual(ret['comment'], 'Patch successfully applied')
  4191. self.assertTrue(ret['changes'])
  4192. # Run again with test=True. Since the pre-check happens before we do
  4193. # the __opts__['test'] check, we should exit with a True result just
  4194. # the same as if we try to run this state on an already-patched file
  4195. # *without* test=True.
  4196. ret = self.run_state(
  4197. 'file.patch',
  4198. name=self.numbers_file,
  4199. source=self.numbers_patch,
  4200. test=True,
  4201. )
  4202. self.assertSaltTrueReturn(ret)
  4203. ret = ret[next(iter(ret))]
  4204. self.assertEqual(ret['comment'], 'Patch was already applied')
  4205. self.assertEqual(ret['changes'], {})
  4206. # Empty the file to ensure that the patch doesn't apply cleanly
  4207. with salt.utils.files.fopen(self.numbers_file, 'w'):
  4208. pass
  4209. # Run again with test=True. Similar to the above run, we are testing
  4210. # that we return before we reach the __opts__['test'] check. In this
  4211. # case we should return a False result because we should already know
  4212. # by this point that the patch will not apply cleanly.
  4213. ret = self.run_state(
  4214. 'file.patch',
  4215. name=self.numbers_file,
  4216. source=self.numbers_patch,
  4217. test=True,
  4218. )
  4219. self.assertSaltFalseReturn(ret)
  4220. ret = ret[next(iter(ret))]
  4221. self.assertIn('Patch would not apply cleanly', ret['comment'])
  4222. self.assertEqual(ret['changes'], {})
  4223. WIN_TEST_FILE = 'c:/testfile'
  4224. @destructiveTest
  4225. @skipIf(not IS_WINDOWS, 'windows test only')
  4226. class WinFileTest(ModuleCase):
  4227. '''
  4228. Test for the file state on Windows
  4229. '''
  4230. def setUp(self):
  4231. self.run_state('file.managed', name=WIN_TEST_FILE, makedirs=True, contents='Only a test')
  4232. def tearDown(self):
  4233. self.run_state('file.absent', name=WIN_TEST_FILE)
  4234. def test_file_managed(self):
  4235. '''
  4236. Test file.managed on Windows
  4237. '''
  4238. self.assertTrue(self.run_state('file.exists', name=WIN_TEST_FILE))
  4239. def test_file_copy(self):
  4240. '''
  4241. Test file.copy on Windows
  4242. '''
  4243. ret = self.run_state('file.copy', name='c:/testfile_copy', makedirs=True, source=WIN_TEST_FILE)
  4244. self.assertTrue(ret)
  4245. def test_file_comment(self):
  4246. '''
  4247. Test file.comment on Windows
  4248. '''
  4249. self.run_state('file.comment', name=WIN_TEST_FILE, regex='^Only')
  4250. with salt.utils.files.fopen(WIN_TEST_FILE, 'r') as fp_:
  4251. self.assertTrue(fp_.read().startswith('#Only'))
  4252. def test_file_replace(self):
  4253. '''
  4254. Test file.replace on Windows
  4255. '''
  4256. self.run_state('file.replace', name=WIN_TEST_FILE, pattern='test', repl='testing')
  4257. with salt.utils.files.fopen(WIN_TEST_FILE, 'r') as fp_:
  4258. self.assertIn('testing', fp_.read())
  4259. def test_file_absent(self):
  4260. '''
  4261. Test file.absent on Windows
  4262. '''
  4263. ret = self.run_state('file.absent', name=WIN_TEST_FILE)
  4264. self.assertTrue(ret)