Read PrimaryCollector parameters from env variables

The following environment variables are expected:
VESMGR_PRICOLLECTOR_USER
VESMGR_PRICOLLECTOR_PASSWORD
VESMGR_PRICOLLECTOR_PASSPHRASE
VESMGR_PRICOLLECTOR_SERVERROOT
VESMGR_PRICOLLECTOR_TOPIC
VESMGR_PRICOLLECTOR_SECURE

Vesmgr now passes the parameters to VESPA via yaml instead of cmd line.

This commit contains also the following changes:
- Correcttion to VESPA configuration: FixAdditionalObject --> FixAdditionalObjects
- Added UT run to Dockerfile as part of the build.

Change-Id: Ib15916dba8c22a19a50da6d77e1e5a54418f2ff4
Signed-off-by: Katri Turunen <katri.turunen@nokia.com>
diff --git a/Dockerfile b/Dockerfile
index cf33106..7ff0724 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -40,6 +40,11 @@
 
 RUN GO111MODULE=on go mod download
 
+# Run vesmgr UT
+RUN export GOPATH=$HOME/go && \
+    export PATH=$GOPATH/bin:$GOROOT/bin:$PATH && \
+    go test ./...
+
 # Install vesmgr
 RUN export GOPATH=$HOME/go && \
     export PATH=$GOPATH/bin:$GOROOT/bin:$PATH && \
diff --git a/README.md b/README.md
index c7a9b19..534b7b2 100644
--- a/README.md
+++ b/README.md
@@ -10,12 +10,19 @@
 
 The VESPA manager container requires the following environment variables:
 
-* VESMGR_HB_INTERVAL - VES heartbeat interval. For example: 30s.
-* VESMGR_MEAS_INTERVAL - Measurement interval. For example: 60s.
-* VESMGR_PRICOLLECTOR_ADDR - Primary collector IP address. For example: 127.0.0.1.
-* VESMGR_PRICOLLECTOR_PORT - Primary collector port id as an integer. For example: 1234.
+* VESMGR_HB_INTERVAL - VES heartbeat interval as a string. For example: 30s.
+* VESMGR_MEAS_INTERVAL - Measurement interval as a string. For example: 60s.
 * VESMGR_PROMETHEUS_ADDR - Prometheus address. For example: http://127.0.0.1:123
 
+* VESMGR_PRICOLLECTOR_ADDR - Primary collector FQDN as a string. For example: ricaux-entry.
+* VESMGR_PRICOLLECTOR_PORT - Primary collector port id as an integer. Default: 8443.
+* VESMGR_PRICOLLECTOR_SERVERROOT - Path before the /eventListener part of the POST URL as a string.
+* VESMGR_PRICOLLECTOR_TOPIC - Primary collector topic as a string.
+* VESMGR_PRICOLLECTOR_SECURE - Use HTTPS for VES collector. Possible string values: true or false.
+* VESMGR_PRICOLLECTOR_USER - User name as a string.
+* VESMGR_PRICOLLECTOR_PASSWORD - Password as a string.
+* VESMGR_PRICOLLECTOR_PASSPHASE - Passphrase as a string.
+
 # Unit Tests
 
 In order to run the VESPA manager unit tests, give the following command:
diff --git a/cmd/vesmgr/config.go b/cmd/vesmgr/config.go
index a2f5466..7c4a2dc 100644
--- a/cmd/vesmgr/config.go
+++ b/cmd/vesmgr/config.go
@@ -21,17 +21,14 @@
 	"gopkg.in/yaml.v2"
 	"time"
 	"io"
