add cleanup for test
diff --git a/dockertests/const.go b/dockertests/const.go
index 26926ff..1fdd217 100644
--- a/dockertests/const.go
+++ b/dockertests/const.go
@@ -15,15 +15,21 @@
package dockertests
const (
- pluginName = "apigeeSyncDockerTest"
- configApidClusterId = "apigeesync_cluster_id"
- configProxyServerBaseURI = "apigeesync_proxy_server_base"
- configLocalStoragePath = "local_storage_path"
- configConsumerKey = "apigeesync_consumer_key"
- configConsumerSecret = "apigeesync_consumer_secret"
- configName = "apigeesync_instance_name"
- ApigeeSyncEventSelector = "ApigeeSync"
- testInitUser = "dockerTestInit"
+ pluginName = "apigeeSyncDockerTest"
+ configApidClusterId = "apigeesync_cluster_id"
+ configProxyServerBaseURI = "apigeesync_proxy_server_base"
+ configLocalStoragePath = "local_storage_path"
+ configConsumerKey = "apigeesync_consumer_key"
+ configConsumerSecret = "apigeesync_consumer_secret"
+ configName = "apigeesync_instance_name"
+ ApigeeSyncEventSelector = "ApigeeSync"
+ testInitUser = "dockerTestInit"
+ basicTables = map[string]bool{
+ "deployment_history": true,
+ "deployment": true,
+ "bundle_config": true,
+ "configuration": true,
+ "apid_cluster": true,
+ "data_scope": true,
+ }
)
-
-
diff --git a/dockertests/docker_test.go b/dockertests/docker_test.go
index 1e0b24d..c787115 100644
--- a/dockertests/docker_test.go
+++ b/dockertests/docker_test.go
@@ -15,26 +15,26 @@
package dockertests
import (
+ "encoding/json"
"github.com/30x/apid-core"
"github.com/30x/apid-core/factory"
_ "github.com/30x/apidApigeeSync"
+ "github.com/apigee-labs/transicator/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"net/http/httptest"
"os"
"testing"
"time"
- "encoding/json"
- "github.com/apigee-labs/transicator/common"
)
var (
- services apid.Services
- log apid.LogService
- dataService apid.DataService
- config apid.ConfigService
- pgUrl string
- pgManager *ManagementPg
+ services apid.Services
+ log apid.LogService
+ dataService apid.DataService
+ config apid.ConfigService
+ pgUrl string
+ pgManager *ManagementPg
clusterIdFromConfig string
)
@@ -48,8 +48,6 @@
pgUrl = os.Getenv("APIGEE_SYNC_DOCKER_PG_URL") + "?sslmode=disable"
os.Setenv("APID_CONFIG_FILE", "./apid_config.yaml")
-
-
apid.Initialize(factory.DefaultServicesFactory())
config = apid.Config()
@@ -73,7 +71,6 @@
initDone := make(chan bool)
handler := &waitSnapshotHandler{initDone}
-
// hang until snapshot received
apid.Events().Listen(ApigeeSyncEventSelector, handler)
@@ -83,47 +80,44 @@
apid.RegisterPlugin(initPlugin)
apid.InitializePlugins("dockerTest")
- <- initDone
+ <-initDone
}, 5)
var _ = AfterSuite(func() {
- //pgManager.CleanupAll()
+ err := pgManager.CleanupAll()
+ Expect(err).Should(Succeed())
})
var _ = Describe("dockerIT", func() {
+ /*
+ * Isolation between tests is not perfect.
+ * If in a test you listen to any event, please make sure you stop listening to it,
+ * and don't let it mess up later tests.
+ */
Context("Generic Replication", func() {
var _ = BeforeEach(func() {
})
var _ = AfterEach(func() {
- //pgManager.CleanupTest()
+ err := pgManager.CleanupTest()
+ Expect(err).Should(Succeed())
})
It("should succesfully download new table from pg", func(done Done) {
- //log.Debug("CS: " + config.GetString(configChangeServerBaseURI))
- //log.Debug("SS: " + config.GetString(configSnapServerBaseURI))
- //log.Debug("Auth: " + config.GetString(configProxyServerBaseURI))
tableName := "docker_test"
targetTablename := "edgex_" + tableName
- apid.Events().ListenFunc(ApigeeSyncEventSelector, func(event apid.Event){
- if s, ok := event.(*common.Snapshot); ok {
- go func() {
- defer GinkgoRecover()
- sqliteDb, err := dataService.DBVersion(s.SnapshotInfo)
- Expect(err).Should(Succeed())
- Expect(verifyTestTable(targetTablename, sqliteDb)).To(BeTrue())
- dropTestTable(tableName, sqliteDb)
- close(done)
- }()
- }
- })
+ handler := &newTableHandler{
+ targetTablename: targetTablename,
+ done: done,
+ }
- createTestTable(tableName);
-
+ apid.Events().Listen(ApigeeSyncEventSelector, handler)
+ createTestTable(tableName)
}, 1)
+
})
})
@@ -153,7 +147,7 @@
err = rows.Scan(&tableName)
Expect(err).Should(Succeed())
- if tableName==targetTableName {
+ if tableName == targetTableName {
return true
}
}
@@ -193,7 +187,7 @@
}
func initPgData() {
- clusterIdFromConfig = config.GetString(configApidClusterId)//"4c6bb536-0d64-43ca-abae-17c08f1a7e58"
+ clusterIdFromConfig = config.GetString(configApidClusterId) //"4c6bb536-0d64-43ca-abae-17c08f1a7e58"
clusterId := clusterIdFromConfig
scopeId := "ae418890-2c22-4c6a-b218-69e261034b96"
deploymentId := "633af126-ee79-4a53-bef7-7ba30da8aad6"
@@ -228,45 +222,44 @@
}
bf := bundleConfigData{
- Id: bundleConfigId,
- Created: t.Format(time.RFC3339),
+ Id: bundleConfigId,
+ Created: t.Format(time.RFC3339),
CreatedBy: testInitUser,
- Updated: t.Format(time.RFC3339),
+ Updated: t.Format(time.RFC3339),
UpdatedBy: testInitUser,
- Name: bundleConfigName,
- Uri: bundleUri,
+ Name: bundleConfigName,
+ Uri: bundleUri,
}
jsonBytes, err := json.Marshal(bf)
Expect(err).Should(Succeed())
-
bfr := &bundleConfigRow{
- id: bf.Id,
- scopeId: scopeId,
- name: bf.Name,
- uri: bf.Uri,
+ id: bf.Id,
+ scopeId: scopeId,
+ name: bf.Name,
+ uri: bf.Uri,
checksumType: "",
- checksum: "",
- created: t,
- createdBy: bf.CreatedBy,
- updated: t,
- updatedBy: bf.UpdatedBy,
+ checksum: "",
+ created: t,
+ createdBy: bf.CreatedBy,
+ updated: t,
+ updatedBy: bf.UpdatedBy,
}
d := &deploymentRow{
- id: deploymentId,
- configId: bundleConfigId,
- clusterId: clusterId,
- scopeId: scopeId,
+ id: deploymentId,
+ configId: bundleConfigId,
+ clusterId: clusterId,
+ scopeId: scopeId,
bundleConfigName: bundleConfigName,
bundleConfigJson: string(jsonBytes),
- configJson: "{}",
- created: t,
- createdBy: testInitUser,
- updated: t,
- updatedBy: testInitUser,
- changeSelector: clusterId,
+ configJson: "{}",
+ created: t,
+ createdBy: testInitUser,
+ updated: t,
+ updatedBy: testInitUser,
+ changeSelector: clusterId,
}
tx, err := pgManager.BeginTransaction()
@@ -295,6 +288,24 @@
}
}
+type newTableHandler struct {
+ targetTablename string
+ done Done
+}
+
+func (n *newTableHandler) Handle(event apid.Event) {
+ if s, ok := event.(*common.Snapshot); ok {
+ go func() {
+ defer GinkgoRecover()
+ sqliteDb, err := dataService.DBVersion(s.SnapshotInfo)
+ Expect(err).Should(Succeed())
+ Expect(verifyTestTable(n.targetTablename, sqliteDb)).To(BeTrue())
+ apid.Events().StopListening(ApigeeSyncEventSelector, n)
+ close(n.done)
+ }()
+ }
+}
+
func TestDockerApigeeSync(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ApigeeSync Docker Suite")
diff --git a/dockertests/management_pg.go b/dockertests/management_pg.go
index d46c8c6..b8a6f3d 100644
--- a/dockertests/management_pg.go
+++ b/dockertests/management_pg.go
@@ -17,6 +17,7 @@
import (
"database/sql"
_ "github.com/lib/pq"
+ "fmt"
)
type ManagementPg struct {
@@ -179,9 +180,39 @@
return tx, err
}
+/*
+ * Delete all new tables or rows created by a test from pg.
+ * Only test data for the whole suite will remain in the pg.
+ */
func (m *ManagementPg) CleanupTest() error {
+ tablesToDelete := make([]string, 0)
+ rows, err := m.pg.Query("SELECT table_name FROM information_schema.tables WHERE table_schema='edgex';")
+ if err != nil {
+ return err
+ }
+ defer rows.Close()
+ for rows.Next() {
+ var tableName string
+ err = rows.Scan(&tableName)
+ if err != nil {
+ return err
+ }
+
+ if !basicTables[tableName] {
+ tablesToDelete = append(tablesToDelete, tableName)
+ }
+ }
+
+ for _, tableName := range tablesToDelete {
+ fmt.Println("Drop table: " + tableName)
+ cleanupSql := "DROP TABLE edgex." + tableName + ";"
+ _, err := m.pg.Exec(cleanupSql)
+ if err != nil {
+ return err
+ }
+ }
cleanupSql := "DELETE FROM edgex.apid_cluster WHERE created_by!='" + testInitUser + "';"
- _, err := m.pg.Exec(cleanupSql)
+ _, err = m.pg.Exec(cleanupSql)
return err
}
diff --git a/dockertests/pg_table_data.go b/dockertests/pg_table_data.go
index 12d047b..0a1aecd 100644
--- a/dockertests/pg_table_data.go
+++ b/dockertests/pg_table_data.go
@@ -78,13 +78,12 @@
changeSelector string
}
-
type bundleConfigData struct {
- Id string `json:"id"`
- Created string `json:"created"`
+ Id string `json:"id"`
+ Created string `json:"created"`
CreatedBy string `json:"createdBy"`
- Updated string `json:"updated"`
+ Updated string `json:"updated"`
UpdatedBy string `json:"updatedBy"`
- Name string `json:"name"`
- Uri string `json:"uri"`
-}
\ No newline at end of file
+ Name string `json:"name"`
+ Uri string `json:"uri"`
+}