|
| 1 | +// Copyright 2026 HAProxy Technologies |
| 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 | + |
| 16 | +package configuration |
| 17 | + |
| 18 | +import ( |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + |
| 24 | + parser "github.com/haproxytech/client-native/v6/config-parser" |
| 25 | + "github.com/haproxytech/client-native/v6/config-parser/options" |
| 26 | + "github.com/haproxytech/client-native/v6/models" |
| 27 | +) |
| 28 | + |
| 29 | +func renderCrtStore(t *testing.T, config string, store *models.CrtStore) string { |
| 30 | + t.Helper() |
| 31 | + p, err := parser.New(options.String(config)) |
| 32 | + require.NoError(t, err) |
| 33 | + require.NoError(t, SerializeCrtStore(p, store)) |
| 34 | + return strings.TrimSpace(p.String()) |
| 35 | +} |
| 36 | + |
| 37 | +// crt-base and key-base take a mandatory path: a store without them must |
| 38 | +// render a bare section, not empty keywords that HAProxy rejects. |
| 39 | +func TestSerializeCrtStoreWithoutBases(t *testing.T) { |
| 40 | + store := &models.CrtStore{CrtStoreBase: models.CrtStoreBase{Name: "cs"}} |
| 41 | + require.Equal(t, "crt-store cs", renderCrtStore(t, "crt-store cs\n", store)) |
| 42 | +} |
| 43 | + |
| 44 | +// Clearing a base on edit removes its line instead of leaving the keyword. |
| 45 | +func TestSerializeCrtStoreClearsBases(t *testing.T) { |
| 46 | + store := &models.CrtStore{CrtStoreBase: models.CrtStoreBase{Name: "cs", CrtBase: "/certs"}} |
| 47 | + out := renderCrtStore(t, "crt-store cs\n crt-base /old\n key-base /keys\n", store) |
| 48 | + require.Equal(t, "crt-store cs\n crt-base /certs", out) |
| 49 | +} |
| 50 | + |
| 51 | +func TestSerializeCrtStoreRoundTrip(t *testing.T) { |
| 52 | + store := &models.CrtStore{CrtStoreBase: models.CrtStoreBase{Name: "cs", CrtBase: "/certs", KeyBase: "/keys"}} |
| 53 | + out := renderCrtStore(t, "crt-store cs\n", store) |
| 54 | + |
| 55 | + p, err := parser.New(options.String(out)) |
| 56 | + require.NoError(t, err) |
| 57 | + got := &models.CrtStore{CrtStoreBase: models.CrtStoreBase{Name: "cs"}} |
| 58 | + require.NoError(t, ParseCrtStore(p, got)) |
| 59 | + require.True(t, got.CrtStoreBase.Equal(store.CrtStoreBase)) |
| 60 | +} |
0 commit comments