+	"os"
+	"strconv"
 )
 
 func basicVespaConf() VESAgentConfiguration {
 	var vespaconf = VESAgentConfiguration {
 		DataDir: "/tmp/data",
 		Debug:   false,
-		PrimaryCollector: CollectorConfiguration {
-			User: "user",
-			Password: "pass",
-			PassPhrase: "pass",
-		},
 		Event: EventConfiguration {
 			VNFName: "vespa-demo", // XXX
 			ReportingEntityID: "1af5bfa9-40b4-4522-b045-40e54f0310f", // XXX
@@ -71,7 +68,7 @@
 	// XXX
 	makeRule := func(expr string, obj_name string, obj_instance string) MetricRule {
 		return MetricRule {
-			Target: "AdditionalObject",
+			Target: "AdditionalObjects",
 			Expr: expr,
 			ObjectInstance: obj_instance,
 			ObjectName: obj_name,
@@ -95,12 +92,35 @@
 
 }
 
+func getCollectorConfiguration(vespaconf *VESAgentConfiguration) {
+	vespaconf.PrimaryCollector.User = os.Getenv("VESMGR_PRICOLLECTOR_USER")
+	vespaconf.PrimaryCollector.Password = os.Getenv("VESMGR_PRICOLLECTOR_PASSWORD")
+	vespaconf.PrimaryCollector.PassPhrase = os.Getenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
+	vespaconf.PrimaryCollector.FQDN = os.Getenv("VESMGR_PRICOLLECTOR_ADDR")
+	vespaconf.PrimaryCollector.ServerRoot = os.Getenv("VESMGR_PRICOLLECTOR_SERVERROOT")
+	vespaconf.PrimaryCollector.Topic = os.Getenv("VESMGR_PRICOLLECTOR_TOPIC")
+	port_str := os.Getenv("VESMGR_PRICOLLECTOR_PORT")
+	if port_str == "" {
+		vespaconf.PrimaryCollector.Port = 8443
+	} else {
+		port, _ := strconv.Atoi(port_str)
+		vespaconf.PrimaryCollector.Port = port
+	}
+	secure_str := os.Getenv("VESMGR_PRICOLLECTOR_SECURE")
+	if secure_str == "true" {
+		vespaconf.PrimaryCollector.Secure = true
+	} else {
+		vespaconf.PrimaryCollector.Secure = false
+	}
+}
+
 func createVespaConfig(writer io.Writer) {
 	vespaconf := basicVespaConf()
 	getRules(&vespaconf)
+	getCollectorConfiguration(&vespaconf)
 	err := yaml.NewEncoder(writer).Encode(vespaconf)
 	if err != nil {
 		logger.Error("Cannot write vespa conf file: %s", err.Error())
 		return
 	}
-}
\ No newline at end of file
+}
diff --git a/cmd/vesmgr/config_test.go b/cmd/vesmgr/config_test.go
index 0a809b1..b255db9 100644
--- a/cmd/vesmgr/config_test.go
+++ b/cmd/vesmgr/config_test.go
@@ -22,26 +22,87 @@
 	"bytes"
 	"github.com/stretchr/testify/assert"
 	"gopkg.in/yaml.v2"
- )
+	"os"
+)
 
- func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
+func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
 	assert.Equal(t, "/tmp/data", vesconf.DataDir)
 	assert.False(t, vesconf.Debug)
 	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)
- }
+}
 
- func TestBasicConfigContainsCorrectValues(t *testing.T) {
+func TestBasicConfigContainsCorrectValues(t *testing.T) {
 	vesconf := basicVespaConf()
 	testBaseConf(t, vesconf)
- }
+}
 
