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

✅ add test by host #2

Open
wants to merge 7 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
9 changes: 4 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ jobs:
tests:
name: Run tests
runs-on: ubuntu-latest
environment: test_ci
strategy:
matrix:
go-version: [ '1.22' ]

steps:
- uses: actions/checkout@v4
- name: Start mockserver
run: docker run -d -v ./tests/mockserver/config:/config -p 1080:1080 -e MOCKSERVER_INITIALIZATION_JSON_PATH=/config/initializerJson.json --name mockserver-go mockserver/mockserver
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
Expand All @@ -24,8 +23,8 @@ jobs:
- name: Install dependencies for container
run: cd docker && go get . && cd -
- name: Run tests for container
run: cd docker && go test && cd -
run: cd docker && go test -v && cd -
env:
NETBOX_URL: http://localhost:1080
NETBOX_TOKEN: testing
NETBOX_URL: ${{ secrets.NETBOX_URL }}
NETBOX_TOKEN: ${{ secrets.NETBOX_TOKEN }}

11 changes: 11 additions & 0 deletions docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ func ContainerCreate(Image ContainerCreateStruct) (ContainerComplete, error){
}
return ContainerResponse, nil
}

func ContainerDelete(containerId int) error{
url := fmt.Sprintf("/docker/containers/%d/", containerId)
_, err := netbox.Request(url, "DELETE", nil)

if err != nil {
return err
}

return nil
}
8 changes: 7 additions & 1 deletion docker/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ module github.com/SaaShup/paashup-sdk/docker

go 1.22.2

require github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240710090620-c8492d0bf61a // indirect
require (
github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240716131611-f2dba91f0f38 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 11 additions & 0 deletions docker/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240708075831-c60b9328b90d h1:32IN
github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240708075831-c60b9328b90d/go.mod h1:h1+jD1MtvhEQ4F9qitcHBr08SPKxW21D6zHubUv1TEE=
github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240710090620-c8492d0bf61a h1:GZjVSyMZsB1gmirfwe6TW4oGCNciM4GV/e5hB4z4mHo=
github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240710090620-c8492d0bf61a/go.mod h1:h1+jD1MtvhEQ4F9qitcHBr08SPKxW21D6zHubUv1TEE=
github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240716131611-f2dba91f0f38 h1:jvh3E6zuZTmPD79CWNo8FOUuOZMlpEPndQHepCkP4S0=
github.com/SaaShup/paashup-sdk/netbox v0.0.0-20240716131611-f2dba91f0f38/go.mod h1:h1+jD1MtvhEQ4F9qitcHBr08SPKxW21D6zHubUv1TEE=
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/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 9 additions & 0 deletions docker/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ func HostCreate(Host HostCreateStruct) (HostComplete, error){
return HostResponse, nil
}

func HostDelete(HostId int) error{
url := fmt.Sprintf("/docker/hosts/%d/", HostId)
_, err := netbox.Request(url, "DELETE", nil)

if err != nil {
return err
}
return nil
}
143 changes: 137 additions & 6 deletions docker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,149 @@ package docker
import (
"testing"
"github.com/SaaShup/paashup-sdk/netbox"
"github.com/stretchr/testify/assert"
"os"
)

func TestHostCreate(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostCreate(HostCreateStruct{Name: "testhost", Endpoint: "http://test:[email protected]:1880/"})
assert.Nil(err)
assert.Equal(host.Name, "testhost")
}

func TestRegistryCreate(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
registry, err := RegistryCreate(RegistryCreateStruct{Name: "testregistry", ServerAddress: "http://test.com:5000", Username: "test", Password: "test", Host: host.Id})
assert.Nil(err)
assert.Equal(registry.Name, "testregistry")
}

func TestImageCreate(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
registry, err := RegistrySearchByName("testregistry", host.Id)

image, err := ImageCreate(ImageCreateStruct{Name: "testimage", Host: host.Id, Registry: registry.Id, ImageID: "0", Version: "0"})
assert.Nil(err)
assert.Equal(image.Name, "testimage")
}

func TestContainerCreate(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
image, err := ImageSearchByName("testimage", host.Id)

containerS := ContainerCreateStruct{Name: "testcontainer", Host: host.Id, Image: image.Id, State: "none", Operation: "create"}
containerS.Restart_policy = "no"
containerS.Labels = []Label{}
containerS.Env = []Env{}
containerS.Ports = []Port{}
containerS.Mounts = []Mount{}
containerS.NetworkSettings = []Network{}

container, err := ContainerCreate(containerS)
assert.Nil(err)
assert.Equal(container.Name, "testcontainer")
}

func TestVolumeCreate(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")

volume, err := VolumeCreate(VolumeCreateStruct{Name: "testvolume", Host: host.Id, Driver: "local"})
assert.Nil(err)
assert.Equal(volume.Name, "testvolume")
}

func TestVolumeList(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

vols, err := VolumeList()
if err != nil {
t.Fatal(err)
}
if len(vols.Results) != 2 {
t.Fatal("No volumes found")
}
assert.Nil(err)
assert.NotZero(len(vols.Results))
}

func TestVolumeListByHost(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
vols, err := VolumeListByHost(host.Id)
assert.Nil(err)
assert.NotZero(len(vols.Results))
assert.Equal(vols.Results[0].Host.Name, "testhost")
}

func TestContainerDelete(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
container, err := ContainerSearchByName(host, "testcontainer")
err = ContainerDelete(container.Id)
assert.Nil(err)
}

func TestVolumeDelete(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
volume, err := VolumeSearchByName("testvolume", host.Id)
err = VolumeDelete(volume.Id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function err(err) {
if err != nil {
t.Fatal(err)
}

err(VolumeSearchByName("testvolume", host.Id))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not idiomatic go, this is:

result, err := FunctionCall(params)
if err != nil {
  t.Fatalf("unexpected error during <description>: %v", err)
}

assert.Nil(err)
}

func TestImageDelete(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
image, err := ImageSearchByName("testimage", host.Id)
err = ImageDelete(image.Id)
assert.Nil(err)
}

func TestRegistryDelete(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
registry, err := RegistrySearchByName("testregistry", host.Id)
err = RegistryDelete(registry.Id)
assert.Nil(err)
}

func TestHostDelete(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
assert := assert.New(t)

host, err := HostSearchByName("testhost")
err = HostDelete(host.Id)
assert.Nil(err)
}
66 changes: 0 additions & 66 deletions tests/mockserver/config/initializerJson.json

This file was deleted.

Loading