Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

add basic test framework for cmd line tests #498

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ endif
endif
endif

# Run cmd line tests
.PHONY: test-cmd
test-cmd: bin
go test -v github.com/kedgeproject/kedge/tests/cmd

# Run all tests
.PHONY: test
test: test-dep validate test-unit test-unit-cover
test: test-dep validate test-unit test-cmd test-unit-cover

# Tests that are run on travs-ci
.PHONY: travis-tests
travis-tests: test-dep validate test-unit-cover test-jsonschema-generation
travis-tests: test-cmd test-dep validate test-unit-cover test-jsonschema-generation

# Install all the required test-dependencies before executing tests (only valid when running `make test`)
.PHONY: test-dep
Expand Down
4 changes: 4 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import:
version: 8d1157a435470616f975ff9bb013bea8d0962067
subpackages:
- unix


- package: github.com/kylelemons/godebug/diff
version: d65d576e9348f5982d7f6d83682b694e731a45c6

# Those are packages that OpenShift patches in its vendor directory.
# Those patched libs are not published elsewhere.
Expand Down
97 changes: 97 additions & 0 deletions tests/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"

"github.com/kylelemons/godebug/diff"
)

var Fixtures = os.ExpandEnv("$GOPATH/src/github.com/kedgeproject/kedge/tests/cmd/fixtures/")
var ProjectPath = "$GOPATH/src/github.com/kedgeproject/kedge/"
var BinaryLocation = os.ExpandEnv(ProjectPath + "kedge")

func TestKedgeGenerate(t *testing.T) {
testCases := []struct {
name string
path string
input string
wantSuccess bool
error string
}{
{
name: "multiple configmaps given and name not specified",
path: "multi-configmapname/app.yaml",
wantSuccess: false,
error: "unable to perform controller operations: unable to fix data: unable to fix ControllerFields: unable to fix configMaps: please specify name for app.configMaps[1]",
},
{
name: "generating deploymentconfig",
path: "controllers/input",
input: Fixtures + "controllers/os.yaml",
wantSuccess: true,
},
{
name: "multiple containers given and name not specified for 2nd contaniner",
path: "multi-containername/nginx.yaml",
wantSuccess: false,
error: "unable to perform controller operations: unable to fix data: unable to fix ControllerFields: unable to fix containers: please specify name for app.containers[1]",
},
{
name: "multiple volume claims with same name",
path: "multi-pvc-same-name/app.yaml",
error: `unable to perform controller operations: unable to validate data: unable to validate controller fields: error validating volume claims: duplicate entry of volume claim "foo"`,
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
command := []string{"generate", "-f=" + Fixtures + tt.path}
stdout, err := runCmd(t, command)
if err != nil && !tt.wantSuccess {

if stdout != tt.error {
t.Fatalf("wanted error: %q\nbut got error: %q %v", tt.error, stdout, err)
} else {
t.Logf("failed with error: %q", stdout)
return
}
} else if err != nil && tt.wantSuccess {
t.Fatalf("wanted success, but test failed with error: %s %v", stdout, err)
} else if err == nil && !tt.wantSuccess {
t.Fatalf("expected to fail but passed")
}

// Read the data from the input file
data, err := ioutil.ReadFile(tt.input)
if err != nil {
t.Fatal(err)
}

if diff := diff.Diff(string(data), fmt.Sprintf("%s\n", stdout)); diff != "" {
t.Fatalf("wanted: \n%s\n======================\ngot: \n%s"+
"\n======================\ndiff: %s", string(data), stdout, diff)
}

})
}
}

func runCmd(t *testing.T, args []string) (string, error) {
cmd := exec.Command(BinaryLocation, args...)

var stdout bytes.Buffer
cmd.Stdout = &stdout

t.Logf("Running: %s", strings.Join(cmd.Args, " "))
err := cmd.Run()
if err != nil {
return strings.TrimSpace(stdout.String()), err
}
return strings.TrimSpace(stdout.String()), nil
}
193 changes: 193 additions & 0 deletions tests/cmd/fixtures/controllers/os.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
status: {}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab-etc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
status: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab
spec:
ports:
- name: ssh
port: 22
targetPort: 22
- name: http
port: 80
targetPort: 80
- name: https
port: 443
targetPort: 443
selector:
app: gitlab
type: LoadBalancer
status:
loadBalancer: {}
---
apiVersion: v1
data:
db-password: Z2l0bGFi
db-user: Z2l0bGFi
redis-password: Z2l0bGFi
kind: Secret
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab
---
apiVersion: v1
data:
gitlab_omnibus_config: |
external_url ENV['EXTERNAL_URL'];
root_pass = ENV['GITLAB_ROOT_PASSWORD'];
gitlab_rails['initial_root_password'] = root_pass unless root_pass.to_s == '';
postgresql['enable'] = false;
gitlab_rails['db_host'] = ENV['DB_HOST'];
gitlab_rails['db_password'] = ENV['DB_PASSWORD'];
gitlab_rails['db_username'] = ENV['DB_USER'];
gitlab_rails['db_database'] = ENV['DB_DATABASE'];
redis['enable'] = false;
gitlab_rails['redis_host'] = ENV['REDIS_HOST'];
gitlab_rails['redis_password'] = ENV['REDIS_PASSWORD'];
unicorn['worker_processes'] = 2;
manage_accounts['enable'] = true;
manage_storage_directories['manage_etc'] = false;
gitlab_shell['auth_file'] = '/gitlab-data/ssh/authorized_keys';
git_data_dir '/gitlab-data/git-data';
gitlab_rails['shared_path'] = '/gitlab-data/shared';
gitlab_rails['uploads_directory'] = '/gitlab-data/uploads';
gitlab_ci['builds_directory'] = '/gitlab-data/builds';
kind: ConfigMap
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab
---
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab
spec:
replicas: 4
strategy:
resources: {}
template:
metadata:
creationTimestamp: null
labels:
app: gitlab
name: gitlab
spec:
containers:
- env:
- name: GITLAB_OMNIBUS_CONFIG
valueFrom:
configMapKeyRef:
key: gitlab_omnibus_config
name: gitlab
- name: GITLAB_ROOT_PASSWORD
- name: EXTERNAL_URL
value: http://your-domain.com/
- name: DB_HOST
value: postgresql
- name: DB_USER
valueFrom:
secretKeyRef:
key: db-user
name: gitlab
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
key: db-password
name: gitlab
- name: DB_DATABASE
value: gitlab
- name: REDIS_HOST
value: redis
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
key: redis-password
name: gitlab
image: gitlab/gitlab-ce:9.4.1-ce.0
livenessProbe:
failureThreshold: 10
httpGet:
path: /help
port: 80
initialDelaySeconds: 200
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
name: gitlab
readinessProbe:
failureThreshold: 3
httpGet:
path: /help
port: 80
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
resources:
limits:
cpu: "1"
memory: 2Gi
requests:
cpu: 500m
memory: 1Gi
volumeMounts:
- mountPath: /etc/gitlab
name: gitlab-etc
- mountPath: /gitlab-data
name: gitlab-data
volumes:
- name: gitlab-etc
persistentVolumeClaim:
claimName: gitlab-etc
- name: gitlab-data
persistentVolumeClaim:
claimName: gitlab-data
test: false
triggers: null
status:
availableReplicas: 0
latestVersion: 0
observedGeneration: 0
replicas: 0
unavailableReplicas: 0
updatedReplicas: 0
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: nginx
containers:
- image: nginx
configData:
configMaps:
- data:
foo: bar
name: foo
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/kylelemons/godebug/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading