Adding more unit tests and updating makefile 43/31643/3
authorKiran <kiran.k.kamineni@intel.com>
Tue, 13 Feb 2018 23:21:46 +0000 (15:21 -0800)
committerKiran <kiran.k.kamineni@intel.com>
Tue, 13 Feb 2018 23:22:00 +0000 (15:22 -0800)
Added unit tests for backend, vault, config and handler
Updated the makefile to call the tests when build target is invoked

Issue-ID: AAF-118
Change-Id: Id9b719a0e2b46070867a7fcbae34b83d19ef3282
Signed-off-by: Kiran <kiran.k.kamineni@intel.com>
sms-service/.gitignore
sms-service/src/sms/Makefile
sms-service/src/sms/backend/backend_test.go [new file with mode: 0644]
sms-service/src/sms/backend/vault_test.go [new file with mode: 0644]
sms-service/src/sms/config/config_test.go [new file with mode: 0644]
sms-service/src/sms/handler/handler.go
sms-service/src/sms/handler/handler_test.go [new file with mode: 0644]
sms-service/src/sms/test/smsconfig_test.json [new file with mode: 0644]

index e98d43b..b83340a 100644 (file)
@@ -1,4 +1,3 @@
 pkg/
 src/sms/.vscode/
 src/sms/vendor/
-src/sms/test/
index a206c55..2751fff 100644 (file)
@@ -4,22 +4,24 @@ DEPENDENCIES := github.com/golang/dep/cmd/dep
 
 export GOPATH ...
 
-all: build test
-deploy: build test
+all: test build
+deploy: test build
 
 build: deps format
-       $(GOPATH)/bin/dep ensure
        go build -o $(GOPATH)/target/$(BINARY) -v sms.go
 
 clean:
        go clean
        rm -f $(GOPATH)/target/$(BINARY)
 
-test:
-       go test -v ./...
+test: deps
+       go test -cover ./...
 
 format:
        go fmt ./...
 
 deps:
-       go get -u $(DEPENDENCIES)
\ No newline at end of file
+       go get -u $(DEPENDENCIES)
+       $(GOPATH)/bin/dep ensure
+
+.PHONY: test
diff --git a/sms-service/src/sms/backend/backend_test.go b/sms-service/src/sms/backend/backend_test.go
new file mode 100644 (file)
index 0000000..a3a616c
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2018 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package backend
+
+import (
+       smsconfig "sms/config"
+       "testing"
+)
+
+func TestInitSecretBackend(t *testing.T) {
+       smsconfig.SMSConfig = &smsconfig.SMSConfiguration{VaultAddress: "http://localhost:8200"}
+       sec, err := InitSecretBackend()
+       // We don't expect an error to be returned as Init only creates a client
+       // Does not expect backend to be running
+       if err != nil {
+               t.Fatal("InitSecretBackend : error creating")
+       }
+       if sec == nil {
+               t.Fatal("InitSecretBackend: returned SecretBackend was nil")
+       }
+}
diff --git a/sms-service/src/sms/backend/vault_test.go b/sms-service/src/sms/backend/vault_test.go
new file mode 100644 (file)
index 0000000..cd7b5a5
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2018 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package backend
+
+import (
+       smsconfig "sms/config"
+       "testing"
+)
+
+var v *Vault
+
+func init() {
+       v = &Vault{}
+}
+
+func TestInit(t *testing.T) {
+       smsconfig.SMSConfig = &smsconfig.SMSConfiguration{VaultAddress: "http://localhost:8200"}
+       v.Init()
+       if v.vaultClient == nil {
+               t.Fatal("Init: Init() failed to create vaultClient")
+       }
+}
+
+func TestGetStatus(t *testing.T) {
+       _, err := v.GetStatus()
+       // Expect error as vault is not running
+       if err == nil {
+               t.Fatal("GetStatus: Error expected, none found")
+       }
+}
diff --git a/sms-service/src/sms/config/config_test.go b/sms-service/src/sms/config/config_test.go
new file mode 100644 (file)
index 0000000..0fcc9af
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package config
+
+import (
+       "runtime"
+       "testing"
+)
+
+func TestReadConfigurationFile(t *testing.T) {
+       conf, err := ReadConfigFile("filedoesnotexist.json")
+       if err == nil {
+               t.Fatal("ReadConfiguationFile: Expected Error, none found")
+       }
+
+       _, v, _, _ := runtime.Caller(0)
+       t.Log(v)
+
+       conf, err = ReadConfigFile("../test/smsconfig_test.json")
+       if err != nil {
+               t.Fatal("ReadConfigurationFile: Error reading file")
+       }
+       if conf.CAFile != "testca.pem" {
+               t.Fatal("ReadConfigurationFile: Incorrect entry read from file")
+       }
+}
index 1b9b869..7a3b7dc 100644 (file)
@@ -24,6 +24,8 @@ import (
        "sms/backend"
 )
 
