From b6afc0971659f61c56865aed4836701ea9e0050e Mon Sep 17 00:00:00 2001 From: Chafik Date: Fri, 29 Mar 2024 11:42:46 +0100 Subject: [PATCH] fix(http-server-go): adjust json file service implementation (#13) --- data/servers.example.json | 2 +- impl/http-server-go/data.go | 2 +- impl/http-server-go/go.mod | 8 +++- impl/http-server-go/go.sum | 9 ++++ impl/http-server-go/service_file_json.go | 14 +++--- impl/http-server-go/service_file_json_test.go | 31 +++++++++++++ impl/http-server-go/service_self_test.go | 45 +++++-------------- impl/http-server-go/vendor/modules.txt | 12 +++++ 8 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 impl/http-server-go/service_file_json_test.go diff --git a/data/servers.example.json b/data/servers.example.json index c4171c6..2e36967 100644 --- a/data/servers.example.json +++ b/data/servers.example.json @@ -4,7 +4,7 @@ "fqdn": "server01.mycompany.com", "id": "123", "ipv4": "192.168.0.26", - "metric": [ + "metrics": [ { "label": "CPU", "ratio": null, diff --git a/impl/http-server-go/data.go b/impl/http-server-go/data.go index aa01cd9..eecd2c4 100644 --- a/impl/http-server-go/data.go +++ b/impl/http-server-go/data.go @@ -1,4 +1,4 @@ package main const Err401 = "Authentication required" -const Err404Runnable = "Runnable Not found" +const Err404Runnable = "Runnable not found" diff --git a/impl/http-server-go/go.mod b/impl/http-server-go/go.mod index a7f67df..6adce7c 100755 --- a/impl/http-server-go/go.mod +++ b/impl/http-server-go/go.mod @@ -12,4 +12,10 @@ require ( github.com/mackerelio/go-osstat v0.2.4 // direct ) -require golang.org/x/sys v0.18.0 // indirect +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.9.0 // indirect + golang.org/x/sys v0.18.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/impl/http-server-go/go.sum b/impl/http-server-go/go.sum index 07776c6..d5300dd 100755 --- a/impl/http-server-go/go.sum +++ b/impl/http-server-go/go.sum @@ -1,3 +1,5 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= @@ -6,5 +8,12 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/mackerelio/go-osstat v0.2.4 h1:qxGbdPkFo65PXOb/F/nhDKpF2nGmGaCFDLXoZjJTtUs= github.com/mackerelio/go-osstat v0.2.4/go.mod h1:Zy+qzGdZs3A9cuIqmgbJvwbmLQH9dJvtio5ZjJTbdlQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/impl/http-server-go/service_file_json.go b/impl/http-server-go/service_file_json.go index 28b49fa..bd71e8f 100644 --- a/impl/http-server-go/service_file_json.go +++ b/impl/http-server-go/service_file_json.go @@ -17,7 +17,7 @@ type ServiceFileJson struct { func (service ServiceFileJson) list(params *openapi.ListRunnablesQueryParams) (*openapi.ListResRunnable, *ServiceError) { config := service.config - items, err := findItems(config) + items, err := findItems(config.serviceFileJsonFilePath) if err != nil { return nil, err } @@ -33,7 +33,7 @@ func (service ServiceFileJson) reboot(id string) (*openapi.RunnableOperationRes, config := service.config logger := service.logger - item, err := findItem(config, id) + item, err := findItem(config.serviceFileJsonFilePath, id) if err != nil { return nil, err } @@ -48,7 +48,7 @@ func (service ServiceFileJson) stop(id string) (*openapi.RunnableOperationRes, * config := service.config logger := service.logger - item, err := findItem(config, id) + item, err := findItem(config.serviceFileJsonFilePath, id) if err != nil { return nil, err } @@ -59,8 +59,8 @@ func (service ServiceFileJson) stop(id string) (*openapi.RunnableOperationRes, * return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil } -func findItems(config *Config) ([]openapi.Runnable, *ServiceError) { - file, err := os.Open(*config.serviceFileJsonFilePath) +func findItems(filePath *string) ([]openapi.Runnable, *ServiceError) { + file, err := os.Open(*filePath) if err != nil { return nil, &ServiceError{HttpStatus: 500, Message: err.Error()} } @@ -81,8 +81,8 @@ func findItems(config *Config) ([]openapi.Runnable, *ServiceError) { return items, nil } -func findItem(config *Config, id string) (*openapi.Runnable, *ServiceError) { - items, err := findItems(config) +func findItem(filePath *string, id string) (*openapi.Runnable, *ServiceError) { + items, err := findItems(filePath) if err != nil { return nil, err } diff --git a/impl/http-server-go/service_file_json_test.go b/impl/http-server-go/service_file_json_test.go new file mode 100644 index 0000000..ebfc88f --- /dev/null +++ b/impl/http-server-go/service_file_json_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "openapi" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFindItems(t *testing.T) { + // Given + filePath := "../../data/servers.example.json" + + // When + items, err := findItems(&filePath) + + // Then + assert.Nil(t, err) + assert.Len(t, items, 2) + item := items[0] + assert.Equal(t, "medium", *item.Flavor.Get()) + assert.Equal(t, "server01.mycompany.com", *item.Fqdn.Get()) + assert.Equal(t, "123", item.Id) + assert.Len(t, item.Metrics, 2) + assert.Equal(t, "server01", item.Name) + assert.Equal(t, "Paris 01", item.Scopes.Geo.Get().Label) + assert.Equal(t, "Project 1", item.Scopes.Logical.Get().Label) + assert.Equal(t, int32(22), item.Ssh.Get().Port) + assert.Equal(t, "nodejs", *item.Stack.Get()) + assert.Equal(t, openapi.OFF, item.Status) +} diff --git a/impl/http-server-go/service_self_test.go b/impl/http-server-go/service_self_test.go index e977a17..a5d0568 100644 --- a/impl/http-server-go/service_self_test.go +++ b/impl/http-server-go/service_self_test.go @@ -3,6 +3,8 @@ package main import ( "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestBuildCPUMetric(t *testing.T) { @@ -15,10 +17,7 @@ func TestBuildCPUMetric(t *testing.T) { value := *metric.Value.Get() // Then - expectedValue := 1.92 - if value != expectedValue { - t.Fatalf("Expected value to be %f, actual %f", expectedValue, value) - } + assert.Equal(t, 1.92, value) } func TestBuildMemoryMetric(t *testing.T) { @@ -33,21 +32,9 @@ func TestBuildMemoryMetric(t *testing.T) { value := *metric.Value.Get() // Then - expectedRatio := 0.83 - if ratio != expectedRatio { - t.Fatalf("Expected ratio to be %f, actual %f", expectedRatio, ratio) - } - expectedThresholds := []float64{1288.24, 1460.01} - if thresholds[0] != expectedThresholds[0] { - t.Fatalf("Expected thresholds[0] to be %f, actual %f", expectedThresholds[0], thresholds[0]) - } - if thresholds[1] != expectedThresholds[1] { - t.Fatalf("Expected thresholds[1] to be %f, actual %f", expectedThresholds[1], thresholds[1]) - } - expectedValue := 1433.33 - if value != expectedValue { - t.Fatalf("Expected value to be %f, actual %f", expectedValue, value) - } + assert.Equal(t, 0.83, ratio) + assert.EqualValues(t, []float64{1288.24, 1460.01}, thresholds) + assert.Equal(t, 1433.33, value) } func TestBuildUptimeMetric(t *testing.T) { @@ -60,14 +47,8 @@ func TestBuildUptimeMetric(t *testing.T) { unit := *metric.Unit.Get() // Then - expectedValue := 34.0 - if value != expectedValue { - t.Fatalf("Expected value to be %f, actual %f", expectedValue, value) - } - expectedUnit := "min" - if unit != expectedUnit { - t.Fatalf("Expected unit to be %s, actual %s", expectedUnit, unit) - } + assert.Equal(t, 34.0, value) + assert.Equal(t, "min", unit) // Given uptime, _ = time.ParseDuration("2h34m") @@ -78,12 +59,6 @@ func TestBuildUptimeMetric(t *testing.T) { unit = *metric.Unit.Get() // Then - expectedValue = 3.00 - if value != expectedValue { - t.Fatalf("Expected value to be %f, actual %f", expectedValue, value) - } - expectedUnit = "h" - if unit != expectedUnit { - t.Fatalf("Expected unit to be %s, actual %s", expectedUnit, unit) - } + assert.Equal(t, 3.00, value) + assert.Equal(t, "h", unit) } diff --git a/impl/http-server-go/vendor/modules.txt b/impl/http-server-go/vendor/modules.txt index e8a4b53..d0275f2 100644 --- a/impl/http-server-go/vendor/modules.txt +++ b/impl/http-server-go/vendor/modules.txt @@ -1,3 +1,6 @@ +# github.com/davecgh/go-spew v1.1.1 +## explicit +github.com/davecgh/go-spew/spew # github.com/felixge/httpsnoop v1.0.4 ## explicit; go 1.13 github.com/felixge/httpsnoop @@ -12,6 +15,15 @@ github.com/gorilla/mux github.com/mackerelio/go-osstat/cpu github.com/mackerelio/go-osstat/memory github.com/mackerelio/go-osstat/uptime +# github.com/pmezard/go-difflib v1.0.0 +## explicit +github.com/pmezard/go-difflib/difflib +# github.com/stretchr/testify v1.9.0 +## explicit; go 1.17 +github.com/stretchr/testify/assert # golang.org/x/sys v0.18.0 ## explicit; go 1.18 golang.org/x/sys/unix +# gopkg.in/yaml.v3 v3.0.1 +## explicit +gopkg.in/yaml.v3