graphql-engine/cli/commands/migrate_delete_test.go
Kali Vara Purushotham Santhati b75ab7233e cli: fix data-race warnings
#### Issue: https://github.com/hasura/graphql-engine-mono/issues/2179

### Problem:

1. There are few warnings when tests are executed with cli if it is built with `race` flag
It is because of a race condition between stdin in migrate_delete for the prompt and stdout for other commands.

2. The integration test of the console used to behave as follows:
    ```
     - Initially there won't be any wg.adds in wait-group so the second go-routine created in console-test (integration-test)
     - It will send the interrupt signal to the channel before the server has been started
     - So practically the server won't start
    ```
3. The read and write for consoleopts.WG has been happening in different go-routines without control or lock system(reading and writing into same memory location without lock system). So there is a chance of data-race(warning for data-race while integration tests are executed)
4. Data-race errors from `promptui` package

### Solution:

1. Use `--force` flag to avoid getting the input from the prompt in `migrate_delete_test.go`
2. Introduced two fields in server and console opts to let know other go-routines whether the server has been started or not
3. To avoid data-race above which are shared b/w go-routines to know the status of servers has been operated atomically.
4. using new package https://github.com/AlecAivazis/survey

https://github.com/hasura/graphql-engine-mono/pull/2231

GitOrigin-RevId: 387eb1be74f24dda34bb3588314b5a909adac227
2021-09-07 13:34:54 +00:00

326 lines
12 KiB
Go

package commands
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
var _ = Describe("hasura migrate delete", func() {
var teardown func()
var hgeEndpoint string
BeforeEach(func() {
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint = fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
teardown = func() {
teardownHGE()
}
})
AfterEach(func() { teardown() })
Context("migrate delete --all", func() {
It("should delete the migrations on server and on source ", func() {
projectDirectory := testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectory},
})
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;", "--database-name", "default"},
WorkingDirectory: projectDirectory,
})
str := string(session.Err.Contents())
i := strings.Index(str, "\"version\"")
version := str[i+10 : i+23]
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "apply", "--database-name", "default"},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"migrations applied",
}
for _, keyword := range wantKeywordList {
Expect(session.Wait(timeout).Err).Should(Say(keyword))
}
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "delete", "--all"},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Err).Should(Say("--database-name flag is required"))
args := []string{"migrate", "delete", "--all", "--database-name", "default", "--force"}
cmd := exec.Command(testutil.CLIBinaryPath, args...)
cmd.Dir = projectDirectory
session, err := Start(
cmd,
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
)
Expect(err).To(BeNil())
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "status", "--database-name", "default"},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
Eventually(session, timeout).Should(Exit(0))
})
})
Context("migrate delete --all --server", func() {
It("should delete the migrations on server and on source ", func() {
projectDirectory := testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectory},
})
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;", "--database-name", "default"},
WorkingDirectory: projectDirectory,
})
str := string(session.Err.Contents())
i := strings.Index(str, "\"version\"")
version := str[i+10 : i+23]
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "apply", "--database-name", "default"},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"migrations applied",
}
for _, keyword := range wantKeywordList {
Expect(session.Wait(timeout).Err).Should(Say(keyword))
}
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "delete", "--all", "--server"},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Err).Should(Say("--database-name flag is required"))
args := []string{"migrate", "delete", "--all", "--server", "--database-name", "default", "--force"}
cmd := exec.Command(testutil.CLIBinaryPath, args...)
cmd.Dir = projectDirectory
session, err := Start(
cmd,
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
)
Expect(err).To(BeNil())
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "status", "--database-name", "default"},
WorkingDirectory: projectDirectory,
})
Eventually(session.Out).Should(Say(version))
Eventually(session.Out).Should(Say("Present"))
Eventually(session.Out).Should(Say("Not Present"))
Eventually(session, timeout).Should(Exit(0))
})
})
Context("migrate delete --version <version>", func() {
It("should delete the migrations on server and on source ", func() {
dirName := testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
})
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;", "--database-name", "default"},
WorkingDirectory: dirName,
})
str := string(session.Err.Contents())
i := strings.Index(str, "\"version\"")
version := str[i+10 : i+23]
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "apply", "--database-name", "default"},
WorkingDirectory: dirName,
})
wantKeywordList := []string{
"migrations applied",
}
for _, keyword := range wantKeywordList {
Expect(session.Wait(timeout).Err).Should(Say(keyword))
}
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "delete", "--version", version, "--database-name", "default"},
WorkingDirectory: dirName,
})
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "status", "--database-name", "default"},
WorkingDirectory: dirName,
})
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
Eventually(session, timeout).Should(Exit(0))
})
})
Context("migrate delete --version <version> (config v2)", func() {
It("should delete the migrations on server and on source ", func() {
projectDirectory := testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectory, "--version", "2"},
})
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;"},
WorkingDirectory: projectDirectory,
})
str := string(session.Err.Contents())
i := strings.Index(str, "\"version\"")
version := str[i+10 : i+23]
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "apply"},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"migrations applied",
}
for _, keyword := range wantKeywordList {
Expect(session.Wait(timeout).Err).Should(Say(keyword))
}
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "delete", "--version", version},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "status"},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
Eventually(session, timeout).Should(Exit(0))
})
})
Context("migrate delete --all (config v2)", func() {
It("should delete the migrations on server and on source ", func() {
projectDirectory := testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectory, "--version", "2"},
})
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;"},
WorkingDirectory: projectDirectory,
})
str := string(session.Err.Contents())
i := strings.Index(str, "\"version\"")
version := str[i+10 : i+23]
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "apply"},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"migrations applied",
}
for _, keyword := range wantKeywordList {
Expect(session.Wait(timeout).Err).Should(Say(keyword))
}
args := []string{"migrate", "delete", "--all", "--force"}
cmd := exec.Command(testutil.CLIBinaryPath, args...)
cmd.Dir = projectDirectory
session, err := Start(
cmd,
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
)
Expect(err).To(BeNil())
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "status"},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
Eventually(session, timeout).Should(Exit(0))
})
})
Context("migrate delete --all --server(config v2)", func() {
It("should delete the migrations on server and on source ", func() {
projectDirectory := testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectory, "--version", "2"},
})
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;"},
WorkingDirectory: projectDirectory,
})
str := string(session.Err.Contents())
i := strings.Index(str, "\"version\"")
version := str[i+10 : i+23]
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "apply"},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"migrations applied",
}
for _, keyword := range wantKeywordList {
Expect(session.Wait(timeout).Err).Should(Say(keyword))
}
args := []string{"migrate", "delete", "--all", "--server", "--force"}
cmd := exec.Command(testutil.CLIBinaryPath, args...)
cmd.Dir = projectDirectory
session, err := Start(
cmd,
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
)
Expect(err).To(BeNil())
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"migrate", "status"},
WorkingDirectory: projectDirectory,
})
Expect(session.Wait(timeout).Out).Should(Say(version))
Expect(session.Wait(timeout).Out).Should(Say("Present"))
Expect(session.Wait(timeout).Out).Should(Say("Not Present"))
Eventually(session, timeout).Should(Exit(0))
})
})
})