@@ -897,4 +897,103 @@ def copy_fixtures(sources, dest)
897
897
end
898
898
end
899
899
end
900
+
901
+ context "legacy facts" do
902
+ let ( :mod_dir ) { tmpdir ( 'module_dir' ) }
903
+ let ( :custom_dir ) { File . join ( mod_dir , 'lib' ) }
904
+ let ( :external_dir ) { File . join ( mod_dir , 'facts.d' ) }
905
+
906
+ before ( :each ) do
907
+ # don't stub facter behavior, since we're relying on
908
+ # `Facter.resolve` to omit legacy facts
909
+ Puppet ::Node ::Facts . indirection . terminus_class = :facter
910
+
911
+ facter_dir = File . join ( custom_dir , 'facter' )
912
+ FileUtils . mkdir_p ( facter_dir )
913
+ File . write ( File . join ( facter_dir , 'custom.rb' ) , <<~END )
914
+ Facter.add(:custom) { setcode { 'a custom value' } }
915
+ END
916
+
917
+ FileUtils . mkdir_p ( external_dir )
918
+ File . write ( File . join ( external_dir , 'external.json' ) , <<~END )
919
+ {"external":"an external value"}
920
+ END
921
+
922
+ # avoid pluginsync'ing contents
923
+ FileUtils . mkdir_p ( Puppet [ :vardir ] )
924
+ FileUtils . cp_r ( custom_dir , Puppet [ :vardir ] )
925
+ FileUtils . cp_r ( external_dir , Puppet [ :vardir ] )
926
+ end
927
+
928
+ def mounts
929
+ {
930
+ # the server needs to provide metadata that matches what the agent has
931
+ # so that the agent doesn't delete them during pluginsync
932
+ file_metadatas : -> ( req , res ) {
933
+ path = case req . path
934
+ when /pluginfacts/
935
+ external_dir
936
+ when /plugins/
937
+ custom_dir
938
+ else
939
+ raise "Unknown mount #{ req . path } "
940
+ end
941
+ request = Puppet ::FileServing ::Metadata . indirection . request (
942
+ :search , path , nil , recurse : true
943
+ )
944
+ data = Puppet ::FileServing ::Metadata . indirection . terminus ( :file ) . search ( request )
945
+ res . body = formatter . render ( data )
946
+ res [ 'Content-Type' ] = formatter . mime
947
+ } ,
948
+ catalog : -> ( req , res ) {
949
+ data = CGI . unescape ( req . query [ 'facts' ] )
950
+ facts = Puppet ::Node ::Facts . convert_from ( 'json' , data )
951
+ node . fact_merge ( facts )
952
+
953
+ catalog = compile_to_catalog ( <<~MANIFEST , node )
954
+ notify { "legacy $osfamily": }
955
+ notify { "custom ${facts['custom']}": }
956
+ notify { "external ${facts['external']}": }
957
+ MANIFEST
958
+
959
+ res . body = formatter . render ( catalog )
960
+ res [ 'Content-Type' ] = formatter . mime
961
+ }
962
+ }
963
+ end
964
+
965
+ it "includes legacy facts by default" do
966
+ server . start_server ( mounts : mounts ) do |port |
967
+ Puppet [ :serverport ] = port
968
+
969
+ agent . command_line . args << '--test'
970
+ expect {
971
+ agent . run
972
+ } . to exit_with ( 2 )
973
+ . and output (
974
+ match ( /defined 'message' as 'legacy [A-Za-z]+'/ )
975
+ . and match ( /defined 'message' as 'custom a custom value'/ )
976
+ . and match ( /defined 'message' as 'external an external value'/ )
977
+ ) . to_stdout
978
+ end
979
+ end
980
+
981
+ it "can exclude legacy facts" do
982
+ server . start_server ( mounts : mounts ) do |port |
983
+ Puppet [ :serverport ] = port
984
+ Puppet [ :include_legacy_facts ] = false
985
+
986
+ agent . command_line . args << '--test'
987
+ expect {
988
+ agent . run
989
+ } . to exit_with ( 2 )
990
+ . and output (
991
+ match ( /defined 'message' as 'legacy '/ )
992
+ . and match ( /defined 'message' as 'custom a custom value'/ )
993
+ . and match ( /defined 'message' as 'external an external value'/ )
994
+ ) . to_stdout
995
+ . and output ( /Warning: Unknown variable: 'osfamily'/ ) . to_stderr
996
+ end
997
+ end
998
+ end
900
999
end
0 commit comments