Skip to content

Commit

Permalink
Merge pull request #157 from ubclaunchpad/rob/#149-instrumented-tests
Browse files Browse the repository at this point in the history
Instrumented tests reorganization, fixed coveralls
  • Loading branch information
bobheadxi authored Apr 7, 2018
2 parents a2af2ac + 62597ce commit 21932d5
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Makefile

# Test environment
test_env
test

# Documentation
README.md
Expand Down
17 changes: 9 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ matrix:
fast_finish: true

before_install:
- GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/) # All the .go files, excluding vendor/
- go get -u honnef.co/go/tools/cmd/megacheck
- go get -u github.com/fzipp/gocyclo
- go get -u github.com/mattn/goveralls
- go get -u github.com/golang/dep/cmd/dep
- go get honnef.co/go/tools/cmd/megacheck
- go get github.com/fzipp/gocyclo
- go get github.com/mattn/goveralls
- go get github.com/golang/dep/cmd/dep
- dep ensure

# Run tests and various code quality checks
# Build daemon, run tests, and make various code quality checks
script:
- make testenv VPS_OS="$VPS_OS" VPS_VERSION="$VERSION" SSH_PORT=69
- make testdaemon SSH_PORT=69 # Send test daemon to test VPS
- go test -v -race -coverprofile=coverage.out ./...
- goveralls -coverprofile=coverage.out -service=travis-ci -repotoken "$COVERALLS_TOKEN"
- GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/)
- gocyclo -over 19 $GO_FILES # Forbid code with huge functions
- go vet ./... # Report suspicious constructs
- megacheck ./... # Static analysis
- gocyclo -over 19 $GO_FILES # Forbid code with huge functions
- goveralls -v -service=travis-ci -repotoken "$COVERALLS_TOKEN"

# Push version-tagged Docker image and build platform binaries
before_deploy:
Expand Down
41 changes: 24 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.PHONY: commands inertia inertia-tagged clean test test-v testenv testdaemon daemon bootstrap web-deps web-run web-build
.PHONY: ls inertia inertia-tagged clean test test-v test-all test-integration test-integration-fast testenv testdaemon daemon bootstrap web-deps web-run web-build

TAG = `git describe --tags`
PACKAGES = `go list ./... | grep -v vendor/`
SSH_PORT = 22
VPS_VERSION = latest
VPS_OS = ubuntu
Expand All @@ -10,7 +9,7 @@ RELEASE = canary
all: inertia

# List all commands
commands:
ls:
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs

# Install Inertia with release version
Expand All @@ -26,40 +25,48 @@ clean:
rm -f ./inertia
find . -type f -name inertia.\* -exec rm {} \;

# Run test suite - creates test VPS and test daemon beforehand
# Run unit test suite
test:
go test ./... -short -ldflags "-X main.Version=test" --cover

# Run unit test suite verbosely
test-v:
go test ./... -short -ldflags "-X main.Version=test" -v --cover

# Run unit and integration tests - creates fresh test VPS and test daemon beforehand
test-all:
make testenv VPS_OS=$(VPS_OS) VPS_VERSION=$(VPS_VERSION)
make testdaemon
go test $(PACKAGES) -ldflags "-X main.Version=test" --cover
go test ./... -ldflags "-X main.Version=test" --cover

# Run test suite - creates test VPS and test daemon beforehand
test-v:
# Run integration tests verbosely - creates fresh test VPS and test daemon beforehand
test-integration:
make testenv VPS_OS=$(VPS_OS) VPS_VERSION=$(VPS_VERSION)
make testdaemon
go test $(PACKAGES) -ldflags "-X main.Version=test" -v --cover
make testdaemon
go test ./... -v -ldflags "-X main.Version=test" --cover

# Run test suite without recreating test VPS
test-dirty:
# Run integration tests verbosely without recreating test VPS
test-integration-fast:
make testdaemon
go test $(PACKAGES) -ldflags "-X main.Version=test" --cover
go test ./... -run -v Integration -ldflags "-X main.Version=test" --cover

# Create test VPS
testenv:
docker stop testvps || true && docker rm testvps || true
docker build -f ./test_env/Dockerfile.$(VPS_OS) \
docker build -f ./test/env/Dockerfile.$(VPS_OS) \
-t $(VPS_OS)vps \
--build-arg VERSION=$(VPS_VERSION) \
./test_env
bash ./test_env/startvps.sh $(SSH_PORT) $(VPS_OS)vps
./test
bash ./test/env/startvps.sh $(SSH_PORT) $(VPS_OS)vps

