This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic test framework for cmd line tests
- Loading branch information
Showing
23 changed files
with
2,383 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 | ||
} |
File renamed without changes.
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,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 |
2 changes: 1 addition & 1 deletion
2
tests/cmd/multi-configmapname/app.yaml → ...cmd/fixtures/multi-configmapname/app.yaml
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name: nginx | ||
containers: | ||
- image: nginx | ||
configData: | ||
configMaps: | ||
- data: | ||
foo: bar | ||
name: foo | ||
|
File renamed without changes.
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.