Skip to content

Commit

Permalink
test: web metadatada
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed Jun 10, 2024
1 parent bcfed93 commit a14f25a
Showing 1 changed file with 175 additions and 4 deletions.
179 changes: 175 additions & 4 deletions pkg/web/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,187 @@

package web

import "testing"
import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/tsuru/rpaas-operator/internal/pkg/rpaas"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/fake"
clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
)

func Test_getMetadata(t *testing.T) {
t.Skip("Not implemented.")
testCases := []struct {
name string
instance string
expectedCode int
manager rpaas.RpaasManager
}{
{
name: "when successfully getting metadata",
instance: "my-instance",
expectedCode: http.StatusOK,
manager: &fake.RpaasManager{
FakeGetMetadata: func(instance string) (*clientTypes.Metadata, error) {
assert.Equal(t, "my-instance", instance)
return &clientTypes.Metadata{
Labels: []clientTypes.MetadataItem{
{Name: "rpaas_instance", Value: "my-instance"},
},
Annotations: []clientTypes.MetadataItem{
{Name: "custom-annotation", Value: "my-annotation"},
},
}, nil
},
},
},
{
name: "when get metadata returns an error",
instance: "my-instance",
expectedCode: http.StatusNotFound,
manager: &fake.RpaasManager{
FakeGetMetadata: func(instance string) (*clientTypes.Metadata, error) {
return nil, rpaas.NotFoundError{Msg: "instance not found"}
},
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
srv := newTestingServer(t, tt.manager)
defer srv.Close()

path := fmt.Sprintf("%s/resources/%s/metadata", srv.URL, tt.instance)

req, err := http.NewRequest(http.MethodGet, path, nil)
assert.NoError(t, err)

rsp, err := srv.Client().Do(req)
assert.NoError(t, err)

assert.Equal(t, tt.expectedCode, rsp.StatusCode)
})
}
}

func Test_setMetadata(t *testing.T) {
t.Skip("Not implemented.")
testCases := []struct {
name string
instance string
expectedCode int
manager rpaas.RpaasManager
}{
{
name: "when successfully setting metadata",
instance: "my-instance",
expectedCode: http.StatusOK,
manager: &fake.RpaasManager{
FakeSetMetadata: func(instance string, metadata *clientTypes.Metadata) error {
return nil
},
},
},
{
name: "when set metadata instance not found",
instance: "my-instance",
expectedCode: http.StatusNotFound,
manager: &fake.RpaasManager{
FakeSetMetadata: func(instance string, metadata *clientTypes.Metadata) error {
return rpaas.NotFoundError{Msg: "instance not found"}
},
},
},
{
name: "when set metadata returns an error",
instance: "my-instance",
expectedCode: http.StatusBadRequest,
manager: &fake.RpaasManager{
FakeSetMetadata: func(instance string, metadata *clientTypes.Metadata) error {
return rpaas.ValidationError{Msg: "invalid metadata"}
},
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
srv := newTestingServer(t, tt.manager)
defer srv.Close()

path := fmt.Sprintf("%s/resources/%s/metadata", srv.URL, tt.instance)

req, err := http.NewRequest(http.MethodPost, path, nil)
assert.NoError(t, err)

req.Header.Set("Content-Type", "application/json")

rsp, err := srv.Client().Do(req)
assert.NoError(t, err)

assert.Equal(t, tt.expectedCode, rsp.StatusCode)
})
}
}

func Test_unsetMetadata(t *testing.T) {
t.Skip("Not implemented.")
testCases := []struct {
name string
instance string
expectedCode int
manager rpaas.RpaasManager
}{
{
name: "when successfully unsetting metadata",
instance: "my-instance",
expectedCode: http.StatusOK,
manager: &fake.RpaasManager{
FakeUnsetMetadata: func(instance string, metadata *clientTypes.Metadata) error {
return nil
},
},
},
{
name: "when unset metadata instance not found",
instance: "my-instance",
expectedCode: http.StatusNotFound,
manager: &fake.RpaasManager{
FakeUnsetMetadata: func(instance string, metadata *clientTypes.Metadata) error {
return rpaas.NotFoundError{Msg: "instance not found"}
},
},
},
{
name: "when unset metadata returns an error",
instance: "my-instance",
expectedCode: http.StatusBadRequest,
manager: &fake.RpaasManager{
FakeUnsetMetadata: func(instance string, metadata *clientTypes.Metadata) error {
return rpaas.ValidationError{Msg: "invalid metadata"}
},
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
srv := newTestingServer(t, tt.manager)
defer srv.Close()

path := fmt.Sprintf("%s/resources/%s/metadata", srv.URL, tt.instance)

req, err := http.NewRequest(http.MethodDelete, path, nil)
assert.NoError(t, err)

req.Header.Set("Content-Type", "application/json")

rsp, err := srv.Client().Do(req)
assert.NoError(t, err)

assert.Equal(t, tt.expectedCode, rsp.StatusCode)
})
}
}

0 comments on commit a14f25a

Please sign in to comment.