Method: Onceover::VendoredModules#component_cache

Defined in:
lib/onceover/vendored_modules.rb

#component_cache(component) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/onceover/vendored_modules.rb', line 71

def component_cache(component)
  # Ideally we want a cache for the version of the puppet agent used in tests
  desired_name = "#{component}-puppet_agent-#{@puppet_version}.json"
  # By default look for any caches created during previous runs
  cache_file = File.join(@cachedir, desired_name)

  # If the user provides their own cache
  if !@force_update && File.directory?(@manual_vendored_dir)
            # Check for any '<component>-puppet_agent-<puppet version>.json' files
            dg = Dir.glob(File.join(@manual_vendored_dir, "#{component}-puppet_agent*"))
            # Check if there are multiple versions of the component cache
            if dg.size > 1
              # If there is the same version supplied as whats being tested against use that
              if dg.any? { |s| s[desired_name] }
                cache_file = File.join(@manual_vendored_dir, desired_name)
              # If there are any with the same major version, use the latest supplied
              elsif dg.any? { |s| s["#{component}-puppet_agent-#{@puppet_major_version}"] }
                maj_match = dg.select { |f| /#{component}-puppet_agent-#{@puppet_major_version}.\d+\.\d+\.json/.match(f) }
                maj_match.each do |f|
                  next unless (version_from_file(cache_file) == version_from_file(desired_name)) || (version_from_file(f) >= version_from_file(cache_file))

                  # if the current cache version matches the desired version, use the first matching major version in user cache
                  # if there are multiple major version matches in user cache, use the latest
                  cache_file = f
                end
              # Otherwise just use the latest supplied
              else
                dg.each { |f| cache_file = f if version_from_file(f) >= version_from_file(cache_file) }
              end
            # If there is only one use that
            elsif dg.size == 1
              cache_file = dg[0]
            end
  end

  # Warn the user if cached version does not match whats being used to test
  cache_version = version_from_file(cache_file)
  logger.warn "Cache for #{component} is for puppet_agent #{cache_version}, while you are testing against puppet_agent #{@puppet_version}. Consider updating your cache to ensure consistent behavior in your tests" if cache_version != @puppet_version

  cache_file
end