package utils import ( "strconv" "testing" "edge-infra.dev/pkg/edge/api/graph/model" "edge-infra.dev/pkg/edge/constants" ) func TestCheckNetworkServicesUpdatePermitted(t *testing.T) { tests := map[string]struct { description string clusterActive bool serviceName string expected bool }{ "after-active": { description: "Test changing not allowed after active", clusterActive: true, serviceName: constants.ServiceTypeEgressTunnelsCIDR, expected: false, }, "before-active": { description: "Test changing not allowed after active", clusterActive: false, serviceName: constants.ServiceTypeEgressTunnelsCIDR, expected: true, }, "never-allowed-inactive": { description: "Test can never change DNS", clusterActive: false, serviceName: constants.ServiceTypeClusterDNS, expected: false, }, "never-allowed-active": { description: "Test can never change DNS", clusterActive: false, serviceName: constants.ServiceTypeClusterDNS, expected: false, }, "always-allowed-active": { description: "Test changing always allowed", clusterActive: true, serviceName: constants.ServiceTypeNTP, expected: true, }, "always-allowed-inactive": { description: "Test changing always allowed", clusterActive: false, serviceName: constants.ServiceTypeNTP, expected: true, }, } for testName, test := range tests { priority := 100 networkServiceInfo := []*model.UpdateNetworkServiceInfo{{ NetworkServiceID: "testid", IP: "192.168.0.1", Family: "inet", Priority: &priority, }} networkServiceByID := map[string]string{ "testid": test.serviceName, } err := CheckNetworkServicesUpdatePermitted(test.clusterActive, networkServiceInfo, networkServiceByID) ok := err == nil if ok != test.expected { t.Errorf("%s test was %v, but expected %v. Err was %v", testName, ok, test.expected, err, ) } } } func TestClusterLabelsTypeMap(t *testing.T) { tests := map[string]struct { description string label []*model.Label expected bool }{ "gke": { description: "Testing the GKE Edge Type", label: []*model.Label{ {Type: "edge-type", Description: "label 1", Key: "gke"}, {Type: "edge-fleet", Description: "label 2", Key: "store"}, }, expected: true, }, "generic": { description: "Testing the Generic Edge Type", label: []*model.Label{ {Type: "edge-type", Description: "label 1", Key: "generic"}, {Type: "edge-fleet", Description: "label 2", Key: "store"}, }, expected: true, }, "dsds": { description: "Testing the DSDS Edge Type", label: []*model.Label{ {Type: "edge-type", Description: "label 1", Key: "dsds"}, {Type: "edge-fleet", Description: "label 2", Key: "store"}, }, expected: true, }, "sds": { description: "Testing the SDS Edge Type. Should be invalid and be False", label: []*model.Label{ {Type: "edge-type", Description: "label 1", Key: "dsds"}, {Type: "edge-fleet", Description: "label 2", Key: "store"}, }, expected: false, }, } for edgeType, test := range tests { labelsMap := ClusterLabelsTypeMap(test.label) _, exists := labelsMap[edgeType] if exists != test.expected { t.Errorf("%s test was %s, but was expected to be %s", edgeType, strconv.FormatBool(exists), strconv.FormatBool(test.expected)) } } } func TestSetVersionToBaseAndMinorVersion(t *testing.T) { type test struct { description string baseVersion string minorVersion string expected []string } tests := []test{ { description: "Testing inputting version with 2 digit patch values", baseVersion: "0.16.15", minorVersion: "1.12.23", expected: []string{"0.16.0", "1.12"}, }, { description: "Testing inputting versions with single digit patch values", baseVersion: "0.16.1", minorVersion: "1.12.1", expected: []string{"0.16.0", "1.12"}, }, } for _, tc := range tests { base, minor := tc.baseVersion, tc.minorVersion baseVersion, minorVersion := SetVersionToBaseAndMinorVersion(&base, &minor) if baseVersion != tc.expected[0] { t.Errorf("test was %s for base version, but was expected to be %s", baseVersion, tc.expected[0]) } if minorVersion != tc.expected[1] { t.Errorf("test was %s for minor version, but was expected to be %s", minorVersion, tc.expected[1]) } } }