Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion lib/teleterm/api/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func NewClusterURI(profileName string) ResourceURI {
}
}

// ParseClusterURI parses a string and returns cluster URI
// ParseClusterURI parses a string and returns a cluster URI.
//
// If given a resource URI, it'll return the URI of the cluster to which the resource belongs to.
// If given a leaf cluster resource URI, it'll return the URI of the leaf cluster.
func ParseClusterURI(path string) (ResourceURI, error) {
URI := New(path)
profileName := URI.GetProfileName()
Expand Down
53 changes: 46 additions & 7 deletions lib/teleterm/api/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ package uri_test

import (
"fmt"
"reflect"
"testing"

"github.com/gravitational/teleport/lib/teleterm/api/uri"

"github.com/stretchr/testify/require"
)

func TestURI(t *testing.T) {
testCases := []struct {
func TestString(t *testing.T) {
t.Parallel()
tests := []struct {
in uri.ResourceURI
out string
}{
Expand All @@ -43,12 +45,49 @@ func TestURI(t *testing.T) {
},
}

for _, tt := range testCases {
for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%v", tt.in), func(t *testing.T) {
t.Parallel()

out := tt.in.String()
if !reflect.DeepEqual(out, tt.out) {
t.Errorf("out %#v, want %#v", out, tt.out)
}
require.Equal(t, tt.out, out)
})
}
}

func TestParseClusterURI(t *testing.T) {
t.Parallel()
tests := []struct {
in string
out uri.ResourceURI
}{
{
"/clusters/cluster.sh",
uri.NewClusterURI("cluster.sh"),
},
{
"/clusters/cluster.sh/servers/server1",
uri.NewClusterURI("cluster.sh"),
},
{
"/clusters/cluster.sh/leaves/leaf.sh",
uri.NewClusterURI("cluster.sh").AppendLeafCluster("leaf.sh"),
},
{
"/clusters/cluster.sh/leaves/leaf.sh/dbs/postgres",
uri.NewClusterURI("cluster.sh").AppendLeafCluster("leaf.sh"),
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.in, func(t *testing.T) {
t.Parallel()

out, err := uri.ParseClusterURI(tt.in)
require.NoError(t, err)
require.Equal(t, tt.out, out)
})
}
}