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
42 changes: 42 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Go

on:
pull_request:
branches: [ "main" ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
pull-requests: read

jobs:
build:
runs-on: tks-gha-runner

steps:
- name: Checkout
uses: actions/checkout@v6

- name: Set up Go
uses: actions/setup-go@v6
with:
go-version-file: 'go.mod'
cache: true

- name: Verify modules
run: |
go mod tidy && go mod verify
if [ -n "$(git status --porcelain go.mod go.sum)" ]; then
echo "go.mod or go.sum are not tidy. Run 'go mod tidy' and commit."
git status
exit 1
fi

- name: Build
run: go build ./...

- name: Test
run: go test -v ./...
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/spectrocloud/maas-client-go
module github.com/cloud104/maas-client-go

go 1.24

require (
github.com/google/uuid v1.6.0
github.com/jarcoal/httpmock v1.4.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.11.1
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ 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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jarcoal/httpmock v1.4.1 h1:0Ju+VCFuARfFlhVXFc2HxlcQkfB+Xq12/EotHko+x2A=
github.com/jarcoal/httpmock v1.4.1/go.mod h1:ftW1xULwo+j0R0JJkJIIi7UKigZUXCLLanykgjwBXL0=
github.com/maxatome/go-testdeep v1.14.0 h1:rRlLv1+kI8eOI3OaBXZwb3O7xY3exRzdW5QyX48g9wI=
github.com/maxatome/go-testdeep v1.14.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
63 changes: 58 additions & 5 deletions maasclient/bootresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,86 @@ package maasclient

import (
"context"
"github.com/stretchr/testify/assert"
"crypto/sha256"
"encoding/hex"
"net/http"
"os"
"testing"

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

func TestGetBootResources(t *testing.T) {
c := NewAuthenticatedClientSet(os.Getenv("MAAS_ENDPOINT"), os.Getenv("MAAS_API_KEY"))
httpClient := &http.Client{}

httpmock.ActivateNonDefault(httpClient)
t.Cleanup(httpmock.DeactivateAndReset)

c := NewAuthenticatedClientSet("http://maas.test", "dummy-api-key", func(client *authenticatedClientSet) { client.WithHTTPClient(httpClient) })

ctx := context.Background()

t.Run("list-all", func(t *testing.T) {
defer httpmock.Reset()

httpmock.RegisterResponder(
http.MethodGet,
"http://maas.test/api/2.0/boot-resources/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/bootresources/list__all.json")),
)

list, err := c.BootResources().List(ctx, nil)
assert.Nil(t, err, "expecting nil error")
assert.NotEmpty(t, list)
})
//

t.Run("list-by-id", func(t *testing.T) {
defer httpmock.Reset()

httpmock.RegisterResponder(
http.MethodGet,
"http://maas.test/api/2.0/boot-resources/7/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/bootresources/get__id-7.json")),
)

res, err := c.BootResources().BootResource(7).Get(ctx)
assert.Nil(t, err)
assert.NotNil(t, res)
})

t.Run("import image", func(t *testing.T) {
tmp, err := os.CreateTemp("", "maas-bootresource-*.tgz")
assert.NoError(t, err)

defer func() { _ = os.Remove(tmp.Name()) }()

content := []byte("dummy boot resource payload\n")
_, err = tmp.Write(content)
assert.NoError(t, err)
assert.NoError(t, tmp.Close())

size := len(content)
sum := sha256.Sum256(content)
sha := hex.EncodeToString(sum[:])

defer httpmock.Reset()

httpmock.RegisterResponder(
http.MethodPost,
"http://maas.test/api/2.0/boot-resources/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/bootresources/import_response__id-99.json")),
)
httpmock.RegisterResponder(
http.MethodPut,
"http://maas.test/api/2.0/boot-resources/99/upload/",
httpmock.NewBytesResponder(200, nil),
)

res, err := c.BootResources().Builder("test-image",
"amd64/generic",
"e9844638c7345d182c5d88e1eaeae74749d02beeca38587a530207fddc0a280a",
"/Users/deepak/maas/ubuntu.tar.gz", 1262032476).Create(ctx)
sha,
tmp.Name(), size).Create(ctx)
assert.Nil(t, err)
err = res.Upload(ctx)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion maasclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"
"time"

"github.com/spectrocloud/maas-client-go/maasclient/oauth1"
"github.com/cloud104/maas-client-go/maasclient/oauth1"
)

