@@ -185,6 +185,11 @@ func (f *Fixture) Client() client.Client {
185185 return f .c
186186}
187187
188+ // Version returns the Elastic Agent version.
189+ func (f * Fixture ) Version () string {
190+ return f .version
191+ }
192+
188193// Prepare prepares the Elastic Agent for usage.
189194//
190195// This must be called before `Configure`, `Run`, or `Install` can be called.
@@ -1012,7 +1017,7 @@ func (f *Fixture) prepareComponents(workDir string, components ...UsableComponen
10121017
10131018 // now remove all that should not be kept; removal is only
10141019 // done by removing the spec file, no need to delete the binary
1015- componentsDir , err := FindComponentsDir (workDir )
1020+ componentsDir , err := FindComponentsDir (workDir , "" )
10161021 if err != nil {
10171022 return err
10181023 }
@@ -1249,28 +1254,68 @@ func getCacheDir(caller string, name string) (string, error) {
12491254 return cacheDir , nil
12501255}
12511256
1252- // FindComponentsDir identifies the directory that holds the components .
1253- func FindComponentsDir (dir string ) (string , error ) {
1257+ // findAgentDataVersionDir identifies the directory that holds the agent data of the given version .
1258+ func findAgentDataVersionDir (dir , version string ) (string , error ) {
12541259 dataDir := filepath .Join (dir , "data" )
12551260 agentVersions , err := os .ReadDir (dataDir )
12561261 if err != nil {
12571262 return "" , fmt .Errorf ("failed to read contents of the data directory %s: %w" , dataDir , err )
12581263 }
12591264 var versionDir string
12601265 for _ , fi := range agentVersions {
1261- if strings .HasPrefix (fi .Name (), "elastic-agent-" ) && fi .IsDir () {
1262- versionDir = fi .Name ()
1266+ filename := fi .Name ()
1267+ if strings .HasPrefix (filename , "elastic-agent-" ) && fi .IsDir () {
1268+ // Below we exclude the hash suffix (7 characters) of the directory to check the version
1269+ if version != "" && filename [:len (filename )- 7 ] != "elastic-agent-" + version {
1270+ // version specified but version mismatch. in case of upgrade we have multiple
1271+ // directories, we don't want first found
1272+ continue
1273+ }
1274+ versionDir = filename
12631275 break
12641276 }
12651277 }
1266- componentsDir := filepath .Join (dataDir , versionDir , "components" )
1278+ if versionDir == "" {
1279+ return "" , fmt .Errorf ("failed to find versioned directory for version %q" , version )
1280+ }
1281+ return filepath .Join (dataDir , versionDir ), nil
1282+ }
1283+
1284+ // FindComponentsDir identifies the directory that holds the components.
1285+ func FindComponentsDir (dir , version string ) (string , error ) {
1286+ versionDir , err := findAgentDataVersionDir (dir , version )
1287+ if err != nil {
1288+ return "" , err
1289+ }
1290+ componentsDir := filepath .Join (versionDir , "components" )
12671291 fi , err := os .Stat (componentsDir )
12681292 if (err != nil && ! os .IsExist (err )) || ! fi .IsDir () {
12691293 return "" , fmt .Errorf ("failed to find components directory at %s: %w" , componentsDir , err )
12701294 }
12711295 return componentsDir , nil
12721296}
12731297
1298+ // FindRunDir identifies the directory that holds the run folder.
1299+ func FindRunDir (fixture * Fixture ) (string , error ) {
1300+ agentWorkDir := fixture .WorkDir ()
1301+ if pf := fixture .PackageFormat (); pf == "deb" || pf == "rpm" {
1302+ // these are hardcoded paths in packages.yml
1303+ agentWorkDir = "/var/lib/elastic-agent"
1304+ }
1305+
1306+ version := fixture .Version ()
1307+ versionDir , err := findAgentDataVersionDir (agentWorkDir , version )
1308+ if err != nil {
1309+ return "" , err
1310+ }
1311+ runDir := filepath .Join (versionDir , "run" )
1312+ fi , err := os .Stat (runDir )
1313+ if (err != nil && ! os .IsExist (err )) || ! fi .IsDir () {
1314+ return "" , fmt .Errorf ("failed to find run directory at %s: %w" , runDir , err )
1315+ }
1316+ return runDir , nil
1317+ }
1318+
12741319// writeSpecFile writes the specification to a specification file at the defined destination.
12751320func writeSpecFile (dest string , spec * component.Spec ) error {
12761321 data , err := yaml .Marshal (spec )
0 commit comments