From 7a2c706e24a99f8c1d19b14a4213b31aecbe5678 Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Fri, 30 May 2025 13:10:48 -0700 Subject: [PATCH 1/4] chore: remove unused API test helper Remove an unused test scaffold for the API package. This scaffold contained one function, which registered the aggregate API router at the root path, but did not serve it. It contained nothing specific to PCS. Most of the API lacks tests entirely. One section (health) sets up its own test server independently. Signed-off-by: Travis Raines <571832+rainest@users.noreply.github.com> --- internal/api/api_test_setup_handler.go | 47 -------------------------- 1 file changed, 47 deletions(-) delete mode 100644 internal/api/api_test_setup_handler.go diff --git a/internal/api/api_test_setup_handler.go b/internal/api/api_test_setup_handler.go deleted file mode 100644 index 3b5d5a66..00000000 --- a/internal/api/api_test_setup_handler.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - * (C) Copyright [2021-2023] Hewlett Packard Enterprise Development LP - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -package api - -import ( - "net/http" - "sync" -) - -// mutex - protects the CreateRouterAndHandler -var mutex = &sync.Mutex{} - -// IsHandled - setups protection for the mutex -var IsHandled bool - -// CreateRouterAndHandler - Create a singleton for setting up routing for the UnitTests -func CreateRouterAndHandler() { - mutex.Lock() - if !IsHandled { - IsHandled = true - //This setups the production routs - router := NewRouter() - //this creates the handle - http.Handle("/", router) - } - mutex.Unlock() -} From 67279642dc8dd18ebca229f00593fefc0b46f879 Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Fri, 30 May 2025 13:49:36 -0700 Subject: [PATCH 2/4] chore: remove unused utility functions Remove several unused utility functions from the model package. Move several API-only types from the model package to the api package. Signed-off-by: Travis Raines <571832+rainest@users.noreply.github.com> --- internal/api/api_helpers.go | 12 ++++- internal/model/models.go | 97 ----------------------------------- internal/model/models_test.go | 70 ------------------------- 3 files changed, 10 insertions(+), 169 deletions(-) delete mode 100644 internal/model/models.go delete mode 100644 internal/model/models_test.go diff --git a/internal/api/api_helpers.go b/internal/api/api_helpers.go index b2a09f6e..fef022b8 100644 --- a/internal/api/api_helpers.go +++ b/internal/api/api_helpers.go @@ -34,6 +34,14 @@ import ( "github.com/OpenCHAMI/power-control/v2/internal/model" ) +type IDList struct { + IDs []uuid.UUID `json:"ids"` +} + +type IDResp struct { + ID uuid.UUID `json:"id"` +} + // WriteJSON - writes JSON to the open http connection func WriteJSON(w http.ResponseWriter, i interface{}) { obj, err := json.Marshal(i) @@ -57,9 +65,9 @@ func WriteHeaders(w http.ResponseWriter, pb model.Passback) { w.WriteHeader(pb.StatusCode) switch val := pb.Obj.(type) { case []uuid.UUID: - WriteJSON(w, model.IDList{IDs: val}) + WriteJSON(w, IDList{IDs: val}) case uuid.UUID: - WriteJSON(w, model.IDResp{ID: val}) + WriteJSON(w, IDResp{ID: val}) default: WriteJSON(w, pb.Obj) } diff --git a/internal/model/models.go b/internal/model/models.go deleted file mode 100644 index 9216ee7a..00000000 --- a/internal/model/models.go +++ /dev/null @@ -1,97 +0,0 @@ -/* - * (C) Copyright [2021-2023] Hewlett Packard Enterprise Development LP - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -package model - -import ( - "fmt" - - "github.com/google/uuid" -) - -type IDList struct { - IDs []uuid.UUID `json:"ids"` -} - -type IDResp struct { - ID uuid.UUID `json:"id"` -} - -func UUIDSliceEquals(obj []uuid.UUID, other []uuid.UUID) bool { - if len(obj) != len(other) { - return false - } - objMap := make(map[uuid.UUID]int) - otherMap := make(map[uuid.UUID]int) - - for _, objE := range obj { - objMap[objE]++ - } - for _, otherE := range other { - otherMap[otherE]++ - } - for objKey, objVal := range objMap { - if otherMap[objKey] != objVal { - return false - } - } - return true -} - -func StringSliceEquals(obj []string, other []string) bool { - if len(obj) != len(other) { - return false - } else if len(obj) == 0 && len(other) == 0 { - return true - } else if obj == nil && other == nil { - return true - } - objMap := make(map[string]int) - otherMap := make(map[string]int) - - for _, objE := range obj { - objMap[objE]++ - } - for _, otherE := range other { - otherMap[otherE]++ - } - for objKey, objVal := range objMap { - if otherMap[objKey] != objVal { - return false - } - } - return true -} - -func NewInvalidInputError(Message string, CompIDs []string) (err error) { - err = fmt.Errorf("%s: %v", Message, CompIDs) - return err -} - -func Find(slice []string, val string) (int, bool) { - for i, item := range slice { - if item == val { - return i, true - } - } - return -1, false -} diff --git a/internal/model/models_test.go b/internal/model/models_test.go deleted file mode 100644 index fc9136e1..00000000 --- a/internal/model/models_test.go +++ /dev/null @@ -1,70 +0,0 @@ -//go:build !integration_tests - -/* - * (C) Copyright [2021-2023] Hewlett Packard Enterprise Development LP - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -package model - -import ( - "strings" - "testing" - - "github.com/google/uuid" - "github.com/stretchr/testify/suite" -) - -type ModelsTS struct { - suite.Suite -} - -func (suite *ModelsTS) TestUUIDSliceEquals() { - u1 := []uuid.UUID{uuid.New(), uuid.New(), uuid.New()} - u2 := []uuid.UUID{uuid.New(), uuid.New()} - u3 := []uuid.UUID{uuid.New(), uuid.New()} - suite.True(UUIDSliceEquals(u1, u1)) - suite.True(UUIDSliceEquals(u2, u2)) - suite.False(UUIDSliceEquals(u1, u2)) - suite.False(UUIDSliceEquals(u3, u2)) -} - -func (suite *ModelsTS) TestStringSliceEquals() { - s1 := []string{"a1", "a2", "a3"} - s2 := []string{"a3", "a2", "a1"} - s3 := []string{"a3", "a2"} - s4 := []string{"a3", "a2", "a4"} - suite.True(StringSliceEquals(s1, s2)) - suite.True(StringSliceEquals(s2, s1)) - suite.False(StringSliceEquals(s3, s1)) - suite.False(StringSliceEquals(s3, s4)) - suite.False(StringSliceEquals(s1, s4)) -} - -func (suite *ModelsTS) TestNewInvalidInputError() { - err := NewInvalidInputError("Message", []string{"a1", "a2", "a3"}) - suite.True(strings.Contains(err.Error(), "Message")) - suite.True(strings.Contains(err.Error(), "[a1 a2 a3]")) -} - -func TestModelsSuite(t *testing.T) { - - suite.Run(t, new(ModelsTS)) -} From 960334f7a9f19c245ec89af046f73021b405206a Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Fri, 30 May 2025 15:06:14 -0700 Subject: [PATCH 3/4] chore: remove empty file Signed-off-by: Travis Raines <571832+rainest@users.noreply.github.com> --- internal/storage/power_status.go | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 internal/storage/power_status.go diff --git a/internal/storage/power_status.go b/internal/storage/power_status.go deleted file mode 100644 index 1d9b03e4..00000000 --- a/internal/storage/power_status.go +++ /dev/null @@ -1,23 +0,0 @@ -// MIT License -// -// (C) Copyright [2022-2023] Hewlett Packard Enterprise Development LP -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -package storage From ec1874c6feeb64eeb1eed638f91761960b3d2668 Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Fri, 30 May 2025 15:06:18 -0700 Subject: [PATCH 4/4] chore: remove InitFromStorage() Remove the InitFromStorage() function from the DistributedLockProvider interface. This method was only used in tests, and was redundant. Init() can handle all cases where InitFromStorage() would be used. Signed-off-by: Travis Raines <571832+rainest@users.noreply.github.com> --- internal/storage/transition_size_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/storage/transition_size_test.go b/internal/storage/transition_size_test.go index 2b841d30..2e16f895 100644 --- a/internal/storage/transition_size_test.go +++ b/internal/storage/transition_size_test.go @@ -64,9 +64,9 @@ func createProviders(t *testing.T, settings *EtcdTestSettings) (StorageProvider, t.Errorf("Storage Init() failed: %v", err) } - err = ds.Init(nil) + ds.Init(nil) if err != nil { - t.Errorf("DistributedLockProvider Init() failed: %v", err) + t.Errorf("DistLock Init() failed: %v", err) } if e, ok := ms.(*ETCDStorage); ok {