|
| 1 | +// Copyright 2020 The Lokomotive Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dex //nolint:testpackage |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/kinvolk/lokomotive/pkg/components/util" |
| 21 | +) |
| 22 | + |
| 23 | +//nolint:funlen |
| 24 | +func TestRenderManifest(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + desc string |
| 27 | + hcl string |
| 28 | + wantErr bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + desc: "Valid config", |
| 32 | + hcl: ` |
| 33 | +component "dex" { |
| 34 | + ingress_host = "foo" |
| 35 | + issuer_host = "bar" |
| 36 | + connector "github" { |
| 37 | + id = "github" |
| 38 | + name = "Github" |
| 39 | + config { |
| 40 | + client_id = "clientid" |
| 41 | + client_secret = "clientsecret" |
| 42 | + redirect_uri = "redirecturi" |
| 43 | + team_name_field = "slug" |
| 44 | + org { |
| 45 | + name = "kinvolk" |
| 46 | + teams = [ |
| 47 | + "lokomotive-developers", |
| 48 | + ] |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + static_client { |
| 53 | + name = "gangway" |
| 54 | + id = "gangway id" |
| 55 | + secret = "gangway secret" |
| 56 | + redirect_uris = ["redirecturis"] |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + `, |
| 61 | + }, |
| 62 | + { |
| 63 | + desc: "invalid config", |
| 64 | + hcl: ` |
| 65 | +component "dex" { |
| 66 | + ingress_host = "NodePort" |
| 67 | +} |
| 68 | + `, |
| 69 | + wantErr: true, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tc := range tests { |
| 74 | + b, d := util.GetComponentBody(tc.hcl, name) |
| 75 | + if d != nil { |
| 76 | + t.Errorf("%s - Error getting component body: %v", tc.desc, d) |
| 77 | + } |
| 78 | + |
| 79 | + c := newComponent() |
| 80 | + d = c.LoadConfig(b, nil) |
| 81 | + |
| 82 | + if !tc.wantErr && d.HasErrors() { |
| 83 | + t.Errorf("%s - Valid config should not return error, got: %s", tc.desc, d) |
| 84 | + } |
| 85 | + |
| 86 | + if tc.wantErr && !d.HasErrors() { |
| 87 | + t.Errorf("%s - Wrong config should have returned an error", tc.desc) |
| 88 | + } |
| 89 | + |
| 90 | + m, err := c.RenderManifests() |
| 91 | + if err != nil { |
| 92 | + t.Errorf("%s - Rendering manifests with valid config should succeed, got: %s", tc.desc, err) |
| 93 | + } |
| 94 | + |
| 95 | + if len(m) == 0 { |
| 96 | + t.Errorf("%s - Rendered manifests shouldn't be empty", tc.desc) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments