-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
IsStandalone()
to agent info, report management mode to gRPC (#…
…4911) * init commit * update runtime_comm * fix startup bug * fix tests, log
- Loading branch information
1 parent
6c20730
commit 249d0b6
Showing
7 changed files
with
201 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package info | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" | ||
"github.com/elastic/elastic-agent/internal/pkg/agent/application/secret" | ||
"github.com/elastic/elastic-agent/internal/pkg/agent/storage" | ||
"github.com/elastic/elastic-agent/internal/pkg/agent/vault" | ||
) | ||
|
||
func TestAgentIDStandaloneWorks(t *testing.T) { | ||
if runtime.GOOS == "darwin" { | ||
// vault requres extra perms on mac | ||
t.Skip() | ||
} | ||
// create a new encrypted disk store | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
tmpPath := t.TempDir() | ||
paths.SetConfig(tmpPath) | ||
|
||
vaultPath := filepath.Join(tmpPath, "vault") | ||
err := secret.CreateAgentSecret(ctx, vault.WithVaultPath(vaultPath)) | ||
require.NoError(t, err) | ||
|
||
setID := "test-id" | ||
testCfg := map[string]interface{}{ | ||
"agent": map[string]interface{}{ | ||
"id": setID, | ||
}, | ||
} | ||
saveToStateStore(t, tmpPath, testCfg) | ||
|
||
got, err := NewAgentInfo(ctx, false) | ||
require.NoError(t, err) | ||
t.Logf("got: %#v", got) | ||
|
||
// check the ID to make sure we've opened the fleet config properly | ||
require.Equal(t, setID, got.agentID) | ||
|
||
// no fleet config, should be standalone | ||
require.True(t, got.isStandalone) | ||
|
||
// update fleet config, this time in managed mode | ||
testCfg = map[string]interface{}{ | ||
"agent": map[string]interface{}{ | ||
"id": setID, | ||
}, | ||
"fleet": map[string]interface{}{ | ||
"enabled": true, | ||
}, | ||
} | ||
saveToStateStore(t, tmpPath, testCfg) | ||
|
||
got, err = NewAgentInfo(ctx, false) | ||
require.NoError(t, err) | ||
t.Logf("got: %#v", got) | ||
require.False(t, got.isStandalone) | ||
|
||
} | ||
|
||
func saveToStateStore(t *testing.T, tmpPath string, in map[string]interface{}) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
encPath := filepath.Join(tmpPath, "fleet.enc") | ||
store, err := storage.NewEncryptedDiskStore(ctx, encPath) | ||
require.NoError(t, err) | ||
|
||
rawYml, err := yaml.Marshal(in) | ||
require.NoError(t, err) | ||
|
||
reader := bytes.NewReader(rawYml) | ||
|
||
err = store.Save(reader) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.