diff --git a/docs/release-notes.d/slices-contains.change.md b/docs/release-notes.d/slices-contains.change.md new file mode 100644 index 000000000..879aa19ae --- /dev/null +++ b/docs/release-notes.d/slices-contains.change.md @@ -0,0 +1 @@ +Replace manual slice membership checks with `slices.Contains` diff --git a/internal/exec/stages/disks/luks.go b/internal/exec/stages/disks/luks.go index 72fde098b..037f5da8b 100644 --- a/internal/exec/stages/disks/luks.go +++ b/internal/exec/stages/disks/luks.go @@ -26,6 +26,7 @@ import ( "path" "path/filepath" "regexp" + "slices" "strconv" "strings" @@ -572,12 +573,7 @@ func newLuksDump(devAlias string) (LuksDump, error) { } func (d LuksDump) hasFlag(flag string) bool { - for _, v := range d.Config.Flags { - if v == flag { - return true - } - } - return false + return slices.Contains(d.Config.Flags, flag) } // collect the cex card domains xx.xxxx diff --git a/internal/resource/http.go b/internal/resource/http.go index da1541fd0..231ddc521 100644 --- a/internal/resource/http.go +++ b/internal/resource/http.go @@ -26,6 +26,7 @@ import ( "net/http" "net/url" "os" + "slices" "strings" "time" @@ -306,13 +307,7 @@ func shouldRetryHttp(statusCode int, opts FetchOptions) bool { return true } - for _, retryCode := range opts.RetryCodes { - if statusCode == retryCode { - return true - } - } - - return false + return slices.Contains(opts.RetryCodes, statusCode) } // httpReaderWithHeader performs an HTTP request on the provided URL with the diff --git a/internal/resource/http_test.go b/internal/resource/http_test.go new file mode 100644 index 000000000..e301ca1fb --- /dev/null +++ b/internal/resource/http_test.go @@ -0,0 +1,44 @@ +// Copyright 2026 Red Hat, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package resource + +import "testing" + +func TestShouldRetryHttp(t *testing.T) { + tests := []struct { + name string + statusCode int + retryCodes []int + out bool + }{ + {"nil", 429, nil, false}, + {"empty", 429, []int{}, false}, + {"one", 429, []int{429}, true}, + {"first", 408, []int{408, 429}, true}, + {"last", 429, []int{408, 429}, true}, + {"missing", 404, []int{408, 429}, false}, + {"below server error", 499, nil, false}, + {"server error", 500, nil, true}, + {"higher server error", 503, []int{}, true}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + opts := FetchOptions{RetryCodes: test.retryCodes} + if got := shouldRetryHttp(test.statusCode, opts); got != test.out { + t.Errorf("shouldRetryHttp(%d, %+v) = %t, want %t", test.statusCode, opts, got, test.out) + } + }) + } +} diff --git a/internal/util/str.go b/internal/util/str.go index dceae5efe..cf283866e 100644 --- a/internal/util/str.go +++ b/internal/util/str.go @@ -14,11 +14,8 @@ package util +import "slices" + func StrSliceContains(slice []string, s string) bool { - for _, e := range slice { - if e == s { - return true - } - } - return false + return slices.Contains(slice, s) }