Skip to content

Commit

Permalink
Fixes and adds tests (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: viktor.kramarenko <[email protected]>
  • Loading branch information
ViktorKram authored Jan 11, 2024
1 parent 323a512 commit 457a461
Show file tree
Hide file tree
Showing 14 changed files with 1,610 additions and 638 deletions.
16 changes: 10 additions & 6 deletions images/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func NewConfig() (*Options, error) {

machId, err := getMachineId()
if err != nil {
return nil, fmt.Errorf("[NewConfig] required %s env variable is not specified, error: %w",
MachineID, err)
return nil, fmt.Errorf("[NewConfig] unable to get %s, error: %w", MachineID, err)
}
opts.MachineId = machId

Expand All @@ -74,10 +73,15 @@ func NewConfig() (*Options, error) {
}

func getMachineId() (string, error) {
id, err := os.ReadFile("/host-root/etc/machine-id")
if err != nil {
return "", err
id := os.Getenv(MachineID)
if id == "" {
byteId, err := os.ReadFile("/host-root/etc/machine-id")
if err != nil {
return "", err
}

id = string(byteId)
}

return string(id), nil
return id, nil
}
58 changes: 5 additions & 53 deletions images/agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

func TestNewConfig(t *testing.T) {
t.Run("AllValuesSet_ReturnsNoError", func(t *testing.T) {
machineIdFile := "./host-root/etc/machine-id"
expNodeName := "test-node"
expMetricsPort := ":0000"
expMachineId := "test-id"
Expand All @@ -38,34 +37,9 @@ func TestNewConfig(t *testing.T) {
if err != nil {
t.Error(err)
}
err = os.Setenv(MachineID, expMachineId)
defer os.Clearenv()

err = os.MkdirAll("./host-root/etc", 0750)
if err != nil {
t.Error(err)
}

file, err := os.Create(machineIdFile)
if err != nil {
t.Error(err)
}
defer func() {
err = file.Close()
if err != nil {
t.Error(err)
}

err = os.RemoveAll("./host-root")
if err != nil {
t.Error(err)
}
}()

_, err = file.Write([]byte(expMachineId))
if err != nil {
t.Error(err)
}

opts, err := NewConfig()

if assert.NoError(t, err) {
Expand Down Expand Up @@ -114,8 +88,8 @@ func TestNewConfig(t *testing.T) {
t.Run("MachineIdNotSet_ReturnsError", func(t *testing.T) {
expMetricsPort := ":0000"
expNodeName := "test-node"
expErrorMsg := fmt.Sprintf("[NewConfig] required %s env variable is not specified, error: %s",
MachineID, "open host-root/etc/machine-id: no such file or directory")
expErrorMsg := fmt.Sprintf("[NewConfig] unable to get %s, error: %s",
MachineID, "open /host-root/etc/machine-id: no such file or directory")

err := os.Setenv(MetricsPort, expMetricsPort)
if err != nil {
Expand All @@ -132,7 +106,6 @@ func TestNewConfig(t *testing.T) {
})

t.Run("MetricsPortNotSet_ReturnsDefaultPort", func(t *testing.T) {
machineIdFile := "./host-root/etc/machine-id"
expNodeName := "test-node"
expMetricsPort := ":8080"
expMachineId := "test-id"
Expand All @@ -141,33 +114,12 @@ func TestNewConfig(t *testing.T) {
if err != nil {
t.Error(err)
}
defer os.Clearenv()

err = os.MkdirAll("./host-root/etc", 0750)
err = os.Setenv(MachineID, expMachineId)
if err != nil {
t.Error(err)
}

file, err := os.Create(machineIdFile)
if err != nil {
t.Error(err)
}
defer func() {
err = file.Close()
if err != nil {
t.Error(err)
}

err = os.RemoveAll("./host-root")
if err != nil {
t.Error(err)
}
}()

_, err = file.Write([]byte(expMachineId))
if err != nil {
t.Error(err)
}
defer os.Clearenv()

opts, err := NewConfig()

Expand Down
7 changes: 2 additions & 5 deletions images/agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module sds-node-configurator
go 1.20

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/go-logr/logr v1.3.0
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/prometheus/client_golang v1.17.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.4
k8s.io/apiextensions-apiserver v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
k8s.io/klog/v2 v2.110.1
k8s.io/utils v0.0.0-20231127182322-b307cd553661
sigs.k8s.io/controller-runtime v0.16.3
)

Expand All @@ -31,7 +32,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -41,14 +41,12 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand All @@ -69,7 +67,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.28.4 // indirect
k8s.io/kube-openapi v0.0.0-20231129212854-f0671cc7e66a // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
Loading

0 comments on commit 457a461

Please sign in to comment.