Skip to content

Commit

Permalink
Switch to Go modules (#14)
Browse files Browse the repository at this point in the history
* Switch to Go modules

* Switch to Go modules and remove vendoring
* Use different fork of go-powershell: replace the current go-powershell
  dependency with https://github.com/antoninbas/go-powershell. This fork
  converts the project to Go modules and removes the dependency on juju
  packages (which are distributed under an LGPL v3 license, making usage
  in commercial products difficult)
* Enable CI: run unit tests using Github workflows

Fixes #13

* Fix unit tests given k8s.io/util version update
  • Loading branch information
antoninbas authored Feb 26, 2021
1 parent 39cfb50 commit dfffed1
Show file tree
Hide file tree
Showing 25 changed files with 96 additions and 1,032 deletions.
31 changes: 20 additions & 11 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ on:
branches: [ master ]

jobs:

build:
test:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
- name: Set up Go 1.15
uses: actions/setup-go@v1
with:
go-version: 1.15

- name: Get
run: go get ./...
- name: Check-out code
uses: actions/checkout@v2
- name: Run unit tests
run: |
.\test.ps1
- name: Build
run: go build -v ./...
tidy:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
with:
go-version: 1.15
- name: Check-out code
uses: actions/checkout@v2
- name: Check tidiness
run: |
./ci/check-tidy.sh
1 change: 0 additions & 1 deletion build.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
go get ./...
go build -v ./...
22 changes: 22 additions & 0 deletions ci/check-tidy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -e

: "${GO:=go}"

goversion=$($GO version)

$GO mod tidy

rc=0
diff=$(git diff --exit-code -- go.mod go.sum) || rc=$?

if [ $rc -ne 0 ]; then
echo "Found some differences when running 'go mod tidy'"
echo "**********"
echo "$diff"
echo "**********"
echo "Please ensure you are using the correct version of go ($goversion), run 'go mod tidy', and commit the changes"
fi

exit $rc
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/rakelkar/gonetsh

go 1.15

require (
github.com/antoninbas/go-powershell v0.1.0
github.com/stretchr/testify v1.6.1
k8s.io/utils v0.0.0-20200410111917-5770800c2500
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/antoninbas/go-powershell v0.1.0 h1:LKwuZJt3loyr++Y2Qc+FZoUt8TwbrTWB3HublPoykxA=
github.com/antoninbas/go-powershell v0.1.0/go.mod h1:01pgKhz1CJxGnCWqXVDgvmp/QmHgWgEdxdYP+1azopE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/utils v0.0.0-20200410111917-5770800c2500 h1:iwoQwtHT3aQtbNKwKlQ2HdCTxv3thi/Tog6lLqIpdO8=
k8s.io/utils v0.0.0-20200410111917-5770800c2500/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
4 changes: 2 additions & 2 deletions netroute/netroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strconv"
"strings"

ps "github.com/benmoss/go-powershell"
psbe "github.com/benmoss/go-powershell/backend"
ps "github.com/antoninbas/go-powershell"
psbe "github.com/antoninbas/go-powershell/backend"

"fmt"
"math/big"
Expand Down
45 changes: 23 additions & 22 deletions netsh/netsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import (
"testing"

"github.com/stretchr/testify/assert"
utilexec "k8s.io/utils/exec"
"k8s.io/utils/exec"
fakeexec "k8s.io/utils/exec/testing"
)

func getFakeExecTemplate(fakeCmd *utilexec.FakeCmd) utilexec.FakeExec {
var fakeTemplate []utilexec.FakeCommandAction
for i := 0; i < len((*fakeCmd).CombinedOutputScript); i++ {
fakeTemplate = append(fakeTemplate, func(cmd string, args ...string) utilexec.Cmd { return utilexec.InitFakeCmd(fakeCmd, cmd, args...) })
func getFakeExecTemplate(fakeCmd *fakeexec.FakeCmd) fakeexec.FakeExec {
var fakeTemplate []fakeexec.FakeCommandAction
for i := 0; i < len(fakeCmd.CombinedOutputScript); i++ {
fakeTemplate = append(fakeTemplate, func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(fakeCmd, cmd, args...) })
}
return utilexec.FakeExec{
return fakeexec.FakeExec{
CommandScript: fakeTemplate,
}
}

func TestGetInterfacesGoldenPath(t *testing.T) {
fakeCmd := utilexec.FakeCmd{
CombinedOutputScript: []utilexec.FakeCombinedOutputAction{
func() ([]byte, error) {
fakeCmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{
func() ([]byte, []byte, error) {
return []byte(`
Configuration for interface "Ethernet"
Expand All @@ -46,16 +47,16 @@ Configuration for interface "Loopback Pseudo-Interface 1"
Subnet Prefix: 127.0.0.0/8 (mask 255.0.0.0)
InterfaceMetric: 75
`), nil
`), nil, nil
},
func() ([]byte, error) {
func() ([]byte, []byte, error) {
return []byte(`
Idx Met MTU State Name
--- ---------- ---------- ------------ ---------------------------
9 25 1500 connected Ethernet
1 75 4294967295 connected Loopback Pseudo-Interface 1
2 15 1500 connected Local Area Connection* 1
14 15 1500 connected Wi-Fi`), nil
14 15 1500 connected Wi-Fi`), nil, nil
},
},
}
Expand Down Expand Up @@ -85,14 +86,14 @@ Configuration for interface "Loopback Pseudo-Interface 1"

func TestGetInterfacesFailsGracefully(t *testing.T) {

fakeCmd := utilexec.FakeCmd{
CombinedOutputScript: []utilexec.FakeCombinedOutputAction{
fakeCmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{
// Failure.
func() ([]byte, error) { return nil, &utilexec.FakeExitError{Status: 2} },
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 2} },
// Empty Response.
func() ([]byte, error) { return []byte{}, nil },
func() ([]byte, []byte, error) { return []byte{}, nil, nil },
// Junk Response.
func() ([]byte, error) { return []byte("fake error from netsh"), nil },
func() ([]byte, []byte, error) { return []byte("fake error from netsh"), nil, nil },
},
}

Expand All @@ -119,17 +120,17 @@ func TestGetInterfacesFailsGracefully(t *testing.T) {
}

func TestGetInterfaceNameToIndexMap(t *testing.T) {
fake := utilexec.FakeCmd{
CombinedOutputScript: []utilexec.FakeCombinedOutputAction{
func() ([]byte, error) { return []byte(`badinput`), nil },
func() ([]byte, error) {
fake := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{
func() ([]byte, []byte, error) { return []byte(`badinput`), nil, nil },
func() ([]byte, []byte, error) {
return []byte(`
Idx Met MTU State Name
--- ---------- ---------- ------------ ---------------------------
9 25 1500 connected Ethernet
1 75 4294967295 connected Loopback Pseudo-Interface 1
2 15 1500 connected vEthernet (New Virtual Switch)
14 15 1500 connected vEthernet (HNS Internal NIC)`), nil
14 15 1500 connected vEthernet (HNS Internal NIC)`), nil, nil
},
},
}
Expand Down
1 change: 0 additions & 1 deletion test.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
go get -t ./...
go test -v ./...
21 changes: 0 additions & 21 deletions vendor/github.com/benmoss/go-powershell/LICENSE

This file was deleted.

110 changes: 0 additions & 110 deletions vendor/github.com/benmoss/go-powershell/README.md

This file was deleted.

38 changes: 0 additions & 38 deletions vendor/github.com/benmoss/go-powershell/backend/local.go

This file was deleted.

Loading

0 comments on commit dfffed1

Please sign in to comment.