2021-09-04 12:16:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tmpPath = "./tmp"
|
|
|
|
const testLabelPath = tmpPath + "/labels"
|
|
|
|
|
|
|
|
func TestGetIdentification(t *testing.T) {
|
2021-09-04 20:17:56 +00:00
|
|
|
localHost := LocalHost{
|
|
|
|
LabelsPath: testLabelPath,
|
|
|
|
HostnameOverride: "test.example.com",
|
|
|
|
InitialLabels: Labels{Label("service:test"), Label("test:1")},
|
|
|
|
}
|
|
|
|
|
|
|
|
discovery, err := localHost.GetIdentification()
|
2021-09-04 12:16:14 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "test.example.com", discovery.Hostname)
|
|
|
|
assert.Equal(t, "service:test", discovery.Labels[0].String())
|
|
|
|
|
|
|
|
err = os.MkdirAll(testLabelPath, os.ModePerm)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = os.WriteFile(testLabelPath+"/test", []byte("service:test2\npublic_ip:1.2.3.4"), 0644)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-09-04 20:17:56 +00:00
|
|
|
discovery, err = localHost.GetIdentification()
|
2021-09-04 12:16:14 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, Label("public_ip:1.2.3.4"), discovery.Labels[3])
|
|
|
|
|
|
|
|
os.RemoveAll(tmpPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadLocalLabels(t *testing.T) {
|
2021-09-04 20:17:56 +00:00
|
|
|
localHost := LocalHost{
|
|
|
|
LabelsPath: testLabelPath,
|
|
|
|
HostnameOverride: "test.example.com",
|
|
|
|
InitialLabels: Labels{Label("service:test"), Label("test:1")},
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
err := os.MkdirAll(testLabelPath, os.ModePerm)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = os.WriteFile(testLabelPath+"/test", []byte("service:test\npublic_ip:1.2.3.4"), 0644)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-09-04 20:17:56 +00:00
|
|
|
labels, err := localHost.loadLocalLabels()
|
2021-09-04 12:16:14 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(labels))
|
|
|
|
assert.Equal(t, "public_ip:1.2.3.4", labels[0].String())
|
|
|
|
|
|
|
|
os.RemoveAll(tmpPath)
|
|
|
|
}
|