Fix NfNamingCode and remove NfcNamingCodes
[ric-plt/vespamgr.git] / cmd / vesmgr / config_test.go
index 08bde19..328e253 100644 (file)
@@ -19,12 +19,13 @@ package main
 import (
        "bytes"
        "encoding/json"
-       "github.com/stretchr/testify/assert"
-       "gopkg.in/yaml.v2"
        "io/ioutil"
        "os"
        "testing"
        "time"
+
+       "github.com/stretchr/testify/assert"
+       "gopkg.in/yaml.v2"
 )
 
 func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
@@ -33,6 +34,14 @@ func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
        assert.Equal(t, vesconf.Event.MaxMissed, 2)
        assert.Equal(t, vesconf.Event.RetryInterval, time.Second*5)
        assert.Equal(t, vesconf.Measurement.Prometheus.KeepAlive, time.Second*30)
+       assert.Equal(t, vesconf.Event.VNFName, defaultVNFName)
+       assert.Equal(t, vesconf.Event.NfNamingCode, defaultNFNamingCode)
+       assert.Equal(t, vesconf.Event.ReportingEntityName, "Vespa")
+       // depending on the credentials with which this test is run,
+       // root or non-root, the code either reads the UUID from the file or
+       // ends up using the default id. Just check the length here,
+       // not the actual value.
+       assert.Len(t, vesconf.Event.ReportingEntityID, len(defaultReportingEntityID))
 }
 
 func TestBasicConfigContainsCorrectValues(t *testing.T) {
@@ -40,7 +49,19 @@ func TestBasicConfigContainsCorrectValues(t *testing.T) {
        testBaseConf(t, vesconf)
 }
 
+func TestBasicConfigContainsCorrectVNFName(t *testing.T) {
+       os.Setenv("VESMGR_VNFNAME", "VNF-111")
+       os.Setenv("VESMGR_NFNAMINGCODE", "code55")
+       vesconf := basicVespaConf()
+       assert.Equal(t, vesconf.Event.VNFName, "VNF-111")
+       assert.Equal(t, vesconf.Event.NfNamingCode, "code55")
+       os.Unsetenv("VESMGR_VNFNAME")
+       os.Unsetenv("VESMGR_NFNAMINGCODE")
+}
+
 func TestCollectorConfiguration(t *testing.T) {
+       os.Unsetenv("VESMGR_VNFNAME")
+       os.Unsetenv("VESMGR_NFNAMINGCODE")
        os.Setenv("VESMGR_PRICOLLECTOR_USER", "user123")
        os.Setenv("VESMGR_PRICOLLECTOR_PASSWORD", "pass123")
        os.Setenv("VESMGR_PRICOLLECTOR_PASSPHRASE", "phrase123")
@@ -64,6 +85,8 @@ func TestCollectorConfiguration(t *testing.T) {
 }
 
 func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
+       os.Unsetenv("VESMGR_VNFNAME")
+       os.Unsetenv("VESMGR_NFNAMINGCODE")
        os.Unsetenv("VESMGR_PRICOLLECTOR_USER")
        os.Unsetenv("VESMGR_PRICOLLECTOR_PASSWORD")
        os.Unsetenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
@@ -130,7 +153,7 @@ func metricsStringToInterfaceArray(metrics string) []interface{} {
 }
 
 func TestParseMetricsRules(t *testing.T) {
-       metricsJson := `{"metrics": [
+       metricsJSON := `{"metrics": [
                        { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
                        { "name": "ricxapp_RMR_ReceiveError", "objectName": "ricxappRMRReceiveErrorCounter", "objectInstance": "ricxappRMRReceiveError" },
                        { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" },
@@ -138,7 +161,7 @@ func TestParseMetricsRules(t *testing.T) {
                        { "name": "ricxapp_SDL_Stored", "objectName": "ricxappSDLStoredCounter", "objectInstance": "ricxappSDLStored" },
                        { "name": "ricxapp_SDL_StoreError", "objectName": "ricxappSDLStoreErrorCounter", "objectInstance": "ricxappSDLStoreError" } ]}`
        appMetrics := make(AppMetrics)
-       var m []interface{} = metricsStringToInterfaceArray(metricsJson)
+       m := metricsStringToInterfaceArray(metricsJSON)
        appMetrics = parseMetricsRules(m, appMetrics)
        assert.Len(t, appMetrics, 6)
        assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
@@ -148,17 +171,17 @@ func TestParseMetricsRules(t *testing.T) {
 
 func TestParseMetricsRulesNoMetrics(t *testing.T) {
        appMetrics := make(AppMetrics)
-       metricsJson := `{"metrics": []`
-       var m []interface{} = metricsStringToInterfaceArray(metricsJson)
+       metricsJSON := `{"metrics": []`
+       m := metricsStringToInterfaceArray(metricsJSON)
        appMetrics = parseMetricsRules(m, appMetrics)
        assert.Empty(t, appMetrics)
 }
 
 func TestParseMetricsRulesAdditionalFields(t *testing.T) {
        appMetrics := make(AppMetrics)
-       metricsJson := `{"metrics": [
+       metricsJSON := `{"metrics": [
                        { "additionalField": "valueIgnored", "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
-       var m []interface{} = metricsStringToInterfaceArray(metricsJson)
+       m := metricsStringToInterfaceArray(metricsJSON)
        appMetrics = parseMetricsRules(m, appMetrics)
        assert.Len(t, appMetrics, 1)
        assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
@@ -167,11 +190,11 @@ func TestParseMetricsRulesAdditionalFields(t *testing.T) {
 
 func TestParseMetricsRulesMissingFields(t *testing.T) {
        appMetrics := make(AppMetrics)
-       metricsJson := `{"metrics": [
+       metricsJSON := `{"metrics": [
                        { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
                        { "name": "ricxapp_RMR_ReceiveError", "objectInstance": "ricxappRMRReceiveError" },
                        { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
-       var m []interface{} = metricsStringToInterfaceArray(metricsJson)
+       m := metricsStringToInterfaceArray(metricsJSON)
        appMetrics = parseMetricsRules(m, appMetrics)
        assert.Len(t, appMetrics, 2)
        assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
@@ -182,11 +205,11 @@ func TestParseMetricsRulesMissingFields(t *testing.T) {
 
 func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
        appMetrics := make(AppMetrics)
-       metricsJson := `{"metrics": [
+       metricsJSON := `{"metrics": [
                        { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" },
                        { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounterXXX", "objectInstance": "ricxappRMRReceivedXXX" },
                        { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
-       var m []interface{} = metricsStringToInterfaceArray(metricsJson)
+       m := metricsStringToInterfaceArray(metricsJSON)
        appMetrics = parseMetricsRules(m, appMetrics)
        assert.Len(t, appMetrics, 2)
        assert.Equal(t, "ricxappRMRreceivedCounter", appMetrics["ricxapp_RMR_Received"].ObjectName)
@@ -195,12 +218,12 @@ func TestParseMetricsRulesDuplicateDefinitionIsIgnored(t *testing.T) {
 
 func TestParseMetricsRulesIncrementalFillOfAppMetrics(t *testing.T) {
        appMetrics := make(AppMetrics)
-       metricsJson1 := `{"metrics": [
+       metricsJSON1 := `{"metrics": [
                        { "name": "ricxapp_RMR_Received", "objectName": "ricxappRMRreceivedCounter", "objectInstance": "ricxappRMRReceived" }]}`
-       metricsJson2 := `{"metrics": [
+       metricsJSON2 := `{"metrics": [
                        { "name": "ricxapp_RMR_Transmitted", "objectName": "ricxappRMRTransmittedCounter", "objectInstance": "ricxappRMRTransmitted" }]}`
-       var m1 []interface{} = metricsStringToInterfaceArray(metricsJson1)
-       var m2 []interface{} = metricsStringToInterfaceArray(metricsJson2)
+       m1 := metricsStringToInterfaceArray(metricsJSON1)
+       m2 := metricsStringToInterfaceArray(metricsJSON2)
        appMetrics = parseMetricsRules(m1, appMetrics)
        appMetrics = parseMetricsRules(m2, appMetrics)
        assert.Len(t, appMetrics, 2)
@@ -226,18 +249,18 @@ func TestParseXAppDescriptor(t *testing.T) {
 }
 
 func TestParseXAppDescriptorWithNoConfig(t *testing.T) {
-       metricsJson := `[{{"metadata": "something", "descriptor": "somethingelse"}},
+       metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse"}},
                         {{"metadata": "something", "descriptor": "somethingelse"}}]`
-       metricsBytes := []byte(metricsJson)
+       metricsBytes := []byte(metricsJSON)
        appMetrics := make(AppMetrics)
        appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
        assert.Empty(t, appMetrics)
 }
 
 func TestParseXAppDescriptorWithNoMetrics(t *testing.T) {
-       metricsJson := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
+       metricsJSON := `[{{"metadata": "something", "descriptor": "somethingelse", "config":{}},
                         {{"metadata": "something", "descriptor": "somethingelse", "config":{}}}]`
-       metricsBytes := []byte(metricsJson)
+       metricsBytes := []byte(metricsJSON)
        appMetrics := make(AppMetrics)
        appMetrics = parseMetricsFromXAppDescriptor(metricsBytes, appMetrics)
        assert.Empty(t, appMetrics)