diff --git a/README.md b/README.md index c4264d0cfda18b62bae5e69fcd12d7250462de2e..4f5f6840df64554091747fe723d59924ebfaa1ed 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Compiled binaries will be stored into ./bin directory. ## Testing Before running tests, make sure you have openGauss server running on `localhost:5432` -interface. Also, you must have `postgres` user that could create new databases +interface. Also, you must have `openGauss` user with password `Gaussdb_123` that could create new databases in your local environment. openGauss-webclient server should not be running at the same time. Execute test suite: diff --git a/data/booktown.sql b/data/booktown.sql index aecccac6c5ba283073537e1782f9775d8162b867..1a11201bd592727635b8ea324b7c24e23a756a95 100644 --- a/data/booktown.sql +++ b/data/booktown.sql @@ -1,17 +1,6 @@ -- -- Selected TOC Entries: -- --- --- TOC Entry ID 1 (OID 0) --- --- Name: booktown Type: DATABASE Owner: postgres --- - -DROP DATABASE IF EXISTS "booktown"; -CREATE DATABASE "booktown"; - -\connect booktown postgres --- -- TOC Entry ID 2 (OID 2991542) -- -- Name: DATABASE "booktown" Type: COMMENT Owner: @@ -461,7 +450,7 @@ CREATE TABLE "money_example" ( -- CREATE TABLE "shipments" ( - "id" integer DEFAULT nextval('"shipments_ship_id_seq"'::text) NOT NULL, + "id" integer NOT NULL, "customer_id" integer, "isbn" text, "ship_date" timestamp with time zone @@ -1025,10 +1014,11 @@ CREATE SEQUENCE "author_ids" start 0 increment 1 maxvalue 2147483647 minvalue 0 -- Name: distinguished_authors Type: TABLE Owner: manager -- -CREATE TABLE "distinguished_authors" ( - "award" text -) -INHERITS ("authors"); +--- This syntax is not supported yet by openGauss +---CREATE TABLE "distinguished_authors" ( +--- "award" text +---) +---INHERITS ("authors"); -- -- TOC Entry ID 107 (OID 3726476) @@ -1293,7 +1283,7 @@ CREATE VIEW "recent_shipments" as SELECT count(*) AS num_shipped, max(shipments. COPY "publishers" FROM stdin; -150 Kids Can Press Kids Can Press, 29 Birch Ave. Toronto, ON  M4V 1E2 +150 Kids Can Press Kids Can Press, 29 Birch Ave. Toronto,�ON��M4V 1E2 91 Henry Holt & Company, Inc. Henry Holt & Company, Inc. 115 West 18th Street New York, NY 10011 113 O'Reilly & Associates O'Reilly & Associates, Inc. 101 Morris St, Sebastopol, CA 95472 62 Watson-Guptill Publications 1515 Boradway, New York, NY 10036 @@ -1633,10 +1623,10 @@ COPY "books" FROM stdin; -- -COPY "distinguished_authors" FROM stdin; -25043 Simon Neil Pulitzer Prize -1809 Geisel Theodor Seuss Pulitzer Prize -\. +---COPY "distinguished_authors" FROM stdin; +---25043 Simon Neil Pulitzer Prize +---1809 Geisel Theodor Seuss Pulitzer Prize +---\. -- -- Data for TOC Entry ID 129 (OID 3727889) -- diff --git a/pkg/bookmarks/bookmarks_test.go b/pkg/bookmarks/bookmarks_test.go index 0b434976cd91d8eebca2b8a53b33552f031ba511..88c67679a17c47c58ccbcf8b7b3072017c8debaa 100644 --- a/pkg/bookmarks/bookmarks_test.go +++ b/pkg/bookmarks/bookmarks_test.go @@ -3,8 +3,8 @@ package bookmarks import ( "testing" - "github.com/sosedoff/pgweb/pkg/command" - "github.com/sosedoff/pgweb/pkg/shared" + "gitee.com/openGauss/openGauss-webclient/pkg/command" + "gitee.com/openGauss/openGauss-webclient/pkg/shared" "github.com/stretchr/testify/assert" ) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index ca0b036d6dc0a3af531f9fcf72806d8b1e1576d7..3393a50888713e5d1392ac85ab75d05db4487d4b 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -160,7 +160,7 @@ func initOptions() { } func printVersion() { - chunks := []string{fmt.Sprintf("Pgweb v%s", command.Version)} + chunks := []string{fmt.Sprintf("openGauss-webclient v%s", command.Version)} if command.GitCommit != "" { chunks = append(chunks, fmt.Sprintf("(git: %s)", command.GitCommit)) diff --git a/pkg/client/client.go b/pkg/client/client.go index 8ef41667cf4cd34a3565f4bdaed3279329fc4a49..431a4f1e6d01f0a9f2f8871c0414a1638fbdbb26 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -26,6 +26,9 @@ var ( cockroachSignature = regexp.MustCompile(`(?i)cockroachdb ccl v([\d\.]+)\s`) cockroachType = "CockroachDB" + + openGaussSignature = regexp.MustCompile(`(?i)openGauss ([\d\.]+)\s`) + openGaussType = "PostgreSQL" ) type Client struct { @@ -165,6 +168,13 @@ func (client *Client) setServerVersion() { client.serverVersion = matches[0][1] return } + + matches = openGaussSignature.FindAllStringSubmatch(version, 1) + if len(matches) > 0 { + client.serverType = postgresType + client.serverVersion = matches[0][1] + return + } } func (client *Client) Test() error { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index cafedaeca7749e14b7102213abc8752d833c924e..7190fc04e1936199362b06301f27760d6cdcdace 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "runtime" + "sort" "testing" "time" @@ -29,6 +30,7 @@ func mapKeys(data map[string]*Objects) []string { for k, _ := range data { result = append(result, k) } + sort.Strings(result) return result } @@ -54,16 +56,14 @@ func initVars() { serverHost = getVar("PGHOST", "localhost") serverPort = getVar("PGPORT", "5432") - serverUser = getVar("PGUSER", "postgres") - serverPassword = getVar("PGPASSWORD", "postgres") - serverDatabase = getVar("PGDATABASE", "booktown") + serverUser = getVar("PGUSER", "openGauss") + serverPassword = getVar("PGPASSWORD", "Gaussdb_123") + serverDatabase = getVar("PGDATABASE", "opengauss_test_booktown") } func setupCommands() { testCommands = map[string]string{ - "createdb": "createdb", - "psql": "psql", - "dropdb": "dropdb", + "gsql": "gsql", } if onWindows() { @@ -82,11 +82,13 @@ func setup() { command.Opts.DisablePrettyJSON = true out, err := exec.Command( - testCommands["createdb"], + testCommands["gsql"], "-U", serverUser, + "-d", "postgres", "-h", serverHost, "-p", serverPort, - serverDatabase, + "-W", serverPassword, + "-c", "CREATE DATABASE "+serverDatabase, ).CombinedOutput() if err != nil { @@ -96,10 +98,11 @@ func setup() { } out, err = exec.Command( - testCommands["psql"], + testCommands["gsql"], "-U", serverUser, "-h", serverHost, "-p", serverPort, + "-W", serverPassword, "-f", "../../data/booktown.sql", serverDatabase, ).CombinedOutput() @@ -112,7 +115,7 @@ func setup() { } func setupClient() { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) testClient, _ = NewFromUrl(url, nil) } @@ -124,11 +127,13 @@ func teardownClient() { func teardown() { _, err := exec.Command( - testCommands["dropdb"], + testCommands["gsql"], "-U", serverUser, + "-d", "postgres", "-h", serverHost, "-p", serverPort, - serverDatabase, + "-W", serverPassword, + "-c", "DROP DATABASE IF EXISTS "+serverDatabase, ).CombinedOutput() if err != nil { @@ -137,19 +142,7 @@ func teardown() { } func testNewClientFromUrl(t *testing.T) { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) - client, err := NewFromUrl(url, nil) - - if err != nil { - defer client.Close() - } - - assert.Equal(t, nil, err) - assert.Equal(t, url, client.ConnectionString) -} - -func testNewClientFromUrl2(t *testing.T) { - url := fmt.Sprintf("postgresql://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) client, err := NewFromUrl(url, nil) if err != nil { @@ -197,7 +190,7 @@ func testDatabases(t *testing.T) { res, err := testClient.Databases() assert.Equal(t, nil, err) - assert.Contains(t, res, "booktown") + assert.Contains(t, res, "opengauss_test_booktown") assert.Contains(t, res, "postgres") } @@ -213,7 +206,6 @@ func testObjects(t *testing.T) { "books", "customers", "daily_inventory", - "distinguished_authors", "dummies", "editions", "employees", @@ -234,7 +226,7 @@ func testObjects(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, []string{"schema", "name", "type", "owner", "comment"}, res.Columns) - assert.Equal(t, []string{"public"}, mapKeys(objects)) + assert.Equal(t, []string{"db4ai", "dbe_perf", "dbe_pldeveloper", "public"}, mapKeys(objects)) assert.Equal(t, tables, objects["public"].Tables) assert.Equal(t, []string{"recent_shipments", "stock_view"}, objects["public"].Views) assert.Equal(t, []string{"author_ids", "book_ids", "shipments_ship_id_seq", "subject_ids"}, objects["public"].Sequences) @@ -370,7 +362,7 @@ func testQueryInvalidTable(t *testing.T) { res, err := testClient.Query("SELECT * FROM books2") assert.NotEqual(t, nil, err) - assert.Equal(t, "pq: relation \"books2\" does not exist", err.Error()) + assert.Contains(t, err.Error(), "pq: relation \"books2\" does not exist") assert.Equal(t, true, res == nil) } @@ -418,7 +410,7 @@ func testHistoryError(t *testing.T) { } func testHistoryUniqueness(t *testing.T) { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) client, _ := NewFromUrl(url, nil) client.Query("SELECT * FROM books WHERE id = 1") @@ -434,7 +426,7 @@ func testReadOnlyMode(t *testing.T) { command.Opts.ReadOnly = false }() - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) client, _ := NewFromUrl(url, nil) err := client.SetReadOnlyMode() @@ -496,7 +488,8 @@ func TestAll(t *testing.T) { testHistoryUniqueness(t) testHistoryError(t) testReadOnlyMode(t) - testDumpExport(t) + //TODO:support gs_dump of openGauss + //testDumpExport(t) teardownClient() teardown() diff --git a/pkg/client/dump.go b/pkg/client/dump.go index d9c1705f90cba03d787ac144c1a2c7f3e1988c47..1d6488c96e323b94f0f835ac98d69ce9902f02a0 100644 --- a/pkg/client/dump.go +++ b/pkg/client/dump.go @@ -22,7 +22,7 @@ type Dump struct { // CanExport returns true if database dump tool could be used without an error func (d *Dump) CanExport() bool { - return exec.Command("pg_dump", "--version").Run() == nil + return exec.Command("gs_dump", "--version").Run() == nil } // Export streams the database dump to the specified writer @@ -47,7 +47,7 @@ func (d *Dump) Export(connstr string, writer io.Writer) error { opts = append(opts, connstr) - cmd := exec.Command("pg_dump", opts...) + cmd := exec.Command("gs_dump", opts...) cmd.Stdout = writer cmd.Stderr = errOutput diff --git a/pkg/client/dump_test.go b/pkg/client/dump_test.go index 61c51714bd16f465189fa1b6bce170a0619a12f1..8901520f884abe69a99240f6c247fc8f3f41bbb2 100644 --- a/pkg/client/dump_test.go +++ b/pkg/client/dump_test.go @@ -9,7 +9,7 @@ import ( ) func testDumpExport(t *testing.T) { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) savePath := "/tmp/dump.sql.gz" os.Remove(savePath) @@ -34,7 +34,7 @@ func testDumpExport(t *testing.T) { assert.NoError(t, err) // Test nonexistent database - invalidURL := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, "foobar") + invalidURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, "foobar") err = dump.Export(invalidURL, saveFile) assert.Contains(t, err.Error(), `database "foobar" does not exist`) @@ -46,7 +46,7 @@ func testDumpExport(t *testing.T) { // Should drop "search_path" param from URI dump = Dump{} - searchPathURL := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable&search_path=private", serverUser, serverHost, serverPort, serverDatabase) + searchPathURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable&search_path=private", serverUser, serverPassword, serverHost, serverPort, serverDatabase) err = dump.Export(searchPathURL, saveFile) assert.NoError(t, err) }