Skip to content

Commit

Permalink
test: implementing metadata client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed Jun 6, 2024
1 parent d4adddc commit eec9a21
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 2 deletions.
1 change: 0 additions & 1 deletion cmd/plugin/rpaasv2/cmd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ func runUnsetMetadata(c *cli.Context) error {
return err
}

fmt.Fprintln(c.App.Writer, metadata)
fmt.Fprintln(c.App.Writer, "Metadata removed successfully")

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpaas/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *client) UnsetMetadata(ctx context.Context, instance string, metadata *t
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Type", "application/json")

response, err := c.do(ctx, req)
if err != nil {
Expand Down
175 changes: 175 additions & 0 deletions pkg/rpaas/client/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,178 @@
// license that can be found in the LICENSE file.

package client

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"

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

func TestClientThroughTsuru_GetMetadata(t *testing.T) {
tests := []struct {
name string
instance string
expectedError string
handler http.HandlerFunc
}{
{
name: "missing instance",
instance: "",
expectedError: "rpaasv2: instance cannot be empty",
handler: func(w http.ResponseWriter, r *http.Request) {},
},
{
name: "unexpected status code",
instance: "my-instance",
expectedError: "rpaasv2: unexpected status code: 404 Not Found, detail: instance not found",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("instance not found"))
},
},
{
name: "success",
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "GET")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/metadata"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))

metadata := types.Metadata{
Labels: []types.MetadataItem{},
Annotations: []types.MetadataItem{},
}

m, _ := json.Marshal(metadata)
w.WriteHeader(http.StatusOK)
w.Write(m)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, server := newClientThroughTsuru(t, tt.handler)
defer server.Close()

metadata, err := client.GetMetadata(context.TODO(), tt.instance)
if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
return
}

assert.NoError(t, err)
assert.NotNil(t, metadata)
})
}
}

func TestClientThroughTsuru_SetMetadata(t *testing.T) {
tests := []struct {
name string
instance string
expectedError string
handler http.HandlerFunc
}{
{
name: "missing instance",
instance: "",
expectedError: "rpaasv2: instance cannot be empty",
handler: func(w http.ResponseWriter, r *http.Request) {},
},
{
name: "unexpected status code",
instance: "my-instance",
expectedError: "rpaasv2: unexpected status code: 404 Not Found, detail: instance not found",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("instance not found"))
},
},
{
name: "success",
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "POST")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/metadata"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.NotNil(t, r.Body)
w.WriteHeader(http.StatusOK)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, server := newClientThroughTsuru(t, tt.handler)
defer server.Close()

err := client.SetMetadata(context.TODO(), tt.instance, &types.Metadata{})
if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
return
}

assert.NoError(t, err)
})
}
}

func TestClientThroughTsuru_UnsetMetadata(t *testing.T) {
tests := []struct {
name string
instance string
expectedError string
handler http.HandlerFunc
}{
{
name: "missing instance",
instance: "",
expectedError: "rpaasv2: instance cannot be empty",
handler: func(w http.ResponseWriter, r *http.Request) {},
},
{
name: "unexpected status code",
instance: "my-instance",
expectedError: "rpaasv2: unexpected status code: 404 Not Found, detail: instance not found",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("instance not found"))
},
},
{
name: "success",
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "DELETE")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/metadata"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.NotNil(t, r.Body)
w.WriteHeader(http.StatusOK)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, server := newClientThroughTsuru(t, tt.handler)
defer server.Close()

err := client.UnsetMetadata(context.TODO(), tt.instance, &types.Metadata{})
if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
return
}

assert.NoError(t, err)
})
}
}
19 changes: 19 additions & 0 deletions pkg/web/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package web

import "testing"

func Test_getMetadata(t *testing.T) {
t.Skip("Not implemented.")
}

func Test_setMetadata(t *testing.T) {
t.Skip("Not implemented.")
}

func Test_unsetMetadata(t *testing.T) {
t.Skip("Not implemented.")
}

0 comments on commit eec9a21

Please sign in to comment.