summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShashank Kumar Shankar <shashank.kumar.shankar@intel.com>2018-05-23 16:01:03 -0700
committerShashank Kumar Shankar <shashank.kumar.shankar@intel.com>2018-05-23 16:03:24 -0700
commit18d9d994e85ef897d570081bc3a7e1da42b49c1e (patch)
treec1e05000de167c04f14fd81cc25fb8583078463d
parent5e6124aa915bd66734214ded0a25740b544bfa52 (diff)
Create Token Service JSON dynamically2.0.0-ONAPbeijing2.0.0-ONAP
This patch makes sure the Token Service JSON is dynically created so that this fits better when deployed on Kubernetes. Change-Id: I4426f68af2a6de4d2ffe4f488d5660c47f13ccaf Issue-ID: MUSIC-55 Signed-off-by: Shashank Kumar Shankar <shashank.kumar.shankar@intel.com>
-rw-r--r--deployment/Dockerfile10
-rwxr-xr-xdeployment/docker-build.sh2
-rw-r--r--src/dkv/api/initialise.go14
-rw-r--r--src/dkv/api/initialise_test.go66
-rw-r--r--src/dkv/api/token_service_map.json1
-rw-r--r--src/dkv/api/utils.go30
-rw-r--r--src/dkv/api/utils_test.go42
7 files changed, 149 insertions, 16 deletions
diff --git a/deployment/Dockerfile b/deployment/Dockerfile
index 1c1e3f6..f9c45ce 100644
--- a/deployment/Dockerfile
+++ b/deployment/Dockerfile
@@ -9,11 +9,11 @@ ENV DATASTORE_IP $DATASTORE_IP
ENV CONSUL_VERSION 1.0.6
RUN apk update && \
- apk add g++ && \
- apk add make && \
+ # apk add g++ && \
+ # apk add make && \
apk add unzip && \
- apk add git && \
- apk add curl && \
+ # apk add git && \
+ # apk add curl && \
apk add wget && \
apk add --no-cache bash
@@ -34,7 +34,7 @@ RUN mkdir /dkv_mount_path && \
WORKDIR /dkv_mount_path
ADD ./dkv /dkv_mount_path/
-ADD ./token_service_map.json /dkv_mount_path/api/
+# ADD ./token_service_map.json /dkv_mount_path/api/
ADD ./docker-entrypoint.sh /dkv_mount_path/
ENTRYPOINT /dkv_mount_path/docker-entrypoint.sh \ No newline at end of file
diff --git a/deployment/docker-build.sh b/deployment/docker-build.sh
index 3b7e3b3..4568e53 100755
--- a/deployment/docker-build.sh
+++ b/deployment/docker-build.sh
@@ -23,7 +23,7 @@ function generate_binary {
cp ../target/dkv .
# Change the following work around for reading token_service.json
- cp ../src/dkv/api/token_service_map.json .
+ # cp ../src/dkv/api/token_service_map.json .
}
function build_image {
diff --git a/src/dkv/api/initialise.go b/src/dkv/api/initialise.go
index ac8825e..228a46f 100644
--- a/src/dkv/api/initialise.go
+++ b/src/dkv/api/initialise.go
@@ -18,6 +18,7 @@ package api
import (
"errors"
+ "log"
"os"
)
@@ -31,6 +32,7 @@ func Initialise() error {
if os.Getenv("DATASTORE") == "" {
return errors.New("DATASTORE environment variable not set.")
}
+
if os.Getenv("DATASTORE") == "consul" {
Datastore = &ConsulStruct{}
} else if os.Getenv("DATASTORE") == "cassandra" {
@@ -38,10 +40,20 @@ func Initialise() error {
} else {
return errors.New("Unrecognised Datastore. Supports only consul or cassandra")
}
+
+ jsonExists, err := JsonChecker(JSONPATH)
+ if jsonExists == false {
+ log.Println("[INFO] token_service_map.json not found. Creating.")
+ err = JsonCreate(JSONPATH)
+ if err != nil {
+ return err
+ }
+ }
+
KeyValues = &KeyValuesStruct{}
Directory = &DirectoryStruct{directory: ""}
- err := Datastore.InitializeDatastoreClient()
+ err = Datastore.InitializeDatastoreClient()
if err != nil {
return err
}
diff --git a/src/dkv/api/initialise_test.go b/src/dkv/api/initialise_test.go
index 363edce..2d54bec 100644
--- a/src/dkv/api/initialise_test.go
+++ b/src/dkv/api/initialise_test.go
@@ -22,41 +22,93 @@ import (
"testing"
)
-func TestInitialise_cassandra(t *testing.T) {
+func TestInitialise_consul(t *testing.T) {
oldDatastore_ip := os.Getenv("DATASTORE_IP")
oldDatastore_type := os.Getenv("DATASTORE")
+ oldJsonExists := JsonChecker
+ oldJsonCreate := JsonCreate
os.Setenv("DATASTORE_IP", "localhost")
- os.Setenv("DATASTORE", "cassandra")
+ os.Setenv("DATASTORE", "consul")
defer func() {
os.Setenv("DATASTORE_IP", oldDatastore_ip)
os.Setenv("DATASTORE", oldDatastore_type)
+ JsonCreate = oldJsonCreate
+ JsonChecker = oldJsonExists
}()
+ JsonChecker = func(path string) (bool, error) {
+ return false, nil
+ }
+
+ JsonCreate = func(path string) error {
+ return nil
+ }
+
err := Initialise()
- assert.Nil(t, err)
+ assert.NotNil(t, err)
}
-func TestInitialise_consulError(t *testing.T) {
+
+func TestInitialise_cassandra(t *testing.T) {
oldDatastore_ip := os.Getenv("DATASTORE_IP")
oldDatastore_type := os.Getenv("DATASTORE")
+ oldMOUNTPATH := os.Getenv("MOUNTPATH")
+ oldJsonChecker := JsonChecker
os.Setenv("DATASTORE_IP", "localhost")
- os.Setenv("DATASTORE", "consul")
+ os.Setenv("DATASTORE", "cassandra")
defer func() {
os.Setenv("DATASTORE_IP", oldDatastore_ip)
os.Setenv("DATASTORE", oldDatastore_type)
+ os.Setenv("MOUNTPATH", oldMOUNTPATH)
+ JsonChecker = oldJsonChecker
}()
+ JsonChecker = func(path string) (bool, error) {
+ return true, nil
+ }
+
+ err := Initialise()
+ assert.Nil(t, err)
+}
+
+func TestInitialise_datastoreUnknown(t *testing.T) {
+ datastore := os.Getenv("DATASTORE")
+ defer os.Setenv("DATASTORE", datastore)
+ os.Setenv("DATASTORE", "test")
+
err := Initialise()
assert.NotNil(t, err)
}
-func TestInitialise_datastoreEmptyError(t *testing.T) {
+func TestInitialise_datastoreEmpty(t *testing.T) {
datastore := os.Getenv("DATASTORE")
- os.Unsetenv("DATASTORE")
defer os.Setenv("DATASTORE", datastore)
+ os.Setenv("DATASTORE", "")
+
+ err := Initialise()
+ assert.NotNil(t, err)
+}
+
+func TestInitialise_noJSON(t *testing.T) {
+ oldDatastore_ip := os.Getenv("DATASTORE_IP")
+ oldDatastore_type := os.Getenv("DATASTORE")
+ oldJsonChecker := JsonChecker
+
+ os.Setenv("DATASTORE_IP", "localhost")
+ os.Setenv("DATASTORE", "consul")
+
+ defer func() {
+ os.Setenv("DATASTORE_IP", oldDatastore_ip)
+ os.Setenv("DATASTORE", oldDatastore_type)
+ JsonChecker = oldJsonChecker
+ }()
+
+ JsonChecker = func(path string) (bool, error) {
+ return false, nil
+ }
err := Initialise()
assert.NotNil(t, err)
diff --git a/src/dkv/api/token_service_map.json b/src/dkv/api/token_service_map.json
deleted file mode 100644
index 51d4dbb..0000000
--- a/src/dkv/api/token_service_map.json
+++ /dev/null
@@ -1 +0,0 @@
-[{"token":"default","service":"default"}] \ No newline at end of file
diff --git a/src/dkv/api/utils.go b/src/dkv/api/utils.go
index c1094ab..383f99c 100644
--- a/src/dkv/api/utils.go
+++ b/src/dkv/api/utils.go
@@ -27,6 +27,8 @@ var (
IoutilRead = ioutil.ReadFile
IoutilWrite = ioutil.WriteFile
JsonReader = ReadJSON
+ JsonChecker = CheckJSONExists
+ JsonCreate = CreateJSON
)
type Token_service_map struct {
@@ -34,6 +36,34 @@ type Token_service_map struct {
Service string `json:"service"`
}
+func CheckJSONExists(path string) (bool, error) {
+ _, err := IoutilRead(path)
+
+ if err != nil {
+ return false, err
+ } else {
+ return true, nil
+ }
+}
+
+func CreateJSON(path string) error {
+ var tsm Token_service_map
+ var tsm_list []Token_service_map
+
+ tsm.Token = "default"
+ tsm.Service = "default"
+ tsm_list = append(tsm_list, tsm)
+ raw, err := json.Marshal(tsm_list)
+ if err != nil {
+ return err
+ }
+ err = IoutilWrite(path, raw, 0644)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
func ReadJSON(path string) ([]Token_service_map, error) {
var tsm_list []Token_service_map
// raw, err := ioutil.ReadFile("./token_service_map.json")
diff --git a/src/dkv/api/utils_test.go b/src/dkv/api/utils_test.go
index 0bca7c5..8b21e52 100644
--- a/src/dkv/api/utils_test.go
+++ b/src/dkv/api/utils_test.go
@@ -21,6 +21,47 @@ import (
"testing"
)
+func TestCheckJSONExists(t *testing.T) {
+ oldIoutilRead := IoutilRead
+
+ defer func() {
+ IoutilRead = oldIoutilRead
+ }()
+
+ IoutilRead = func(path string) ([]byte, error) {
+ return []byte("test"), nil
+ }
+
+ _, err := JsonChecker("path")
+ assert.Equal(t, nil, err, "Error should be nil.")
+}
+
+func TestCreateJSON(t *testing.T) {
+ oldIoutilWrite := IoutilWrite
+
+ defer func() {
+ IoutilWrite = oldIoutilWrite
+ }()
+
+ IoutilWrite = func(val string, b []byte, f os.FileMode) error {
+ return nil
+ }
+
+ err := JsonCreate("path")
+ assert.Equal(t, nil, err, "Error should be nil.")
+}
+
+func TestCheckJSONExists_err(t *testing.T) {
+ oldIoutilRead := IoutilRead
+
+ defer func() {
+ IoutilRead = oldIoutilRead
+ }()
+
+ _, err := JsonChecker("path")
+ assert.NotNil(t, err, "Err should not be nil.")
+}
+
func TestReadJSON(t *testing.T) {
oldIoutilRead := IoutilRead
@@ -77,7 +118,6 @@ func TestWriteJSON(t *testing.T) {
assert.Equal(t, nil, err, "Error should be nil.")
}
-
func TestDeleteInJSON(t *testing.T) {
oldReadJson := JsonReader
defer func() {