Skip to content

Commit

Permalink
cli,ci: testsuite enhancements
Browse files Browse the repository at this point in the history
- remove `HASURA_TEST_CLI_HGE_DOCKER_TAG` & `HASURA_TEST_CLI_HGE_DOCKER_REPO` env variables
- add `HASURA_TEST_CLI_HGE_DOCKER_IMAGE` environment variable to configure hge image used in tests
- add template test project directories
- add helper functions to manipulate the template projects in individual tests
- add config v2 tests

Co-authored-by: Kali Vara Purushotham Santhati <72007599+purush7@users.noreply.github.com>
GitOrigin-RevId: 009a74c
  • Loading branch information
2 people authored and hasura-bot committed Jun 3, 2021
1 parent f3643f2 commit d8198a8
Show file tree
Hide file tree
Showing 61 changed files with 6,096 additions and 517 deletions.
7 changes: 4 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ jobs:
name: test cli
working_directory: cli
command: |
export HASURA_TEST_CLI_HGE_DOCKER_TAG=$(curl https://releases.hasura.io/graphql-engine | jq -r ".latest")
export HASURA_TEST_CLI_HGE_DOCKER_IMAGE="hasura/graphql-engine:$(curl https://releases.hasura.io/graphql-engine | jq -r '.latest')"
echo "HASURA_TEST_CLI_HGE_DOCKER_IMAGE = $HASURA_TEST_CLI_HGE_DOCKER_IMAGE"
make integration_tests_config_v2
HASURA_GRAPHQL_TEST_ADMIN_SECRET="abcd" make integration_tests_config_v2
- store_artifacts:
Expand Down Expand Up @@ -449,8 +450,8 @@ jobs:
chmod +x /build/_cli_output/binaries/cli-hasura-linux-amd64
export SOURCE_IMAGE=$(docker load -i /build/_server_output/image.tar | grep "^Loaded image: " | sed "s/Loaded image: //g")
echo "SOURCE_IMAGE = $SOURCE_IMAGE"
export HASURA_TEST_CLI_HGE_DOCKER_TAG=$(echo "$SOURCE_IMAGE" | sed "s/.*:\(.*\)$/\1/")
echo "HASURA_TEST_CLI_HGE_DOCKER_TAG = $HASURA_TEST_CLI_HGE_DOCKER_TAG"
export HASURA_TEST_CLI_HGE_DOCKER_IMAGE="$SOURCE_IMAGE"
echo "HASURA_TEST_CLI_HGE_DOCKER_IMAGE = $HASURA_TEST_CLI_HGE_DOCKER_IMAGE"
make test-all
export HASURA_GRAPHQL_TEST_ADMIN_SECRET="abcd"
export HASURA_GRAPHQL_ADMIN_SECRET="abcd"
Expand Down
6 changes: 3 additions & 3 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ copy-cli-ext: clean-cli-ext
# run tests
.PHONY: test
test: copy-cli-ext
if [ -z "${HASURA_TEST_CLI_HGE_DOCKER_TAG}" ]; then echo "Please set HASURA_TEST_CLI_HGE_DOCKER_TAG env for populating testutil.HasuraVersion and run test"; exit 1; fi
if [ -z "${HASURA_TEST_CLI_HGE_DOCKER_IMAGE}" ]; then echo "Please set HASURA_TEST_CLI_HGE_DOCKER_IMAGE env for populating testutil.HasuraDockerImage and run test"; exit 1; fi
go test -ldflags "-X github.com/hasura/graphql-engine/cli/version.BuildVersion=$(VERSION)" -v -tags="${TEST_TAGS}" `go list ./... | grep -v integration_test | grep -v commands`
integration_tests_config_v3: copy-cli-ext
if [ -z "${HASURA_TEST_CLI_HGE_DOCKER_TAG}" ]; then echo "Please set HASURA_TEST_CLI_HGE_DOCKER_TAG env for populating testutil.HasuraVersion and run test"; exit 1; fi
if [ -z "${HASURA_TEST_CLI_HGE_DOCKER_IMAGE}" ]; then echo "Please set HASURA_TEST_CLI_HGE_DOCKER_IMAGE env for populating testutil.HasuraDockerImage and run test"; exit 1; fi
go test -ldflags "-X github.com/hasura/graphql-engine/cli/version.BuildVersion=$(VERSION)" -v -tags="${TEST_TAGS}" -run Commands/config=v3 ./integration_test
integration_tests_config_v2: copy-cli-ext
if [ -z "${HASURA_TEST_CLI_HGE_DOCKER_TAG}" ]; then echo "Please set HASURA_TEST_CLI_HGE_DOCKER_TAG env for populating testutil.HasuraVersion and run test"; exit 1; fi
if [ -z "${HASURA_TEST_CLI_HGE_DOCKER_IMAGE}" ]; then echo "Please set HASURA_TEST_CLI_HGE_DOCKER_IMAGE env for populating testutil.HasuraDockerImage and run test"; exit 1; fi
go test -ldflags "-X github.com/hasura/graphql-engine/cli/version.BuildVersion=$(VERSION)" -v -tags="${TEST_TAGS}" -run Commands/config=v2 ./integration_test
test-e2e:
ifndef HAS_GINKGO
Expand Down
14 changes: 5 additions & 9 deletions cli/commands/actions_codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,34 @@ import (
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("actions_codegen", func() {
var _ = Describe("hasura actions codegen", func() {

var dirName string
var session *Session
var teardown func()
BeforeEach(func() {
dirName = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraVersion)
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
})
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)

teardown = func() {
session.Kill()
os.RemoveAll(dirName)
teardownHGE()
}
})

AfterEach(func() {
teardown()
})
AfterEach(func() { teardown() })

Context("actions codegen tests", func() {
It("creates the code for all actions spcified framework and in directory as in config.yaml file", func() {
It("creates the code for all actions specified framework and in directory as in config.yaml file", func() {
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"actions", "use-codegen", "--framework", "nodejs-express", "--output-dir", "codegen", "--with-starter-kit", "true"},
WorkingDirectory: dirName,
})
session = testutil.Hasura(testutil.CmdOpts{
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{"actions", "codegen"},
WorkingDirectory: dirName,
})
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/actions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var _ = Describe("actions_create", func() {
var teardown func()
BeforeEach(func() {
dirName = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraVersion)
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
Expand Down
12 changes: 4 additions & 8 deletions cli/commands/actions_use_codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,30 @@ import (
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("actions_use_codegen", func() {
var _ = Describe("hasura actions use-codegen", func() {

var dirName string
var session *Session
var teardown func()
BeforeEach(func() {
dirName = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraVersion)
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
})
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)

teardown = func() {
session.Kill()
os.RemoveAll(dirName)
teardownHGE()
}
})

AfterEach(func() {
teardown()
})
AfterEach(func() { teardown() })

Context("actions use codegen tests", func() {
It("should change the config.yaml file and create the nodejs-express directory ", func() {
session = testutil.Hasura(testutil.CmdOpts{
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{"actions", "use-codegen", "--framework", "nodejs-express", "--output-dir", "codegen", "--with-starter-kit", "true"},
WorkingDirectory: dirName,
})
Expand Down
33 changes: 33 additions & 0 deletions cli/commands/commands_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package commands

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hasura/graphql-engine/cli/util"

"github.com/hasura/graphql-engine/cli"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -38,3 +44,30 @@ func editEndpointInConfig(configFilePath, endpoint string) {
Expect(err).ShouldNot(HaveOccurred())

}

func editSourceNameInConfigV3ProjectTemplate(projectDir, sourceName, postgresConnectionString string) {
// assumes it's renaming a copy of commands/testdata/config-v3-test-project
Expect(os.Rename(filepath.Join(projectDir, "migrations", "pg"), filepath.Join(projectDir, "migrations", sourceName))).To(BeNil())
Expect(os.Rename(filepath.Join(projectDir, "seeds", "pg"), filepath.Join(projectDir, "seeds", sourceName))).To(BeNil())

databaseYamlFilename := filepath.Join(projectDir, "metadata", "databases", "databases.yaml")
databasesYaml, err := ioutil.ReadFile(databaseYamlFilename)
Expect(err).To(BeNil())
newDatabasesYaml := strings.Replace(string(databasesYaml), "pg", fmt.Sprintf("%v", sourceName), -1)
newDatabasesYaml = strings.Replace(string(newDatabasesYaml), "database_url: TO_BE_FILLED", fmt.Sprintf("database_url: %v", postgresConnectionString), -1)
Expect(ioutil.WriteFile(databaseYamlFilename, []byte(newDatabasesYaml), 0655)).To(BeNil())

Expect(os.Rename(filepath.Join(projectDir, "metadata", "databases", "pg"), filepath.Join(projectDir, "metadata", "databases", sourceName))).To(BeNil())
}

func copyTestConfigV2Project(dest string) {
p, err := filepath.Abs("testdata/config-v2-test-project")
Expect(err).To(BeNil())
Expect(util.CopyDir(p, dest)).To(BeNil())
}

func copyTestConfigV3Project(dest string) {
p, err := filepath.Abs("testdata/config-v3-test-project")
Expect(err).To(BeNil())
Expect(util.CopyDir(p, dest)).To(BeNil())
}
113 changes: 80 additions & 33 deletions cli/commands/metadata_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,64 @@ package commands

import (
"fmt"
"os"
"path/filepath"

"github.com/Pallinder/go-randomdata"

"github.com/hasura/graphql-engine/cli/internal/testutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
"os"
"path/filepath"
)

var _ = Describe("metadata_apply", func() {
var dirName string
var session *Session
var commonMetadataCommandsTest = func(projectDirectory string) {
Context("should apply metadata to server", func() {
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{"metadata", "apply"},
WorkingDirectory: projectDirectory,
})
Eventually(session, 60*40).Should(Exit(0))
Eventually(session.Wait().Err.Contents()).Should(ContainSubstring("Metadata applied"))
})
Context("apply metadata to server and it should output the metadata of project to stdout", func() {
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{"metadata", "apply", "--output", "json"},
WorkingDirectory: projectDirectory,
})
Eventually(session, 60*40).Should(Exit(0))
Eventually(isJSON(session.Wait().Out.Contents())).Should(BeTrue())
})
}

var testConfigV2 = func(projectDirectory string) {
Context("apply migrations", func() {
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"migrate", "apply"},
WorkingDirectory: projectDirectory,
})
})
commonMetadataCommandsTest(projectDirectory)
}

var _ = Describe("hasura metadata apply (config v3)", func() {
var projectDirectory string
var teardown func()
BeforeEach(func() {
dirName = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraVersion)
projectDirectory = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
})
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)

sourceName := randomdata.SillyName()
connectionString, teardownPG := testutil.StartPGContainer(GinkgoT())
testutil.AddPGSourceToHasura(GinkgoT(), hgeEndpoint, connectionString, sourceName)
copyTestConfigV3Project(projectDirectory)
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
editSourceNameInConfigV3ProjectTemplate(projectDirectory, sourceName, connectionString)

teardown = func() {
session.Kill()
os.RemoveAll(dirName)
os.RemoveAll(projectDirectory)
teardownPG()
teardownHGE()
}
})
Expand All @@ -35,25 +69,38 @@ var _ = Describe("metadata_apply", func() {
})

It("metadata apply", func() {
Context("should apply metadata to server", func() {
session = testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"metadata", "export"},
WorkingDirectory: dirName,
})
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"metadata", "apply"},
WorkingDirectory: dirName,
})
Eventually(session, 60*40).Should(Exit(0))
Eventually(session.Wait().Err.Contents()).Should(ContainSubstring("Metadata applied"))
})
Context("apply metadata to server and it should output the metadata of project to stdout", func() {
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{"metadata", "apply", "--output", "json"},
WorkingDirectory: dirName,
})
Eventually(session, 60*40).Should(Exit(0))
Eventually(isJSON(session.Wait().Out.Contents())).Should(BeTrue())
})
commonMetadataCommandsTest(projectDirectory)
})
})

