|
| 1 | +/* |
| 2 | +Copyright 2026 Cozystack contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "slices" |
| 21 | + "testing" |
| 22 | + |
| 23 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// TestResolveLayerStack: RD wins over RG; both empty → default. |
| 27 | +func TestResolveLayerStack(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + cases := []struct { |
| 31 | + name string |
| 32 | + rd []string |
| 33 | + rg []string |
| 34 | + want []string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "RD set, RG ignored", |
| 38 | + rd: []string{"LUKS", "STORAGE"}, |
| 39 | + rg: []string{"DRBD", "STORAGE"}, |
| 40 | + want: []string{"LUKS", "STORAGE"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "RD empty, RG wins", |
| 44 | + rg: []string{"DRBD", "LUKS", "STORAGE"}, |
| 45 | + want: []string{"DRBD", "LUKS", "STORAGE"}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "both empty → default", |
| 49 | + want: []string{"DRBD", "STORAGE"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "RD empty slice → default", |
| 53 | + rd: []string{}, |
| 54 | + rg: []string{}, |
| 55 | + want: []string{"DRBD", "STORAGE"}, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, c := range cases { |
| 60 | + t.Run(c.name, func(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + got := apiv1.ResolveLayerStack(c.rd, c.rg) |
| 64 | + if !slices.Equal(got, c.want) { |
| 65 | + t.Errorf("got %v, want %v", got, c.want) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// TestLayerInStack: case-insensitive presence check. |
| 72 | +func TestLayerInStack(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + stack := []string{"DRBD", "STORAGE"} |
| 76 | + |
| 77 | + if !apiv1.LayerInStack(stack, "DRBD") { |
| 78 | + t.Errorf("DRBD should be in %v", stack) |
| 79 | + } |
| 80 | + |
| 81 | + if !apiv1.LayerInStack(stack, "drbd") { |
| 82 | + t.Errorf("case-insensitive match for drbd should hit DRBD") |
| 83 | + } |
| 84 | + |
| 85 | + if apiv1.LayerInStack(stack, "LUKS") { |
| 86 | + t.Errorf("LUKS not in %v", stack) |
| 87 | + } |
| 88 | + |
| 89 | + if apiv1.LayerInStack(nil, "DRBD") { |
| 90 | + t.Errorf("empty stack should not match anything") |
| 91 | + } |
| 92 | +} |
0 commit comments