-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read metadata from envvars with fallback
- Loading branch information
1 parent
89e1bec
commit 681ddda
Showing
4 changed files
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package helper | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetDeviceInfoFromEnv(t *testing.T) { | ||
setUpEnvVars() | ||
_ = os.Setenv("XELON_CLOUD_ID", "5") | ||
_ = os.Setenv("XELON_LOCAL_VM_ID", "abcd1234") | ||
_ = os.Setenv("XELON_VM_HOSTNAME", "test.local") | ||
|
||
deviceInfo := getDeviceInfoFromEnv() | ||
|
||
assert.Equal(t, "5", deviceInfo.Metadata.CloudID) | ||
assert.Equal(t, "abcd1234", deviceInfo.Metadata.LocalVMID) | ||
assert.Equal(t, "test.local", deviceInfo.Metadata.Hostname) | ||
} | ||
|
||
func TestGetDeviceInfoFromFile_valid(t *testing.T) { | ||
setUpEnvVars() | ||
metadataFile := filepath.Join("testdata", "valid-metadata.json") | ||
|
||
deviceInfo, err := getDeviceInfoFromFile(metadataFile) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "1", deviceInfo.Metadata.CloudID) | ||
assert.Equal(t, "abcd1234", deviceInfo.Metadata.LocalVMID) | ||
assert.Equal(t, "test.local", deviceInfo.Metadata.Hostname) | ||
} | ||
|
||
func TestGetDeviceInfoFromFile_invalid(t *testing.T) { | ||
setUpEnvVars() | ||
metadataFile := filepath.Join("testdata", "invalid-metadata.json") | ||
|
||
_, err := getDeviceInfoFromFile(metadataFile) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetDeviceInfo(t *testing.T) { | ||
setUpEnvVars() | ||
// XELON_LOCAL_VM_ID is not defined, fallback to file values | ||
_ = os.Setenv("XELON_CLOUD_ID", "5") | ||
metadataFile := filepath.Join("testdata", "valid-metadata.json") | ||
|
||
deviceInfo, err := getDeviceInfo(metadataFile) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "1", deviceInfo.Metadata.CloudID) | ||
} | ||
|
||
func setUpEnvVars() { | ||
_ = os.Unsetenv("XELON_CLOUD_ID") | ||
_ = os.Unsetenv("XELON_LOCAL_VM_ID") | ||
_ = os.Unsetenv("XELON_VM_HOSTNAME") | ||
} |
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,2 @@ | ||
{ | ||
"invalid-json-format": "some-value" |
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,7 @@ | ||
{ | ||
"metadata": { | ||
"cloudId": "1", | ||
"local_id": "abcd1234", | ||
"hostname": "test.local" | ||
} | ||
} |