func()

in pkg/testing/fixture_install.go [226:365]


func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallOpts, shouldEnroll bool, opts []process.CmdOption) ([]byte, error) {
	f.t.Logf("[test %s] Inside fixture installNoPkgManager function", f.t.Name())
	if installOpts == nil {
		// default options when not provided
		installOpts = &InstallOpts{}
	}

	// Removes install params to prevent enrollment
	removeEnrollParams := func(installOpts *InstallOpts) {
		installOpts.URL = ""
		installOpts.EnrollmentToken = ""
		installOpts.ESHost = ""
	}

	installArgs := []string{"install"}
	if !shouldEnroll {
		removeEnrollParams(installOpts)
	}

	installArgs = append(installArgs, installOpts.ToCmdArgs()...)
	out, err := f.Exec(ctx, installArgs, opts...)
	if err != nil {
		f.DumpProcesses("-install")
		return out, fmt.Errorf("error running agent install command: %w", err)
	}

	f.installed = true
	f.installOpts = installOpts

	installDir := "Agent"
	socketRunSymlink := paths.ControlSocketRunSymlink("")
	if installOpts.Namespace != "" {
		installDir = paths.InstallDirNameForNamespace(installOpts.Namespace)
		socketRunSymlink = paths.ControlSocketRunSymlink(installOpts.Namespace)
	}

	if installOpts.BasePath == "" {
		f.workDir = filepath.Join(paths.DefaultBasePath, "Elastic", installDir)
	} else {
		f.workDir = filepath.Join(installOpts.BasePath, "Elastic", installDir)
	}

	// we just installed agent, the control socket is at a well-known location
	socketPath := fmt.Sprintf("unix://%s", socketRunSymlink) // use symlink as that works for all versions
	if runtime.GOOS == "windows" {
		// Windows uses a fixed named pipe, that is always the same.
		// It is the same even running in unprivileged mode.
		socketPath = paths.WindowsControlSocketInstalledPath
	} else if !installOpts.Privileged {
		// Unprivileged versions move the socket to inside the installed directory
		// of the Elastic Agent.
		socketPath = paths.ControlSocketFromPath(runtime.GOOS, f.workDir)
	}
	c := client.New(client.WithAddress(socketPath))
	f.setClient(c)

	f.t.Cleanup(func() {
		if f.t.Failed() {
			f.DumpProcesses("-cleanup")
		}
	})

	f.t.Cleanup(func() {
		// check for running agents after uninstall had a chance to run
		// there can be a single agent left when using --develop mode
		if f.installOpts != nil && f.installOpts.Namespace != "" {
			// Only consider the main agent process and not sub-processes so that we can detect when
			// multiple agents are running without needing to know the number of input sub-processes to expect.
			agentProcesses := getElasticAgentProcesses(f.t)
			assert.LessOrEqualf(f.t, len(agentProcesses), 1, "More than one agent left running at the end of the test when second agent in namespace %s was used: %v", f.installOpts.Namespace, agentProcesses)
			// The agent left running has to be the non-development agent. The development agent should be uninstalled first as a convention.
			if len(agentProcesses) > 0 {
				assert.NotContainsf(f.t, agentProcesses[0].Cmdline, paths.InstallDirNameForNamespace(f.installOpts.Namespace),
					"The agent installed into namespace %s was left running at the end of the test or was not uninstalled first: %v", f.installOpts.Namespace, agentProcesses)
			}
			return
		}

		// If not using an installation namespace, there should be no elastic-agent or agentbeat processes left running.
		processes := getElasticAgentAndAgentbeatProcesses(f.t)
		assert.Empty(f.t, processes, "there should be no running agent at the end of the test")
	})

	f.t.Cleanup(func() {
		f.t.Logf("[test %s] Inside fixture cleanup function", f.t.Name())

		if !f.installed {
			f.t.Logf("skipping uninstall; agent not installed (fixture.installed is false)")
			// not installed; no need to clean up or collect diagnostics
			return
		}

		// diagnostics is collected when either the environment variable
		// AGENT_COLLECT_DIAG=true or the test is marked failed
		collect := collectDiagFlag()
		failed := f.t.Failed()
		if collect || failed {
			if collect {
				f.t.Logf("collecting diagnostics; AGENT_COLLECT_DIAG=true")
			} else if failed {
				f.t.Logf("collecting diagnostics; test failed")
			}
			f.collectDiagnostics()
		}

		// environment variable AGENT_KEEP_INSTALLED=true will skip the uninstallation
		// useful to debug the issue with the Elastic Agent
		if f.t.Failed() && keepInstalledFlag() {
			f.t.Logf("skipping uninstall; test failed and AGENT_KEEP_INSTALLED=true")
			return
		}

		if keepInstalledFlag() {
			f.t.Logf("ignoring AGENT_KEEP_INSTALLED=true as test succeeded, " +
				"keeping the agent installed will jeopardise other tests")
		}

		// 5 minute timeout, to ensure that it at least doesn't get stuck.
		// original context is not used as it could have a timeout on the context
		// for the install and we don't want that context to prevent the uninstall
		uninstallCtx, uninstallCancel := context.WithTimeout(context.Background(), 5*time.Minute)
		defer uninstallCancel()
		out, err := f.Uninstall(uninstallCtx, &UninstallOpts{Force: true, UninstallToken: f.uninstallToken})
		f.setClient(nil)
		if err != nil &&
			(errors.Is(err, ErrNotInstalled) ||
				strings.Contains(
					err.Error(),
					"elastic-agent: no such file or directory")) {
			f.t.Logf("fixture.Install Cleanup: agent was already uninstalled, skipping uninstall")
			// Agent fixture has already been uninstalled, perhaps by
			// an explicit call to fixture.Uninstall, so nothing needs
			// to be done here.
			return
		}
		require.NoErrorf(f.t, err, "uninstalling agent failed. Output: %q", out)
	})

	return out, nil
}