Skip to content

Commit

Permalink
internal/core/resources: Unittests for ValidateResources
Browse files Browse the repository at this point in the history
Signed-off-by: Wesley Hershberger <[email protected]>
  • Loading branch information
MggMuggins committed Aug 9, 2024
1 parent bde792b commit b007966
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions internal/rest/resources/resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package resources

import (
"testing"

"github.com/canonical/microcluster/v3/rest"
)

var validServers = map[string]rest.Server{
"coreConsumer": {
CoreAPI: true,
Resources: []rest.Resources{
{
PathPrefix: "core_consumer",
},
},
},
}

func TestValidateEndpointsValidServers(t *testing.T) {
err := ValidateEndpoints(validServers, "localhost:8000")
if err != nil {
t.Errorf("Valid server failed validation: %s", err)
}
}

var invalidServers = map[string]rest.Server{
"emptyResources": {
CoreAPI: true,
},
"duplicate": {
Resources: []rest.Resources{
{
PathPrefix: "dup",
Endpoints: []rest.Endpoint{
{
Path: "duplicate",
},
},
},
{
PathPrefix: "dup",
Endpoints: []rest.Endpoint{
{
Path: "duplicate",
},
},
},
},
},
"overlapCore": {
CoreAPI: true,
Resources: []rest.Resources{
{
PathPrefix: "core",
},
},
},
"overlapCoreMultipart": {
CoreAPI: true,
Resources: []rest.Resources{
{
PathPrefix: "core/subpoint",
},
},
},
}

func TestValidateEndpointsInvalidServers(t *testing.T) {
for serverName, server := range invalidServers {
servers := map[string]rest.Server{
serverName: server,
}
err := ValidateEndpoints(servers, "localhost:8000")
if err == nil {
t.Errorf("Invalid server %q passed validation", serverName)
}
}
}

0 comments on commit b007966

Please sign in to comment.