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 5 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
7 changes: 3 additions & 4 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 @@ -26,6 +25,6 @@ jobs:
- name: Run tests for container
run: cd docker && go test && 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
}
2 changes: 1 addition & 1 deletion docker/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ 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
2 changes: 2 additions & 0 deletions docker/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ 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=
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
}
163 changes: 162 additions & 1 deletion docker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,88 @@ import (
"os"
)

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

host, err := HostCreate(HostCreateStruct{Name: "testhost", Endpoint: "http://test:[email protected]:1880/"})
if err != nil {
t.Fatal(err)
}
if host.Name != "testhost" {
t.Fatal(host.Name)
}
}

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

host, err := HostSearchByName("testhost")
registry, err := RegistryCreate(RegistryCreateStruct{Name: "testregistry", ServerAddress: "http://test.com:5000", Username: "test", Password: "test", Host: host.Id})
if err != nil {
t.Fatal(err)
}
if registry.Name != "testregistry" {
t.Fatal(registry.Name)
}
}

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

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"})
if err != nil {
t.Fatal(err)
}
if image.Name != "testimage" {
t.Fatal(image.Name)
}
}

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

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)
if err != nil {
t.Fatal(err)
}
if container.Name != "testcontainer" {
t.Fatal(container.Name)
}
}

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

host, err := HostSearchByName("testhost")

volume, err := VolumeCreate(VolumeCreateStruct{Name: "testvolume", Host: host.Id, Driver: "local"})
if err != nil {
t.Fatal(err)
}
if volume.Name != "testvolume" {
t.Fatal(volume.Name)
}
}

func TestVolumeList(t *testing.T) {
netbox.NETBOX_URL = os.Getenv("NETBOX_URL")
netbox.NETBOX_TOKEN = os.Getenv("NETBOX_TOKEN")
Expand All @@ -14,7 +96,86 @@ func TestVolumeList(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(vols.Results) != 2 {
if len(vols.Results) == 0 {
t.Fatal("No volumes found")
}
}

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

host, err := HostSearchByName("testhost")
vols, err := VolumeListByHost(host.Id)
if err != nil {
t.Fatal(err)
}
if len(vols.Results) > 0 {
if vols.Results[0].Host.Name != "testhost" {
t.Fatal(vols.Results[0].Host.Name)
}
} else {
t.Fatal("No volumes found")
}
}

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

host, err := HostSearchByName("testhost")
container, err := ContainerSearchByName(host, "testcontainer")
err = ContainerDelete(container.Id)
if err != nil {
t.Fatal(err)
}

}

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

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)
}

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

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

host, err := HostSearchByName("testhost")
image, err := ImageSearchByName("testimage", host.Id)
err = ImageDelete(image.Id)
if err != nil {
t.Fatal(err)
}
}

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

host, err := HostSearchByName("testhost")
registry, err := RegistrySearchByName("testregistry", host.Id)
err = RegistryDelete(registry.Id)
if err != nil {
t.Fatal(err)
}
}

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

host, err := HostSearchByName("testhost")
err = HostDelete(host.Id)
if err != nil {
t.Fatal(err)
}

}
66 changes: 0 additions & 66 deletions tests/mockserver/config/initializerJson.json

This file was deleted.

Loading