Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add linter #364

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down Expand Up @@ -42,14 +41,21 @@ jobs:
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
- name: Run go vet
run: go vet ./...
- name: Run go lint
if: ${{ matrix.os == 'ubuntu-20.04' }}
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0
with:
version: latest
- name: Install Test Converter and run tests
if: ${{ matrix.os == 'ubuntu-20.04' }}
run: |
export GOPATH="$HOME/go/"
export PATH=$PATH:$GOPATH/bin
go install github.com/jstemmer/go-junit-report/v2@latest
go test -v 2>&1 ./... | go-junit-report -set-exit-code > rpc-go-unit.xml

go test -covermode=atomic -coverprofile=coverage.out -race -v ./... > test_output.txt 2>&1 || true
cat test_output.txt
cat test_output.txt | go-junit-report -set-exit-code > junit.xml
if grep -q "FAIL" test_output.txt; then exit 1; fi
- name: run the tests with coverage
run: go test ./... -coverprofile=coverage.out -covermode=atomic

Expand Down
7 changes: 5 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@
"program": "${workspaceFolder}/cmd/main.go",
"args": [
"configure",
"enablewifiport",
"-h"
"mebx",
"-mebxpassword",
"P@ssw0rd",
"-password",
"P@ssw0rd"
],
"console": "integratedTerminal"
},
Expand Down
24 changes: 17 additions & 7 deletions cmd/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (

//export rpcCheckAccess
func rpcCheckAccess() int {
rc, err := checkAccess()
err := checkAccess()
if err != nil {
log.Error(err.Error())
return handleError(err)
}
return int(rc)
return int(utils.Success)
}

//export rpcExec
Expand All @@ -41,12 +41,22 @@ func rpcExec(Input *C.char, Output **C.char) int {
args, err := r.Read()
if err != nil {
log.Error(err.Error())
return int(utils.InvalidParameterCombination)
return utils.InvalidParameterCombination.Code
}
args = append([]string{"rpc"}, args...)
rc := runRPC(args)
if rc != utils.Success {
err = runRPC(args)
if err != nil {
*Output = C.CString("rpcExec failed: " + inputString)
}
return int(rc)
return handleError(err)
}

func handleError(err error) int {
if customErr, ok := err.(utils.CustomError); ok {
log.Error(customErr.Error())
return customErr.Code
} else {
log.Error(err.Error())
return utils.GenericFailure.Code
}
}
53 changes: 30 additions & 23 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ const AccessErrMsg = "Failed to execute due to access issues. " +
"the MEI driver is installed, " +
"and the runtime has administrator or root privileges."

func checkAccess() (utils.ReturnCode, error) {
func checkAccess() error {
amtCommand := amt.NewAMTCommand()
rc, err := amtCommand.Initialize()
if rc != utils.Success || err != nil {
return utils.AmtNotDetected, err
err := amtCommand.Initialize()
if err != nil {
return err
}
return utils.Success, nil
return nil
}

func runRPC(args []string) utils.ReturnCode {
flags, rc := parseCommandLine(args)
if rc != utils.Success {
return rc
func runRPC(args []string) error {
flags, err := parseCommandLine(args)
if err != nil {
return err
}
if flags.Local {
rc = local.ExecuteCommand(flags)
err = local.ExecuteCommand(flags)
} else {
rc = rps.ExecuteCommand(flags)
err = rps.ExecuteCommand(flags)
}
return rc
return err
}

func parseCommandLine(args []string) (*flags.Flags, utils.ReturnCode) {
func parseCommandLine(args []string) (*flags.Flags, error) {
//process flags
flags := flags.NewFlags(args)
rc := flags.ParseFlags()
error := flags.ParseFlags()

if flags.Verbose {
log.SetLevel(log.TraceLevel)
Expand All @@ -67,21 +67,28 @@ func parseCommandLine(args []string) (*flags.Flags, utils.ReturnCode) {
FullTimestamp: true,
})
}
return flags, rc
return flags, error
}

func main() {
rc, err := checkAccess()
if rc != utils.Success {
if err != nil {
log.Error(err.Error())
}
err := checkAccess()
if err != nil {
log.Error(AccessErrMsg)
os.Exit(int(rc))
handleErrorAndExit(err)
}
rc = runRPC(os.Args)

err = runRPC(os.Args)
if err != nil {
handleErrorAndExit(err)
}
}

func handleErrorAndExit(err error) {
if customErr, ok := err.(utils.CustomError); ok {
log.Error(customErr.Error())
os.Exit(customErr.Code)
} else {
log.Error(err.Error())
os.Exit(utils.GenericFailure.Code)
}
os.Exit(int(rc))
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ module rpc
go 1.20

// uncomment if developing with go-wsman-messages locally
// replace github.com/open-amt-cloud-toolkit/go-wsman-messages => ../go-wsman-messages
// replace github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 => ../go-wsman-messages

require (
github.com/google/uuid v1.4.0
github.com/gorilla/websocket v1.5.1
github.com/hirochachacha/go-smb2 v1.1.0
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/open-amt-cloud-toolkit/go-wsman-messages v1.14.0
github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.1.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.16.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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/geoffgarside/ber v1.1.0 h1:qTmFG4jJbwiSzSXoNJeHcOprVzZ8Ulde2Rrrifu5U9w=
github.com/geoffgarside/ber v1.1.0/go.mod h1:jVPKeCbj6MvQZhwLYsGwaGI52oUorHoHKNecGT85ZCc=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRKbaRTZI=
Expand All @@ -13,8 +15,8 @@ github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2l
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/open-amt-cloud-toolkit/go-wsman-messages v1.14.0 h1:m8SCNN3JXr0+aTo+gJ/VxbXL34dO+ztVhTQA0lgsp/8=
github.com/open-amt-cloud-toolkit/go-wsman-messages v1.14.0/go.mod h1:+cN55LgpvGZsTNu1mAh2XbhKJkk0AbMdJZOeyB5CzkI=
github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.1.1 h1:p05Atv8KwPD31n2z59TY6knQTUFX/9c6CcTF1CdB558=
github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.1.1/go.mod h1:bwbM46G0ryPI8djtb9wdJH5EW6/g2/RjCaKg7qRsgK0=
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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
13 changes: 8 additions & 5 deletions internal/amt/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r ChangeEnabledResponse) IsNewInterfaceVersion() bool {
}

type Interface interface {
Initialize() (utils.ReturnCode, error)
Initialize() error
GetChangeEnabled() (ChangeEnabledResponse, error)
EnableAMT() error
DisableAMT() error
Expand Down Expand Up @@ -117,20 +117,20 @@ func NewAMTCommand() AMTCommand {
}

// Initialize determines if rpc is able to initialize the heci driver
func (amt AMTCommand) Initialize() (utils.ReturnCode, error) {
func (amt AMTCommand) Initialize() error {
// initialize HECI interface
err := amt.PTHI.Open(false)

if err != nil {
if err.Error() == "The handle is invalid." {
return utils.HECIDriverNotDetected, errors.New("AMT not found: MEI/driver is missing or the call to the HECI driver failed")
return utils.HECIDriverNotDetected //, errors.New("AMT not found: MEI/driver is missing or the call to the HECI driver failed")
} else {
return utils.HECIDriverNotDetected, errors.New("unable to initialize")
return utils.HECIDriverNotDetected //, errors.New("unable to initialize")
}
}

defer amt.PTHI.Close()
return utils.Success, nil
return nil
}

// GetVersionDataFromME ...
Expand Down Expand Up @@ -177,6 +177,9 @@ func (amt AMTCommand) GetChangeEnabled() (ChangeEnabledResponse, error) {
}
defer amt.PTHI.Close()
rawVal, err := amt.PTHI.GetIsAMTEnabled()
if err != nil {
return ChangeEnabledResponse(0), err
}
return ChangeEnabledResponse(rawVal), nil
}

Expand Down
37 changes: 19 additions & 18 deletions internal/amt/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"rpc/pkg/pthi"
"rpc/pkg/utils"
"testing"
"time"

Expand Down Expand Up @@ -153,19 +154,19 @@ func init() {
amt.PTHI = MockPTHICommands{}
}
func TestInitializeNoError(t *testing.T) {
result, err := amt.Initialize()
assert.NoError(t, err, result)
err := amt.Initialize()
assert.NoError(t, err)
}
func TestInitializeMEIError(t *testing.T) {
flag = true
result, err := amt.Initialize()
assert.Error(t, err, result)
err := amt.Initialize()
assert.Error(t, err, utils.HECIDriverNotDetected)
flag = false
}
func TestInitializeError(t *testing.T) {
flag1 = true
result, err := amt.Initialize()
assert.Error(t, err, result)
err := amt.Initialize()
assert.Error(t, err, utils.HECIDriverNotDetected)
flag1 = false
}
func TestGetVersionDataFromME(t *testing.T) {
Expand All @@ -179,19 +180,19 @@ func TestGetVersionDataFromMEError(t *testing.T) {
assert.Equal(t, "", result)
}

func TestGetVersionDataFromMETimeout1sec(t *testing.T) {
returnError = true
result, err := amt.GetVersionDataFromME("", 1*time.Second)
assert.Equal(t, "amt internal error", err.Error())
assert.Equal(t, "", result)
}
// func TestGetVersionDataFromMETimeout1sec(t *testing.T) {
// returnError = true
// result, err := amt.GetVersionDataFromME("", 1*time.Second)
// assert.Equal(t, "amt internal error", err.Error())
// assert.Equal(t, "", result)
// }

func TestGetVersionDataFromMETimeout16sec(t *testing.T) {
returnError = true
result, err := amt.GetVersionDataFromME("", 16*time.Second)
assert.Equal(t, "amt internal error", err.Error())
assert.Equal(t, "", result)
}
// func TestGetVersionDataFromMETimeout16sec(t *testing.T) {
// returnError = true
// result, err := amt.GetVersionDataFromME("", 16*time.Second)
// assert.Equal(t, "amt internal error", err.Error())
// assert.Equal(t, "", result)
// }
func TestGetIsAMTEnabled(t *testing.T) {
result, err := amt.GetChangeEnabled()
assert.NoError(t, err)
Expand Down
14 changes: 6 additions & 8 deletions internal/config/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package config

type (
Config struct {
Password string `yaml:"password"`
WifiConfigs `yaml:"wifiConfigs"`
Ieee8021xConfigs `yaml:"ieee8021xConfigs"`
ACMSettings `yaml:"acmactivate"`
Password string `yaml:"password"`
WifiConfigs []WifiConfig `yaml:"wifiConfigs"`
Ieee8021xConfigs []Ieee8021xConfig `yaml:"ieee8021xConfigs"`
ACMSettings ACMSettings `yaml:"acmactivate"`
}
WifiConfigs []WifiConfig
WifiConfig struct {
WifiConfig struct {
ProfileName string `yaml:"profileName"`
SSID string `yaml:"ssid"`
Priority int `yaml:"priority"`
Expand All @@ -26,8 +25,7 @@ type (
PrivateKey string `yaml:"privateKey"`
Password string `yaml:"password"`
}
Ieee8021xConfigs []Ieee8021xConfig
Ieee8021xConfig struct {
Ieee8021xConfig struct {
ProfileName string `yaml:"profileName"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Expand Down
Loading
Loading