add test for data.go
diff --git a/api.go b/api.go index 79ed8d2..6afdbe3 100644 --- a/api.go +++ b/api.go
@@ -37,6 +37,12 @@ ) const ( + deploymentsEndpoint = "/configurations" + blobEndpointPath = "/blobs" + blobEndpoint = blobEndpointPath + "/{blobId}" +) + +const ( API_ERR_BAD_BLOCK = iota + 1 API_ERR_INTERNAL ) @@ -52,6 +58,10 @@ kindCollection = "Collection" ) +const ( + headerSteam = "application/octet-stream" +) + type deploymentsResult struct { deployments []DataDeployment err error @@ -83,12 +93,6 @@ ApiDeploymentsResponse []ApiDeploymentDetails `json:"contents"` } -const ( - deploymentsEndpoint = "/configurations" - blobEndpointPath = "/blob" - blobEndpoint = blobEndpointPath + "/{blobId}" -) - type apiManagerInterface interface { InitAPI() addChangedDeployment(string) @@ -197,6 +201,7 @@ } */ +// TODO use If-None-Match and ETag func (a *apiManager) apiReturnBlobData(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -215,6 +220,7 @@ if err != nil { a.writeInternalError(w, err.Error()) } + w.Header().Set("Content-type", headerSteam) }
diff --git a/api_test.go b/api_test.go index 4acd20a..d8d0668 100644 --- a/api_test.go +++ b/api_test.go
@@ -34,31 +34,30 @@ ) var _ = Describe("api", func() { - Context("GET /deployments", func() { - var testCount int - var dummyDbMan *dummyDbManager - var testApiMan *apiManager + var testCount int + var dummyDbMan *dummyDbManager + var testApiMan *apiManager - var _ = BeforeEach(func() { - testCount += 1 - dummyDbMan = &dummyDbManager{} - testApiMan = &apiManager{ - dbMan: dummyDbMan, - deploymentsEndpoint: deploymentsEndpoint + strconv.Itoa(testCount), - blobEndpoint: blobEndpointPath + strconv.Itoa(testCount) + "/{blobId}", - eTag: int64(testCount * 10), - deploymentsChanged: make(chan interface{}, 5), - addSubscriber: make(chan chan deploymentsResult), - removeSubscriber: make(chan chan deploymentsResult), - } - testApiMan.InitAPI() - time.Sleep(100 * time.Millisecond) - }) + var _ = BeforeEach(func() { + testCount += 1 + dummyDbMan = &dummyDbManager{} + testApiMan = &apiManager{ + dbMan: dummyDbMan, + deploymentsEndpoint: deploymentsEndpoint + strconv.Itoa(testCount), + blobEndpoint: blobEndpointPath + strconv.Itoa(testCount) + "/{blobId}", + eTag: int64(testCount * 10), + deploymentsChanged: make(chan interface{}, 5), + addSubscriber: make(chan chan deploymentsResult), + removeSubscriber: make(chan chan deploymentsResult), + } + testApiMan.InitAPI() + time.Sleep(100 * time.Millisecond) + }) - var _ = AfterEach(func() { - testApiMan = nil - }) - + var _ = AfterEach(func() { + testApiMan = nil + }) + Context("GET /configurations", func() { It("should get empty set if no deployments", func() { // setup http client uri, err := url.Parse(testUrl) @@ -144,6 +143,7 @@ Expect(res.StatusCode).To(Equal(http.StatusNotModified)) }) + // block is not enabled now XIt("should get empty set after blocking if no deployments", func() { start := time.Now() @@ -242,6 +242,37 @@ }) }) + + Context("GET /blobs", func() { + It("should get file bytesfrom endpoint", func() { + // setup http client + uri, err := url.Parse(testUrl) + Expect(err).Should(Succeed()) + uri.Path = blobEndpointPath + strconv.Itoa(testCount) + "/test" + + // set test data + testFile, err := ioutil.TempFile(bundlePath, "test") + randString := GenerateUUID() + testFile.Write([]byte(randString)) + err = testFile.Close() + Expect(err).Should(Succeed()) + dummyDbMan.localFSLocation = testFile.Name() + + log.Debug("randString: " + randString) + + // http get + res, err := http.Get(uri.String()) + Expect(err).Should(Succeed()) + defer res.Body.Close() + Expect(res.StatusCode).Should(Equal(http.StatusOK)) + + // parse response + body, err := ioutil.ReadAll(res.Body) + Expect(err).Should(Succeed()) + Expect(string(body)).Should(Equal(randString)) + }) + }) + }) func setTestDeployments(dummyDbMan *dummyDbManager, self string) []ApiDeploymentDetails { @@ -305,6 +336,7 @@ type dummyDbManager struct { unreadyDeployments []DataDeployment readyDeployments []DataDeployment + localFSLocation string } func (d *dummyDbManager) setDbVersion(version string) { @@ -328,7 +360,7 @@ } func (d *dummyDbManager) getLocalFSLocation(string) (string, error) { - return "", nil + return d.localFSLocation, nil } func GenerateUUID() string {
diff --git a/data_test.go b/data_test.go new file mode 100644 index 0000000..e57e56f --- /dev/null +++ b/data_test.go
@@ -0,0 +1,119 @@ +// Copyright 2017 Google 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 apiGatewayConfDeploy + +import ( + "github.com/30x/apid-core" + "github.com/30x/apid-core/data" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "strconv" + "sync" + "time" +) + +var _ = Describe("data", func() { + var testCount int + var testDbMan *dbManager + var _ = BeforeEach(func() { + testCount += 1 + testDbMan = &dbManager{ + data: services.Data(), + dbMux: sync.RWMutex{}, + } + testDbMan.setDbVersion("test" + strconv.Itoa(testCount)) + initTestDb(testDbMan.getDb()) + testDbMan.initDb() + time.Sleep(100 * time.Millisecond) + }) + + var _ = AfterEach(func() { + testDbMan = nil + data.Delete(data.VersionedDBID("common", "test"+strconv.Itoa(testCount))) + }) + + Context("db tests", func() { + It("should succefully initialized tables", func() { + // edgex_blob_available + rows, err := testDbMan.getDb().Query(` + SELECT count(*) from edgex_blob_available; + `) + Expect(err).Should(Succeed()) + defer rows.Close() + var count int + for rows.Next() { + rows.Scan(&count) + } + Expect(count).Should(Equal(0)) + + // metadata_runtime_entity_metadata + rows, err = testDbMan.getDb().Query(` + SELECT count(*) from metadata_runtime_entity_metadata; + `) + Expect(err).Should(Succeed()) + defer rows.Close() + for rows.Next() { + rows.Scan(&count) + } + Expect(count).Should(Equal(1)) + }) + + }) + +}) + +//initialize DB for tests +func initTestDb(db apid.DB) { + _, err := db.Exec(` + CREATE TABLE metadata_runtime_entity_metadata ( + id text, + organization_id text, + environment_id text, + bean_blob_id text, + resource_blob_id text, + type text, + name text, + revision text, + path text, + created_at blob, + created_by text, + updated_at blob, + updated_by text, + _change_selector text, + primary key (id)); + `) + Expect(err).Should(Succeed()) + + _, err = db.Exec(` + INSERT INTO "metadata_runtime_entity_metadata" VALUES( + '1dc4895e-6494-4b59-979f-5f4c89c073b4', + '73fcac6c-5d9f-44c1-8db0-333efda3e6e8', + '', + 'gcs:SHA-512:39ca7ae89bb9468af34df8bc873748b4035210c91bcc01359c092c1d51364b5f3df06bc69a40621acfaa46791af9ea41bc0f3429a84738ba1a7c8d394859601a', + '', + 'ORGANIZATION', + 'Org1', + '', + '/organizations/Org1/', + '2017-06-27 03:14:45.748+00:00', + 'defaultUser', + '2017-06-27 03:15:03.557+00:00', + 'defaultUser', + '73fcac6c-5d9f-44c1-8db0-333efda3e6e8' + ); + `) + Expect(err).Should(Succeed()) + +}
diff --git a/init.go b/init.go index bc8f2a9..fd12337 100644 --- a/init.go +++ b/init.go
@@ -38,6 +38,7 @@ configConcurrentDownloads = "apigeesync_concurrent_downloads" configDownloadQueueSize = "apigeesync_download_queue_size" configBlobServerBaseURI = "apigeesync_blob_server_base" + configStoragePath = "local_storage_path" ) var ( @@ -140,7 +141,7 @@ blobServerURL = config.GetString(configBlobServerBaseURI) relativeBundlePath := config.GetString(configBundleDirKey) - storagePath := config.GetString("local_storage_path") + storagePath := config.GetString(configStoragePath) bundlePath = path.Join(storagePath, relativeBundlePath) if err := os.MkdirAll(bundlePath, 0700); err != nil { return pluginData, fmt.Errorf("Failed bundle directory creation: %v", err)