// authenticatedClient
Expand Down
63 changes: 61 additions & 2 deletions maasclient/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,32 @@ package maasclient

import (
"context"
"os"
"net/http"
"testing"

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

func TestGetDNSResources(t *testing.T) {
c := NewAuthenticatedClientSet(os.Getenv("MAAS_ENDPOINT"), os.Getenv("MAAS_API_KEY"))
httpClient := &http.Client{}

httpmock.ActivateNonDefault(httpClient)
t.Cleanup(httpmock.DeactivateAndReset)

c := NewAuthenticatedClientSet("http://maas.test", "dummy-api-key", func(client *authenticatedClientSet) { client.WithHTTPClient(httpClient) })

ctx := context.Background()

t.Run("no-options", func(t *testing.T) {
defer httpmock.Reset()

httpmock.RegisterResponder(
http.MethodGet,
"http://maas.test/api/2.0/dnsresources/?all=true",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/dnsresources/list__all.json")),
)

res, err := c.DNSResources().List(ctx, nil)
assert.Nil(t, err, "expecting nil error")
assert.NotNil(t, res, "expecting non-nil result")
Expand All @@ -49,6 +63,12 @@ func TestGetDNSResources(t *testing.T) {
})

t.Run("get maas-1.maas", func(t *testing.T) {
httpmock.RegisterResponder(
http.MethodGet,
"http://maas.test/api/2.0/dnsresources/?fqdn=maas-1.maas.sc",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/dnsresources/list__fqdn-maas-1.maas.sc.json")),
)

filters := ParamsBuilder().Add(FQDNKey, "maas-1.maas.sc")
res, err := c.DNSResources().List(ctx, filters)
assert.Nil(t, err, "expecting nil error")
Expand All @@ -62,6 +82,20 @@ func TestGetDNSResources(t *testing.T) {
})

t.Run("create test-unit-1.maas", func(t *testing.T) {
defer httpmock.Reset()

httpmock.RegisterResponder(
http.MethodPost,
"http://maas.test/api/2.0/dnsresources/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/dnsresources/create__id-201.json")),
)

httpmock.RegisterResponder(
http.MethodDelete,
"http://maas.test/api/2.0/dnsresources/201/",
httpmock.NewJsonResponderOrPanic(204, nil),
)

res, err := c.DNSResources().
Builder().
WithFQDN("test-unit-1.maas.sc").
Expand All @@ -78,6 +112,31 @@ func TestGetDNSResources(t *testing.T) {
})

t.Run("create test-unit-2.maas", func(t *testing.T) {
defer httpmock.Reset()

httpmock.RegisterResponder(
http.MethodPost,
"http://maas.test/api/2.0/dnsresources/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/dnsresources/create__id-202.json")),
)

httpmock.RegisterResponder(
http.MethodPut,
"http://maas.test/api/2.0/dnsresources/202/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/dnsresources/update__id-202.json")),
)

httpmock.RegisterResponder(
http.MethodGet,
"http://maas.test/api/2.0/dnsresources/202/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/dnsresources/update__id-202.json")),
)

httpmock.RegisterResponder(
http.MethodDelete,
"http://maas.test/api/2.0/dnsresources/202/",
httpmock.NewJsonResponderOrPanic(204, nil),
)

//err := c.DNSResources().DNSResource(148).Delete(ctx)
//assert.Nil(t, err)
Expand Down
19 changes: 16 additions & 3 deletions maasclient/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@ package maasclient

import (
"context"
"github.com/stretchr/testify/assert"
"os"
"net/http"
"testing"

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

func TestDomain(t *testing.T) {
c := NewAuthenticatedClientSet(os.Getenv("MAAS_ENDPOINT"), os.Getenv("MAAS_API_KEY"))
httpClient := &http.Client{}

httpmock.ActivateNonDefault(httpClient)
t.Cleanup(httpmock.DeactivateAndReset)

c := NewAuthenticatedClientSet("http://maas.test", "dummy-api-key", func(client *authenticatedClientSet) { client.WithHTTPClient(httpClient) })

ctx := context.Background()

t.Run("list domains", func(t *testing.T) {
httpmock.RegisterResponder(
http.MethodGet,
"http://maas.test/api/2.0/domains/",
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/domains/list__all.json")),
)

res, err := c.Domains().List(ctx)
assert.Nil(t, err)
assert.NotNil(t, res)
Expand Down
Loading