| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package vnodes |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | ) |
| 13 | |
| 14 | func TestLoad(t *testing.T) { |
| 15 | nodes := Load("testdata") |
| 16 | assert.NotNil(t, nodes) |
| 17 | require.Contains(t, nodes, "first") |
| 18 | require.Contains(t, nodes, "second") |
| 19 | assert.Equal(t, "first", nodes["first"].Name) |
| 20 | assert.Equal(t, "second", nodes["second"].Name) |
| 21 | assert.NotNil(t, Load("not_exist")) |
| 22 | } |
| 23 | |
| 24 | func TestIsStockConfig(t *testing.T) { |
| 25 | assert.True(t, isStockConfig("/usr/lib/netdata/conf.d/vnodes/test.conf")) |
| 26 | assert.False(t, isStockConfig("/etc/netdata/vnodes/test.conf")) |
| 27 | } |
| 28 | |
| 29 | func TestLoad_IgnoresCustomNameInFileAndUsesHostnameIdentity(t *testing.T) { |
| 30 | dir := t.TempDir() |
| 31 | cfgPath := filepath.Join(dir, "vnodes.yaml") |
| 32 | cfg := ` |
| 33 | - hostname: host-a |
| 34 | name: custom-name |
| 35 | guid: 11111111-2222-3333-4444-555555555555 |
| 36 | ` |
| 37 | require.NoError(t, os.WriteFile(cfgPath, []byte(cfg), 0o644)) |
| 38 | |
| 39 | nodes := Load(dir) |
| 40 | require.Len(t, nodes, 1) |
| 41 | require.Contains(t, nodes, "host-a") |
| 42 | assert.Equal(t, "host-a", nodes["host-a"].Name) |
| 43 | assert.Equal(t, "host-a", nodes["host-a"].Hostname) |
| 44 | } |