Skip to content

Commit

Permalink
Added some unit tests, mainly about configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Jul 18, 2019
1 parent 0c03161 commit b4e17a0
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 1 deletion.
2 changes: 1 addition & 1 deletion example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ projects:

- name: forward-composieux-website
forward:
- *composieux-fr
- *composieux-fr-local
52 changes: 52 additions & 0 deletions internal/config/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoadSingleFile(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/config.yaml"
MultipleFilepath = dir + "/../tests/config/config.unknown.*.yaml"

// When
conf, err := Load()

// Then
assert.IsType(t, new(Config), conf)
assert.Nil(t, err)

assert.Len(t, conf.Projects, 4)
assert.Equal(t, conf.Watcher.Exclude, []string{
".git",
"node_modules",
})
}

func TestLoadMultipleFiles(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/unknown.yaml"
MultipleFilepath = dir + "/../tests/config/config.multiple.*.yaml"

// Remove single config created file after test
defer os.Remove(Filepath)

// When
conf, err := Load()

// Then
assert.IsType(t, new(Config), conf)
assert.Nil(t, err)

assert.Len(t, conf.Projects, 4)
assert.Equal(t, conf.Watcher.Exclude, []string{
".git",
"node_modules",
"/event/an/absolute/path/in/multiple/files",
})
}
60 changes: 60 additions & 0 deletions internal/tests/config/config.multiple.forward.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Kubernetes forwards

<: &kubernetes-context preprod

<: &graphql-forward
name: graphql
type: kubernetes
values:
context: *kubernetes-context
namespace: backend
labels:
app: graphql
hostname: graphql.svc.local
ports:
- 8080:8000

<: &grpc-api-kubernetes-remote
name: grpc-api
type: kubernetes-remote
values:
context: *kubernetes-context
namespace: backend
labels:
app: grpc-api
ports:
- 8080:8080
- 8001:8001

<: &grpc-api-forward
name: grpc-api
type: kubernetes
values:
context: *kubernetes-context
namespace: backend
labels:
app: grpc-api
hostname: grpc-api.svc.local
ports:
- 8080:8080

<: &composieux-fr-local
name: composieux-fr-local
type: ssh
values:
remote: vincent@composieux.fr
args:
- "-i/Users/name/.ssh/private_key"
hostname: composieux.fr.svc.local
ports:
- 8080:80

<: &composieux-fr-remote
name: composieux-fr-remote
type: ssh-remote
values:
remote: vincent@composieux.fr
args:
- "-i/Users/name/.ssh/private_key"
ports:
- 8080:80
41 changes: 41 additions & 0 deletions internal/tests/config/config.multiple.local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Local applications

<: &graphql-local
name: graphql
path: github.com/eko/graphql
watch: true
hostname: graphql.svc.local
executable: go
args:
- run
- cmd/main.go
env:
HTTP_PORT: 8005
setup:
- go get github.com/eko/graphql
- echo You can use ~/path syntax and environment variables like $GOPATH in your commands

<: &grpc-api-local
name: grpc-api
path: github.com/eko/grpc-api
watch: true
hostname: grpc-api.svc.local
executable: go
args:
- run
- main.go
env:
GRPC_PORT: 8006
setup:
- go get github.com/eko/grpc-api
- echo You can use ~/path syntax and environment variables like $GOPATH in your commands

<: &elasticsearch-local
name: elasticsearch
path: /Users/vincent/dev/docker
watch: true
executable: docker
args:
- start
- -i
- elastic
33 changes: 33 additions & 0 deletions internal/tests/config/config.multiple.project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Settings

gopath: /dev/golang

watcher: # Optional
exclude:
- .git
- node_modules
- /event/an/absolute/path/in/multiple/files

# Projects

projects:
- name: full
local:
- *graphql-local
- *grpc-api-local
- *elasticsearch-local

- name: graphql
local:
- *graphql-local
forward:
- *grpc-api-forward

- name: forward-only
forward:
- *graphql-forward
- *grpc-api-forward

- name: forward-composieux-website
forward:
- *composieux-fr-local
133 changes: 133 additions & 0 deletions internal/tests/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Settings

watcher:
exclude:
- .git
- node_modules

# Kubernetes forwards

<: &kubernetes-context preprod

<: &graphql-forward
name: graphql
type: kubernetes
values:
context: *kubernetes-context
namespace: backend
labels:
app: graphql
hostname: graphql.svc.local # Optional
ports:
- 8080:8000