# Create test daemon and scp the image to the test VPS for use.
# Requires Inertia version to be "test"
testdaemon:
rm -f ./inertia-daemon-image
docker build -t ubclaunchpad/inertia:test .
docker save -o ./inertia-daemon-image ubclaunchpad/inertia:test
chmod 400 ./test_env/test_key
scp -i ./test_env/test_key \
chmod 400 ./test/keys/id_rsa
scp -i ./test/keys/id_rsa \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-P $(SSH_PORT) \
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ Inertia is a cross-platform command line tool that simplifies setup and manageme

# :package: Usage

All you need to get started is a docker-compose project, the Inertia CLI, and access to a virtual private server. To download the CLI:
All you need to get started is a docker-compose project, the Inertia CLI, and access to a virtual private server. To install the CLI using [Homebrew](https://brew.sh):

```bash
$> brew install ubclaunchpad/tap/inertia
$> inertia
```

For other platforms, you can also [download the appropriate binary for your platform from the Releases page](https://github.com/ubclaunchpad/inertia/releases) or [install Inertia from source](#installing-from-source).
For other platforms, you can also [download the appropriate binary from the Releases page](https://github.com/ubclaunchpad/inertia/releases) or [install Inertia from source](#installing-from-source).

## Setup

Expand Down Expand Up @@ -140,7 +139,7 @@ Inertia is set up serverside by executing a script over SSH that installs Docker

# :construction: Development

This section outlines the various tools available to help you get started developing Inertia. Run `make commands` to list all the Makefile shortcuts available.
This section outlines the various tools available to help you get started developing Inertia. Run `make ls` to list all the Makefile shortcuts available.

If you would like to contribute, feel free to comment on an issue or make one and open up a pull request!

Expand Down Expand Up @@ -169,7 +168,7 @@ $> inertia --version # check what version you have installed

Alternatively, you can manually edit `.inertia.toml` to use your desired daemon version - see the [Release Streams](#release-streams) documentation for more details.

Note that if you install Inertia using these commands or any variation of `go install`, you may have to remove the binary using `go clean -i github.com/ubclaunchpad/inertia` to go back to using an Inertia CLI installed using Homebrew.
Note that if you install Inertia using these commands or any variation of `go install`, you may have to remove the binary using `go clean -i github.com/ubclaunchpad/inertia` to go back to using an Inertia CLI installed using Homebrew. To go back to a `go install`ed version of Inertia, you need to run `brew uninstall inertia`.

For development, you should install a build tagged as `test` so that you can make use `make testdaemon` for local development. See the next section for more details.

Expand All @@ -182,8 +181,8 @@ $> make RELEASE=test # installs current Inertia build and mark as "test"
You will need Docker installed and running to run the Inertia test suite, which includes a number of integration tests.

```bash
$> make test # test against ubuntu:latest
$> make test VPS_OS=ubuntu VERSION=14.04 # test against ubuntu:14.04
$> make test-all # test against ubuntu:latest
$> make test-all VPS_OS=ubuntu VERSION=14.04 # test against ubuntu:14.04
```

You can also manually start a container that sets up a mock VPS for testing:
Expand All @@ -201,7 +200,7 @@ You can [SSH into this testvps container](https://bobheadxi.github.io/dockercept
$> cd /path/to/my/dockercompose/project
$> inertia init
$> inertia remote add local
# PEM file: inertia/test_env/test_key, User: 'root', Address: 0.0.0.0
# PEM file: inertia/test/keys/id_rsa, User: 'root', Address: 0.0.0.0
$> inertia local init
$> inertia local status
```
Expand Down
16 changes: 10 additions & 6 deletions client/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,19 @@ func (remote *RemoteVPS) Bootstrap(runner SSHSession, name string, config *Confi
println("=============================\n")

// Output deploy key to user.
println("GitHub Deploy Key (add to https://www.github.com/<your_repo>/settings/keys/new): ")
println(">> GitHub Deploy Key (add to https://www.github.com/<your_repo>/settings/keys/new): ")
println(pub.String())

// Output Webhook url to user.
println("GitHub WebHook URL (add to https://www.github.com/<your_repo>/settings/hooks/new): ")
println("http://" + remote.IP + ":" + remote.Daemon.Port)
println("Github WebHook Secret: " + remote.Daemon.Secret + "\n")

println("Inertia daemon successfully deployed! Add your webhook url and deploy\nkey to enable continuous deployment.")
println(">> GitHub WebHook URL (add to https://www.github.com/<your_repo>/settings/hooks/new): ")
println("WebHook Address: https://" + remote.IP + ":" + remote.Daemon.Port)
println("WebHook Secret: " + remote.Daemon.Secret)
println(`Note that you will have to disable SSH verification in your webhook
settings - Inertia uses self-signed certificates that GitHub won't
be able to verify.` + "\n")

println(`Inertia daemon successfully deployed! Add your webhook url and deploy
key to enable continuous deployment.`)
fmt.Printf("Then run 'inertia %s up' to deploy your application.\n", name)

return nil
Expand Down
20 changes: 12 additions & 8 deletions client/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func getTestConfig(writer io.Writer) *Config {
return config
}

func getInstrumentedTestRemote() *RemoteVPS {
func getTestRemote() *RemoteVPS {
remote := &RemoteVPS{
IP: "0.0.0.0",
PEM: "../test_env/test_key",
PEM: "../test/keys/id_rsa",
User: "root",
Daemon: &DaemonConfig{
Port: "8081",
Expand All @@ -41,7 +41,7 @@ func getInstrumentedTestRemote() *RemoteVPS {
}

func TestInstallDocker(t *testing.T) {
remote := getInstrumentedTestRemote()
remote := getTestRemote()
script, err := ioutil.ReadFile("bootstrap/docker.sh")
assert.Nil(t, err)

Expand All @@ -52,7 +52,7 @@ func TestInstallDocker(t *testing.T) {
}

func TestDaemonUp(t *testing.T) {
remote := getInstrumentedTestRemote()
remote := getTestRemote()
script, err := ioutil.ReadFile("bootstrap/daemon-up.sh")
assert.Nil(t, err)
actualCommand := fmt.Sprintf(string(script), "latest", "8081", "0.0.0.0")
Expand All @@ -68,7 +68,7 @@ func TestDaemonUp(t *testing.T) {
}

func TestKeyGen(t *testing.T) {
remote := getInstrumentedTestRemote()
remote := getTestRemote()
script, err := ioutil.ReadFile("bootstrap/token.sh")
assert.Nil(t, err)
tokenScript := fmt.Sprintf(string(script), "test")
Expand All @@ -83,7 +83,7 @@ func TestKeyGen(t *testing.T) {
}

func TestBootstrap(t *testing.T) {
remote := getInstrumentedTestRemote()
remote := getTestRemote()
dockerScript, err := ioutil.ReadFile("bootstrap/docker.sh")
assert.Nil(t, err)

Expand All @@ -110,8 +110,12 @@ func TestBootstrap(t *testing.T) {
assert.Equal(t, tokenScript, session.Calls[3])
}

func TestInstrumentedBootstrap(t *testing.T) {
remote := getInstrumentedTestRemote()
func TestBootstrapIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

remote := getTestRemote()
session := &SSHRunner{r: remote}
var writer bytes.Buffer
err := remote.Bootstrap(session, "testvps", getTestConfig(&writer))
Expand Down
4 changes: 2 additions & 2 deletions client/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (runner *mockSSHRunner) RunSession() error {
}

func TestRun(t *testing.T) {
remote := getInstrumentedTestRemote()
remote := getTestRemote()
session := mockSSHRunner{r: remote}
cmd := "ls -lsa"

Expand All @@ -38,7 +38,7 @@ func TestRun(t *testing.T) {
}

func TestRunInteractive(t *testing.T) {
remote := getInstrumentedTestRemote()
remote := getTestRemote()
session := mockSSHRunner{r: remote}
cmd := "ls -lsa"

Expand Down
2 changes: 1 addition & 1 deletion daemon/inertia/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestAuthorizationSignatureInvalid(t *testing.T) {
}

func TestGetGithubKey(t *testing.T) {
inertiaKeyPath := path.Join(os.Getenv("GOPATH"), "/src/github.com/ubclaunchpad/inertia/test_env/test_key")
inertiaKeyPath := path.Join(os.Getenv("GOPATH"), "/src/github.com/ubclaunchpad/inertia/test/keys/id_rsa")
pemFile, err := os.Open(inertiaKeyPath)
assert.Nil(t, err)
_, err = GetGithubKey(pemFile)
Expand Down
1 change: 1 addition & 0 deletions daemon/inertia/project/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
NoContainersResp = "There are currently no active containers."
)

// ProjectName is user-assigned name of deployed project
var ProjectName = "project"

// Deploy does git pull, docker-compose build, docker-compose up
Expand Down
20 changes: 16 additions & 4 deletions daemon/inertia/project/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var urlVariations = []string{
}

func getInertiaDeployTestKey() (ssh.AuthMethod, error) {
pemFile, err := os.Open("../../../test_env/test_key")
pemFile, err := os.Open("../../../test/keys/id_rsa")
if err != nil {
return nil, err
}
Expand All @@ -33,7 +33,11 @@ func getInertiaDeployTestKey() (ssh.AuthMethod, error) {
return ssh.NewPublicKeys("git", bytes, "")
}

func TestClone(t *testing.T) {
func TestCloneIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

dir := "./test_clone/"
repo, err := Clone(dir, inertiaDeployTest, "dev", nil, os.Stdout)
defer os.RemoveAll(dir)
Expand All @@ -44,7 +48,11 @@ func TestClone(t *testing.T) {
assert.Equal(t, "dev", head.Name().Short())
}

func TestForcePull(t *testing.T) {
func TestForcePullIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

dir := "./test_force_pull/"
auth, err := getInertiaDeployTestKey()
assert.Nil(t, err)
Expand All @@ -63,7 +71,11 @@ func TestForcePull(t *testing.T) {
assert.Nil(t, err)
}

func TestUpdateRepository(t *testing.T) {
func TestUpdateRepositoryIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

dir := "./test_update/"
repo, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: inertiaDeployTest,
Expand Down
4 changes: 2 additions & 2 deletions test_env/Dockerfile.amazon → test/env/Dockerfile.amazon
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ RUN echo "export VISIBLE=now" >> /etc/profile

# Copy test key to allow use
RUN mkdir ~/.ssh/ ; touch ~/.ssh/authorized_keys
COPY . .
RUN cat test_key.pub >> ~/.ssh/authorized_keys
COPY ./keys/ .
RUN cat id_rsa.pub >> ~/.ssh/authorized_keys

EXPOSE 0-9000
CMD ["/usr/sbin/sshd", "-D"]
4 changes: 2 additions & 2 deletions test_env/Dockerfile.centos → test/env/Dockerfile.centos
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ RUN echo "export VISIBLE=now" >> /etc/profile

# Copy test key to allow use
RUN mkdir $HOME/.ssh/
COPY . .
RUN cat test_key.pub >> $HOME/.ssh/authorized_keys
COPY ./keys/ .
RUN cat id_rsa.pub >> $HOME/.ssh/authorized_keys

EXPOSE 0-9000
CMD ["/usr/sbin/init", "/usr/sbin/sshd", "-D"]
4 changes: 2 additions & 2 deletions test_env/Dockerfile.debian → test/env/Dockerfile.debian
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ RUN echo "export VISIBLE=now" >> /etc/profile

# Copy test key to allow use
RUN mkdir $HOME/.ssh/
COPY . .
RUN cat test_key.pub >> $HOME/.ssh/authorized_keys
COPY ./keys/ .
RUN cat id_rsa.pub >> $HOME/.ssh/authorized_keys

EXPOSE 0-9000
CMD ["/usr/sbin/sshd", "-D"]
4 changes: 2 additions & 2 deletions test_env/Dockerfile.ubuntu → test/env/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ RUN echo "export VISIBLE=now" >> /etc/profile

# Copy test key to allow use
RUN mkdir $HOME/.ssh/
COPY . .
RUN cat test_key.pub >> $HOME/.ssh/authorized_keys
COPY ./keys/ .
RUN cat id_rsa.pub >> $HOME/.ssh/authorized_keys

EXPOSE 0-9000
CMD ["/usr/sbin/sshd", "-D"]
2 changes: 1 addition & 1 deletion test_env/startvps.sh → test/env/startvps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ echo ""
echo "Test VPS is online (kill using 'docker kill testvps')"
echo "SSH port: " $(docker port testvps 22)
echo "Daemon port: " $(docker port testvps 8081)
echo "Test key: " $GOPATH/src/github.com/ubclaunchpad/inertia/test_env/test_key
echo "Test key: " $GOPATH/src/github.com/ubclaunchpad/inertia/test/keys/id_rsa
File renamed without changes.
File renamed without changes.

0 comments on commit 21932d5

Please sign in to comment.