Skip to content

Commit

Permalink
feat: Switch to public handlerinspector module
Browse files Browse the repository at this point in the history
  • Loading branch information
dploeger committed Dec 22, 2023
1 parent 254ae76 commit fbf5779
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 334 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module vmware-rest-proxy
go 1.21

require (
github.com/dodevops/golang-handlerinspector v0.1.0
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/assert/v2 v2.2.0
github.com/go-resty/resty/v2 v2.10.0
Expand All @@ -12,7 +13,6 @@ require (
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -26,7 +26,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
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/dodevops/golang-handlerinspector v0.1.0 h1:iPSaw9izmNSvoVGUn3btGbpjXz6iaus27QEeGv6tKrI=
github.com/dodevops/golang-handlerinspector v0.1.0/go.mod h1:0oVA3heviGeSZQ1eqt/yjcouhP3B95g/2l732lK48r4=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
93 changes: 47 additions & 46 deletions internal/endpoints/vms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package endpoints
import (
"encoding/json"
"fmt"
"github.com/dodevops/golang-handlerinspector/pkg/builder"
"github.com/dodevops/golang-handlerinspector/pkg/inspector"
"github.com/gin-gonic/gin"
"github.com/go-playground/assert/v2"
"github.com/go-resty/resty/v2"
"net/http"
"net/http/httptest"
"testing"
"vmware-rest-proxy/internal/api"
"vmware-rest-proxy/pkg/handlerinspector"
)

// AUTHTOKEN holds a test token that should be issued and used in all tests
const AUTHTOKEN = "testtoken"

// sessionRule holds a handlerinspector Rule for the session api
var sessionRule = handlerinspector.NewRule("session").
WithCondition(handlerinspector.HasPath("/api/session")).
// sessionRule holds a builder Rule for the session api
var sessionRule = builder.NewRule("session").
WithCondition(builder.HasPath("/api/session")).
ReturnBodyFromFunction(func(r *http.Request) string {
if r.Method == "POST" {
return fmt.Sprintf(`"%s"`, AUTHTOKEN)
Expand Down Expand Up @@ -52,13 +53,13 @@ func testRequests(handler http.Handler, requests []*http.Request) *httptest.Resp

// TestVMSEndpoint_GetSession checks if the session endpoint is called
func TestVMSEndpoint_GetSession(t *testing.T) {
b := handlerinspector.NewBuilder().
b := builder.NewBuilder().
WithRule(sessionRule).
WithRule(
handlerinspector.NewRule("vms").
WithCondition(handlerinspector.HasPath("/api/vcenter/vm")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("vms").
WithCondition(builder.HasPath("/api/vcenter/vm")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody("[]").
ReturnHeader("Content-Type", "application/json").
Build(),
Expand All @@ -68,21 +69,21 @@ func TestVMSEndpoint_GetSession(t *testing.T) {
req.SetBasicAuth("test", "test")
w := testRequests(b.Build(), []*http.Request{req})

i := handlerinspector.NewInspector(b)
i := inspector.NewInspector(b)
assert.Equal(t, i.Failed(), false)
assert.Equal(t, i.AllWereCalled(), true)
assert.Equal(t, http.StatusOK, w.Code)
}

// TestVMSEndpoint_GetVMS checks the vms endpoint
func TestVMSEndpoint_GetVMS(t *testing.T) {
b := handlerinspector.NewBuilder().
b := builder.NewBuilder().
WithRule(sessionRule).
WithRule(
handlerinspector.NewRule("vms").
WithCondition(handlerinspector.HasPath("/api/vcenter/vm")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("vms").
WithCondition(builder.HasPath("/api/vcenter/vm")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody(`[{"VM": "1", "Name": "test1"}, {"VM": "2", "Name": "test2"}]`).
ReturnHeader("Content-Type", "application/json").
Build(),
Expand All @@ -101,7 +102,7 @@ func TestVMSEndpoint_GetVMS(t *testing.T) {
err := json.NewDecoder(w.Body).Decode(&r)
assert.Equal(t, err, nil)

i := handlerinspector.NewInspector(b)
i := inspector.NewInspector(b)
assert.Equal(t, i.Failed(), false)
assert.Equal(t, i.AllWereCalled(), true)
assert.Equal(t, http.StatusOK, w.Code)
Expand All @@ -115,51 +116,51 @@ func TestVMSEndpoint_GetVMS(t *testing.T) {

// TestVMSEndpoint_GetVMTags checks the /vms/tags endpoint
func TestVMSEndpoint_GetVMTags(t *testing.T) {
b := handlerinspector.NewBuilder().
b := builder.NewBuilder().
WithRule(sessionRule).
WithRule(
handlerinspector.NewRule("list-associated-tags").
WithCondition(handlerinspector.HasPath("/api/cis/tagging/tag-association")).
WithCondition(handlerinspector.HasMethod("POST")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
WithCondition(handlerinspector.HasQueryParam("action", "list-attached-tags")).
WithCondition(handlerinspector.HasBody(`{"object_id":{"id":"1","type":"VirtualMachine"}}`)).
builder.NewRule("list-associated-tags").
WithCondition(builder.HasPath("/api/cis/tagging/tag-association")).
WithCondition(builder.HasMethod("POST")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
WithCondition(builder.HasQueryParam("action", "list-attached-tags")).
WithCondition(builder.HasBody(`{"object_id":{"id":"1","type":"VirtualMachine"}}`)).
ReturnBody(`["1", "2"]`).
ReturnHeader("Content-Type", "application/json").
Build(),
).
WithRule(
handlerinspector.NewRule("tag-data-1").
WithCondition(handlerinspector.HasPath("/api/cis/tagging/tag/1")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("tag-data-1").
WithCondition(builder.HasPath("/api/cis/tagging/tag/1")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody(`{"category_id": "1", "name": "testtag1"}`).
ReturnHeader("Content-Type", "application/json").
Build(),
).
WithRule(
handlerinspector.NewRule("tag-data-2").
WithCondition(handlerinspector.HasPath("/api/cis/tagging/tag/2")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("tag-data-2").
WithCondition(builder.HasPath("/api/cis/tagging/tag/2")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody(`{"category_id": "2", "name": "testtag2"}`).
ReturnHeader("Content-Type", "application/json").
Build(),
).
WithRule(
handlerinspector.NewRule("tag-category-1").
WithCondition(handlerinspector.HasPath("/api/cis/tagging/category/1")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("tag-category-1").
WithCondition(builder.HasPath("/api/cis/tagging/category/1")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody(`{"name": "testcategory1"}`).
ReturnHeader("Content-Type", "application/json").
Build(),
).
WithRule(
handlerinspector.NewRule("tag-category-2").
WithCondition(handlerinspector.HasPath("/api/cis/tagging/category/2")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("tag-category-2").
WithCondition(builder.HasPath("/api/cis/tagging/category/2")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody(`{"name": "testcategory2"}`).
ReturnHeader("Content-Type", "application/json").
Build(),
Expand All @@ -178,7 +179,7 @@ func TestVMSEndpoint_GetVMTags(t *testing.T) {
err := json.NewDecoder(w.Body).Decode(&r)
assert.Equal(t, err, nil)

i := handlerinspector.NewInspector(b)
i := inspector.NewInspector(b)
assert.Equal(t, i.Failed(), false)
assert.Equal(t, i.AllWereCalled(), true)
assert.Equal(t, http.StatusOK, w.Code)
Expand All @@ -192,13 +193,13 @@ func TestVMSEndpoint_GetVMTags(t *testing.T) {

// TestVMSEndpoint_GetFQDN checks the vm/fqdn endpoint
func TestVMSEndpoint_GetFQDN(t *testing.T) {
b := handlerinspector.NewBuilder().
b := builder.NewBuilder().
WithRule(sessionRule).
WithRule(
handlerinspector.NewRule("get-fqdm").
WithCondition(handlerinspector.HasPath("/api/vcenter/vm/1/guest/networking")).
WithCondition(handlerinspector.HasMethod("GET")).
WithCondition(handlerinspector.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
builder.NewRule("get-fqdm").
WithCondition(builder.HasPath("/api/vcenter/vm/1/guest/networking")).
WithCondition(builder.HasMethod("GET")).
WithCondition(builder.HasHeader("Vmware-Api-Session-Id", AUTHTOKEN)).
ReturnBody(`{"dns_values":{"domain_name":"example.com","host_name":"test"}}`).
ReturnHeader("Content-Type", "application/json").
Build(),
Expand All @@ -214,7 +215,7 @@ func TestVMSEndpoint_GetFQDN(t *testing.T) {
err := json.NewDecoder(w.Body).Decode(&r)
assert.Equal(t, err, nil)

i := handlerinspector.NewInspector(b)
i := inspector.NewInspector(b)
assert.Equal(t, i.Failed(), false)
assert.Equal(t, i.AllWereCalled(), true)
assert.Equal(t, http.StatusOK, w.Code)
Expand Down
73 changes: 0 additions & 73 deletions pkg/handlerinspector/builder.go

This file was deleted.

Loading

0 comments on commit fbf5779

Please sign in to comment.