Skip to content

Commit

Permalink
fix(http-server-go): adjust json file service implementation (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
c100k authored Mar 29, 2024
1 parent ea7d4a5 commit b6afc09
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 45 deletions.
2 changes: 1 addition & 1 deletion data/servers.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"fqdn": "server01.mycompany.com",
"id": "123",
"ipv4": "192.168.0.26",
"metric": [
"metrics": [
{
"label": "CPU",
"ratio": null,
Expand Down
2 changes: 1 addition & 1 deletion impl/http-server-go/data.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

const Err401 = "Authentication required"
const Err404Runnable = "Runnable Not found"
const Err404Runnable = "Runnable not found"
8 changes: 7 additions & 1 deletion impl/http-server-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
9 changes: 9 additions & 0 deletions impl/http-server-go/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
14 changes: 7 additions & 7 deletions impl/http-server-go/service_file_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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()}
}
Expand All @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions impl/http-server-go/service_file_json_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
45 changes: 10 additions & 35 deletions impl/http-server-go/service_self_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestBuildCPUMetric(t *testing.T) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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")
Expand All @@ -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)
}
12 changes: 12 additions & 0 deletions impl/http-server-go/vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit b6afc09

Please sign in to comment.