Skip to content

Commit

Permalink
feat(tests): Adds override for package version (#849)
Browse files Browse the repository at this point in the history
Adds a "override_versions_file" option for EXPECT_PHP_PACKAGE that
allows specifying overrides for given expected versions detected. This
is useful for when a package did not modify the internal data structure
used to by the agent to detect its version, causing the package test to
fail.

The override file should be in the same directory as the
test_php_package.php test and is a JSON file implementing a simple
dictionary like:

{
    "4.13.0": "4.12.0"
}

In this case this specifies if the packge version "4.13.0" is detected
then override this expectation with the version "4.12.0". In this
example the upstream package bumped up to 4.13.0 but did not change the
internal version variable so the agent still detects it as 4.12.0.
  • Loading branch information
mfulb authored Mar 13, 2024
1 parent b033a3d commit 86f7753
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
77 changes: 67 additions & 10 deletions daemon/internal/newrelic/integration/php_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ type PhpPackagesCollection struct {
// PHP packages config describes how to collect the JSON for the packages installed
// for the current test case
type PhpPackagesConfiguration struct {
path string
command string
supportedListFile string
expectedPackages []string
packageNameOnly []string
path string
command string
supportedListFile string
overrideVersionsFile string
expectedPackages []string
packageNameOnly []string
}

// composer package JSON
Expand Down Expand Up @@ -135,6 +136,7 @@ func NewPhpPackagesCollection(path string, config []byte) (*PhpPackagesCollectio
var supportedListFile string
var expectedPackages string
var packageNameOnly string
var overrideVersionsFile string
var expectedPackagesArr []string
var packageNameOnlyArr []string
var commandOK, supportedOK, expectedOK bool
Expand Down Expand Up @@ -177,13 +179,36 @@ func NewPhpPackagesCollection(path string, config []byte) (*PhpPackagesCollectio
}
}

// option file containing overrides for expected package versions
// this is useful when a package is detected as the wrong version
// because the internal mechanism the agent uses to get pacakge
// versions was not updated by the upstream maintainers properly
// on a release.
//
// this is a JSON file of the format
// {
// "<expected>": "<override",
// ...
// }
//
// such as:
// {
// "4.13.0": "4.12.0",
// "3.4.5": "3.4.4"
// }
//
// which creates overrides to version "4.13.0" to change its expecation to "4.12.0"
// and "3.4.5" to be changed to an expectation of "3.4.4"
overrideVersionsFile, ok = params["override_versions_file"]

p := &PhpPackagesCollection{
config: PhpPackagesConfiguration{
command: command,
path: path,
supportedListFile: supportedListFile,
expectedPackages: expectedPackagesArr,
packageNameOnly: packageNameOnlyArr},
command: command,
path: path,
supportedListFile: supportedListFile,
overrideVersionsFile: overrideVersionsFile,
expectedPackages: expectedPackagesArr,
packageNameOnly: packageNameOnlyArr},
}

return p, nil
Expand Down Expand Up @@ -220,6 +245,38 @@ func ParsePackagesList(expectedPackages string) ([]string, error) {
return strings.Split(tmp, ","), nil
}

func ParseOverrideVersionsFile(path, overrideVersionFile string) (map[string]interface{}, error) {
jsonFile, err := os.Open(filepath.Dir(path) + "/" + overrideVersionFile)
if err != nil {
return nil, fmt.Errorf("error opening versions override list %s!", err.Error())
} else {
defer jsonFile.Close()
}

versions_json, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("Error reading versions override list %s!", err.Error())
}

var versions_override map[string]interface{}

err = json.Unmarshal([]byte(versions_json), &versions_override)
if nil != err {
return nil, fmt.Errorf("Error unmarshalling versions override list %s\n", err.Error())
}

return versions_override, nil
}

// Returns name of versions override file (if exists)
func (pkgs *PhpPackagesCollection) OverrideVersionsFile() string {
if nil == pkgs {
return ""
}

return pkgs.config.overrideVersionsFile
}

// Detects installed PHP packages
//
// Returns : []PhpPackage with extracted package info, sorted by package name
Expand Down
21 changes: 20 additions & 1 deletion daemon/internal/newrelic/integration/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ func (t *Test) comparePhpPackages(harvest *newrelic.Harvest) {
var expectedPackages []PhpPackage
var expectedPkgsCollection *PhpPackagesCollection
var expectNullPkgs bool
var version_overrides map[string]interface{}

if nil != t.phpPackagesConfig {
var err error
Expand Down Expand Up @@ -545,6 +546,15 @@ func (t *Test) comparePhpPackages(harvest *newrelic.Harvest) {
t.Fatal(fmt.Errorf("EXPECTED_PHP_PACKAGES used but no packages detected in environment!"))
}

// load any expected versions overrides
if 0 < len(expectedPkgsCollection.OverrideVersionsFile()) {
var err error
version_overrides, err = ParseOverrideVersionsFile(t.Path, expectedPkgsCollection.OverrideVersionsFile())
if nil != err {
t.Fatal(err)
}
}

audit, err := newrelic.IntegrationData(harvest.PhpPackages, newrelic.AgentRunID("?? agent run id"), time.Now())
if nil != err {
t.Fatal(err)
Expand Down Expand Up @@ -580,7 +590,16 @@ func (t *Test) comparePhpPackages(harvest *newrelic.Harvest) {
t.AddNote(fmt.Sprintf("Tested package name only for packages: %+v", expectedPkgsCollection.config.packageNameOnly))
}
}
if testPackageNameOnly || expectedPackages[i].Version == actualPackages[i].Version {

// see if a version override exists for the expectation
expected_version := expectedPackages[i].Version
override_version, ok := version_overrides[expected_version]
if ok {
t.AddNote(fmt.Sprintf("Overrode detected version %s with %s", expected_version, override_version))
expected_version = override_version.(string)
}

if testPackageNameOnly || expected_version == actualPackages[i].Version {
continue
}
}
Expand Down

0 comments on commit 86f7753

Please sign in to comment.