+// handler stores two interface implementations that implement
+// the backend functionality
 type handler struct {
        secretBackend backend.SecretBackend
        loginBackend  backend.LoginBackend
@@ -94,7 +96,7 @@ func (h handler) deleteSecretHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 // struct that tracks various status items for SMS and backend
-type status struct {
+type backendStatus struct {
        Seal bool `json:"sealstatus"`
 }
 
@@ -106,7 +108,7 @@ func (h handler) statusHandler(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       status := status{Seal: s}
+       status := backendStatus{Seal: s}
        err = json.NewEncoder(w).Encode(status)
        if err != nil {
                http.Error(w, err.Error(), 500)
@@ -120,6 +122,7 @@ func (h handler) loginHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 // CreateRouter returns an http.Handler for the registered URLs
+// Takes an interface implementation as input
 func CreateRouter(b backend.SecretBackend) http.Handler {
        h := handler{secretBackend: b}
 
diff --git a/sms-service/src/sms/handler/handler_test.go b/sms-service/src/sms/handler/handler_test.go
new file mode 100644 (file)
index 0000000..3ca2ae6
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2018 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package handler
+
+import (
+       "encoding/json"
+       "net/http"
+       "net/http/httptest"
+       "reflect"
+       smsbackend "sms/backend"
+       "strings"
+       "testing"
+)
+
+var h handler
+
+// Here we are using the anonymous variable feature of golang to
+// override methods form an interface
+type TestBackend struct {
+       smsbackend.SecretBackend
+}
+
+func (b *TestBackend) Init() error {
+       return nil
+}
+
+func (b *TestBackend) GetStatus() (bool, error) {
+       return true, nil
+}
+
+func (b *TestBackend) GetSecretDomain(name string) (smsbackend.SecretDomain, error) {
+       return smsbackend.SecretDomain{}, nil
+}
+
+func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) {
+       return smsbackend.Secret{}, nil
+}
+
+func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) {
+       return smsbackend.SecretDomain{}, nil
+}
+
+func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) (smsbackend.Secret, error) {
+       return smsbackend.Secret{}, nil
+}
+
+func (b *TestBackend) DeleteSecretDomain(name string) error {
+       return nil
+}
+
+func (b *TestBackend) DeleteSecret(dom string, name string) error {
+       return nil
+}
+
+func init() {
+       testBackend := &TestBackend{}
+       h = handler{secretBackend: testBackend}
+}
+
+func TestCreateRouter(t *testing.T) {
+       router := CreateRouter(h.secretBackend)
+       if router == nil {
+               t.Fatal("CreateRouter: Got error when none expected")
+       }
+}
+
+func TestStatusHandler(t *testing.T) {
+       req, err := http.NewRequest("GET", "/v1/sms/status", nil)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       rr := httptest.NewRecorder()
+       hr := http.HandlerFunc(h.statusHandler)
+
+       hr.ServeHTTP(rr, req)
+
+       ret := rr.Code
+       if ret != http.StatusOK {
+               t.Errorf("statusHandler returned wrong status code: %v vs %v",
+                       ret, http.StatusOK)
+       }
+
+       expected := backendStatus{}
+       got := backendStatus{}
+       expectedStr := strings.NewReader(`{"sealstatus":true}`)
+       json.NewDecoder(expectedStr).Decode(&expected)
+       json.NewDecoder(rr.Body).Decode(&got)
+
+       if reflect.DeepEqual(expected, got) == false {
+               t.Errorf("statusHandler returned unexpected body: got %v vs %v",
+                       rr.Body.String(), expectedStr)
+       }
+}
diff --git a/sms-service/src/sms/test/smsconfig_test.json b/sms-service/src/sms/test/smsconfig_test.json
new file mode 100644 (file)
index 0000000..b34bf78
--- /dev/null
@@ -0,0 +1,7 @@
+{
+    "cafile": "testca.pem",
+    "servercert": "testserver.cert",
+    "serverkey": "testserver.key",
+
+    "vaultaddress": "http://localhost:8200"
+}