From 9e3a8d34b0e1e3d3e23865af140ae401f3fb484f Mon Sep 17 00:00:00 2001 From: zhangl Date: Thu, 27 Oct 2022 14:58:34 -0400 Subject: [PATCH] remove unused tests Signed-off-by: zhangl --- cmd/embedded-receive-file/main.go | 4 +- cmd/embedded-upload-files/main.go | 2 +- tests/endtoend/.gitignore | 3 - tests/endtoend/endToEnd_test.go | 295 -------------------- tests/endtoend/helpers_test.go | 401 ---------------------------- tests/endtoend/objectUpdate_test.go | 362 ------------------------- tests/endtoend/startup_test.go | 64 ----- 7 files changed, 3 insertions(+), 1128 deletions(-) delete mode 100644 tests/endtoend/.gitignore delete mode 100644 tests/endtoend/endToEnd_test.go delete mode 100644 tests/endtoend/helpers_test.go delete mode 100644 tests/endtoend/objectUpdate_test.go delete mode 100644 tests/endtoend/startup_test.go diff --git a/cmd/embedded-receive-file/main.go b/cmd/embedded-receive-file/main.go index 95dbd4e..3d292b8 100644 --- a/cmd/embedded-receive-file/main.go +++ b/cmd/embedded-receive-file/main.go @@ -90,7 +90,7 @@ func main() { os.Stdin.Read(buffer) goOn = false syncClient.StopPollingForUpdates() - syncClient.Stop(5) + syncClient.Stop(5, false) close(stopChannel) waitGroup.Wait() } @@ -115,7 +115,7 @@ func fileReceiver(syncClient *client.SyncServiceClient, updatesChannel chan *cli } func deleteFile(syncClient *client.SyncServiceClient, object *client.ObjectMetaData) { - syncClient.MarkObjectDeleted(object) + syncClient.MarkObjectDeleted("", object) } func receiveFile(syncClient *client.SyncServiceClient, object *client.ObjectMetaData) { diff --git a/cmd/embedded-upload-files/main.go b/cmd/embedded-upload-files/main.go index ff9765a..1a5f3ca 100755 --- a/cmd/embedded-upload-files/main.go +++ b/cmd/embedded-upload-files/main.go @@ -151,7 +151,7 @@ func main() { } goOn = false waitGroup.Wait() - syncClient.Stop(1) + syncClient.Stop(1, false) } func curTime() float64 { diff --git a/tests/endtoend/.gitignore b/tests/endtoend/.gitignore deleted file mode 100644 index 47f781c..0000000 --- a/tests/endtoend/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -css*/ -ess*/ - diff --git a/tests/endtoend/endToEnd_test.go b/tests/endtoend/endToEnd_test.go deleted file mode 100644 index 45dd3eb..0000000 --- a/tests/endtoend/endToEnd_test.go +++ /dev/null @@ -1,295 +0,0 @@ -package endtoend - -import ( - "context" - "flag" - "fmt" - "io" - "io/ioutil" - "os" - "os/exec" - "strings" - "testing" - - "github.com/open-horizon/edge-utilities/logger" - "github.com/open-horizon/edge-utilities/logger/log" - "github.com/open-horizon/edge-utilities/logger/trace" - - "github.com/globalsign/mgo" - "github.com/open-horizon/edge-sync-service/common" - "github.com/open-horizon/edge-sync-service/core/base" - "github.com/open-horizon/edge-sync-service/core/security" -) - -type syncServiceInfo struct { - nodeType string - id int - port uint16 - command *exec.Cmd - context context.Context - cancelFunc context.CancelFunc - stdout io.Reader - stderr io.Reader -} - -var cssPort uint16 - -const ( - mongoDBName = "d_end_to_end" - minListeningPort uint16 = 7000 - maxListeningPort uint16 = 7999 -) - -var currentListeningPort = minListeningPort - -var orgIDForTests = "test" -var objectIDForTests = "type1" - -var numberOfCSSs int -var numberOfESSs int - -func init() { - flag.IntVar(&numberOfCSSs, "csss", 0, "Number of CSS instances for some tests") - flag.IntVar(&numberOfESSs, "esss", 0, "Number of ESS instances for some tests") -} - -func TestMain(m *testing.M) { - - common.Configuration.MongoDbName = mongoDBName - - err := common.Load("") - if err != nil { - fmt.Println("Failed to load configuration") - os.Exit(1) - } - - // Don't use common.Configuration.NodeType, it has a default value - if os.Getenv("NODE_TYPE") == "" { - os.Exit(m.Run()) - } else { - runSyncService() - } -} - -func runSyncService() { - fmt.Printf("Running as %s\n", os.Getenv("NODE_TYPE")) - base.ConfigStandaloneSyncService() - base.StandaloneSyncService(&security.TestAuthenticate{}) -} - -func startSyncService(css bool, storageProvider string, count int, t *testing.T) []syncServiceInfo { - result := make([]syncServiceInfo, 0, count) - - name, err := os.Executable() - if err != nil { - t.Errorf("Failed to get the name of the executable. Error: %s\n", err) - return nil - } - - if storageProvider == common.Mongo { - if err := dropMongoDB(); err != nil { - t.Errorf("Failed to drop the Mongo DB %s. Error: %s\n", mongoDBName, err) - return nil - } - } - - for index := 0; index < count; index++ { - context, cancel := context.WithCancel(context.Background()) - command := exec.CommandContext(context, name, "-c", "end-to-end.conf") - - port := getListeningPort() - if css && index == 0 { - cssPort = port - } - - env := make([]string, 0, 100) - var nodeType string - if css { - nodeType = common.CSS - env = append(env, - fmt.Sprintf("MONGO_DB_NAME=%s", mongoDBName), - ) - } else { - nodeType = common.ESS - env = append(env, - "LISTENING_TYPE=unsecure", - "DESTINATION_TYPE=END-TO-END-TEST", - fmt.Sprintf("DESTINATION_ID=ess%d", index), - fmt.Sprintf("OrgID=%s", orgIDForTests), - "HTTP_CSS_HOST=localhost", - fmt.Sprintf("HTTP_CSS_PORT=%d", cssPort), - "HTTP_POLLING_INTERVAL=1", - "GOMAXPROCS=5", - ) - } - - lowerNodeType := strings.ToLower(nodeType) - - persistenceRootPath := fmt.Sprintf("./%s%d/persist", lowerNodeType, index) - if err = os.RemoveAll(persistenceRootPath); err != nil { - t.Errorf("Failed to remove old persistence directory. Error: %s", err) - } - if err = os.MkdirAll(persistenceRootPath, 0700); err != nil { - t.Errorf("Failed to create persistence directory. Error: %s", err) - } - - logRootPath := fmt.Sprintf("./%s%d/log", lowerNodeType, index) - if err = os.RemoveAll(logRootPath); err != nil { - t.Errorf("Failed to remove old log directory. Error: %s", err) - } - if err = os.MkdirAll(logRootPath, 0700); err != nil { - t.Errorf("Failed to create log directory. Error: %s", err) - } - - traceRootPath := fmt.Sprintf("./%s%d/trace", lowerNodeType, index) - if err = os.RemoveAll(traceRootPath); err != nil { - t.Errorf("Failed to remove old trace directory. Error: %s", err) - } - if err = os.MkdirAll(traceRootPath, 0700); err != nil { - t.Errorf("Failed to create trace directory. Error: %s", err) - } - - env = append(env, - fmt.Sprintf("NODE_TYPE=%s", nodeType), - fmt.Sprintf("STORAGE_PROVIDER=%s", storageProvider), - "COMMUNICATION_PROTOCOL=http", - fmt.Sprintf("UNSECURE_LISTENING_PORT=%d", port), - fmt.Sprintf("PERSISTENCE_ROOT_PATH=%s", persistenceRootPath), - "OBJECT_ACTIVATION_INTERVAL=1", - "RESEND_INTERVAL=1", - "LOG_TRACE_DESTINATION=file", - fmt.Sprintf("LOG_ROOT_PATH=%s", logRootPath), - fmt.Sprintf("TRACE_ROOT_PATH=%s", traceRootPath), - "LOG_LEVEL=TRACE", - "TRACE_LEVEL=TRACE", - ) - - command.Env = env - - stdout, err := command.StdoutPipe() - if err != nil { - t.Errorf("Failed to get pipe of stdout. Error: %s\n", err) - cancel() - return result - } - stderr, err := command.StderrPipe() - if err != nil { - t.Errorf("Failed to get pipe of stderr. Error: %s\n", err) - cancel() - return result - } - - err = command.Start() - if err != nil { - t.Errorf("Failed to start %s%d. Error: %s\n", lowerNodeType, index, err) - cancel() - return result - } - - result = append(result, syncServiceInfo{nodeType, index, port, command, context, cancel, stdout, stderr}) - } - - return result -} - -func stopSyncService(servers []syncServiceInfo, t *testing.T) { - for _, server := range servers { - server.cancelFunc() - - stdoutBytes, err := ioutil.ReadAll(server.stdout) - if err != nil { - t.Errorf("Failed to get stdout from %s-%d. Error: %s\n", server.nodeType, server.id, err) - return - } - t.Logf("stdout from %s-%d \n%s\n", server.nodeType, server.id, string(stdoutBytes)) - - stderrBytes, err := ioutil.ReadAll(server.stderr) - if err != nil { - t.Errorf("Failed to get stderr from %s-%d. Error: %s\n", server.nodeType, server.id, err) - return - } - t.Logf("stderr from %s-%d\n%s\n", server.nodeType, server.id, string(stderrBytes)) - - file, err := os.Open(fmt.Sprintf("./%s%d/log/sync-service.log", strings.ToLower(server.nodeType), server.id)) - if err != nil { - t.Errorf("Failed to open the log from %s-%d, Error: %s\n", server.nodeType, server.id, err) - } - bytesRead, err := ioutil.ReadAll(file) - if err != nil { - t.Errorf("Failed to read the log from %s-%d, Error: %s\n", server.nodeType, server.id, err) - } - t.Logf("Log from %s-%d \n%s\n", server.nodeType, server.id, string(bytesRead)) - - file, err = os.Open(fmt.Sprintf("./%s%d/trace/sync-service.log", strings.ToLower(server.nodeType), server.id)) - if err != nil { - t.Errorf("Failed to open the trace from %s-%d, Error: %s\n", server.nodeType, server.id, err) - } - bytesRead, err = ioutil.ReadAll(file) - if err != nil { - t.Errorf("Failed to read the trace from %s-%d, Error: %s\n", server.nodeType, server.id, err) - } - t.Logf("Trace from %s-%d \n%s\n", server.nodeType, server.id, string(bytesRead)) - - server.command.Wait() - } -} - -func dropMongoDB() error { - dialInfo := &mgo.DialInfo{ - Addrs: strings.Split(common.Configuration.MongoAddressCsv, ","), - } - session, err := mgo.DialWithInfo(dialInfo) - if err != nil { - return err - } - db := session.DB(mongoDBName) - err = db.DropDatabase() - if err == mgo.ErrNotFound { - err = nil - } - session.Close() - - return err -} - -func getListeningPort() uint16 { - result := currentListeningPort - - currentListeningPort++ - if currentListeningPort > maxListeningPort { - currentListeningPort = minListeningPort - } - - return result -} - -func getNumberOfCSSs(def int) int { - if numberOfCSSs != 0 { - return numberOfCSSs - } - return def -} - -func getNumberOfESSs(def int) int { - if numberOfESSs != 0 { - return numberOfESSs - } - return def -} - -func setupLogger(t *testing.T) { - if t != nil { - parms := logger.Parameters{RootPath: "./" + t.Name(), FileName: "log", Prefix: "", Level: "TRACE", - MaxFileSize: 409600, MaxCompressedFilesNumber: 2, Destinations: "file", MaintenanceInterval: 3600} - log.Init(parms) - parms.FileName = "trace" - trace.Init(parms) - } else { - log.Stop() - trace.Stop() - - parms := logger.Parameters{Level: "NONE", MaxFileSize: 0, MaxCompressedFilesNumber: 0, Destinations: "stdout", MaintenanceInterval: 0} - log.Init(parms) - trace.Init(parms) - } -} diff --git a/tests/endtoend/helpers_test.go b/tests/endtoend/helpers_test.go deleted file mode 100644 index 6144177..0000000 --- a/tests/endtoend/helpers_test.go +++ /dev/null @@ -1,401 +0,0 @@ -package endtoend - -import ( - "bytes" - "context" - "fmt" - "net/http" - "net/http/httputil" - "net/url" - "sync" - "testing" - "time" - - "github.com/open-horizon/edge-sync-service-client/client" - "github.com/open-horizon/edge-sync-service/common" -) - -type driverInfo struct { - cssInfo []syncServiceInfo - essInfo []syncServiceInfo - toESS bool - receivingClient *client.SyncServiceClient - sendingClient *client.SyncServiceClient - proxy *proxyInfo - destType string - destID string - testCtx *testContext - expectedStatus map[string]string -} - -type proxyInfo struct { - cssPort uint16 - callsBlocked bool - proxyHTTPServer *http.Server - reverseProxy *httputil.ReverseProxy -} - -func startSyncServiceProxy() *proxyInfo { - info := proxyInfo{cssPort: cssPort, proxyHTTPServer: &http.Server{}} - - proxyURL, _ := url.Parse(fmt.Sprintf("http://localhost:%d/", info.cssPort)) - info.reverseProxy = httputil.NewSingleHostReverseProxy(proxyURL) - - cssPort = getListeningPort() - info.proxyHTTPServer.Addr = fmt.Sprintf(":%d", cssPort) - - info.proxyHTTPServer.Handler = &info - - go info.proxyHTTPServer.ListenAndServe() - - time.Sleep(2 * time.Second) - - return &info -} - -func (info *proxyInfo) blockCalls(block bool) { - info.callsBlocked = block -} - -func (info *proxyInfo) stop() { - info.proxyHTTPServer.Shutdown(context.Background()) -} - -func (info *proxyInfo) ServeHTTP(writer http.ResponseWriter, request *http.Request) { - if info.callsBlocked { - writer.WriteHeader(http.StatusBadGateway) - } else { - info.reverseProxy.ServeHTTP(writer, request) - } -} - -type ingressInfo struct { - ingressHTTPServer *http.Server - reverseProxies []*httputil.ReverseProxy - index int - indexLock *sync.Mutex -} - -func startSyncServiceIngress(cssInfo []syncServiceInfo) *ingressInfo { - info := ingressInfo{ingressHTTPServer: &http.Server{}, indexLock: &sync.Mutex{}} - - cssPort = getListeningPort() - info.ingressHTTPServer.Addr = fmt.Sprintf(":%d", cssPort) - - info.ingressHTTPServer.Handler = &info - - info.reverseProxies = make([]*httputil.ReverseProxy, len(cssInfo)) - for index, css := range cssInfo { - proxyURL, _ := url.Parse(fmt.Sprintf("http://localhost:%d/", css.port)) - info.reverseProxies[index] = httputil.NewSingleHostReverseProxy(proxyURL) - } - - go info.ingressHTTPServer.ListenAndServe() - - time.Sleep(2 * time.Second) - - return &info -} - -func (info *ingressInfo) ServeHTTP(writer http.ResponseWriter, request *http.Request) { - info.indexLock.Lock() - reverseProxy := info.reverseProxies[info.index] - info.index++ - if info.index >= len(info.reverseProxies) { - info.index = 0 - } - info.indexLock.Unlock() - - reverseProxy.ServeHTTP(writer, request) -} - -func (info *ingressInfo) stop() { - info.ingressHTTPServer.Shutdown(context.Background()) -} - -func newClient(info syncServiceInfo, admin bool) *client.SyncServiceClient { - syncClient := client.NewSyncServiceClient("http", "localhost", info.port) - - if info.nodeType == common.CSS { - syncClient.SetOrgID(orgIDForTests) - } - - var appKey string - if admin { - appKey = fmt.Sprintf("testerAdmin@%s", orgIDForTests) - } else { - appKey = fmt.Sprintf("testerUser@%s", orgIDForTests) - } - syncClient.SetAppKeyAndSecret(appKey, "") - - return syncClient -} - -func testUpdateObjectOneTestHelper(test *updateObjectTest, driver *driverInfo, destTest int, objectPerESS bool, essID int, t *testing.T) bool { - metadata := *test.metadata - if test.operation == testOpAutoActivate { - metadata.ActivationTime = time.Now().Add(time.Duration(2) * time.Second).UTC().Format(time.RFC3339) - } else { - metadata.ActivationTime = "" - } - if test.operation == testOpExpiration { - metadata.Expiration = time.Now().Add(time.Duration(6) * time.Second).UTC().Format(time.RFC3339) - } else { - metadata.Expiration = "" - } - - if objectPerESS { - metadata.ObjectID = fmt.Sprintf("%s-ess%d", metadata.ObjectID, essID) - } - - switch destTest { - case testDestTypeIDPair: - metadata.DestType = driver.destType - if objectPerESS { - metadata.DestID = fmt.Sprintf("ess%d", essID) - } else { - metadata.DestID = driver.destID - } - metadata.DestinationsList = nil - - case testDestType: - metadata.DestType = driver.destType - metadata.DestID = "" - metadata.DestinationsList = nil - - case testDestList: - metadata.DestType = "" - metadata.DestID = "" - metadata.DestinationsList = []string{driver.destType + ":" + driver.destID} - } - - changing := testContextChanging{testID: test.num, consume: test.consume, expectedObject: test.metadata, testMap: driver.testCtx.changing.testMap} - if !test.metadata.NoData { - changing.expectedData = []byte(test.data) - } else { - changing.expectedData = nil - } - driver.testCtx.changeTestData(&changing) - - expectedStatusID := metadata.ObjectType + ":" + metadata.ObjectID - - switch test.operation { - case testOpUpdate, testOpAutoActivate, testOpExpiration: - if err := driver.sendingClient.UpdateObject(&metadata); err != nil { - if driver.toESS || test.operation != testOpExpiration { - t.Errorf("Failed to update object %s:%s in test number %d. Error: %s", - metadata.ObjectType, metadata.ObjectID, test.num, err) - } else { - return false - } - } else { - if !driver.toESS && test.operation == testOpExpiration { - t.Error("Update of object to ESS with expiration should have failed") - } - } - - if !metadata.NoData && !metadata.MetaOnly { - if err := driver.sendingClient.UpdateObjectData(&metadata, bytes.NewBuffer([]byte(test.data))); err != nil { - t.Errorf("Failed to update object data %s:%s in test number %d. Error: %s", - metadata.ObjectType, metadata.ObjectID, test.num, err) - } - } - - if !metadata.Inactive || test.operation == testOpAutoActivate { - if test.consume { - driver.expectedStatus[expectedStatusID] = common.Consumed - } else { - driver.expectedStatus[expectedStatusID] = common.Delivered - } - } - - case testOpDelete: - driver.testCtx.changing.expectedObject.Deleted = true - if err := driver.sendingClient.DeleteObject(metadata.ObjectType, metadata.ObjectID); err != nil { - t.Errorf("Failed to delete object %s:%s in test number %d. Error: %s", - metadata.ObjectType, metadata.ObjectID, test.num, err) - } - - delete(driver.expectedStatus, expectedStatusID) - - case testOpActivate: - if err := driver.sendingClient.ActivateObject(&metadata); err != nil { - t.Errorf("Failed to activate object %s:%s in test number %d. Error: %s", - metadata.ObjectType, metadata.ObjectID, test.num, err) - } - - if test.consume { - driver.expectedStatus[expectedStatusID] = common.Consumed - } else { - driver.expectedStatus[expectedStatusID] = common.Delivered - } - } - - return true -} - -func setupDriver(toESS bool, cssStorageProvider string, essStorageProvider string, withProxy bool, blocked bool, t *testing.T) *driverInfo { - driver := driverInfo{toESS: toESS} - - driver.cssInfo = startSyncService(true, cssStorageProvider, 1, t) - - if withProxy { - driver.proxy = startSyncServiceProxy() - driver.proxy.blockCalls(blocked) - } - - driver.essInfo = startSyncService(false, essStorageProvider, 1, t) - - time.Sleep(6 * time.Second) - - cssClient := newClient(driver.cssInfo[0], true) - essClient := newClient(driver.essInfo[0], true) - - if toESS { - driver.sendingClient = cssClient - driver.receivingClient = essClient - driver.destType = "END-TO-END-TEST" - driver.destID = fmt.Sprintf("ess%d", driver.essInfo[0].id) - } else { - driver.sendingClient = essClient - driver.receivingClient = cssClient - } - - driver.testCtx = &testContext{ - objectChannel: make(chan *client.ObjectMetaData, 20), - stopChannel: make(chan int, 1), - resultChannel: make(chan resultChannelData, 20), - changing: &testContextChanging{}, - } - go driver.testCtx.objectUpdateReceiver(0, false, driver.receivingClient) - driver.receivingClient.StartPollingForUpdates(objectIDForTests, 1, driver.testCtx.objectChannel) - - driver.expectedStatus = make(map[string]string) - - return &driver -} - -func checkObjectUpdatesAfterTest(driver *driverInfo, toESS bool, t *testing.T) { - if toESS { - if objStatus, err := driver.sendingClient.GetDestinationObjects(driver.destType, driver.destID); err != nil { - t.Errorf("Failed to retrieve status of objects sent to %s/%s. Error: %s", driver.destType, driver.destID, err) - } else { - - for _, status := range objStatus { - if expected, ok := driver.expectedStatus[status.ObjectType+":"+status.ObjectID]; ok { - if expected != status.Status { - t.Errorf("The status for %s:%s was %s. Expected %s.", status.ObjectType, status.ObjectID, status.Status, expected) - } - } else { - t.Errorf("Received status for %s:%s that wasn't in the expected set of statuses", status.ObjectType, status.ObjectID) - } - } - } - } -} - -type testContextChanging struct { - testID int - consume bool - expectedObject *client.ObjectMetaData - expectedData []byte - testMap map[string]updateObjectTest -} - -type testContext struct { - objectChannel chan *client.ObjectMetaData - stopChannel chan int - resultChannel chan resultChannelData - changing *testContextChanging -} - -func (ctx *testContext) objectUpdateReceiver(id int, objectPerEss bool, syncClient *client.SyncServiceClient) { - keepRunning := true - message := "" - for keepRunning { - select { - case object := <-ctx.objectChannel: - consume := ctx.changing.consume - expectedObject := ctx.changing.expectedObject - if ctx.changing.testMap != nil { - if test, ok := ctx.changing.testMap[object.ObjectID]; ok { - consume = test.consume - expectedObject = test.metadata - } - } - - var expectedObjectID string - if objectPerEss { - expectedObjectID = fmt.Sprintf("%s-ess%d", expectedObject.ObjectID, id) - } else { - expectedObjectID = expectedObject.ObjectID - } - - if object.ObjectType != expectedObject.ObjectType || object.ObjectID != expectedObjectID { - message = fmt.Sprintf("Received the incorrect object. Expected %s:%s. Received %s:%s", - ctx.changing.expectedObject.ObjectType, ctx.changing.expectedObject.ObjectID, object.ObjectType, object.ObjectID) - } - if object.Deleted != expectedObject.Deleted { - message = fmt.Sprintf("The Deleted flag of %s:%s was incorrect. Expected %t. Received %t", - object.ObjectType, object.ObjectID, ctx.changing.expectedObject.Deleted, object.Deleted) - } - if object.MetaOnly != expectedObject.MetaOnly { - message = fmt.Sprintf("The MetaOnly flag of %s:%s was incorrect. Expected %t. Received %t", - object.ObjectType, object.ObjectID, ctx.changing.expectedObject.MetaOnly, object.MetaOnly) - } - if object.NoData != expectedObject.NoData { - message = fmt.Sprintf("The NoData flag of %s:%s was incorrect. Expected %t. Received %t", - object.ObjectType, object.ObjectID, ctx.changing.expectedObject.NoData, object.NoData) - } - - if object.Deleted { - if err := syncClient.MarkObjectDeleted(object); err != nil { - message = err.Error() - } - } else if !object.NoData { - var buffer bytes.Buffer - var err error - if syncClient.FetchObjectData(object, &buffer) { - if nil != ctx.changing.expectedData && 0 != bytes.Compare(buffer.Bytes(), ctx.changing.expectedData) { - message = fmt.Sprintf("Data for %s:%s doesn't match expected data", - object.ObjectType, object.ObjectID) - } else { - if consume { - err = syncClient.MarkObjectConsumed(object) - } else { - err = syncClient.MarkObjectReceived(object) - } - if err != nil { - message = err.Error() - } - } - } else { - message = fmt.Sprintf("Failed to fetch the data of %s:%s.", - object.ObjectType, object.ObjectID) - } - } else { - var err error - if consume { - err = syncClient.MarkObjectConsumed(object) - } else { - err = syncClient.MarkObjectReceived(object) - } - if err != nil { - message = err.Error() - } - } - ctx.resultChannel <- resultChannelData{ctx.changing.testID, id, message} - - case <-ctx.stopChannel: - keepRunning = false - } - } -} - -func (ctx *testContext) changeTestData(changing *testContextChanging) { - ctx.changing.testID = changing.testID - ctx.changing.consume = changing.consume - ctx.changing.expectedData = changing.expectedData - ctx.changing.expectedObject = changing.expectedObject - ctx.changing.testMap = changing.testMap -} diff --git a/tests/endtoend/objectUpdate_test.go b/tests/endtoend/objectUpdate_test.go deleted file mode 100644 index 8e45294..0000000 --- a/tests/endtoend/objectUpdate_test.go +++ /dev/null @@ -1,362 +0,0 @@ -package endtoend - -import ( - "fmt" - "testing" - "time" - - "github.com/open-horizon/edge-sync-service-client/client" - "github.com/open-horizon/edge-sync-service/common" -) - -const ( - testOpActivate = iota - testOpAutoActivate - testOpDelete - testOpExpiration - testOpUpdate -) - -const ( - testDestTypeIDPair = iota - testDestType - testDestList - testDestMax // Must be last in the list -) - -type updateObjectTest struct { - num int - metadata *client.ObjectMetaData - operation int - data string - consume bool - extraTime int -} - -type resultChannelData struct { - testID int - id int - message string -} - -var nonBlockingUpdateObjectTests = []updateObjectTest{ - {0, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id1"}, testOpUpdate, "qazwsxedcrfvtgbyhnujm", true, 0}, - {1, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id2"}, testOpUpdate, "poiuytrewqlkjhgfdsa", true, 0}, - {2, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id2"}, testOpDelete, "poiuytrewqlkjhgfdsa", true, 0}, - {3, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id3", Inactive: true}, testOpUpdate, "okmijnuhbygvtcrdxesz", true, 0}, - {4, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id3"}, testOpActivate, "okmijnuhbygvtcrdxesz", true, 0}, - {5, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id4", Inactive: true}, testOpAutoActivate, "zxcvbnmasdfghjkl", true, 4}, - {6, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id5", NoData: true}, testOpUpdate, "", true, 0}, - {7, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id6"}, testOpExpiration, "poiuytrewqlkjhgfdsa", true, 4}, - {8, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id7"}, testOpUpdate, "qwertyuiopasdfghjkl", false, 0}, - {9, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id8"}, testOpUpdate, "rdxtfcygvplokmijnuhb", false, 0}, - {10, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id8"}, testOpDelete, "rdxtfcygvplokmijnuhb", false, 0}, - {11, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id9", Inactive: true}, testOpUpdate, "otfcrdxeszwaqplokmijn", false, 0}, - {12, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id9"}, testOpActivate, "otfcrdxeszwaqplokmijn", false, 0}, - {13, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id10", Inactive: true}, testOpAutoActivate, "asdfghjklzxcvbnm", false, 4}, - {14, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id10", MetaOnly: true}, testOpUpdate, "asdfghjklzxcvbnm", false, 0}, - {15, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id11", NoData: true}, testOpUpdate, "", false, 0}, - {16, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id12"}, testOpExpiration, "poiuytrewqlkjhgfdsa", false, 4}, -} - -func TestUpdateObjectToCSS(t *testing.T) { - testUpdateObjectHelper(nonBlockingUpdateObjectTests, false, testDestTypeIDPair, common.Mongo, common.InMemory, t) - - testUpdateObjectHelper(nonBlockingUpdateObjectTests, false, testDestTypeIDPair, common.Bolt, common.Bolt, t) -} - -func TestUpdateObjectToESS(t *testing.T) { - for destTest := 0; destTest < testDestMax; destTest++ { - testUpdateObjectHelper(nonBlockingUpdateObjectTests, true, destTest, common.Mongo, common.InMemory, t) - - testUpdateObjectHelper(nonBlockingUpdateObjectTests, true, destTest, common.Bolt, common.Bolt, t) - } -} - -var blockedRegistrationUpdateObjectTests = []updateObjectTest{ - {0, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id101"}, testOpUpdate, "qazwsxedcrfvtgbyhnujm", true, 0}, - {1, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id102"}, testOpUpdate, "lkjhgfdsamznxbcvpqowieuryt", false, 0}, - {2, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id103", NoData: true}, testOpUpdate, "", true, 0}, - {3, &client.ObjectMetaData{ObjectType: objectIDForTests, ObjectID: "id104", NoData: true}, testOpUpdate, "", false, 0}, -} - -func TestBlockedRegistrationUpdateObjectToCSS(t *testing.T) { - testBlockedRegistrationUpdateObjectHelper(blockedRegistrationUpdateObjectTests, false, testDestTypeIDPair, - common.Mongo, common.InMemory, t) - - testBlockedRegistrationUpdateObjectHelper(blockedRegistrationUpdateObjectTests, false, testDestTypeIDPair, - common.Bolt, common.Bolt, t) -} - -func TestBlockedRegistrationUpdateObjectToESS(t *testing.T) { - for destTest := 0; destTest < testDestMax; destTest++ { - if destTest != testDestList { - testBlockedRegistrationUpdateObjectHelper(blockedRegistrationUpdateObjectTests, true, destTest, common.Mongo, common.InMemory, t) - - testBlockedRegistrationUpdateObjectHelper(blockedRegistrationUpdateObjectTests, true, destTest, common.Bolt, common.Bolt, t) - } - } -} - -func TestSendToMultipleESSs(t *testing.T) { - testSendToMultipleESSsHelper(nonBlockingUpdateObjectTests, 1, false, common.Mongo, common.InMemory, t) -} - -func TestSendToMultipleESSsObjectPerESS(t *testing.T) { - testSendToMultipleESSsHelper(nonBlockingUpdateObjectTests, 1, true, common.Mongo, common.InMemory, t) -} - -func TestMultipleCSSsToESSs(t *testing.T) { - testSendToMultipleESSsHelper(nonBlockingUpdateObjectTests, getNumberOfCSSs(2), false, common.Mongo, common.InMemory, t) -} - -func TestMultipleCSSsToESSsObjectPerESS(t *testing.T) { - testSendToMultipleESSsHelper(nonBlockingUpdateObjectTests, getNumberOfCSSs(2), true, common.Mongo, common.InMemory, t) -} - -func testUpdateObjectHelper(tests []updateObjectTest, toESS bool, destTest int, - cssStorageProvider string, essStorageProvider string, t *testing.T) { - - toESSString := "toCSS" - if toESS { - toESSString = "toESS" - } - t.Logf("Running TestUpdateObject%s. destination test=%d, cssStorageProvider=%s, essStorageProvider=%s", - toESSString, destTest, cssStorageProvider, essStorageProvider) - - driver := setupDriver(toESS, cssStorageProvider, essStorageProvider, false, false, t) - - for _, test := range tests { - if !testUpdateObjectOneTestHelper(&test, driver, destTest, false, 0, t) { - break - } - - timer := time.NewTimer(time.Duration(5+test.extraTime) * time.Second) - select { - case <-timer.C: - if !test.metadata.Inactive || test.operation == testOpAutoActivate { - t.Errorf("Sub test with %s:%s in test number %d timed out.", test.metadata.ObjectType, test.metadata.ObjectID, test.num) - } - - case data := <-driver.testCtx.resultChannel: - if test.metadata.Inactive && test.operation != testOpAutoActivate { - t.Errorf("Received a message for an inactive object %s:%s in test number %d. Message: %s", - test.metadata.ObjectType, test.metadata.ObjectID, test.num, data.message) - } else { - if data.message != "" { - t.Error(data.message) - } - } - if !timer.Stop() { - <-timer.C - } - } - } - - driver.testCtx.stopChannel <- 1 - - checkObjectUpdatesAfterTest(driver, toESS, t) - - stopSyncService(driver.cssInfo, t) - stopSyncService(driver.essInfo, t) -} - -func testBlockedRegistrationUpdateObjectHelper(tests []updateObjectTest, toESS bool, destTest int, - cssStorageProvider string, essStorageProvider string, t *testing.T) { - - toESSString := "toCSS" - if toESS { - toESSString = "toESS" - } - t.Logf("Running TestBlockedRegistrationUpdateObject%s. destination test=%d, cssStorageProvider=%s, essStorageProvider=%s", - toESSString, destTest, cssStorageProvider, essStorageProvider) - - driver := setupDriver(toESS, cssStorageProvider, essStorageProvider, true, true, t) - driver.testCtx.changing.testMap = make(map[string]updateObjectTest) - - for _, test := range tests { - driver.testCtx.changing.testMap[test.metadata.ObjectID] = test - testUpdateObjectOneTestHelper(&test, driver, destTest, false, 0, t) - } - - // Draining the channel resultChannel - timer := time.NewTimer(10 * time.Second) - keepLooping := true - for keepLooping { - select { - case <-timer.C: - keepLooping = false - - case data := <-driver.testCtx.resultChannel: - t.Errorf("Received a message when none were expected. Message: %s", data.message) - } - } - - driver.testCtx.changing.expectedData = nil - driver.proxy.blockCalls(false) - time.Sleep(5 * time.Second) - - objectsReceivedAtDestination := 0 - keepLooping = true - timer = time.NewTimer(10 * time.Second) - for keepLooping { - select { - case <-timer.C: - keepLooping = false - - case data := <-driver.testCtx.resultChannel: - if data.message != "" { - t.Error(data.message) - } else { - objectsReceivedAtDestination++ - } - } - } - - if objectsReceivedAtDestination != len(tests) { - t.Errorf("The destination did not receive all of the objects. It received %d, expected %d", - objectsReceivedAtDestination, len(tests)) - } - - checkObjectUpdatesAfterTest(driver, toESS, t) - - driver.testCtx.stopChannel <- 1 - - stopSyncService(driver.cssInfo, t) - stopSyncService(driver.essInfo, t) - - driver.proxy.stop() -} - -func testSendToMultipleESSsHelper(tests []updateObjectTest, numOfCSSs int, objectPerESS bool, cssStorageProvider string, essStorageProvider string, - t *testing.T) { - numOfESSs := getNumberOfESSs(5) - - driver := &driverInfo{toESS: true} - driver.destType = "END-TO-END-TEST" - driver.expectedStatus = make(map[string]string) - var ingress *ingressInfo - - cssInfo := startSyncService(true, cssStorageProvider, numOfCSSs, t) - if numOfCSSs > 1 { - ingress = startSyncServiceIngress(cssInfo) - } - essInfo := startSyncService(false, essStorageProvider, numOfESSs, t) - - driver.sendingClient = newClient(syncServiceInfo{nodeType: common.CSS, port: cssPort}, true) - testContexts := make([]*testContext, 0, len(essInfo)) - resultChannel := make(chan resultChannelData, numOfESSs*5) - - extraTimeForManyESSs := numOfESSs / 25 - - for index, ess := range essInfo { - testCtx := &testContext{ - objectChannel: make(chan *client.ObjectMetaData, 20), - stopChannel: make(chan int, 1), - resultChannel: resultChannel, - changing: &testContextChanging{}, - } - testContexts = append(testContexts, testCtx) - receivingClient := newClient(ess, true) - - go testCtx.objectUpdateReceiver(index, objectPerESS, receivingClient) - receivingClient.StartPollingForUpdates("type1", 1, testCtx.objectChannel) - } - driver.testCtx = testContexts[0] - - time.Sleep(time.Duration(4+extraTimeForManyESSs) * time.Second) - - for _, test := range tests { - // Setup extra contexts - for index := 1; index < len(testContexts); index++ { - changing := testContextChanging{testID: test.num, consume: test.consume, expectedObject: test.metadata, testMap: driver.testCtx.changing.testMap} - if !test.metadata.NoData { - changing.expectedData = []byte(test.data) - } else { - changing.expectedData = nil - } - testContexts[index].changeTestData(&changing) - } - - if objectPerESS { - for essID := 0; essID < len(essInfo); essID++ { - testUpdateObjectOneTestHelper(&test, driver, testDestTypeIDPair, true, essID, t) - } - } else { - if !testUpdateObjectOneTestHelper(&test, driver, testDestType, false, 0, t) { - break - } - } - - keepWaiting := true - count := 0 - resultsReceived := make([]bool, len(essInfo), len(essInfo)) - messages := make([]string, 0) - timer := time.NewTimer(time.Duration(5+extraTimeForManyESSs+test.extraTime) * time.Second) - - for keepWaiting { - select { - case <-timer.C: - keepWaiting = false - - case data := <-resultChannel: - if data.testID != test.num { - continue - } - - if test.metadata.Inactive && test.operation != testOpAutoActivate { - messages = append(messages, - fmt.Sprintf("Received a message for an inactive object %s:%s in test number %d. Message: %s", - test.metadata.ObjectType, test.metadata.ObjectID, test.num, data.message)) - } else { - if data.message != "" { - messages = append(messages, fmt.Sprintf("ES %d: %s", data.id, data.message)) - } - } - resultsReceived[data.id] = true - keepWaiting = false - count++ - - checkReceived: - for _, received := range resultsReceived { - if !received { - keepWaiting = true - break checkReceived - } - } - } - } - - success := true - failedESS := -1 - for essID, received := range resultsReceived { - if !received { - success = false - failedESS = essID - break - } - } - - for _, message := range messages { - t.Error(message) - } - - if !success && (!test.metadata.Inactive || test.operation == testOpAutoActivate) { - t.Errorf("Sub test with %s:%s in test number %d on ESS %d timed out.", test.metadata.ObjectType, test.metadata.ObjectID, test.num, failedESS) - } - } - - time.Sleep(time.Duration(4+extraTimeForManyESSs) * time.Second) - - for _, testCtx := range testContexts { - testCtx.stopChannel <- 1 - } - - stopSyncService(essInfo, t) - - if numOfCSSs > 1 { - ingress.stop() - } - - stopSyncService(cssInfo, t) -} diff --git a/tests/endtoend/startup_test.go b/tests/endtoend/startup_test.go deleted file mode 100644 index 2bdf41e..0000000 --- a/tests/endtoend/startup_test.go +++ /dev/null @@ -1,64 +0,0 @@ -package endtoend - -import ( - "testing" - "time" - - "github.com/open-horizon/edge-sync-service/common" -) - -func TestStartup(t *testing.T) { - testStartupHelper(common.Mongo, common.InMemory, false, t) - - testStartupHelper(common.Bolt, common.Bolt, false, t) -} - -func TestOfflineEssRegister(t *testing.T) { - testStartupHelper(common.Bolt, common.InMemory, true, t) -} - -func testStartupHelper(cssStorageProvider string, essStorageProvider string, block bool, t *testing.T) { - const numberOfESSs = 1 - - cssInfo := startSyncService(true, cssStorageProvider, 1, t) - - var proxy *proxyInfo - if block { - proxy = startSyncServiceProxy() - proxy.blockCalls(true) - } - - essInfo := startSyncService(false, essStorageProvider, numberOfESSs, t) - - time.Sleep(6 * time.Second) - - cssClient := newClient(cssInfo[0], true) - - if block { - time.Sleep(4 * time.Second) - - destinations, err := cssClient.GetDestinations() - if err != nil { - t.Errorf("Failed to get destinations from %s%d. Error: %s", cssInfo[0].nodeType, cssInfo[0].id, err) - } else { - if 0 != len(destinations) { - t.Errorf("Received %d destinations instead of the expected 0", len(destinations)) - } - } - - proxy.blockCalls(false) - time.Sleep(4 * time.Second) - } - - destinations, err := cssClient.GetDestinations() - if err != nil { - t.Errorf("Failed to get destinations from %s%d. Error: %s", cssInfo[0].nodeType, cssInfo[0].id, err) - } else { - if numberOfESSs != len(destinations) { - t.Errorf("Received %d destinations instead of the expected %d", len(destinations), numberOfESSs) - } - } - - stopSyncService(cssInfo, t) - stopSyncService(essInfo, t) -}