{"record":{"id":"5a53d0b8d27fe6bc","repo":"cilium/cilium","slug":"t-with-name-s-already-in-test-scope","errorCode":null,"errorMessage":"%T with name %s already in test scope","messagePattern":"%T with name (.+?) already in test scope","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cilium-cli/connectivity/check/policy.go","lineNumber":286,"sourceCode":"\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}\n\n// policyApplyDeleteLock guarantees that only one connectivity test instance\n// can apply or delete policies in case of connectivity test concurrency > 1","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/cilium-cli/connectivity/check/policy.go#L268-L304","documentation":"RegisterPolicy maintains a map of policy objects keyed by name for the current connectivity test scope. This error is thrown when a policy whose name is already present in the map is registered again. Duplicate registration is rejected because it would silently overwrite a previously registered policy and break later lookups by name.","triggerScenarios":"Calling RegisterPolicy twice (or with variadic args) passing two different policy objects that share the same metadata.name within the same test scope, e.g. re-adding an updated version of a policy instead of mutating the existing one.","commonSituations":"A test step re-registers a policy after modifying it instead of using the create-or-update path; two policies in different files were given the same name; a shared helper is invoked in a loop with a constant policy name.","solutions":["Use unique names per policy in a test scope (prefix names with the test/step name).","If the policy content changed, mutate the existing registered object rather than registering a new one with the same name.","Check the map before registering: `if _, ok := scope[name]; ok { ... }` to detect collisions at the call site.","When re-running within a fresh scope, ensure the scope map is reset so stale keys do not collide."],"exampleFix":"// before (second registration collides)\nscope, _ := check.RegisterPolicy(scope, p1)\nscope, _ := check.RegisterPolicy(scope, p2) // p2 has same name as p1\n// after\np2.Name = p2.Name + \"-v2\"\nscope, _ := check.RegisterPolicy(scope, p2)","handlingStrategy":"validation","validationCode":"func assertUniqueNames[T interface{ GetName() string }](ps ...T) error {\n    seen := map[string]bool{}\n    for _, p := range ps {\n        n := p.GetName()\n        if seen[n] { return fmt.Errorf(\"duplicate policy name %q\", n) }\n        seen[n] = true\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"scope, err := check.RegisterPolicy(scope, policies...)\nif err != nil {\n    if strings.Contains(err.Error(), \"already in test scope\") {\n        // deduplicate or rename policies before registering\n    }\n    return err\n}","preventionTips":["Give each policy a name unique within the test scope (prefix with test/step name).","Mutate an existing registered policy instead of re-registering an object with the same name.","Keep a local map of used names in test helpers to catch collisions early."],"tags":["cilium-cli","network-policy","duplicate-key","test-scope"],"backgroundTag":"duplicate-key-registration","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}