Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Linters, Spellcheck, and Tests

on:
push:
paths:
- '**.go'
workflow_dispatch:

jobs:
LintnTest:
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install dependencies
run: make installpkgs
- name: Run linters
run: make lint
- name: Run tests
run: make test

Spellcheck:
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@v1.29.7
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.dll
*.so
*.dylib
*.html
*.json

# Test binary, built with `go test -c`
*.test
Expand Down
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Run tests and log the test coverage
test:
go test -v -race -coverprofile=".cover.out" $$(go list ./... | grep -v /tmp)

# Runs source code linters and catches common errors
lint:
test -z $$(gofmt -l .) || (echo "Code isn't gofmt'ed!" && exit 1)
go vet $$(go list ./... | grep -v /tmp)
gosec -quiet -fmt=golint -exclude-dir="tmp" ./...
staticcheck ./...
govulncheck -test ./...
# pointerinterface ./...

# Runs spellchecker on the code and comments
# This requires this tool to be installed from https://github.com/crate-ci/typos?tab=readme-ov-file
# Example installation (if you have rust installed): cargo install typos-cli
spellcheck:
typos .

# All in one check
runchecks: test lint spellcheck

# Generate pretty coverage report
analyse:
go tool cover -html=".cover.out" -o="cover.html"
@echo -e "\nCOVERAGE\n===================="
go tool cover -func=.cover.out
@echo -e "\nCYCLOMATIC COMPLEXITY\n===================="
gocyclo -avg -top 10 .

# Updates 3rd party packages and tools
installpkgs:
go mod download
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
go install golang.org/x/vuln/cmd/govulncheck@latest
# go install code.larus.se/lmas/pointerinterface@latest

# Clean up built binary and other temporary files (ignores errors from rm)
clean:
go clean
rm .cover.out cover.html
1 change: 0 additions & 1 deletion components/device.go → components/host.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func NewDevice() *HostingDevice {
func Hostname() (string, error) {
name, err := os.Hostname()
if err != nil {
log.Println(err.Error())
return "", err
}
return name, nil
Expand Down
41 changes: 41 additions & 0 deletions components/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package components

import (
"testing"
)

func TestHostname(t *testing.T) {
res, err := Hostname()

if res == "" || err != nil {
t.Errorf("Expected a host name and no error, got: %s and %v", res, err)
}
}

func TestIpAddresses(t *testing.T) {
res, err := IpAddresses()

if len(res) == 0 || err != nil {
t.Errorf("Expected IP addresses and no error, got: %s and %v", res, err)
}
}

func TestMacAddresses(t *testing.T) {
ip, err := IpAddresses()
if err != nil {
t.Fatalf("An error occurred in getting IP Addresses for the Mac Address test")
}
res, err := MacAddresses(ip)

if len(res) == 0 || err != nil {
t.Errorf("Expected no error, got: %s and %v", res, err)
}
}

func TestNewDevice(t *testing.T) {
res := NewDevice()

if res == nil {
t.Errorf("Expected a new device, got: %v", res)
}
}
4 changes: 2 additions & 2 deletions components/husk.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Synecdoque
* Copyright (c) 2025 Synecdoque
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,7 +35,7 @@ type Husk struct {
Certificate string `json:"-"`
CA_cert string `json:"-"`
TlsConfig *tls.Config `json:"-"` // client side mutual TLS configuration
DName pkix.Name `json:"distinguishedName"`
DName pkix.Name `json:"-"`
Details map[string][]string `json:"details"`
ProtoPort map[string]int `json:"protoPort"`
InfoLink string `json:"onlineDocumentation"`
Expand Down
38 changes: 38 additions & 0 deletions components/husk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package components

import (
"testing"
)

type sProtocolsTestStruct struct {
input map[string]int
expectedOutput []string
}

var sProtocolsTestParams = []sProtocolsTestStruct{
{makeEmptyProtoPortMap(), nil},
{makeProtoPortMapWithPortZero(), nil},
{makeFullProtoPortMap(), []string{"Port1", "Port2"}},
}

func makeEmptyProtoPortMap() map[string]int {
return make(map[string]int)
}

func makeProtoPortMapWithPortZero() map[string]int {
return map[string]int{"Port": 0}
}

func makeFullProtoPortMap() map[string]int {
return map[string]int{"Port1": 123, "Port2": 404, "Port3": 0}
}

func TestSProtocols(t *testing.T) {
for _, testCase := range sProtocolsTestParams {
res := SProtocols(testCase.input)

if len(res) != len(testCase.expectedOutput) {
t.Errorf("Expected %v, got: %v", testCase.expectedOutput, res)
}
}
}
13 changes: 7 additions & 6 deletions components/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package components
type Service struct {
ID int `json:"-"` // Id assigned by the Service Registrar
Definition string `json:"definition"` // Service definition or purpose
SubPath string `json:"-"` // The URL subpath after the resource's
SubPath string `json:"subpath"` // The URL subpath after the resource's
Details map[string][]string `json:"details"` // Metadata or details about the service
RegPeriod int `json:"registrationPeriod"` // The period until the registrar is expecting a sign of life
RegTimestamp string `json:"-"` // the creation date in the Service Registry to ensure that reRegistration is with the same record
Expand All @@ -37,7 +37,7 @@ type Service struct {
CUnit string `json:"costUnit"` // cost unit
}

// type Services is a collection of service stucts
// type Services is a collection of service structs
type Services map[string]*Service

// Merge method is used in the configuration use case to prevent the subpath or description to be changed or "configured"
Expand Down Expand Up @@ -113,10 +113,11 @@ func MergeDetails(map1, map2 map[string][]string) map[string][]string {

// A Cervice is a consumed service
type Cervice struct {
Definition string
Details map[string][]string
Nodes map[string][]string
Protos []string
IReferentce string // Internal reference when consuming more than one service of the same type
Definition string // Service definition or purpose
Details map[string][]string
Nodes map[string][]string
Protos []string
}

// Cervises is a collection of "Cervice" structs
Expand Down
Loading
Loading