|
| 1 | +package devfile |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/devfile/api/v2/pkg/validation/variables" |
| 8 | + "github.com/devfile/library/pkg/devfile/parser" |
| 9 | + "github.com/devfile/library/pkg/devfile/parser/data/v2/common" |
| 10 | +) |
| 11 | + |
| 12 | +func TestParseDevfileAndValidate(t *testing.T) { |
| 13 | + devfileContent := `commands: |
| 14 | +- exec: |
| 15 | + commandLine: ./main {{ PARAMS }} |
| 16 | + component: runtime |
| 17 | + group: |
| 18 | + isDefault: true |
| 19 | + kind: run |
| 20 | + workingDir: ${PROJECT_SOURCE} |
| 21 | + id: run |
| 22 | +components: |
| 23 | +- container: |
| 24 | + endpoints: |
| 25 | + - name: http |
| 26 | + targetPort: 8080 |
| 27 | + image: golang:latest |
| 28 | + memoryLimit: 1024Mi |
| 29 | + mountSources: true |
| 30 | + name: runtime |
| 31 | +metadata: |
| 32 | + description: Stack with the latest Go version |
| 33 | + displayName: Go Runtime |
| 34 | + icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg |
| 35 | + language: go |
| 36 | + name: my-go-app |
| 37 | + projectType: go |
| 38 | + tags: |
| 39 | + - Go |
| 40 | + version: 1.0.0 |
| 41 | +schemaVersion: 2.1.0 |
| 42 | +` |
| 43 | + |
| 44 | + devfileContentWithVariable := devfileContent + `variables: |
| 45 | + PARAMS: foo` |
| 46 | + type args struct { |
| 47 | + args parser.ParserArgs |
| 48 | + } |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + args args |
| 52 | + wantVarWarning variables.VariableWarning |
| 53 | + wantCommandLine string |
| 54 | + wantVariables map[string]string |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "with external overriding variables", |
| 58 | + args: args{ |
| 59 | + args: parser.ParserArgs{ |
| 60 | + ExternalVariables: map[string]string{ |
| 61 | + "PARAMS": "bar", |
| 62 | + }, |
| 63 | + Data: []byte(devfileContentWithVariable), |
| 64 | + }, |
| 65 | + }, |
| 66 | + |
| 67 | + wantCommandLine: "./main bar", |
| 68 | + wantVariables: map[string]string{ |
| 69 | + "PARAMS": "bar", |
| 70 | + }, |
| 71 | + wantVarWarning: variables.VariableWarning{ |
| 72 | + Commands: map[string][]string{}, |
| 73 | + Components: map[string][]string{}, |
| 74 | + Projects: map[string][]string{}, |
| 75 | + StarterProjects: map[string][]string{}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "with new external variables", |
| 80 | + args: args{ |
| 81 | + args: parser.ParserArgs{ |
| 82 | + ExternalVariables: map[string]string{ |
| 83 | + "OTHER": "other", |
| 84 | + }, |
| 85 | + Data: []byte(devfileContentWithVariable), |
| 86 | + }, |
| 87 | + }, |
| 88 | + |
| 89 | + wantCommandLine: "./main foo", |
| 90 | + wantVariables: map[string]string{ |
| 91 | + "PARAMS": "foo", |
| 92 | + "OTHER": "other", |
| 93 | + }, |
| 94 | + wantVarWarning: variables.VariableWarning{ |
| 95 | + Commands: map[string][]string{}, |
| 96 | + Components: map[string][]string{}, |
| 97 | + Projects: map[string][]string{}, |
| 98 | + StarterProjects: map[string][]string{}, |
| 99 | + }, |
| 100 | + }, { |
| 101 | + name: "with new external variables", |
| 102 | + args: args{ |
| 103 | + args: parser.ParserArgs{ |
| 104 | + ExternalVariables: map[string]string{ |
| 105 | + "PARAMS": "baz", |
| 106 | + }, |
| 107 | + Data: []byte(devfileContent), |
| 108 | + }, |
| 109 | + }, |
| 110 | + |
| 111 | + wantCommandLine: "./main baz", |
| 112 | + wantVariables: map[string]string{ |
| 113 | + "PARAMS": "baz", |
| 114 | + }, |
| 115 | + wantVarWarning: variables.VariableWarning{ |
| 116 | + Commands: map[string][]string{}, |
| 117 | + Components: map[string][]string{}, |
| 118 | + Projects: map[string][]string{}, |
| 119 | + StarterProjects: map[string][]string{}, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + gotD, gotVarWarning, err := ParseDevfileAndValidate(tt.args.args) |
| 126 | + if err != nil { |
| 127 | + t.Errorf("ParseDevfileAndValidate() error = %v, wantErr nil", err) |
| 128 | + return |
| 129 | + } |
| 130 | + commands, err := gotD.Data.GetCommands(common.DevfileOptions{}) |
| 131 | + if err != nil { |
| 132 | + t.Errorf("unexpected error getting commands") |
| 133 | + } |
| 134 | + expectedCommandLine := commands[0].Exec.CommandLine |
| 135 | + if expectedCommandLine != tt.wantCommandLine { |
| 136 | + t.Errorf("command line is %q, should be %q", expectedCommandLine, tt.wantCommandLine) |
| 137 | + } |
| 138 | + if !reflect.DeepEqual(gotVarWarning, tt.wantVarWarning) { |
| 139 | + t.Errorf("ParseDevfileAndValidate() gotVarWarning = %v, want %v", gotVarWarning, tt.wantVarWarning) |
| 140 | + } |
| 141 | + variables := gotD.Data.GetDevfileWorkspaceSpec().Variables |
| 142 | + if !reflect.DeepEqual(variables, tt.wantVariables) { |
| 143 | + t.Errorf("variables are %+v, expected %+v", variables, tt.wantVariables) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments