...

Source file src/edge-infra.dev/pkg/edge/api/utils/resolver_helpers_test.go

Documentation: edge-infra.dev/pkg/edge/api/utils

     1  package utils
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"edge-infra.dev/pkg/edge/api/graph/model"
     8  	"edge-infra.dev/pkg/edge/constants"
     9  )
    10  
    11  func TestCheckNetworkServicesUpdatePermitted(t *testing.T) {
    12  	tests := map[string]struct {
    13  		description   string
    14  		clusterActive bool
    15  		serviceName   string
    16  		expected      bool
    17  	}{
    18  		"after-active": {
    19  			description:   "Test changing not allowed after active",
    20  			clusterActive: true,
    21  			serviceName:   constants.ServiceTypeEgressTunnelsCIDR,
    22  			expected:      false,
    23  		},
    24  		"before-active": {
    25  			description:   "Test changing not allowed after active",
    26  			clusterActive: false,
    27  			serviceName:   constants.ServiceTypeEgressTunnelsCIDR,
    28  			expected:      true,
    29  		},
    30  		"never-allowed-inactive": {
    31  			description:   "Test can never change DNS",
    32  			clusterActive: false,
    33  			serviceName:   constants.ServiceTypeClusterDNS,
    34  			expected:      false,
    35  		},
    36  		"never-allowed-active": {
    37  			description:   "Test can never change DNS",
    38  			clusterActive: false,
    39  			serviceName:   constants.ServiceTypeClusterDNS,
    40  			expected:      false,
    41  		},
    42  		"always-allowed-active": {
    43  			description:   "Test changing always allowed",
    44  			clusterActive: true,
    45  			serviceName:   constants.ServiceTypeNTP,
    46  			expected:      true,
    47  		},
    48  		"always-allowed-inactive": {
    49  			description:   "Test changing always allowed",
    50  			clusterActive: false,
    51  			serviceName:   constants.ServiceTypeNTP,
    52  			expected:      true,
    53  		},
    54  	}
    55  
    56  	for testName, test := range tests {
    57  		priority := 100
    58  		networkServiceInfo := []*model.UpdateNetworkServiceInfo{{
    59  			NetworkServiceID: "testid",
    60  			IP:               "192.168.0.1",
    61  			Family:           "inet",
    62  			Priority:         &priority,
    63  		}}
    64  		networkServiceByID := map[string]string{
    65  			"testid": test.serviceName,
    66  		}
    67  		err := CheckNetworkServicesUpdatePermitted(test.clusterActive, networkServiceInfo, networkServiceByID)
    68  		ok := err == nil
    69  		if ok != test.expected {
    70  			t.Errorf("%s test was %v, but expected %v. Err was %v",
    71  				testName,
    72  				ok,
    73  				test.expected,
    74  				err,
    75  			)
    76  		}
    77  	}
    78  }
    79  
    80  func TestClusterLabelsTypeMap(t *testing.T) {
    81  	tests := map[string]struct {
    82  		description string
    83  		label       []*model.Label
    84  		expected    bool
    85  	}{
    86  		"gke": {
    87  			description: "Testing the GKE Edge Type",
    88  			label: []*model.Label{
    89  				{Type: "edge-type", Description: "label 1", Key: "gke"},
    90  				{Type: "edge-fleet", Description: "label 2", Key: "store"},
    91  			},
    92  			expected: true,
    93  		},
    94  		"generic": {
    95  			description: "Testing the Generic Edge Type",
    96  			label: []*model.Label{
    97  				{Type: "edge-type", Description: "label 1", Key: "generic"},
    98  				{Type: "edge-fleet", Description: "label 2", Key: "store"},
    99  			},
   100  			expected: true,
   101  		},
   102  		"dsds": {
   103  			description: "Testing the DSDS Edge Type",
   104  			label: []*model.Label{
   105  				{Type: "edge-type", Description: "label 1", Key: "dsds"},
   106  				{Type: "edge-fleet", Description: "label 2", Key: "store"},
   107  			},
   108  			expected: true,
   109  		},
   110  		"sds": {
   111  			description: "Testing the SDS Edge Type. Should be invalid and be False",
   112  			label: []*model.Label{
   113  				{Type: "edge-type", Description: "label 1", Key: "dsds"},
   114  				{Type: "edge-fleet", Description: "label 2", Key: "store"},
   115  			},
   116  			expected: false,
   117  		},
   118  	}
   119  
   120  	for edgeType, test := range tests {
   121  		labelsMap := ClusterLabelsTypeMap(test.label)
   122  		_, exists := labelsMap[edgeType]
   123  
   124  		if exists != test.expected {
   125  			t.Errorf("%s test was %s, but was expected to be %s", edgeType, strconv.FormatBool(exists), strconv.FormatBool(test.expected))
   126  		}
   127  	}
   128  }
   129  
   130  func TestSetVersionToBaseAndMinorVersion(t *testing.T) {
   131  	type test struct {
   132  		description  string
   133  		baseVersion  string
   134  		minorVersion string
   135  		expected     []string
   136  	}
   137  	tests := []test{
   138  		{
   139  			description:  "Testing inputting version with 2 digit patch values",
   140  			baseVersion:  "0.16.15",
   141  			minorVersion: "1.12.23",
   142  			expected:     []string{"0.16.0", "1.12"},
   143  		},
   144  		{
   145  			description:  "Testing inputting versions with single digit patch values",
   146  			baseVersion:  "0.16.1",
   147  			minorVersion: "1.12.1",
   148  			expected:     []string{"0.16.0", "1.12"},
   149  		},
   150  	}
   151  
   152  	for _, tc := range tests {
   153  		base, minor := tc.baseVersion, tc.minorVersion
   154  		baseVersion, minorVersion := SetVersionToBaseAndMinorVersion(&base, &minor)
   155  		if baseVersion != tc.expected[0] {
   156  			t.Errorf("test was %s for base version, but was expected to be %s", baseVersion, tc.expected[0])
   157  		}
   158  		if minorVersion != tc.expected[1] {
   159  			t.Errorf("test was %s for minor version, but was expected to be %s", minorVersion, tc.expected[1])
   160  		}
   161  	}
   162  }
   163  

View as plain text