- func TestYamlGeneratio(t *testing.T) {
+func TestCollectorConfiguration(t *testing.T) {
+	os.Setenv("VESMGR_PRICOLLECTOR_USER", "user123")
+	os.Setenv("VESMGR_PRICOLLECTOR_PASSWORD", "pass123")
+	os.Setenv("VESMGR_PRICOLLECTOR_PASSPHRASE", "phrase123")
+	os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "1.2.3.4")
+	os.Setenv("VESMGR_PRICOLLECTOR_PORT", "1234")
+	os.Setenv("VESMGR_PRICOLLECTOR_SERVERROOT", "vescollector")
+	os.Setenv("VESMGR_PRICOLLECTOR_TOPIC", "sometopic")
+	os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "true")
+
+	vesconf := basicVespaConf()
+	getCollectorConfiguration(&vesconf)
+
+	assert.Equal(t, "user123", vesconf.PrimaryCollector.User)
+	assert.Equal(t, "pass123", vesconf.PrimaryCollector.Password)
+	assert.Equal(t, "phrase123", vesconf.PrimaryCollector.PassPhrase)
+	assert.Equal(t, "1.2.3.4", vesconf.PrimaryCollector.FQDN)
+	assert.Equal(t, 1234, vesconf.PrimaryCollector.Port)
+	assert.Equal(t, "vescollector", vesconf.PrimaryCollector.ServerRoot)
+	assert.Equal(t, "sometopic", vesconf.PrimaryCollector.Topic)
+	assert.Equal(t, true, vesconf.PrimaryCollector.Secure)
+}
+
+func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
+	os.Unsetenv("VESMGR_PRICOLLECTOR_USER")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_PASSWORD")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_ADDR")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_PORT")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_SERVERROOT")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_TOPIC")
+	os.Unsetenv("VESMGR_PRICOLLECTOR_SECURE")
+
+	vesconf := basicVespaConf()
+	getCollectorConfiguration(&vesconf)
+
+	assert.Equal(t, "", vesconf.PrimaryCollector.User)
+	assert.Equal(t, "", vesconf.PrimaryCollector.Password)
+	assert.Equal(t, "", vesconf.PrimaryCollector.PassPhrase)
+	assert.Equal(t, "", vesconf.PrimaryCollector.FQDN)
+	assert.Equal(t, 8443, vesconf.PrimaryCollector.Port)
+	assert.Equal(t, "", vesconf.PrimaryCollector.ServerRoot)
+	assert.Equal(t, "", vesconf.PrimaryCollector.Topic)
+	assert.Equal(t, false, vesconf.PrimaryCollector.Secure)
+}
+
+func TestCollectorConfigurationWhenPrimaryCollectorPortIsNotInteger(t *testing.T) {
+	os.Setenv("VESMGR_PRICOLLECTOR_PORT", "abcd")
+	vesconf := basicVespaConf()
+	getCollectorConfiguration(&vesconf)
+	assert.Equal(t, 0, vesconf.PrimaryCollector.Port)
+}
+
+func TestCollectorConfigurationWhenPrimaryCollectorSecureIsNotTrueOrFalse(t *testing.T) {
+	os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "foo")
+	vesconf := basicVespaConf()
+	getCollectorConfiguration(&vesconf)
+	assert.Equal(t, false, vesconf.PrimaryCollector.Secure)
+}
+
+func TestYamlGeneration(t *testing.T) {
 	buffer := new(bytes.Buffer)
 	createVespaConfig(buffer)
 	var vesconf VESAgentConfiguration
 	err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
 	assert.Nil(t, err)
 	testBaseConf(t, vesconf)
- }
+}
diff --git a/cmd/vesmgr/vesmgr.go b/cmd/vesmgr/vesmgr.go
index 436842b..0867517 100755
--- a/cmd/vesmgr/vesmgr.go
+++ b/cmd/vesmgr/vesmgr.go
@@ -63,7 +63,7 @@
 
 func startVesagent() chan error {
 	/* Start ves-agent */
-	cmd := exec.Command(vesagent.name, "-i", os.Getenv("VESMGR_HB_INTERVAL"), "-m", os.Getenv("VESMGR_MEAS_INTERVAL"), "-f", os.Getenv("VESMGR_PRICOLLECTOR_ADDR"), "-p", os.Getenv("VESMGR_PRICOLLECTOR_PORT"), "--Measurement.Prometheus.Address", os.Getenv("VESMGR_PROMETHEUS_ADDR"))
+	cmd := exec.Command(vesagent.name, "-i", os.Getenv("VESMGR_HB_INTERVAL"), "-m", os.Getenv("VESMGR_MEAS_INTERVAL"), "--Measurement.Prometheus.Address", os.Getenv("VESMGR_PROMETHEUS_ADDR"))
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
 	ch := make(chan error)
diff --git a/ves-agent-chart/templates/deployment.yaml b/ves-agent-chart/templates/deployment.yaml
index 4fcb131..1bd4560 100644
--- a/ves-agent-chart/templates/deployment.yaml
+++ b/ves-agent-chart/templates/deployment.yaml
@@ -46,4 +46,7 @@
           envFrom:
             - configMapRef:
                 name: vespa-config
+            - secretRef:
+                name: vespa-secrets
+