var _ = Describe("hasura metadata apply (config v2)", func() {
var projectDirectoryLatest, projectDirectoryV13 string
var teardown func()
BeforeEach(func() {
projectDirectoryLatest = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
copyTestConfigV2Project(projectDirectoryLatest)
editEndpointInConfig(filepath.Join(projectDirectoryLatest, defaultConfigFilename), hgeEndpoint)

projectDirectoryV13 = testutil.RandDirName()
hgeEndPortV13, teardownHGEV13 := testutil.StartHasura(GinkgoT(), "hasura/graphql-engine:v1.3.3")
hgeEndpointV13 := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPortV13)
copyTestConfigV2Project(projectDirectoryV13)
editEndpointInConfig(filepath.Join(projectDirectoryV13, defaultConfigFilename), hgeEndpointV13)

teardown = func() {
os.RemoveAll(projectDirectoryLatest)
os.RemoveAll(projectDirectoryV13)
teardownHGE()
teardownHGEV13()
}
})

AfterEach(func() { teardown() })

It("metadata apply", func() {
testConfigV2(projectDirectoryLatest)
testConfigV2(projectDirectoryV13)
})
})
8 changes: 3 additions & 5 deletions cli/commands/metadata_clear_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import (
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("metadata_clear", func() {
var _ = Describe("hasura metadata clear", func() {

var dirName string
var session *Session
var teardown func()
BeforeEach(func() {
dirName = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraVersion)
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
})
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)

teardown = func() {
session.Kill()
os.RemoveAll(dirName)
teardownHGE()
}
Expand All @@ -38,7 +36,7 @@ var _ = Describe("metadata_clear", func() {

Context("metadata clear test", func() {
It("should clear metadata on server", func() {
session = testutil.Hasura(testutil.CmdOpts{
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{"metadata", "clear"},
WorkingDirectory: dirName,
})
Expand Down
8 changes: 3 additions & 5 deletions cli/commands/metadata_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import (
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("metadata_diff", func() {
var _ = Describe("hasura metadata diff", func() {

var dirName string
var session *Session
var teardown func()
BeforeEach(func() {
dirName = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraVersion)
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", dirName},
})
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)

teardown = func() {
session.Kill()
os.RemoveAll(dirName)
teardownHGE()
}
Expand All @@ -38,7 +36,7 @@ var _ = Describe("metadata_diff", func() {

Context("metadata diff test", func() {
It("should output diff between metadata on server and local project", func() {
session = testutil.Hasura(testutil.CmdOpts{
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{"metadata", "diff"},
WorkingDirectory: dirName,
})
Expand Down
Loading

0 comments on commit d8198a8

Please sign in to comment.