{"record":{"id":"876866f41064760e","repo":"cilium/cilium","slug":"adding-t-with-empty-name-to-test-v","errorCode":null,"errorMessage":"adding %T with empty name to test: %v","messagePattern":"adding %T with empty name to test: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cilium-cli/connectivity/check/policy.go","lineNumber":283,"sourceCode":"\tif t.expectFunc == nil {\n\t\treturn ResultOK, ResultOK\n\t}\n\n\tegress, ingress = t.expectFunc(a)\n\tif egress.Drop {\n\t\tt.Debugf(\"Expecting egress drops for Action %s: %v\", a.name, egress)\n\t}\n\tif ingress.Drop {\n\t\tt.Debugf(\"Expecting ingress drops for Action %s: %v\", a.name, ingress)\n\t}\n\n\treturn egress, ingress\n}\n\nfunc RegisterPolicy[T policy](current map[string]T, policies ...T) (map[string]T, error) {\n\tfor _, p := range policies {\n\t\tif p.GetName() == \"\" {\n\t\t\treturn current, fmt.Errorf(\"adding %T with empty name to test: %v\", p, p)\n\t\t}\n\t\tif _, ok := current[p.GetName()]; ok {\n\t\t\treturn current, fmt.Errorf(\"%T with name %s already in test scope\", p, p.GetName())\n\t\t}\n\n\t\tcurrent[p.GetName()] = p\n\t}\n\n\treturn current, nil\n}\n\nfunc sumMap(m map[string]int) int {\n\tsum := 0\n\tfor _, v := range m {\n\t\tsum += v\n\t}\n\treturn sum\n}","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/cilium-cli/connectivity/check/policy.go#L265-L301","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npolicy := &ciliumv2.CiliumNetworkPolicy{Spec: spec}\nscope, err := check.RegisterPolicy(scope, policy)\n// after\npolicy := &ciliumv2.CiliumNetworkPolicy{\n    ObjectMeta: metav1.ObjectMeta{Name: \"l4-policy\", Namespace: ns},\n    Spec: spec,\n}\nscope, err := check.RegisterPolicy(scope, policy)","handlingStrategy":"validation","validationCode":"func validatePolicies[T interface{ GetName() string }](ps ...T) error {\n    for _, p := range ps {\n        if p.GetName() == \"\" {\n            return fmt.Errorf(\"policy %T has empty metadata.name\", p)\n        }\n    }\n    return nil\n}\n// call before RegisterPolicy\nif err := validatePolicies(policies...); err != nil { return err }","typeGuard":"func hasName[T interface{ GetName() string }](p T) bool {\n    return p.GetName() != \"\"\n}","tryCatchPattern":null,"preventionTips":["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(...) }."],"tags":["cilium-cli","network-policy","validation","test-scope"],"backgroundTag":"missing-required-field","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}