cilium/cilium · error
adding %T with empty name to test: %v
Error message
adding %T with empty name to test: %v
What it means
RegisterPolicy is a generic helper that adds policy objects (CiliumNetworkPolicy/NetworkPolicy) to the test scope map, keyed by object name. This error is thrown when a policy object passed to it has an empty GetName() result. The framework refuses to register unnamed policies because they could not be tracked, looked up, or unambiguously referenced later in the test.
Source
Thrown at cilium-cli/connectivity/check/policy.go:283
if t.expectFunc == nil {
return ResultOK, ResultOK
}
egress, ingress = t.expectFunc(a)
if egress.Drop {
t.Debugf("Expecting egress drops for Action %s: %v", a.name, egress)
}
if ingress.Drop {
t.Debugf("Expecting ingress drops for Action %s: %v", a.name, ingress)
}
return egress, ingress
}
func RegisterPolicy[T policy](current map[string]T, policies ...T) (map[string]T, error) {
for _, p := range policies {
if p.GetName() == "" {
return current, fmt.Errorf("adding %T with empty name to test: %v", p, p)
}
if _, ok := current[p.GetName()]; ok {
return current, fmt.Errorf("%T with name %s already in test scope", p, p.GetName())
}
current[p.GetName()] = p
}
return current, nil
}
func sumMap(m map[string]int) int {
sum := 0
for _, v := range m {
sum += v
}
return sum
}View on GitHub (pinned to ac7b90affa)
Solutions
- Set ObjectMeta.Name on the policy before calling RegisterPolicy.
- Check any policy loaded from YAML/JSON actually includes metadata.name in the file.
- Validate policies at construction time (assert GetName() != "") before handing them to the framework.
- Log the policy struct at the call site — the error prints %v of the whole object to identify which one is unnamed.
Example fix
// before
policy := &ciliumv2.CiliumNetworkPolicy{Spec: spec}
scope, err := check.RegisterPolicy(scope, policy)
// after
policy := &ciliumv2.CiliumNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{Name: "l4-policy", Namespace: ns},
Spec: spec,
}
scope, err := check.RegisterPolicy(scope, policy) Defensive patterns
Strategy: validation
Validate before calling
func validatePolicies[T interface{ GetName() string }](ps ...T) error {
for _, p := range ps {
if p.GetName() == "" {
return fmt.Errorf("policy %T has empty metadata.name", p)
}
}
return nil
}
// call before RegisterPolicy
if err := validatePolicies(policies...); err != nil { return err } Type guard
func hasName[T interface{ GetName() string }](p T) bool {
return p.GetName() != ""
} Prevention
- Always set ObjectMeta.Name (and Namespace) when constructing policies in tests.
- Validate deserialized YAML/JSON policies include metadata.name before registration.
- Add an assertion right after constructing a policy: if p.GetName() == "" { t.Fatal(...) }.
When it happens
Trigger: A policy struct was constructed (or unmarshalled) without setting metadata.name before being passed to RegisterPolicy, e.g. RegisterPolicy(scope, &ciliumv2.CiliumNetworkPolicy{}) with no ObjectMeta.Name.
Common situations: A test author builds a CiliumNetworkPolicy inline and forgets to set Name; a YAML policy file fails to deserialize the name field; code refactoring renamed the name field so it is no longer populated.
Understand the failure class
Background: "Missing required field" and "field is required" errors: why libraries reject payloads that omit mandatory fields — this error's family across 20 libraries.
Related errors
- %T with name %s already in test scope
- cannot unmarshal nil into RuleIPOrCIDR
- top-level description field found
- egress.to.domainNames is only supported for egress.action=Ac
- port 53 must be specified for DNS rules
AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31).
Data as JSON: /api/errors/876866f41064760e.
Report an issue: GitHub.