|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux authors |
| 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 meta_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + |
| 24 | + "github.com/fluxcd/pkg/apis/meta" |
| 25 | +) |
| 26 | + |
| 27 | +func TestMakeDependsOn(t *testing.T) { |
| 28 | + g := NewWithT(t) |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + deps []string |
| 33 | + want []meta.DependencyReference |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "empty", |
| 37 | + deps: []string{}, |
| 38 | + want: []meta.DependencyReference{}, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "single name only", |
| 42 | + deps: []string{"redis"}, |
| 43 | + want: []meta.DependencyReference{ |
| 44 | + {Name: "redis"}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "single with namespace", |
| 49 | + deps: []string{"default/redis"}, |
| 50 | + want: []meta.DependencyReference{ |
| 51 | + {Namespace: "default", Name: "redis"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "single with CEL expression", |
| 56 | + deps: []string{"redis@status.ready==true"}, |
| 57 | + want: []meta.DependencyReference{ |
| 58 | + {Name: "redis", ReadyExpr: "status.ready==true"}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "single with namespace and CEL expression", |
| 63 | + deps: []string{"default/redis@status.ready==true"}, |
| 64 | + want: []meta.DependencyReference{ |
| 65 | + {Namespace: "default", Name: "redis", ReadyExpr: "status.ready==true"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "multiple dependencies", |
| 70 | + deps: []string{ |
| 71 | + "redis", |
| 72 | + "default/postgres@status.ready==true", |
| 73 | + "infra/cert-manager", |
| 74 | + }, |
| 75 | + want: []meta.DependencyReference{ |
| 76 | + {Name: "redis"}, |
| 77 | + {Namespace: "default", Name: "postgres", ReadyExpr: "status.ready==true"}, |
| 78 | + {Namespace: "infra", Name: "cert-manager"}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "CEL expression with multiple operators", |
| 83 | + deps: []string{"default/app@status.ready==true && status.observed==1"}, |
| 84 | + want: []meta.DependencyReference{ |
| 85 | + {Namespace: "default", Name: "app", ReadyExpr: "status.ready==true && status.observed==1"}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "CEL expression with function calls", |
| 90 | + deps: []string{"infra/ingress@has(status.loadBalancer.ingress)"}, |
| 91 | + want: []meta.DependencyReference{ |
| 92 | + {Namespace: "infra", Name: "ingress", ReadyExpr: "has(status.loadBalancer.ingress)"}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tt := range tests { |
| 98 | + t.Run(tt.name, func(t *testing.T) { |
| 99 | + got := meta.MakeDependsOn(tt.deps) |
| 100 | + g.Expect(got).To(Equal(tt.want)) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestDependencyReferenceString(t *testing.T) { |
| 106 | + g := NewWithT(t) |
| 107 | + |
| 108 | + tests := []struct { |
| 109 | + name string |
| 110 | + ref meta.DependencyReference |
| 111 | + want string |
| 112 | + }{ |
| 113 | + { |
| 114 | + name: "name only", |
| 115 | + ref: meta.DependencyReference{Name: "redis"}, |
| 116 | + want: "redis", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "namespace and name", |
| 120 | + ref: meta.DependencyReference{Namespace: "default", Name: "redis"}, |
| 121 | + want: "default/redis", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "name and CEL expression", |
| 125 | + ref: meta.DependencyReference{Name: "redis", ReadyExpr: "status.ready==true"}, |
| 126 | + want: "redis@status.ready==true", |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "namespace, name, and CEL expression", |
| 130 | + ref: meta.DependencyReference{Namespace: "default", Name: "redis", ReadyExpr: "status.ready==true"}, |
| 131 | + want: "default/redis@status.ready==true", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "name with complex CEL expression", |
| 135 | + ref: meta.DependencyReference{Name: "app", ReadyExpr: "status.ready==true && status.observed==1"}, |
| 136 | + want: "app@status.ready==true && status.observed==1", |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, tt := range tests { |
| 141 | + t.Run(tt.name, func(t *testing.T) { |
| 142 | + got := tt.ref.String() |
| 143 | + g.Expect(got).To(Equal(tt.want)) |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestDependencyReferenceRoundTrip(t *testing.T) { |
| 149 | + g := NewWithT(t) |
| 150 | + |
| 151 | + tests := []meta.DependencyReference{ |
| 152 | + {Name: "redis"}, |
| 153 | + {Namespace: "default", Name: "postgres"}, |
| 154 | + {Name: "cache", ReadyExpr: "status.ready==true"}, |
| 155 | + {Namespace: "infra", Name: "ingress", ReadyExpr: "has(status.loadBalancer.ingress)"}, |
| 156 | + } |
| 157 | + |
| 158 | + for _, original := range tests { |
| 159 | + t.Run(original.String(), func(t *testing.T) { |
| 160 | + // Serialize to string |
| 161 | + str := original.String() |
| 162 | + |
| 163 | + // Parse back from string |
| 164 | + parsed := meta.MakeDependsOn([]string{str}) |
| 165 | + g.Expect(parsed).To(HaveLen(1)) |
| 166 | + |
| 167 | + // Should match original |
| 168 | + g.Expect(parsed[0]).To(Equal(original)) |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
0 commit comments