Skip to content

Commit

Permalink
add unit test for sem version delete
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Mar 29, 2024
1 parent 5bb7d51 commit 19d20eb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 34 deletions.
67 changes: 46 additions & 21 deletions adapter/internal/discovery/xds/semantic_versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func updateSemanticVersioningInMapForUpdateAPI(org string, apiRangeIdentifiers m
vhost, _ := ExtractVhostFromAPIIdentifier(apiRangeIdentifier)
if _, ok := currentAPISemVersion[GetMajorVersionRange(*semVersion)]; !ok {
currentAPISemVersion[GetMajorVersionRange(*semVersion)] = *semVersion
} else if currentAPISemVersion[GetMajorVersionRange(*semVersion)].Compare(*semVersion) {
} else if !semVersion.Compare(currentAPISemVersion[GetMajorVersionRange(*semVersion)]) {
version := currentAPISemVersion[GetMajorVersionRange(*semVersion)]
currentAPISemVersion[GetMajorVersionRange(*semVersion)] = *semVersion
oldVersion = &oldSemVersion{
Expand All @@ -104,7 +104,7 @@ func updateSemanticVersioningInMapForUpdateAPI(org string, apiRangeIdentifiers m
}
if _, ok := currentAPISemVersion[GetMinorVersionRange(*semVersion)]; !ok {
currentAPISemVersion[GetMinorVersionRange(*semVersion)] = *semVersion
} else if currentAPISemVersion[GetMinorVersionRange(*semVersion)].Compare(*semVersion) {
} else if !semVersion.Compare(currentAPISemVersion[GetMinorVersionRange(*semVersion)]) {
version := currentAPISemVersion[GetMinorVersionRange(*semVersion)]
currentAPISemVersion[GetMinorVersionRange(*semVersion)] = *semVersion
if oldVersion != nil {
Expand All @@ -122,10 +122,10 @@ func updateSemanticVersioningInMapForUpdateAPI(org string, apiRangeIdentifiers m
}
}
}
updateOldRegex(org, oldSemVersions)
updateOldRegex(org, oldSemVersions, true)
}

func updateOldRegex(org string, oldSemVersions []oldSemVersion) {
func updateOldRegex(org string, oldSemVersions []oldSemVersion, remove bool) {
if len(oldSemVersions) < 1 {
return
}
Expand Down Expand Up @@ -159,20 +159,38 @@ func updateOldRegex(org string, oldSemVersions []oldSemVersion) {
for _, route := range api.routes {
regex := route.GetMatch().GetSafeRegex().GetRegex()
regexRewritePattern := route.GetRoute().GetRegexRewrite().GetPattern().GetRegex()
if updateMajor {
if remove && updateMajor {
regex = strings.Replace(regex, GetMajorMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion),
GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion), 1)
regexRewritePattern = strings.Replace(regexRewritePattern,
GetMajorMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion),
GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion), 1)
}
if updateMinor {
if remove && updateMinor {
regex = strings.Replace(regex, GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMinorSemVersion),
GetVersionMatchRegex(oldSelectedSemVersion.OldMinorSemVersion.Version), 1)
regexRewritePattern = strings.Replace(regexRewritePattern,
GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMinorSemVersion),
GetVersionMatchRegex(oldSelectedSemVersion.OldMinorSemVersion.Version), 1)
}
if !remove && updateMajor {
regex = strings.Replace(regex, GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion),
GetMajorMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion), 1)
regex = strings.Replace(regex, GetVersionMatchRegex(oldSelectedSemVersion.OldMajorSemVersion.Version),
GetMajorMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion), 1)
regexRewritePattern = strings.Replace(regexRewritePattern,
GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion),
GetMajorMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion), 1)
regexRewritePattern = strings.Replace(regexRewritePattern,
GetVersionMatchRegex(oldSelectedSemVersion.OldMajorSemVersion.Version),
GetMajorMinorVersionRangeRegex(oldSelectedSemVersion.OldMajorSemVersion), 1)
}
if !remove && updateMinor {
regex = strings.Replace(regex, GetVersionMatchRegex(oldSelectedSemVersion.OldMinorSemVersion.Version),
GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMinorSemVersion), 1)
regexRewritePattern = strings.Replace(regexRewritePattern, GetVersionMatchRegex(oldSelectedSemVersion.OldMinorSemVersion.Version),
GetMinorVersionRangeRegex(oldSelectedSemVersion.OldMinorSemVersion), 1)
}
pathSpecifier := &routev3.RouteMatch_SafeRegex{
SafeRegex: &matcherv3.RegexMatcher{
Regex: regex,
Expand All @@ -188,6 +206,7 @@ func updateOldRegex(org string, oldSemVersions []oldSemVersion) {

// updateSemanticVersioningInMap updates the latest version ranges of the APIs in the organization
func updateSemanticVersioningInMap(org string, apiRangeIdentifiers map[string]struct{}) {
oldSemVersionsMap := make(map[string]*oldSemVersion)
oldSemVersions := make([]oldSemVersion, 0)
// Iterate all the APIs in the API range
for vuuid, api := range orgAPIMap[org] {
Expand All @@ -213,40 +232,46 @@ func updateSemanticVersioningInMap(org string, apiRangeIdentifiers map[string]st
orgIDLatestAPIVersionMap[org][apiRangeIdentifier] = make(map[string]semantic_version.SemVersion)
orgIDLatestAPIVersionMap[org][apiRangeIdentifier][GetMajorVersionRange(*semVersion)] = *semVersion
orgIDLatestAPIVersionMap[org][apiRangeIdentifier][GetMinorVersionRange(*semVersion)] = *semVersion
oldSemVersionsMap[GetMajorVersionRange(*semVersion)] = &oldSemVersion{
Vhost: vhost,
APIName: apiName,
OldMajorSemVersion: semVersion,
OldMinorSemVersion: semVersion,
}
} else {
var oldVersion *oldSemVersion
var newVersion *oldSemVersion
if _, ok := currentAPISemVersion[GetMajorVersionRange(*semVersion)]; !ok {
currentAPISemVersion[GetMajorVersionRange(*semVersion)] = *semVersion
} else if currentAPISemVersion[GetMajorVersionRange(*semVersion)].Compare(*semVersion) {
version := currentAPISemVersion[GetMajorVersionRange(*semVersion)]
oldVersion = &oldSemVersion{
} else if !semVersion.Compare(currentAPISemVersion[GetMajorVersionRange(*semVersion)]) {
newVersion = &oldSemVersion{
Vhost: vhost,
APIName: apiName,
OldMajorSemVersion: &version,
OldMajorSemVersion: semVersion,
}
currentAPISemVersion[GetMajorVersionRange(*semVersion)] = *semVersion
oldSemVersionsMap[GetMajorVersionRange(*semVersion)] = newVersion
}
if _, ok := currentAPISemVersion[GetMinorVersionRange(*semVersion)]; !ok {
currentAPISemVersion[GetMinorVersionRange(*semVersion)] = *semVersion
} else if currentAPISemVersion[GetMinorVersionRange(*semVersion)].Compare(*semVersion) {
version := currentAPISemVersion[GetMinorVersionRange(*semVersion)]
if oldVersion != nil {
oldVersion.OldMinorSemVersion = &version
} else if !semVersion.Compare(currentAPISemVersion[GetMinorVersionRange(*semVersion)]) {
if newVersion != nil {
newVersion.OldMinorSemVersion = semVersion
} else {
oldVersion = &oldSemVersion{
newVersion = &oldSemVersion{
Vhost: vhost,
APIName: apiName,
OldMinorSemVersion: &version,
OldMinorSemVersion: semVersion,
}
oldSemVersionsMap[GetMinorVersionRange(*semVersion)] = newVersion
}
currentAPISemVersion[GetMinorVersionRange(*semVersion)] = *semVersion
}
if oldVersion != nil {
oldSemVersions = append(oldSemVersions, *oldVersion)
}
}
}
updateOldRegex(org, oldSemVersions)
for _, sem := range oldSemVersionsMap {
oldSemVersions = append(oldSemVersions, *sem)
}
updateOldRegex(org, oldSemVersions, false)
}

func updateSemRegexForNewAPI(adapterInternalAPI model.AdapterInternalAPI, routes []*routev3.Route, vhost string) {
Expand Down
52 changes: 42 additions & 10 deletions adapter/internal/discovery/xds/semantic_versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_type_matcherv3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
"github.com/stretchr/testify/assert"
"github.com/wso2/apk/adapter/config"
"github.com/wso2/apk/adapter/internal/oasparser/model"
semantic_version "github.com/wso2/apk/adapter/pkg/semanticversion"
Expand Down Expand Up @@ -468,11 +469,17 @@ func TestUpdateRoutingRulesOnAPIDelete(t *testing.T) {
apiID2.SetVersion("v1.0")
apiID2ResourcePath := "^/mock-api/v1\\.0/orders([/]{0,1})"

var apiID3 model.AdapterInternalAPI
apiID3.SetName("Mock API")
apiID3.UUID = "apiID3"
apiID3.SetVersion("v1.5")
apiID3ResourcePath := "^/mock-api/v1(?:\\.5)?/orders([/]{0,1})"
var apiID21 model.AdapterInternalAPI
apiID21.SetName("Mock API")
apiID21.UUID = "apiID21"
apiID21.SetVersion("v1.1")
apiID21ResourcePath := "^/mock-api/v1\\.1/orders([/]{0,1})"

var apiID25 model.AdapterInternalAPI
apiID25.SetName("Mock API")
apiID25.UUID = "apiID25"
apiID25.SetVersion("v1.5")
apiID25ResourcePath := "^/mock-api/v1(?:\\.5)?/orders([/]{0,1})"

orgAPIMap = map[string]map[string]*EnvoyInternalAPI{
"org3": {
Expand All @@ -486,9 +493,13 @@ func TestUpdateRoutingRulesOnAPIDelete(t *testing.T) {
adapterInternalAPI: &apiID2,
routes: generateRoutes(apiID2ResourcePath),
},
"gw.com:apiID3": &EnvoyInternalAPI{
adapterInternalAPI: &apiID3,
routes: generateRoutes(apiID3ResourcePath),
"gw.com:apiID21": &EnvoyInternalAPI{
adapterInternalAPI: &apiID21,
routes: generateRoutes(apiID21ResourcePath),
},
"gw.com:apiID25": &EnvoyInternalAPI{
adapterInternalAPI: &apiID25,
routes: generateRoutes(apiID25ResourcePath),
},
},
}
Expand All @@ -497,6 +508,8 @@ func TestUpdateRoutingRulesOnAPIDelete(t *testing.T) {
name string
organizationID string
apiIdentifier string
apiCheck string
expectedRegex string
api *model.AdapterInternalAPI
deleteVersion string
}{
Expand All @@ -506,13 +519,26 @@ func TestUpdateRoutingRulesOnAPIDelete(t *testing.T) {
apiIdentifier: "gw.com:Test API",
api: &apiID1,
deleteVersion: "v1.0",
apiCheck: "gw.com:apiID25",
expectedRegex: "^/mock-api/v1(?:\\.5)?/orders([/]{0,1})",
},
{
name: "Delete latest minor version",
name: "Delete latest minor version v1.5",
organizationID: "org4",
apiIdentifier: "gw.com:Mock API",
api: &apiID3,
api: &apiID25,
deleteVersion: "v1.5",
apiCheck: "gw.com:apiID21",
expectedRegex: "^/mock-api/v1(?:\\.1)?/orders([/]{0,1})",
},
{
name: "Delete latest minor version v1.1",
organizationID: "org4",
apiIdentifier: "gw.com:Mock API",
api: &apiID21,
deleteVersion: "v1.1",
apiCheck: "gw.com:apiID2",
expectedRegex: "^/mock-api/v1(?:\\.0)?/orders([/]{0,1})",
},
}

Expand All @@ -526,6 +552,12 @@ func TestUpdateRoutingRulesOnAPIDelete(t *testing.T) {
}
}
}
if tt.apiCheck != "" {
routes := orgAPIMap["org4"][tt.apiCheck].routes
assert.Equal(t, tt.expectedRegex, routes[0].GetMatch().GetSafeRegex().GetRegex(),
"Expected regex: %s, Got: %s",
)
}
})
}
}
Expand Down
5 changes: 2 additions & 3 deletions adapter/internal/discovery/xds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,6 @@ func PopulateInternalMaps(adapterInternalAPI *model.AdapterInternalAPI, labels,
for vHost := range vHosts {
tobeUpdatedAPIRangeIdentifiers[GenerateIdentifierForAPIWithoutVersion(vHost, apiName)] = struct{}{}
}
logger.LoggerXds.Errorf("Updating semantic versioning for API: %v", tobeUpdatedAPIRangeIdentifiers)
updateSemanticVersioningInMapForUpdateAPI(adapterInternalAPI.OrganizationID, tobeUpdatedAPIRangeIdentifiers, adapterInternalAPI)
}

Expand Down Expand Up @@ -589,9 +588,9 @@ func RemoveAPIFromAllInternalMaps(uuid string) map[string]struct{} {
apiRangeID := GenerateIdentifierForAPIWithoutVersion(vhost, envoyInternalAPI.adapterInternalAPI.GetTitle())
if _, exists := orgIDLatestAPIVersionMap[orgID]; exists {
if apiVersionMap, exists := orgIDLatestAPIVersionMap[orgID][apiRangeID]; exists {
for versionRange, latestVersion := range apiVersionMap {
for _, latestVersion := range apiVersionMap {
if latestVersion.Version == envoyInternalAPI.adapterInternalAPI.GetVersion() {
delete(orgIDLatestAPIVersionMap[orgID][apiRangeID], versionRange)
delete(orgIDLatestAPIVersionMap[orgID], apiRangeID)
tobeUpdatedAPIRangeIdentifiers[apiRangeID] = struct{}{}
}
}
Expand Down

0 comments on commit 19d20eb

Please sign in to comment.