github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/desktop_test.go (about)

     1  /*
     2  Copyright 2022 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/gravitational/trace"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestWindowsDesktopsSorter(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	testValsUnordered := []string{"d", "b", "a", "c"}
    31  
    32  	makeDesktops := func(testVals []string, testField string) []WindowsDesktop {
    33  		desktops := make([]WindowsDesktop, len(testVals))
    34  		for i := 0; i < len(testVals); i++ {
    35  			testVal := testVals[i]
    36  			var err error
    37  			desktops[i], err = NewWindowsDesktopV3(
    38  				getTestVal(testField == ResourceMetadataName, testVal),
    39  				nil,
    40  				WindowsDesktopSpecV3{
    41  					Addr: getTestVal(testField == ResourceSpecAddr, testVal),
    42  				})
    43  
    44  			require.NoError(t, err)
    45  		}
    46  		return desktops
    47  	}
    48  
    49  	cases := []struct {
    50  		name      string
    51  		fieldName string
    52  	}{
    53  		{
    54  			name:      "by name",
    55  			fieldName: ResourceMetadataName,
    56  		},
    57  		{
    58  			name:      "by addr",
    59  			fieldName: ResourceSpecAddr,
    60  		},
    61  	}
    62  
    63  	for _, c := range cases {
    64  		c := c
    65  		t.Run(fmt.Sprintf("%s desc", c.name), func(t *testing.T) {
    66  			sortBy := SortBy{Field: c.fieldName, IsDesc: true}
    67  			servers := WindowsDesktops(makeDesktops(testValsUnordered, c.fieldName))
    68  			require.NoError(t, servers.SortByCustom(sortBy))
    69  			targetVals, err := servers.GetFieldVals(c.fieldName)
    70  			require.NoError(t, err)
    71  			require.IsDecreasing(t, targetVals)
    72  		})
    73  
    74  		t.Run(fmt.Sprintf("%s asc", c.name), func(t *testing.T) {
    75  			sortBy := SortBy{Field: c.fieldName}
    76  			servers := WindowsDesktops(makeDesktops(testValsUnordered, c.fieldName))
    77  			require.NoError(t, servers.SortByCustom(sortBy))
    78  			targetVals, err := servers.GetFieldVals(c.fieldName)
    79  			require.NoError(t, err)
    80  			require.IsIncreasing(t, targetVals)
    81  		})
    82  	}
    83  
    84  	// Test error.
    85  	sortBy := SortBy{Field: "unsupported"}
    86  	desktops := makeDesktops(testValsUnordered, "does-not-matter")
    87  	require.True(t, trace.IsNotImplemented(WindowsDesktops(desktops).SortByCustom(sortBy)))
    88  }
    89  
    90  func TestInvalidDesktopName(t *testing.T) {
    91  	_, err := NewWindowsDesktopV3("name-contains.period", nil,
    92  		WindowsDesktopSpecV3{Addr: "desktop.example.com:3389"})
    93  	require.True(t, trace.IsBadParameter(err), "want bad parameter error, got %v", err)
    94  }