-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(e2e): add initial CLI tests (#392)
Approved-by: Alexander Jung <[email protected]>
- Loading branch information
Showing
8 changed files
with
239 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
name: kraft CLI Tests | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
branches: [staging] | ||
paths: | ||
- '**' | ||
- '!.github/**' | ||
|
||
jobs: | ||
|
||
e2e-cli: | ||
name: Test kraft CLI commands | ||
runs-on: ubuntu-latest | ||
|
||
env: | ||
KRAFTKIT_NO_CHECK_UPDATES: 'true' | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.20.2' | ||
cache: false | ||
|
||
- name: Go caches | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.cache/go-build | ||
~/go/pkg/mod | ||
key: ${{ github.job }}-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ github.job }}-${{ runner.os }}-go- | ||
- name: Install libgit2/git2go | ||
run: make git2go | ||
|
||
- name: Install Ginkgo | ||
run: go install github.com/onsi/ginkgo/v2/ginkgo | ||
|
||
- name: Install kraft | ||
run: | | ||
go install \ | ||
-tags static \ | ||
-gcflags=all='' \ | ||
-ldflags='-s -w -X "kraftkit.sh/internal/version.version=v0.0.0-ci-e2e" -X "kraftkit.sh/internal/version.commit=0000000" -X "kraftkit.sh/internal/version.buildTime=Thu Jan 1 00:00:00 UTC 1970"' \ | ||
./cmd/kraft | ||
- name: Run e2e tests | ||
run: ginkgo -v -p -randomize-all ./test/e2e/cli/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build tools | ||
|
||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
|
||
package hack | ||
|
||
// These imports ensure that build tools are included in Go modules so that we | ||
// can `go install` them in module-aware mode. | ||
// See https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module | ||
import _ "github.com/onsi/ginkgo/v2/ginkgo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
|
||
package cli_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:stylecheck | ||
. "github.com/onsi/gomega" //nolint:stylecheck | ||
) | ||
|
||
func TestCli(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "CLI Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
|
||
package cli_test | ||
|
||
import ( | ||
"os/exec" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:stylecheck | ||
. "github.com/onsi/gomega" //nolint:stylecheck | ||
|
||
fcmd "kraftkit.sh/test/e2e/framework/cmd" | ||
) | ||
|
||
var _ = Describe("kraft version", func() { | ||
var cmd *exec.Cmd | ||
|
||
var stdout *fcmd.IOStream | ||
var stderr *fcmd.IOStream | ||
|
||
BeforeEach(func() { | ||
stdout = fcmd.NewIOStream() | ||
stderr = fcmd.NewIOStream() | ||
|
||
cmd = fcmd.NewKraft(stdout, stderr) | ||
cmd.Args = append(cmd.Args, "version") | ||
}) | ||
|
||
When("invoked without flags or positional arguments", func() { | ||
It("should print the version and exit gracefully", func() { | ||
err := cmd.Run() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(stderr.String()).To(BeEmpty()) | ||
Expect(stdout).To(MatchRegexp(`^kraft [\w\.-]+ \([\w]+\) [\w\.-]+ .+\n$`)) | ||
}) | ||
}) | ||
|
||
When("invoked with the --help flag", func() { | ||
BeforeEach(func() { | ||
// The help subsystem is managed by cobra and fails when top-level flags | ||
// are passed, so we ensure to keep only the command name and subcommand | ||
// from the original cmd. | ||
cmd.Args = []string{cmd.Args[0], cmd.Args[len(cmd.Args)-1], "--help"} | ||
}) | ||
|
||
It("should print the command's help", func() { | ||
err := cmd.Run() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(stderr.String()).To(BeEmpty()) | ||
Expect(stdout).To(MatchRegexp(`^Show kraft version information\n`)) | ||
}) | ||
}) | ||
|
||
When("invoked with positional arguments", func() { | ||
BeforeEach(func() { | ||
cmd.Args = append(cmd.Args, "some-arg") | ||
}) | ||
|
||
It("should print an error and exit", func() { | ||
err := cmd.Run() | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err).To(BeAssignableToTypeOf((*exec.ExitError)(nil))) | ||
|
||
eErr := err.(*exec.ExitError) | ||
Expect(eErr.ProcessState.ExitCode()).To(Equal(1)) | ||
|
||
Expect(stderr.String()).To(BeEmpty()) | ||
Expect(stdout).To(MatchRegexp(`^unknown command "some-arg" for "kraft version"\n$`)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
|
||
gomegafmt "github.com/onsi/gomega/format" | ||
) | ||
|
||
// NewKraft returns a kraft OS command that uses the given IO streams and has | ||
// pre-set flags to use the given paths. | ||
func NewKraft(stdout, stderr *IOStream) *exec.Cmd { | ||
cmd := exec.Command("kraft") | ||
cmd.Stdout = stdout | ||
cmd.Stderr = stderr | ||
|
||
return cmd | ||
} | ||
|
||
// IOStream represents an IO stream to be used by OS commands and suitable | ||
// for assertions and reporting in tests. | ||
type IOStream struct { | ||
b *bytes.Buffer | ||
} | ||
|
||
var ( | ||
_ io.ReadWriter = (*IOStream)(nil) | ||
_ fmt.Stringer = (*IOStream)(nil) | ||
_ gomegafmt.GomegaStringer = (*IOStream)(nil) | ||
) | ||
|
||
// NewIOStream returns an initialized IOStream. | ||
func NewIOStream() *IOStream { | ||
return &IOStream{ | ||
b: &bytes.Buffer{}, | ||
} | ||
} | ||
|
||
func (s *IOStream) Read(p []byte) (n int, err error) { | ||
return s.b.Read(p) | ||
} | ||
|
||
func (s *IOStream) Write(p []byte) (n int, err error) { | ||
return s.b.Write(p) | ||
} | ||
|
||
func (s *IOStream) String() string { | ||
return s.b.String() | ||
} | ||
|
||
func (s *IOStream) GomegaString() string { | ||
return s.String() | ||
} |