-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add integration tests #42
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,223 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/shipyard/shipyard-cli/pkg/types" | ||
"github.com/shipyard/shipyard-cli/tests/server" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
cmd := exec.Command("go", "build", "-o", "shipyard", "..") | ||
var stderr bytes.Buffer | ||
cmd.Stderr = &stderr | ||
if err := cmd.Run(); err != nil { | ||
fmt.Printf("Setup failure: %s", stderr.String()) | ||
os.Exit(1) | ||
} | ||
srv := &http.Server{ | ||
Addr: ":8000", | ||
ReadHeaderTimeout: time.Second, | ||
Handler: server.NewHandler(), | ||
} | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil { | ||
log.Fatalf("Could not start server: %v\n", err) | ||
} | ||
}() | ||
|
||
code := m.Run() | ||
if err := os.Remove("shipyard"); err != nil { | ||
fmt.Printf("Cleanup failure: %v", err) | ||
} | ||
os.Exit(code) | ||
} | ||
|
||
func TestGetAllEnvironments(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
args []string | ||
ids []string | ||
output string | ||
}{ | ||
{ | ||
name: "default org", | ||
args: []string{"get", "envs", "--json"}, | ||
ids: []string{"default-1", "default-2"}, | ||
}, | ||
{ | ||
name: "non default org", | ||
args: []string{"get", "envs", "--org", "pugs", "--json"}, | ||
ids: []string{"pug-1", "pug-2"}, | ||
}, | ||
{ | ||
name: "non existent org", | ||
args: []string{"get", "envs", "--org", "cats"}, | ||
output: "Command error: user org not found\n", | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
c := newCmd(test.args) | ||
if err := c.cmd.Run(); err != nil { | ||
if diff := cmp.Diff(c.stdErr.String(), test.output); diff != "" { | ||
t.Error(diff) | ||
} | ||
return | ||
} | ||
var resp types.RespManyEnvs | ||
if err := json.Unmarshal(c.stdOut.Bytes(), &resp); err != nil { | ||
t.Fatal(err) | ||
} | ||
var ids []string | ||
for i := range resp.Data { | ||
ids = append(ids, resp.Data[i].ID) | ||
} | ||
want := test.ids | ||
if !cmp.Equal(ids, want) { | ||
t.Error(cmp.Diff(ids, want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetEnvironmentByID(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
args []string | ||
id string | ||
output string | ||
}{ | ||
{ | ||
name: "default org", | ||
args: []string{"get", "env", "default-1", "--json"}, | ||
id: "default-1", | ||
}, | ||
{ | ||
name: "non default org", | ||
args: []string{"get", "env", "pug-1", "--org", "pugs", "--json"}, | ||
id: "pug-1", | ||
}, | ||
{ | ||
name: "non existent env", | ||
args: []string{"get", "env", "sharpei-1", "--org", "pugs", "--json"}, | ||
output: "Command error: environment not found\n", | ||
}, | ||
{ | ||
name: "non existent org", | ||
args: []string{"get", "env", "cat-1", "--org", "cats"}, | ||
output: "Command error: user org not found\n", | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
c := newCmd(test.args) | ||
if err := c.cmd.Run(); err != nil { | ||
if diff := cmp.Diff(c.stdErr.String(), test.output); diff != "" { | ||
t.Error(diff) | ||
} | ||
return | ||
} | ||
var resp types.Response | ||
if err := json.Unmarshal(c.stdOut.Bytes(), &resp); err != nil { | ||
t.Fatal(err) | ||
} | ||
want := test.id | ||
got := resp.Data.ID | ||
if !cmp.Equal(got, want) { | ||
t.Error(cmp.Diff(got, want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRebuildEnvironment(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
args []string | ||
output string | ||
}{ | ||
{ | ||
name: "default org", | ||
args: []string{"rebuild", "env", "default-1"}, | ||
output: "Environment queued for a rebuild.\n", | ||
}, | ||
{ | ||
name: "non default org", | ||
args: []string{"rebuild", "env", "pug-1", "--org", "pugs"}, | ||
output: "Environment queued for a rebuild.\n", | ||
}, | ||
{ | ||
name: "non existent env", | ||
args: []string{"rebuild", "env", "sharpei-1", "--org", "pugs"}, | ||
output: "Command error: environment not found\n", | ||
}, | ||
{ | ||
name: "non existent org", | ||
args: []string{"rebuild", "env", "pug-1", "--org", "cats"}, | ||
output: "Command error: user org not found\n", | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
c := newCmd(test.args) | ||
err := c.cmd.Run() | ||
if err != nil { | ||
if diff := cmp.Diff(c.stdErr.String(), test.output); diff != "" { | ||
t.Error(diff) | ||
} | ||
return | ||
} | ||
if diff := cmp.Diff(c.stdOut.String(), test.output); diff != "" { | ||
t.Error(diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// nolint:gosec // Bad arguments can't be passed in. | ||
func newCmd(args []string) *cmdWrapper { | ||
c := cmdWrapper{ | ||
args: args, | ||
} | ||
c.cmd = exec.Command("./shipyard", commandLine(c.args)...) | ||
c.cmd.Env = []string{"SHIPYARD_BUILD_URL=http://localhost:8000"} | ||
stderr, stdout := new(bytes.Buffer), new(bytes.Buffer) | ||
c.cmd.Stderr = stderr | ||
c.cmd.Stdout = stdout | ||
c.stdErr = stderr | ||
c.stdOut = stdout | ||
return &c | ||
} | ||
|
||
func commandLine(in []string) []string { | ||
args := []string{"--config", "config.yaml"} | ||
args = append(args, in...) | ||
return args | ||
} | ||
|
||
type cmdWrapper struct { | ||
cmd *exec.Cmd | ||
args []string | ||
stdErr *bytes.Buffer | ||
stdOut *bytes.Buffer | ||
} |
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,2 @@ | ||
api_token: test | ||
org: default |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this have to build? That'd leave artifacts lying around. It seems like it'd make more sense to do these sorts of tests on the functions themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but that'd be not quite end-to-end. Also, TestMain deletes the binary after every run, so there shouldn't be any artifacts lying around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++ to what @dietb said here, I'm not sure if we necessarily need to test compilation (that + artifact gen should be done as part of CI anyways, right?), but IMO its not really an issue as it gets cleaned up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd need to keep track of the binary (of a single architecture) in Git, which might be annoying. When there are changes to the code, someone will need to recompile it before running the integration tests.
It's not about testing compilation but running tests against the current code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not getting any benefit from running the compiled binary vs the functions directly from what I can tell. Meanwhile, the actual functions themselves don't have the test cases attached that were written for this testing.
For the sake of simplicity and avoiding artifact tracking and depending on people to manually, I'd suggest not doing it this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason for making these tests is ensuring the high-level configuration of the CLI works properly.
That the config file's values are honored, that env vars properly override those, that the most important flags (such as
org
) are properly seen by our configuration (viper). This used to be a source of bugs. So the tests themselves are made to catch those specific bugs.I think we could test the functions themselves when we refactor the general setup, such that a lookup of anything is just an arbitrary function that can be mocked. But currently, all lookups are
viper.GetString
, which is not easy to test.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to expand on what I said earlier -- def shouldn't track the binaries in git, I was referring to tracking them in GitHub itself when creating a release that points to a tag. That being said def out of scope of this, so ignore what I said earlier about it.