Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Shriram Sharma <shriram_sharma@intuit.com>
  • Loading branch information
shriramsharma committed Aug 30, 2024
1 parent 99785b0 commit 80ceea9
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
6 changes: 6 additions & 0 deletions admiral/pkg/clusters/virtualservice_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ func getMeshHTTPPortForRollout(ports map[string]map[string]uint32) (uint32, erro
if ports == nil {
return 0, fmt.Errorf("ports map is nil")
}
if len(ports) == 0 {
return 0, fmt.Errorf("ports map is empty")
}
rolloutPorts, ok := ports[common.Rollout]
if !ok {
return 0, fmt.Errorf("rollout ports not found")
Expand All @@ -598,6 +601,9 @@ func getMeshHTTPPortForDeployment(ports map[string]map[string]uint32) (uint32, e
if ports == nil {
return 0, fmt.Errorf("ports map is nil")
}
if len(ports) == 0 {
return 0, fmt.Errorf("ports map is empty")
}
deploymentPorts, ok := ports[common.Deployment]
if !ok {
return 0, fmt.Errorf("deployment ports not found")
Expand Down
152 changes: 152 additions & 0 deletions admiral/pkg/clusters/virtualservice_routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1836,3 +1836,155 @@ func TestGetBaseVirtualServiceForIngress(t *testing.T) {
}

}

func TestGetMeshHTTPPortForRollout(t *testing.T) {

testCases := []struct {
name string
ports map[string]map[string]uint32
expectedError error
expectedPort uint32
}{
{
name: "Given a nil ports map, " +
"When getMeshHTTPPortForRollout is invoked, " +
"Then it should return an error",
expectedError: fmt.Errorf("ports map is nil"),
},
{
name: "Given a empty ports map, " +
"When getMeshHTTPPortForRollout is invoked, " +
"Then it should return an error",
ports: make(map[string]map[string]uint32),
expectedError: fmt.Errorf("ports map is empty"),
},
{
name: "Given a ports map with no valid rollout port " +
"When getMeshHTTPPortForRollout is invoked, " +
"Then it should return an error",
ports: map[string]map[string]uint32{
common.Deployment: {},
},
expectedError: fmt.Errorf("rollout ports not found"),
},
{
name: "Given a ports map with invalid port " +
"When getMeshHTTPPortForRollout is invoked, " +
"Then it should return an error",
ports: map[string]map[string]uint32{
common.Rollout: {"http": 0},
},
expectedError: fmt.Errorf("no valid port found for rollout"),
},
{
name: "Given a ports map with valid port " +
"When getMeshHTTPPortForRollout is invoked, " +
"Then it should return the port",
ports: map[string]map[string]uint32{
common.Rollout: {"http": 8080},
},
expectedError: nil,
expectedPort: 8080,
},
{
name: "Given a ports map with multiple ports " +
"When getMeshHTTPPortForRollout is invoked, " +
"Then it should return the first port",
ports: map[string]map[string]uint32{
common.Rollout: {"http2": 8090, "http": 8080},
},
expectedError: nil,
expectedPort: 8090,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual, err := getMeshHTTPPortForRollout(tc.ports)
if tc.expectedError != nil {
require.NotNil(t, err)
require.Equal(t, tc.expectedError.Error(), err.Error())
} else {
require.Nil(t, err)
require.Equal(t, tc.expectedPort, actual)
}
})
}

}

func TestGetMeshHTTPPortForDeployment(t *testing.T) {

testCases := []struct {
name string
ports map[string]map[string]uint32
expectedError error
expectedPort uint32
}{
{
name: "Given a nil ports map, " +
"When getMeshHTTPPortForDeployment is invoked, " +
"Then it should return an error",
expectedError: fmt.Errorf("ports map is nil"),
},
{
name: "Given a empty ports map, " +
"When getMeshHTTPPortForDeployment is invoked, " +
"Then it should return an error",
ports: make(map[string]map[string]uint32),
expectedError: fmt.Errorf("ports map is empty"),
},
{
name: "Given a ports map with no valid rollout port " +
"When getMeshHTTPPortForDeployment is invoked, " +
"Then it should return an error",
ports: map[string]map[string]uint32{
common.Rollout: {},
},
expectedError: fmt.Errorf("deployment ports not found"),
},
{
name: "Given a ports map with invalid port " +
"When getMeshHTTPPortForDeployment is invoked, " +
"Then it should return an error",
ports: map[string]map[string]uint32{
common.Deployment: {"http": 0},
},
expectedError: fmt.Errorf("no valid port found for deployment"),
},
{
name: "Given a ports map with valid port " +
"When getMeshHTTPPortForDeployment is invoked, " +
"Then it should return the port",
ports: map[string]map[string]uint32{
common.Deployment: {"http": 8080},
},
expectedError: nil,
expectedPort: 8080,
},
{
name: "Given a ports map with multiple ports " +
"When getMeshHTTPPortForDeployment is invoked, " +
"Then it should return the first port",
ports: map[string]map[string]uint32{
common.Deployment: {"http2": 8090, "http": 8080},
},
expectedError: nil,
expectedPort: 8090,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual, err := getMeshHTTPPortForDeployment(tc.ports)
if tc.expectedError != nil {
require.NotNil(t, err)
require.Equal(t, tc.expectedError.Error(), err.Error())
} else {
require.Nil(t, err)
require.Equal(t, tc.expectedPort, actual)
}
})
}

}

0 comments on commit 80ceea9

Please sign in to comment.