|
9 | 9 | "testing" |
10 | 10 |
|
11 | 11 | clustermgmtv4 "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/config" |
| 12 | + clustermgmtv4errors "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/error" |
12 | 13 | "github.com/stretchr/testify/assert" |
13 | 14 | "github.com/stretchr/testify/require" |
14 | 15 | "k8s.io/utils/ptr" |
@@ -405,10 +406,9 @@ func TestStorageContainerCheck(t *testing.T) { |
405 | 406 | return resp, nil |
406 | 407 | }, |
407 | 408 | }, |
408 | | - expectedAllowed: false, |
409 | | - expectedError: true, |
410 | | - expectedCauseMessage: "failed to check if storage container named \"missing-container\" exists:" + |
411 | | - " no storage container named \"missing-container\" found on cluster named", |
| 409 | + expectedAllowed: false, |
| 410 | + expectedError: false, |
| 411 | + expectedCauseMessage: "storage container \"missing-container\" not found on cluster \"test-cluster\"", |
412 | 412 | }, |
413 | 413 | { |
414 | 414 | name: "multiple storage containers found", |
@@ -483,10 +483,9 @@ func TestStorageContainerCheck(t *testing.T) { |
483 | 483 | return resp, nil |
484 | 484 | }, |
485 | 485 | }, |
486 | | - expectedAllowed: false, |
487 | | - expectedError: true, |
488 | | - expectedCauseMessage: "failed to check if storage container named \"duplicate-container\" exists:" + |
489 | | - " multiple storage containers found with name", |
| 486 | + expectedAllowed: false, |
| 487 | + expectedError: false, |
| 488 | + expectedCauseMessage: "multiple storage containers named \"duplicate-container\" found on cluster \"test-cluster\"", |
490 | 489 | }, |
491 | 490 | { |
492 | 491 | name: "successful storage container check", |
@@ -601,8 +600,8 @@ func TestStorageContainerCheck(t *testing.T) { |
601 | 600 | }, |
602 | 601 | expectedAllowed: false, |
603 | 602 | expectedError: true, |
604 | | - expectedCauseMessage: "failed to check if storage container named \"valid-container\" exists:" + |
605 | | - " failed to get cluster: API error", |
| 603 | + expectedCauseMessage: "failed to check if storage container \"valid-container\" exists: " + |
| 604 | + "failed to get cluster \"test-cluster\": API error", |
606 | 605 | }, |
607 | 606 | { |
608 | 607 | name: "error listing storage containers", |
@@ -667,11 +666,12 @@ func TestStorageContainerCheck(t *testing.T) { |
667 | 666 | }, |
668 | 667 | expectedAllowed: false, |
669 | 668 | expectedError: true, |
670 | | - expectedCauseMessage: "failed to check if storage container named \"valid-container\" exists:" + |
671 | | - " failed to list storage containers: API error listing containers", |
| 669 | + // preflight.Cause {Message: "failed to check if storage container \"valid-container\" exists in cluster \"test-cluster\": API error listing containers", Field: "test.field.path"} |
| 670 | + expectedCauseMessage: "failed to check if storage container \"valid-container\" exists in cluster " + |
| 671 | + "\"test-cluster\": API error listing containers", |
672 | 672 | }, |
673 | 673 | { |
674 | | - name: "invalid response data type", |
| 674 | + name: "error response from ListStorageContainers", |
675 | 675 | nodeSpec: &carenv1.NutanixNodeSpec{ |
676 | 676 | MachineDetails: carenv1.NutanixMachineDetails{ |
677 | 677 | Cluster: capxv1.NutanixResourceIdentifier{ |
@@ -728,18 +728,20 @@ func TestStorageContainerCheck(t *testing.T) { |
728 | 728 | *clustermgmtv4.ListStorageContainersApiResponse, |
729 | 729 | error, |
730 | 730 | ) { |
731 | | - // Return a non-nil response but with nil Data or wrong type to simulate data conversion error |
732 | | - return &clustermgmtv4.ListStorageContainersApiResponse{ |
733 | | - ObjectType_: ptr.To("wrong-data-type"), |
734 | | - }, nil |
| 731 | + resp := &clustermgmtv4.ListStorageContainersApiResponse{} |
| 732 | + err := resp.SetData(*clustermgmtv4errors.NewErrorResponse()) |
| 733 | + require.NoError(t, err) |
| 734 | + return resp, nil |
735 | 735 | }, |
736 | 736 | }, |
737 | | - expectedAllowed: false, |
738 | | - expectedError: true, |
739 | | - expectedCauseMessage: "failed to check if storage container named \"valid-container\" exists: failed to find a matching storage container", //nolint:lll // long error message |
| 737 | + expectedAllowed: false, |
| 738 | + expectedError: true, |
| 739 | + expectedCauseMessage: "failed to check if storage container \"valid-container\" exists in cluster " + |
| 740 | + "\"test-cluster\": failed to get data returned by ListStorageContainers" + |
| 741 | + "(filter=\"name eq 'valid-container' and clusterExtId eq 'cluster-uuid-123'\")", |
740 | 742 | }, |
741 | 743 | { |
742 | | - name: "nil response data type", |
| 744 | + name: "nil data from ListStorageContainers", |
743 | 745 | nodeSpec: &carenv1.NutanixNodeSpec{ |
744 | 746 | MachineDetails: carenv1.NutanixMachineDetails{ |
745 | 747 | Cluster: capxv1.NutanixResourceIdentifier{ |
@@ -796,15 +798,12 @@ func TestStorageContainerCheck(t *testing.T) { |
796 | 798 | *clustermgmtv4.ListStorageContainersApiResponse, |
797 | 799 | error, |
798 | 800 | ) { |
799 | | - // Return a non-nil response but with nil Data or wrong type to simulate data conversion error |
800 | | - return &clustermgmtv4.ListStorageContainersApiResponse{ |
801 | | - Data: nil, |
802 | | - }, nil |
| 801 | + return &clustermgmtv4.ListStorageContainersApiResponse{}, nil |
803 | 802 | }, |
804 | 803 | }, |
805 | 804 | expectedAllowed: false, |
806 | | - expectedError: true, |
807 | | - expectedCauseMessage: "failed to check if storage container named \"valid-container\" exists: failed to find a matching storage container", //nolint:lll // long error message |
| 805 | + expectedError: false, |
| 806 | + expectedCauseMessage: "storage container \"valid-container\" not found on cluster \"test-cluster\"", |
808 | 807 | }, |
809 | 808 | { |
810 | 809 | name: "multiple storage class configs with success", |
@@ -982,17 +981,16 @@ func TestGetCluster(t *testing.T) { |
982 | 981 | errorContains: "API error", |
983 | 982 | }, |
984 | 983 | { |
985 | | - name: "get cluster by UUID - invalid response data", |
| 984 | + name: "get cluster by UUID - error response", |
986 | 985 | clusterIdentifier: &capxv1.NutanixResourceIdentifier{ |
987 | 986 | Type: capxv1.NutanixIdentifierUUID, |
988 | 987 | UUID: ptr.To("test-uuid-invalid"), |
989 | 988 | }, |
990 | 989 | client: &mocknclient{ |
991 | 990 | getClusterByIdFunc: func(id *string) (*clustermgmtv4.GetClusterApiResponse, error) { |
992 | | - // Return an invalid data type |
993 | | - resp := &clustermgmtv4.GetClusterApiResponse{ |
994 | | - ObjectType_: ptr.To("wrong-data-type"), |
995 | | - } |
| 991 | + resp := &clustermgmtv4.GetClusterApiResponse{} |
| 992 | + err := resp.SetData(*clustermgmtv4errors.NewErrorResponse()) |
| 993 | + require.NoError(t, err) |
996 | 994 | return resp, nil |
997 | 995 | }, |
998 | 996 | }, |
@@ -1083,6 +1081,33 @@ func TestGetCluster(t *testing.T) { |
1083 | 1081 | expectError: true, |
1084 | 1082 | errorContains: "no clusters were returned", |
1085 | 1083 | }, |
| 1084 | + { |
| 1085 | + name: "get cluster by name - error response", |
| 1086 | + clusterIdentifier: &capxv1.NutanixResourceIdentifier{ |
| 1087 | + Type: capxv1.NutanixIdentifierName, |
| 1088 | + Name: ptr.To("test-cluster-nil"), |
| 1089 | + }, |
| 1090 | + client: &mocknclient{ |
| 1091 | + listClustersFunc: func(page, |
| 1092 | + limit *int, |
| 1093 | + filter, |
| 1094 | + orderby, |
| 1095 | + apply, |
| 1096 | + select_ *string, |
| 1097 | + args ...map[string]interface{}, |
| 1098 | + ) ( |
| 1099 | + *clustermgmtv4.ListClustersApiResponse, |
| 1100 | + error, |
| 1101 | + ) { |
| 1102 | + resp := &clustermgmtv4.ListClustersApiResponse{} |
| 1103 | + err := resp.SetData(*clustermgmtv4errors.NewErrorResponse()) |
| 1104 | + require.NoError(t, err) |
| 1105 | + return resp, nil |
| 1106 | + }, |
| 1107 | + }, |
| 1108 | + expectError: true, |
| 1109 | + errorContains: "failed to get data returned by ListClusters", |
| 1110 | + }, |
1086 | 1111 | { |
1087 | 1112 | name: "get cluster by name - nil data", |
1088 | 1113 | clusterIdentifier: &capxv1.NutanixResourceIdentifier{ |
|
0 commit comments