diff --git a/.github/workflows/agent.yml b/.github/workflows/agent.yml index a59a9277bbc..8edef911810 100644 --- a/.github/workflows/agent.yml +++ b/.github/workflows/agent.yml @@ -99,12 +99,7 @@ jobs: run: make env-up - name: Run tests - run: | - umask 000 - ls -la /usr/local - whoami - sudo mkdir -p /usr/local/percona/pmm2 - make test-cover + run: make test-cover - name: Upload coverage results uses: codecov/codecov-action@v3 diff --git a/agent/config/config.go b/agent/config/config.go index 2e0cafd02b4..064082a9164 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -37,6 +37,7 @@ import ( ) const pathBaseDefault = "/usr/local/percona/pmm2" +const agentTmpPath = "agent-tmp" // temporary directory for agent config files, relative to workdir // Server represents PMM Server configuration. type Server struct { @@ -239,21 +240,21 @@ func get(args []string, cfg *Config, l *logrus.Entry) (configFileF string, err e } if cfg.Paths.TempDir == "" { - cfg.Paths.TempDir = filepath.Join(cfg.Paths.PathsBase, "tmp") - l.Infof("TempDir is undefined, will create one at %q", cfg.Paths.TempDir) - err := os.MkdirAll(cfg.Paths.TempDir, 0o700) - if err != nil { - l.WithError(err).Panicf("unable to create the temporary directory %q", cfg.Paths.TempDir) + wd, err := os.Getwd() + if err == nil { + cfg.Paths.TempDir = filepath.Join(wd, agentTmpPath) + } else { + // As a last resort, if unable to get the workdir, use system temp directory. + cfg.Paths.TempDir = filepath.Join(os.TempDir(), agentTmpPath) } + l.Infof("Temporary directory is undefined, will create one at %q", cfg.Paths.TempDir) + createTempDir(cfg.Paths.TempDir, l) } else { - l.Infof("TempDir is defined as %q", cfg.Paths.TempDir) - err := IsWritable(cfg.Paths.TempDir) - if err != nil { - l.WithError(err).Infof("temporary directory %q is not writable, will attempt to re-create it", cfg.Paths.TempDir) - err := os.MkdirAll(cfg.Paths.TempDir, 0o700) - if err != nil { - l.WithError(err).Panicf("unable to create the temporary directory %q", cfg.Paths.TempDir) - } + l.Infof("Temporary directory is defined as %q", cfg.Paths.TempDir) + _, err := os.Stat(cfg.Paths.TempDir) + if err != nil && errors.Is(err, fs.ErrNotExist) { + l.WithError(err).Infof("Temporary directory %q does not exist, will attempt to create it", cfg.Paths.TempDir) + createTempDir(cfg.Paths.TempDir, l) } } @@ -538,3 +539,10 @@ func IsWritable(path string) error { } return unix.Access(path, unix.W_OK) } + +func createTempDir(path string, l *logrus.Entry) { + err := os.MkdirAll(path, 0o700) + if err != nil { + l.WithError(err).Panicf("Unable to create the temporary directory %q", path) + } +} diff --git a/agent/config/config_test.go b/agent/config/config_test.go index fcfb6b9452c..108de2b5b46 100644 --- a/agent/config/config_test.go +++ b/agent/config/config_test.go @@ -42,6 +42,19 @@ func removeConfig(t *testing.T, name string) { require.NoError(t, os.Remove(name)) } +func generateTempDir(t *testing.T, path string) string { + t.Helper() + wd, err := os.Getwd() + require.NoError(t, err) + t.Logf("Working directory: %s, tmpdir: %s", wd, filepath.Join(wd, path)) + return filepath.Join(wd, path) +} + +func removeTempDir(t *testing.T, path string) { + t.Helper() + require.NoError(t, os.RemoveAll(path)) +} + func TestLoadFromFile(t *testing.T) { t.Run("Normal", func(t *testing.T) { name := writeConfig(t, &Config{ID: "agent-id"}) @@ -83,10 +96,6 @@ func TestLoadFromFile(t *testing.T) { } func TestGet(t *testing.T) { - const dir = "/usr/local/percona/pmm2" - err := os.MkdirAll(dir, os.ModePerm) - assert.NoError(t, err) - t.Run("OnlyFlags", func(t *testing.T) { var actual Config configFilepath, err := get([]string{ @@ -96,6 +105,9 @@ func TestGet(t *testing.T) { }, &actual, logrus.WithField("test", t.Name())) require.NoError(t, err) + tmpDir := generateTempDir(t, agentTmpPath) + defer removeTempDir(t, tmpDir) + expected := Config{ ID: "agent-id", ListenAddress: "127.0.0.1", @@ -114,7 +126,7 @@ func TestGet(t *testing.T) { RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter", AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter", VMAgent: "/usr/local/percona/pmm2/exporters/vmagent", - TempDir: "/usr/local/percona/pmm2/tmp", + TempDir: tmpDir, PTSummary: "/usr/local/percona/pmm2/tools/pt-summary", PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary", PTMySQLSummary: "/usr/local/percona/pmm2/tools/pt-mysql-summary", @@ -147,6 +159,9 @@ func TestGet(t *testing.T) { }, &actual, logrus.WithField("test", t.Name())) require.NoError(t, err) + tmpDir := generateTempDir(t, agentTmpPath) + defer removeTempDir(t, tmpDir) + expected := Config{ ID: "agent-id", ListenAddress: "0.0.0.0", @@ -165,7 +180,7 @@ func TestGet(t *testing.T) { RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter", AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter", VMAgent: "/usr/local/percona/pmm2/exporters/vmagent", - TempDir: "/usr/local/percona/pmm2/tmp", + TempDir: tmpDir, PTSummary: "/usr/local/percona/pmm2/tools/pt-summary", PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary", PTMongoDBSummary: "/usr/local/percona/pmm2/tools/pt-mongodb-summary", @@ -200,6 +215,9 @@ func TestGet(t *testing.T) { }, &actual, logrus.WithField("test", t.Name())) require.NoError(t, err) + tmpDir := generateTempDir(t, agentTmpPath) + defer removeTempDir(t, tmpDir) + expected := Config{ ID: "flag-id", ListenAddress: "127.0.0.1", @@ -218,7 +236,7 @@ func TestGet(t *testing.T) { RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter", AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter", VMAgent: "/usr/local/percona/pmm2/exporters/vmagent", - TempDir: "/usr/local/percona/pmm2/tmp", + TempDir: tmpDir, PTSummary: "/usr/local/percona/pmm2/tools/pt-summary", PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary", PTMySQLSummary: "/usr/local/percona/pmm2/tools/pt-mysql-summary", @@ -261,6 +279,9 @@ func TestGet(t *testing.T) { }, &actual, logrus.WithField("test", t.Name())) require.NoError(t, err) + tmpDir := generateTempDir(t, agentTmpPath) + defer removeTempDir(t, tmpDir) + expected := Config{ ID: "flag-id", ListenAddress: "127.0.0.1", @@ -279,7 +300,7 @@ func TestGet(t *testing.T) { RDSExporter: "/base/rds_exporter", // default value AzureExporter: "/base/azure_exporter", // default value VMAgent: "/base/vmagent", // default value - TempDir: "/usr/local/percona/pmm2/tmp", + TempDir: tmpDir, PTSummary: "/usr/local/percona/pmm2/tools/pt-summary", PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary", PTMongoDBSummary: "/usr/local/percona/pmm2/tools/pt-mongodb-summary", @@ -322,6 +343,8 @@ func TestGet(t *testing.T) { }, &actual, logrus.WithField("test", t.Name())) require.NoError(t, err) + defer removeTempDir(t, "/foo/tmp") + expected := Config{ ID: "flag-id", ListenAddress: "127.0.0.1", @@ -379,6 +402,9 @@ func TestGet(t *testing.T) { }, &actual, logrus.WithField("test", t.Name())) require.NoError(t, err) + tmpDir := generateTempDir(t, agentTmpPath) + defer removeTempDir(t, tmpDir) + expected := Config{ ID: "flag-id", ListenAddress: "127.0.0.1", @@ -397,7 +423,7 @@ func TestGet(t *testing.T) { RDSExporter: "/foo/exporters/rds_exporter", // default value AzureExporter: "/foo/exporters/azure_exporter", // default value VMAgent: "/foo/exporters/vmagent", // default value - TempDir: "/base/tmp", + TempDir: tmpDir, PTSummary: "/base/tools/pt-summary", PTPGSummary: "/base/tools/pt-pg-summary", PTMongoDBSummary: "/base/tools/pt-mongodb-summary", @@ -426,6 +452,10 @@ func TestGet(t *testing.T) { "--id=flag-id", "--debug", }, &actual, logrus.WithField("test", t.Name())) + + tmpDir := generateTempDir(t, agentTmpPath) + defer removeTempDir(t, tmpDir) + expected := Config{ ID: "flag-id", ListenAddress: "127.0.0.1", @@ -441,7 +471,7 @@ func TestGet(t *testing.T) { RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter", AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter", VMAgent: "/usr/local/percona/pmm2/exporters/vmagent", - TempDir: "/usr/local/percona/pmm2/tmp", + TempDir: tmpDir, PTSummary: "/usr/local/percona/pmm2/tools/pt-summary", PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary", PTMongoDBSummary: "/usr/local/percona/pmm2/tools/pt-mongodb-summary", diff --git a/managed/cmd/pmm-managed/main.go b/managed/cmd/pmm-managed/main.go index 1108fefb86b..2eac6ed7cf0 100644 --- a/managed/cmd/pmm-managed/main.go +++ b/managed/cmd/pmm-managed/main.go @@ -563,7 +563,7 @@ func setup(ctx context.Context, deps *setupDeps) bool { return false } ssoDetails, err := models.GetPerconaSSODetails(ctx, db.Querier) - if err != nil { + if err != nil && !errors.Is(err, models.ErrNotConnectedToPortal) { deps.l.Warnf("Failed to get Percona SSO Details: %s.", err) } if err = deps.supervisord.UpdateConfiguration(settings, ssoDetails); err != nil {