<: &grpc-api-kubernetes-remote
name: grpc-api
type: kubernetes-remote
values:
context: *kubernetes-context
namespace: backend
labels:
app: grpc-api
ports:
- 8080:8080
- 8001:8001

<: &grpc-api-forward
name: grpc-api
type: kubernetes
values:
context: *kubernetes-context
namespace: backend
labels:
app: grpc-api
hostname: grpc-api.svc.local
ports:
- 8080:8080

<: &composieux-fr-local
name: composieux-fr-local
type: ssh
values:
remote: vincent@composieux.fr
args:
- "-i/Users/name/.ssh/private_key"
hostname: composieux.fr.svc.local
ports:
- 8080:80

<: &composieux-fr-remote
name: composieux-fr-remote
type: ssh-remote
values:
remote: vincent@composieux.fr
args:
- "-i/Users/name/.ssh/private_key"
ports:
- 8080:80

# Local applications

<: &graphql-local
name: graphql
path: github.com/eko/graphql
watch: true
hostname: graphql.svc.local
executable: go
args:
- run
- cmd/main.go
env:
HTTP_PORT: 8005
setup:
- go get github.com/eko/graphql
- echo You can use ~/path syntax and environment variables like $GOPATH in your commands

<: &grpc-api-local
name: grpc-api
path: github.com/eko/grpc-api
watch: true
hostname: grpc-api.svc.local
executable: go
args:
- run
- main.go
env:
GRPC_PORT: 8006
setup:
- go get github.com/eko/grpc-api
- echo You can use ~/path syntax and environment variables like $GOPATH in your commands

<: &elasticsearch-local
name: elasticsearch
path: /Users/vincent/dev/docker
watch: true
executable: docker
args:
- start
- -i
- elastic

# Projects

projects:
- name: full
local:
- *graphql-local
- *grpc-api-local
- *elasticsearch-local

- name: graphql
local:
- *graphql-local
forward:
- *grpc-api-forward

- name: forward-only
forward:
- *graphql-forward
- *grpc-api-forward

- name: forward-composieux-website
forward:
- *composieux-fr-local
35 changes: 35 additions & 0 deletions pkg/forwarder/kubernetes/forwarder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kubernetes

import (
"testing"

"github.com/eko/monday/internal/config"
"github.com/stretchr/testify/assert"
)

func TestNewForwarder(t *testing.T) {
// Given
name := "test-forward"
context := "aws-int1"
namespace := "platform"
ports := []string{"8080:8080"}
labels := map[string]string{
"app": "my-test-app",
}

// When
forwarder, err := NewForwarder(config.ForwarderKubernetes, name, context, namespace, ports, labels)

// Then
assert.IsType(t, new(Forwarder), forwarder)
assert.Nil(t, err)

assert.Equal(t, config.ForwarderKubernetes, forwarder.forwardType)
assert.Equal(t, name, forwarder.name)
assert.Equal(t, context, forwarder.context)
assert.Equal(t, namespace, forwarder.namespace)
assert.Equal(t, ports, forwarder.ports)

assert.Len(t, forwarder.portForwarders, 0)
assert.Len(t, forwarder.deployments, 0)
}
33 changes: 33 additions & 0 deletions pkg/forwarder/ssh/forwarder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ssh

import (
"testing"

"github.com/eko/monday/internal/config"
"github.com/stretchr/testify/assert"
)

func TestNewForwarder(t *testing.T) {
// Given
remote := "root@acme.tld"
localPort := "8080"
forwardPort := "8081"
args := []string{"-i /tmp/my/private.key"}

// forwardType, remote, localPort, forwardPort string, args []string

// When
forwarder, err := NewForwarder(config.ForwarderSSH, remote, localPort, forwardPort, args)

// Then
assert.IsType(t, new(Forwarder), forwarder)
assert.Nil(t, err)

assert.Equal(t, config.ForwarderSSH, forwarder.forwardType)
assert.Equal(t, remote, forwarder.remote)
assert.Equal(t, localPort, forwarder.localPort)
assert.Equal(t, forwardPort, forwarder.forwardPort)
assert.Equal(t, args, forwarder.args)

assert.Nil(t, forwarder.cmd)
}

0 comments on commit b4e17a0

Please sign in to comment.