[ISSUE-66918282] refactor tests
diff --git a/api_test.go b/api_test.go index fc97ea6..630d993 100644 --- a/api_test.go +++ b/api_test.go
@@ -24,7 +24,6 @@ mathrand "math/rand" "net/http" "net/url" - "os" "strconv" "strings" "time" @@ -539,78 +538,3 @@ } return detail } - -type dummyDbManager struct { - unreadyBlobIds []string - readyDeployments []Configuration - localFSLocation string - fileResponse chan string - version string - configurations map[string]*Configuration - lsn string - err error -} - -func (d *dummyDbManager) setDbVersion(version string) { - d.version = version -} - -func (d *dummyDbManager) initDb() error { - return nil -} - -func (d *dummyDbManager) getUnreadyBlobs() ([]string, error) { - return d.unreadyBlobIds, nil -} - -func (d *dummyDbManager) getReadyConfigurations(typeFilter string) ([]Configuration, error) { - if typeFilter == "" { - return d.readyDeployments, nil - } - return []Configuration{*(d.configurations[typeFilter])}, nil -} - -func (d *dummyDbManager) getAllConfigurations(typeFilter string) ([]Configuration, error) { - if typeFilter == "" { - return d.readyDeployments, nil - } - return []Configuration{*(d.configurations[typeFilter])}, nil -} - -func (d *dummyDbManager) updateLocalFsLocation(blobId, localFsLocation string) error { - file, err := os.Open(localFsLocation) - if err != nil { - return err - } - buff := make([]byte, 36) - _, err = file.Read(buff) - if err != nil { - return err - } - go func(buff []byte) { - d.fileResponse <- string(buff) - }(buff) - - return nil -} - -func (d *dummyDbManager) getLocalFSLocation(string) (string, error) { - return d.localFSLocation, nil -} - -func (d *dummyDbManager) getConfigById(id string) (*Configuration, error) { - return d.configurations[id], d.err -} -func (d *dummyDbManager) getLSN() string { - return d.lsn -} - -func (d *dummyDbManager) updateLSN(LSN string) error { - d.lsn = LSN - return nil -} - -func (d *dummyDbManager) loadLsnFromDb() error { - - return nil -}
diff --git a/bundle.go b/bundle.go index ab7e073..c9d7d80 100644 --- a/bundle.go +++ b/bundle.go
@@ -106,7 +106,6 @@ } } -//TODO: add tests for this func (bm *bundleManager) downloadBlobsWithCallback(blobs []string, callback func()) { c := &BunchDownloadRequest{
diff --git a/bundle_test.go b/bundle_test.go index fc1ec0d..c77114f 100644 --- a/bundle_test.go +++ b/bundle_test.go
@@ -17,15 +17,11 @@ import ( "net/http" - "bytes" - "encoding/json" "github.com/apid/apid-core/util" - "github.com/gorilla/mux" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "io" mathrand "math/rand" - "strings" "sync/atomic" "time" ) @@ -220,78 +216,3 @@ }) }) - -type dummyApiManager struct { - initCalled chan bool - notifyChan chan bool -} - -func (a *dummyApiManager) InitAPI() { - go func() { - a.initCalled <- true - }() -} - -func (a *dummyApiManager) notifyNewChange() { - a.notifyChan <- true -} - -type dummyBlobServer struct { - serverEndpoint string - signedEndpoint string - signedTimeout *int32 - blobTimeout *int32 - resetTimeout bool -} - -func (b *dummyBlobServer) start() { - services.API().HandleFunc(b.serverEndpoint, b.returnSigned).Methods("GET") - services.API().HandleFunc(b.signedEndpoint, b.returnBlob).Methods("GET") -} - -// send a dummy uri as response -func (b *dummyBlobServer) returnSigned(w http.ResponseWriter, r *http.Request) { - defer GinkgoRecover() - if atomic.LoadInt32(b.signedTimeout) == int32(1) { - if b.resetTimeout { - atomic.StoreInt32(b.signedTimeout, 0) - } - time.Sleep(time.Second) - } - vars := mux.Vars(r) - blobId := vars["blobId"] - - uriString := strings.Replace(bundleTestUrl+b.signedEndpoint, "{blobId}", blobId, 1) - log.Debug("dummyBlobServer returnSigned: " + uriString) - - res := blobServerResponse{ - Id: blobId, - Kind: "Blob", - Self: r.RequestURI, - SignedUrl: uriString, - SignedUrlExpiryTimestamp: time.Now().Add(3 * time.Hour).Format(time.RFC3339), - } - - resBytes, err := json.Marshal(res) - Expect(err).Should(Succeed()) - _, err = io.Copy(w, bytes.NewReader(resBytes)) - Expect(err).Should(Succeed()) - w.Header().Set("Content-Type", headerSteam) -} - -// send blobId back as response -func (b *dummyBlobServer) returnBlob(w http.ResponseWriter, r *http.Request) { - defer GinkgoRecover() - if atomic.LoadInt32(b.blobTimeout) == int32(1) { - if b.resetTimeout { - atomic.StoreInt32(b.blobTimeout, 0) - } - time.Sleep(time.Second) - } - vars := mux.Vars(r) - blobId := vars["blobId"] - log.Debug("dummyBlobServer returnBlob id=" + blobId) - _, err := io.Copy(w, bytes.NewReader([]byte(blobId))) - Expect(err).Should(Succeed()) - w.Header().Set("Content-Type", headerSteam) -}
diff --git a/data.go b/data.go index 869c90b..9734130 100644 --- a/data.go +++ b/data.go
@@ -53,7 +53,6 @@ setDbVersion(string) initDb() error getUnreadyBlobs() ([]string, error) - getReadyConfigurations(typeFilter string) ([]Configuration, error) getAllConfigurations(typeFilter string) ([]Configuration, error) updateLocalFsLocation(string, string) error getLocalFSLocation(string) (string, error)
diff --git a/data_test.go b/data_test.go index 2cfded0..e8114c3 100644 --- a/data_test.go +++ b/data_test.go
@@ -38,6 +38,8 @@ "gcs:SHA-512:8fcc902465ccb32ceff25fa9f6fb28e3b314dbc2874c0f8add02f4e29c9e2798d344c51807aa1af56035cf09d39c800cf605d627ba65723f26d8b9c83c82d2f2": true, "gcs:SHA-512:0c648779da035bfe0ac21f6268049aa0ae74d9d6411dadefaec33991e55c2d66c807e06f7ef84e0947f7c7d63b8c9e97cf0684cbef9e0a86b947d73c74ae7455": true, } + + allConfigs map[string]bool ) var _ = Describe("data", func() { @@ -54,6 +56,14 @@ initTestDb(testDbMan.getDb()) err := testDbMan.initDb() Expect(err).Should(Succeed()) + allConfigs = map[string]bool{ + "1dc4895e-6494-4b59-979f-5f4c89c073b4": true, + "319963ff-217e-4ecc-8d6e-c3665e962d1e": true, + "3af44bb7-0a74-4283-860c-3561e6c19132": true, + "d5ffd9db-4795-43eb-b645-d2a0b6c8ac6a": true, + "84ac8d68-b3d1-4bcc-ad0d-c6a0ed67e16c": true, + "3ecd351c-1173-40bf-b830-c194e5ef9038": true, + } time.Sleep(100 * time.Millisecond) }) @@ -145,9 +155,20 @@ confs, err := testDbMan.getAllConfigurations("") Expect(err).Should(Succeed()) Expect(len(confs)).Should(Equal(6)) + for _, conf := range confs { + Expect(allConfigs[conf.ID]).Should(BeTrue()) + allConfigs[conf.ID] = false + } }) - It("should get empty slice if no configurations are ready", func() { + It("should get empty slice if no configurations", func() { + trancateTestMetadataTable(testDbMan.getDb()) + confs, err := testDbMan.getReadyConfigurations("") + Expect(err).Should(Succeed()) + Expect(len(confs)).Should(BeZero()) + }) + + XIt("should get empty slice if no configurations are ready", func() { confs, err := testDbMan.getReadyConfigurations("") Expect(err).Should(Succeed()) Expect(len(confs)).Should(BeZero()) @@ -208,7 +229,7 @@ Expect(err).ShouldNot(Succeed()) }) - It("should successfully get all ready configurations", func() { + XIt("should successfully get all ready configurations", func() { err := testDbMan.updateLocalFsLocation(readyBlobId, testBlobLocalFsPrefix+readyBlobId) Expect(err).Should(Succeed()) @@ -226,24 +247,22 @@ } }) - It("should get ready configurations by type filter", func() { + It("should get all configurations by type filter", func() { err := testDbMan.updateLocalFsLocation(readyBlobId, testBlobLocalFsPrefix+readyBlobId) Expect(err).Should(Succeed()) err = testDbMan.updateLocalFsLocation(readyResourceId, testBlobLocalFsPrefix+readyResourceId) Expect(err).Should(Succeed()) - confs, err := testDbMan.getReadyConfigurations("ORGANIZATION") + confs, err := testDbMan.getAllConfigurations("ORGANIZATION") Expect(err).Should(Succeed()) - Expect(len(confs)).Should(Equal(1)) - Expect(confs[0].ID).Should(Equal("319963ff-217e-4ecc-8d6e-c3665e962d1e")) + Expect(len(confs)).Should(Equal(2)) - confs, err = testDbMan.getReadyConfigurations("ENVIRONMENT") + confs, err = testDbMan.getAllConfigurations("ENVIRONMENT") Expect(err).Should(Succeed()) - Expect(len(confs)).Should(Equal(1)) - Expect(confs[0].ID).Should(Equal("1dc4895e-6494-4b59-979f-5f4c89c073b4")) + Expect(len(confs)).Should(Equal(4)) - confs, err = testDbMan.getReadyConfigurations("INVALID-TYPE") + confs, err = testDbMan.getAllConfigurations("INVALID-TYPE") Expect(err).Should(Succeed()) Expect(len(confs)).Should(Equal(0)) }) @@ -419,3 +438,14 @@ Expect(err).Should(Succeed()) Expect(tx.Commit()).Should(Succeed()) } + +func trancateTestMetadataTable(db apid.DB) { + tx, err := db.Begin() + Expect(err).Should(Succeed()) + defer tx.Rollback() + _, err = tx.Exec(` + DELETE FROM metadata_runtime_entity_metadata; + `) + Expect(err).Should(Succeed()) + Expect(tx.Commit()).Should(Succeed()) +}
diff --git a/listener_test.go b/listener_test.go index 000a6e9..d08934e 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -293,38 +293,6 @@ }) }) -type dummyBundleManager struct { - blobChan chan string -} - -func (bm *dummyBundleManager) initializeBundleDownloading() { - -} - -func (bm *dummyBundleManager) downloadBlobsWithCallback(blobs []string, callback func()) { - go func() { - for _, id := range blobs { - bm.blobChan <- id - } - }() - go callback() -} - -func (bm *dummyBundleManager) makeDownloadRequest(blobId string, bunchRequest *BunchDownloadRequest) *DownloadRequest { - return &DownloadRequest{ - blobId: blobId, - bunchRequest: bunchRequest, - } -} - -func (bm *dummyBundleManager) deleteBlobs(blobIds []string) { - -} - -func (bm *dummyBundleManager) Close() { - -} - func rowFromDeployment(dep *Configuration) common.Row { row := common.Row{} row["id"] = &common.ColumnVal{Value: dep.ID}
diff --git a/mock_test.go b/mock_test.go new file mode 100644 index 0000000..b272eaf --- /dev/null +++ b/mock_test.go
@@ -0,0 +1,190 @@ +package apiGatewayConfDeploy + +import ( + "bytes" + "encoding/json" + "github.com/gorilla/mux" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "io" + "net/http" + "os" + "strings" + "sync/atomic" + "time" +) + +type dummyDbManager struct { + unreadyBlobIds []string + readyDeployments []Configuration + localFSLocation string + fileResponse chan string + version string + configurations map[string]*Configuration + lsn string + err error +} + +func (d *dummyDbManager) setDbVersion(version string) { + d.version = version +} + +func (d *dummyDbManager) initDb() error { + return nil +} + +func (d *dummyDbManager) getUnreadyBlobs() ([]string, error) { + return d.unreadyBlobIds, nil +} + +func (d *dummyDbManager) getAllConfigurations(typeFilter string) ([]Configuration, error) { + if typeFilter == "" { + return d.readyDeployments, nil + } + return []Configuration{*(d.configurations[typeFilter])}, nil +} + +func (d *dummyDbManager) updateLocalFsLocation(blobId, localFsLocation string) error { + file, err := os.Open(localFsLocation) + if err != nil { + return err + } + buff := make([]byte, 36) + _, err = file.Read(buff) + if err != nil { + return err + } + go func(buff []byte) { + d.fileResponse <- string(buff) + }(buff) + + return nil +} + +func (d *dummyDbManager) getLocalFSLocation(string) (string, error) { + return d.localFSLocation, nil +} + +func (d *dummyDbManager) getConfigById(id string) (*Configuration, error) { + return d.configurations[id], d.err +} +func (d *dummyDbManager) getLSN() string { + return d.lsn +} + +func (d *dummyDbManager) updateLSN(LSN string) error { + d.lsn = LSN + return nil +} + +func (d *dummyDbManager) loadLsnFromDb() error { + + return nil +} + +type dummyApiManager struct { + initCalled chan bool + notifyChan chan bool +} + +func (a *dummyApiManager) InitAPI() { + go func() { + a.initCalled <- true + }() +} + +func (a *dummyApiManager) notifyNewChange() { + a.notifyChan <- true +} + +type dummyBundleManager struct { + blobChan chan string +} + +func (bm *dummyBundleManager) initializeBundleDownloading() { + +} + +func (bm *dummyBundleManager) downloadBlobsWithCallback(blobs []string, callback func()) { + go func() { + for _, id := range blobs { + bm.blobChan <- id + } + }() + go callback() +} + +func (bm *dummyBundleManager) makeDownloadRequest(blobId string, bunchRequest *BunchDownloadRequest) *DownloadRequest { + return &DownloadRequest{ + blobId: blobId, + bunchRequest: bunchRequest, + } +} + +func (bm *dummyBundleManager) deleteBlobs(blobIds []string) { + +} + +func (bm *dummyBundleManager) Close() { + +} + +type dummyBlobServer struct { + serverEndpoint string + signedEndpoint string + signedTimeout *int32 + blobTimeout *int32 + resetTimeout bool +} + +func (b *dummyBlobServer) start() { + services.API().HandleFunc(b.serverEndpoint, b.returnSigned).Methods("GET") + services.API().HandleFunc(b.signedEndpoint, b.returnBlob).Methods("GET") +} + +// send a dummy uri as response +func (b *dummyBlobServer) returnSigned(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() + if atomic.LoadInt32(b.signedTimeout) == int32(1) { + if b.resetTimeout { + atomic.StoreInt32(b.signedTimeout, 0) + } + time.Sleep(time.Second) + } + vars := mux.Vars(r) + blobId := vars["blobId"] + + uriString := strings.Replace(bundleTestUrl+b.signedEndpoint, "{blobId}", blobId, 1) + log.Debug("dummyBlobServer returnSigned: " + uriString) + + res := blobServerResponse{ + Id: blobId, + Kind: "Blob", + Self: r.RequestURI, + SignedUrl: uriString, + SignedUrlExpiryTimestamp: time.Now().Add(3 * time.Hour).Format(time.RFC3339), + } + + resBytes, err := json.Marshal(res) + Expect(err).Should(Succeed()) + _, err = io.Copy(w, bytes.NewReader(resBytes)) + Expect(err).Should(Succeed()) + w.Header().Set("Content-Type", headerSteam) +} + +// send blobId back as response +func (b *dummyBlobServer) returnBlob(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() + if atomic.LoadInt32(b.blobTimeout) == int32(1) { + if b.resetTimeout { + atomic.StoreInt32(b.blobTimeout, 0) + } + time.Sleep(time.Second) + } + vars := mux.Vars(r) + blobId := vars["blobId"] + log.Debug("dummyBlobServer returnBlob id=" + blobId) + _, err := io.Copy(w, bytes.NewReader([]byte(blobId))) + Expect(err).Should(Succeed()) + w.Header().Set("Content-Type", headerSteam) +}