add pg insert
diff --git a/dockertests/const.go b/dockertests/const.go
index 4f4266f..3b309e1 100644
--- a/dockertests/const.go
+++ b/dockertests/const.go
@@ -1,6 +1,5 @@
package dockertests
-
const (
dockerCsPort string = "9000"
dockerSsPort string = "9001"
@@ -12,4 +11,7 @@
configConsumerSecret = "apigeesync_consumer_secret"
configName = "apigeesync_instance_name"
ApigeeSyncEventSelector = "ApigeeSync"
+ testInitUser = "dockerTestInit"
)
+
+
diff --git a/dockertests/docker_test.go b/dockertests/docker_test.go
index 2980483..d2b45bb 100644
--- a/dockertests/docker_test.go
+++ b/dockertests/docker_test.go
@@ -10,16 +10,15 @@
"os"
"testing"
"time"
+ "encoding/json"
)
-
-
var (
- services apid.Services
- log apid.LogService
- data apid.DataService
- config apid.ConfigService
- pgUrl string
+ services apid.Services
+ log apid.LogService
+ data apid.DataService
+ config apid.ConfigService
+ pgUrl string
pgManager *ManagementPg
)
@@ -28,10 +27,17 @@
* apidApigeeSync and runs tests.
*/
var _ = BeforeSuite(func() {
- hostname := "http://" + os.Getenv("APIGEE_SYNC_DOCKER_IP")
- pgUrl = os.Getenv("APIGEE_SYNC_DOCKER_PG_URL")
+ //hostname := "http://" + os.Getenv("APIGEE_SYNC_DOCKER_IP")
+ pgUrl = os.Getenv("APIGEE_SYNC_DOCKER_PG_URL") + "?sslmode=disable"
os.Setenv("APID_CONFIG_FILE", "./apid_config.yaml")
+ // init pg driver and data
+ var err error
+ pgManager, err = InitDb(pgUrl)
+ Expect(err).Should(Succeed())
+ initPgData()
+
+
apid.Initialize(factory.DefaultServicesFactory())
config = apid.Config()
@@ -43,18 +49,15 @@
testServer := initDummyAuthServer()
// Setup dependencies
- config.Set(configChangeServerBaseURI, hostname+":"+dockerCsPort+"/")
- config.Set(configSnapServerBaseURI, hostname+":"+dockerSsPort+"/")
+ //config.Set(configChangeServerBaseURI, hostname+":"+dockerCsPort+"/")
+ //config.Set(configSnapServerBaseURI, hostname+":"+dockerSsPort+"/")
config.Set(configProxyServerBaseURI, testServer.URL)
// init plugin
apid.RegisterPlugin(initPlugin)
apid.InitializePlugins("dockerTest")
- // init pg driver
- var err error
- pgManager, err = InitDb(pgUrl)
- Expect(err).Should(Succeed())
+
})
var _ = Describe("dockerIT", func() {
@@ -64,29 +67,16 @@
})
+ var _ = AfterEach(func() {
+ pgManager.Cleanup()
+ })
+
It("should succesfully download table from pg", func() {
log.Debug("CS: " + config.GetString(configChangeServerBaseURI))
log.Debug("SS: " + config.GetString(configSnapServerBaseURI))
log.Debug("Auth: " + config.GetString(configProxyServerBaseURI))
- cluster := &apidCluster{
- id: "fed02735-0589-4998-bf00-e4d0df7af45b",
- name: "apidcA",
- description: "desc",
- appName: "UOA",
- created: time.Now(),
- createdBy: "userA",
- updated: time.Now(),
- updatedBy: "userA",
- changeSelector: "fed02735-0589-4998-bf00-e4d0df7af45b",
- }
-
- tx, err := pgManager.BeginTransaction()
- Expect(err).Should(Succeed())
- pgManager.InsertApidCluster(tx, cluster)
-
time.Sleep(5 * time.Second)
- Expect(1).To(Equal(1))
}, 30)
})
})
@@ -116,6 +106,98 @@
return pluginData, nil
}
+func initPgData() {
+ clusterId := "4c6bb536-0d64-43ca-abae-17c08f1a7e58"
+ scopeId := "ae418890-2c22-4c6a-b218-69e261034b96"
+ deploymentId := "633af126-ee79-4a53-bef7-7ba30da8aad6"
+ bundleConfigId := "613ce223-6c73-43f4-932c-3c69b0c7c65d"
+ bundleConfigName := "good"
+ bundleUri := "https://gist.github.com/alexkhimich/843cf70ffd6a8b4d44442876ba0487b7/archive/d74360596ff9a4320775d590b3f5a91bdcdf61d2.zip"
+ t := time.Now()
+
+ cluster := &apidClusterRow{
+ id: clusterId,
+ name: "apidcA",
+ description: "desc",
+ appName: "UOA",
+ created: t,
+ createdBy: testInitUser,
+ updated: t,
+ updatedBy: testInitUser,
+ changeSelector: clusterId,
+ }
+
+ ds := &dataScopeRow{
+ id: scopeId,
+ clusterId: clusterId,
+ scope: "abc1",
+ org: "org1",
+ env: "env1",
+ created: t,
+ createdBy: testInitUser,
+ updated: t,
+ updatedBy: testInitUser,
+ changeSelector: clusterId,
+ }
+
+ bf := bundleConfigData{
+ Id: bundleConfigId,
+ Created: t.Format(time.RFC3339),
+ CreatedBy: testInitUser,
+ Updated: t.Format(time.RFC3339),
+ UpdatedBy: testInitUser,
+ Name: bundleConfigName,
+ Uri: bundleUri,
+ }
+
+ jsonBytes, err := json.Marshal(bf)
+ Expect(err).Should(Succeed())
+
+ log.Warn(string(jsonBytes))
+
+ bfr := &bundleConfigRow{
+ id: bf.Id,
+ scopeId: scopeId,
+ name: bf.Name,
+ uri: bf.Uri,
+ checksumType: "",
+ checksum: "",
+ created: t,
+ createdBy: bf.CreatedBy,
+ updated: t,
+ updatedBy: bf.UpdatedBy,
+ }
+
+ d := &deploymentRow{
+ id: deploymentId,
+ configId: bundleConfigId,
+ clusterId: clusterId,
+ scopeId: scopeId,
+ bundleConfigName: bundleConfigName,
+ bundleConfigJson: string(jsonBytes),
+ configJson: "{}",
+ created: t,
+ createdBy: testInitUser,
+ updated: t,
+ updatedBy: testInitUser,
+ changeSelector: clusterId,
+ }
+
+ tx, err := pgManager.BeginTransaction()
+ defer tx.Rollback()
+ Expect(err).Should(Succeed())
+ err = pgManager.InsertApidCluster(tx, cluster)
+ Expect(err).Should(Succeed())
+ err = pgManager.InsertDataScope(tx, ds)
+ Expect(err).Should(Succeed())
+ err = pgManager.InsertBundleConfig(tx, bfr)
+ Expect(err).Should(Succeed())
+ err = pgManager.InsertDeployment(tx, d)
+ Expect(err).Should(Succeed())
+ err = tx.Commit()
+ Expect(err).Should(Succeed())
+}
+
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 bacd9cc..787bccb 100644
--- a/dockertests/management_pg.go
+++ b/dockertests/management_pg.go
@@ -3,7 +3,6 @@
import (
"database/sql"
_ "github.com/lib/pq"
- "time"
)
type ManagementPg struct {
@@ -11,18 +10,6 @@
pg *sql.DB
}
-type apidCluster struct {
- id string
- name string
- description string
- appName string
- created time.Time
- createdBy string
- updated time.Time
- updatedBy string
- changeSelector string
-}
-
func InitDb(dbUrl string) (*ManagementPg, error) {
db, err := sql.Open("postgres", dbUrl)
if err != nil {
@@ -35,7 +22,7 @@
}, nil
}
-func (m *ManagementPg) InsertApidCluster(tx *sql.Tx, cluster *apidCluster) error {
+func (m *ManagementPg) InsertApidCluster(tx *sql.Tx, cluster *apidClusterRow) error {
stmt, err := tx.Prepare(`INSERT INTO edgex.apid_cluster(
id,
name,
@@ -67,8 +54,119 @@
return err
}
+func (m *ManagementPg) InsertDataScope(tx *sql.Tx, ds *dataScopeRow) error {
+ stmt, err := tx.Prepare(`INSERT INTO edgex.data_scope (
+ id,
+ apid_cluster_id,
+ scope,
+ org,
+ env,
+ created,
+ created_by,
+ updated,
+ updated_by,
+ _change_selector
+ )
+ VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`)
+ if err != nil {
+ return err
+ }
+
+ _, err = stmt.Exec(
+ ds.id,
+ ds.clusterId,
+ ds.scope,
+ ds.org,
+ ds.env,
+ ds.created,
+ ds.createdBy,
+ ds.updated,
+ ds.updatedBy,
+ ds.changeSelector,
+ )
+
+ return err
+}
+
+func (m *ManagementPg) InsertBundleConfig(tx *sql.Tx, bf *bundleConfigRow) error {
+ stmt, err := tx.Prepare(`INSERT INTO edgex.bundle_config (
+ id,
+ data_scope_id,
+ name,
+ uri,
+ checksumtype,
+ checksum,
+ created,
+ created_by,
+ updated,
+ updated_by
+ )
+ VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`)
+ if err != nil {
+ return err
+ }
+
+ _, err = stmt.Exec(
+ bf.id,
+ bf.scopeId,
+ bf.name,
+ bf.uri,
+ bf.checksumType,
+ bf.checksum,
+ bf.created,
+ bf.createdBy,
+ bf.updated,
+ bf.updatedBy,
+ )
+
+ return err
+}
+
+func (m *ManagementPg) InsertDeployment(tx *sql.Tx, d *deploymentRow) error {
+ stmt, err := tx.Prepare(`INSERT INTO edgex.deployment (
+ id,
+ bundle_config_id,
+ apid_cluster_id,
+ data_scope_id,
+ bundle_config_name,
+ bundle_config_json,
+ config_json,
+ created,
+ created_by,
+ updated,
+ updated_by,
+ _change_selector,
+ )
+ VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)`)
+ if err != nil {
+ return err
+ }
+
+ _, err = stmt.Exec(
+ d.id,
+ d.configId,
+ d.clusterId,
+ d.scopeId,
+ d.bundleConfigName,
+ d.bundleConfigJson,
+ d.configJson,
+ d.created,
+ d.createdBy,
+ d.updated,
+ d.updatedBy,
+ d.changeSelector,
+ )
+
+ return err
+}
+
func (m *ManagementPg) BeginTransaction() (*sql.Tx, error) {
tx, err := m.pg.Begin()
return tx, err
}
+func (m *ManagementPg) Cleanup() error {
+ cleanupSql := "DELETE FROM edgex.apid_cluster WHERE created_by!='" + testInitUser + "';"
+ _, err := m.pg.Exec(cleanupSql)
+ return err
+}
diff --git a/dockertests/pg_table_data.go b/dockertests/pg_table_data.go
new file mode 100644
index 0000000..9b068be
--- /dev/null
+++ b/dockertests/pg_table_data.go
@@ -0,0 +1,76 @@
+package dockertests
+
+import "time"
+
+type apidClusterRow struct {
+ id string
+ name string
+ description string
+ appName string
+ created time.Time
+ createdBy string
+ updated time.Time
+ updatedBy string
+ changeSelector string
+}
+
+/* FOREIGN KEY (apid_cluster_id)
+ * REFERENCES apid_cluster(id) ON DELETE CASCADE
+ */
+type dataScopeRow struct {
+ id string
+ clusterId string
+ scope string
+ org string
+ env string
+ created time.Time
+ createdBy string
+ updated time.Time
+ updatedBy string
+ changeSelector string
+}
+
+/* FOREIGN KEY (data_scope_id)
+ * REFERENCES data_scope(id) ON DELETE CASCADE
+ */
+type bundleConfigRow struct {
+ id string
+ scopeId string
+ name string
+ uri string
+ checksumType string
+ checksum string
+ created time.Time
+ createdBy string
+ updated time.Time
+ updatedBy string
+}
+
+/* FOREIGN KEY (bundle_config_id)
+ * REFERENCES bundle_config(id) ON DELETE CASCADE
+ */
+type deploymentRow struct {
+ id string
+ configId string
+ clusterId string
+ scopeId string
+ bundleConfigName string
+ bundleConfigJson string
+ configJson string
+ created time.Time
+ createdBy string
+ updated time.Time
+ updatedBy string
+ changeSelector string
+}
+
+
+type bundleConfigData struct {
+ Id string `json:"id"`
+ Created string `json:"created"`
+ CreatedBy string `json:"createdBy"`
+ Updated string `json:"updated"`
+ UpdatedBy string `json:"updatedBy"`
+ Name string `json:"name"`
+ Uri string `json:"uri"`
+}
\ No newline at end of file
diff --git a/glide.yaml b/glide.yaml
index 643a0c9..b583bf1 100644
--- a/glide.yaml
+++ b/glide.yaml
@@ -16,6 +16,10 @@
import:
- package: github.com/30x/apid-core
version: master
+- package: github.com/lib/pq
+ version: master
testImport:
- package: github.com/onsi/ginkgo/ginkgo
+ version: master
- package: github.com/onsi/gomega
+ version: master