Files
odo/tests/documentation/command-reference/doc_command_reference_init_test.go
Parthvi Vala 7c563b5de3 Doc Automation: Command Reference: odo init (#6442)
* Pilot work

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add more tests

* CompareDocOutput returns strings missing from cmdout and file

* Finish automating docs for odo init command reference

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Cleanup

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add make target for doc automation tests; create a separate folder for doc automation tests; add documentation for helper functions

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Attempt at fixing validation test

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix validation failures

* Rename ReplaceTimeInString to ReplaceAllTimeInString

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Replace all time strings with a static value for both cmd and mdx output

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add test suite for command reference tests

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix failing test and skip empty directory interactive test; add comment
for tests with specific check

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix make target

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* StripSpinner relies on statement to decide if it should strip the line or let it be

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add check for unicode spinner frames just in case it is present; it is flaky

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Make interactive tests work

* Make it pretty

* Update odo version

Signed-off-by: Parthvi Vala <pvala@redhat.com>
2023-01-05 14:03:26 -05:00

208 lines
8.3 KiB
Go

package docautomation
import (
"fmt"
"path/filepath"
"strings"
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-developer/odo/tests/helper"
)
var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster), func() {
var commonVar helper.CommonVar
var commonPath = filepath.Join("command-reference", "docs-mdx", "init")
var outputStringFormat = "```console\n$ odo %s\n%s```\n"
BeforeEach(func() {
commonVar = helper.CommonBeforeEach()
helper.Chdir(commonVar.Context)
Expect(helper.VerifyFileExists(".odo/env/env.yaml")).To(BeFalse())
})
AfterEach(func() {
helper.CommonAfterEach(commonVar)
})
// interactive tests do not provide the same output every time,
// so we'll skip these tests until we have more coverage and then investigate a better way to test this
Context("Interactive Mode", func() {
It("Empty directory", func() {
args := []string{"odo", "init"}
out, err := helper.RunInteractive(args, []string{"ODO_LOG_LEVEL=0"}, func(ctx helper.InteractiveContext) {
helper.ExpectString(ctx, "Select language")
helper.SendLine(ctx, "Java")
helper.ExpectString(ctx, "Select project type")
helper.SendLine(ctx, "")
helper.ExpectString(ctx, "Select container for which you want to change configuration?")
helper.SendLine(ctx, "")
helper.ExpectString(ctx, "Which starter project do you want to use")
helper.SendLine(ctx, "")
helper.ExpectString(ctx, "Enter component name")
helper.SendLine(ctx, "my-java-maven-app")
helper.ExpectString(ctx, "Changes will be directly reflected on the cluster.")
})
Expect(err).To(BeNil())
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, args[1], helper.StripSpinner(got))
want := helper.GetMDXContent(filepath.Join(commonPath, "interactive_mode_empty_directory_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
When("the directory is not empty", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
})
It("Directory with sources", func() {
args := []string{"odo", "init"}
out, err := helper.RunInteractive(args, []string{"ODO_LOG_LEVEL=0"}, func(ctx helper.InteractiveContext) {
helper.ExpectString(ctx, "Is this correct?")
helper.SendLine(ctx, "")
helper.ExpectString(ctx, "✓ Downloading devfile \"nodejs:2.1.1\" from registry \"DefaultDevfileRegistry\"")
helper.ExpectString(ctx, "Select container for which you want to change configuration?")
helper.SendLine(ctx, "")
helper.ExpectString(ctx, "Enter component name")
helper.SendLine(ctx, "")
helper.ExpectString(ctx, "Changes will be directly reflected on the cluster.")
})
Expect(err).To(BeNil())
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, args[1], helper.StripSpinner(got))
want := helper.GetMDXContent(filepath.Join(commonPath, "interactive_mode_directory_with_sources_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
})
})
Context("Non Interactive Mode", func() {
It("Fetch Devfile of a specific version", func() {
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--devfile-version", "2.0.0"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
want := helper.GetMDXContent(filepath.Join(commonPath, "versioned_devfile_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
It("Fetch Devfile of the latest version", func() {
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--devfile-version", "latest"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
want := helper.GetMDXContent(filepath.Join(commonPath, "latest_versioned_devfile_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
It("Fetch Devfile from a URL", func() {
args := []string{"init", "--devfile-path", "https://registry.devfile.io/devfiles/nodejs-angular", "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
want := helper.GetMDXContent(filepath.Join(commonPath, "devfile_from_url_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
Context("fetching devfile from a registry", func() {
When("setting up the registry", func() {
const (
defaultReg = "DefaultDevfileRegistry"
defaultRegURL = "https://registry.devfile.io"
stagingReg = "StagingRegistry"
stagingRegURL = "https://registry.stage.devfile.io"
)
BeforeEach(func() {
helper.Cmd("odo", "preference", "remove", "registry", defaultReg, "-f").ShouldPass()
helper.Cmd("odo", "preference", "add", "registry", defaultReg, defaultRegURL).ShouldPass()
helper.Cmd("odo", "preference", "add", "registry", stagingReg, stagingRegURL).ShouldPass()
})
AfterEach(func() {
helper.Cmd("odo", "preference", "remove", "registry", stagingReg, "-f").ShouldPass()
helper.SetDefaultDevfileRegistryAsStaging()
})
removePreferenceKeys := func(docString string) string {
return "[...]\n\n" + docString[strings.Index(docString, "Devfile registries"):]
}
checkRegistriesOutput := func() {
args := []string{"preference", "view"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := helper.StripAnsi(out)
got = removePreferenceKeys(got)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
want := helper.GetMDXContent(filepath.Join(commonPath, "registry_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
}
It("Fetch Devfile from a specific registry of the list", func() {
By("checking for required registries", func() {
checkRegistriesOutput()
})
By("checking for the init output", func() {
args := []string{"init", "--name", "my-spring-app", "--devfile", "java-springboot", "--devfile-registry", "DefaultDevfileRegistry", "--starter", "springbootproject"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
want := helper.GetMDXContent(filepath.Join(commonPath, "devfile_from_specific_registry_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
})
It("Fetch Devfile from any registry of the list", func() {
By("checking for required registries", func() {
checkRegistriesOutput()
})
By("checking for the registry list output", func() {
args := []string{"registry", "--devfile", "nodejs-react"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := helper.StripAnsi(out)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
want := helper.GetMDXContent(filepath.Join(commonPath, "registry_list_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
By("checking for the init output", func() {
args := []string{"init", "--devfile", "nodejs-react", "--name", "my-nr-app"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
want := helper.GetMDXContent(filepath.Join(commonPath, "devfile_from_any_registry_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
})
})
})
It("Fetch Devfile from a URL", func() {
args := []string{"init", "--devfile-path", "https://registry.devfile.io/devfiles/nodejs-angular", "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
want := helper.GetMDXContent(filepath.Join(commonPath, "devfile_from_url_output.mdx"))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty())
})
})
})