-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: client.go file helper methods (#360)
* refactor: client.go file helper methods Refactored the existing methods and functions to be unit testable. Also made some methods that do not use the struct as generic functions. The changes were primarily an effort to add unit test coverage. * refactor: more testable read file function * test: new nutanixcluster types unit tests * test: additional test cases for errors * fixup! refactor: client.go file helper methods * fixup! refactor: client.go file helper methods * fixup! refactor: more testable read file function * fixup! refactor: client.go file helper methods
- Loading branch information
Showing
13 changed files
with
1,079 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright 2024 Nutanix | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/nutanix-cloud-native/prism-go-client/environment/credentials" | ||
) | ||
|
||
func TestGetCredentialRefForCluster(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
nutanixCluster *NutanixCluster | ||
expectedCredentialsRef *credentials.NutanixCredentialReference | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "all info is set", | ||
nutanixCluster: &NutanixCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: corev1.NamespaceDefault, | ||
}, | ||
Spec: NutanixClusterSpec{ | ||
PrismCentral: &credentials.NutanixPrismEndpoint{ | ||
Address: "address", | ||
Port: 9440, | ||
CredentialRef: &credentials.NutanixCredentialReference{ | ||
Kind: credentials.SecretKind, | ||
Name: "creds", | ||
Namespace: corev1.NamespaceDefault, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedCredentialsRef: &credentials.NutanixCredentialReference{ | ||
Kind: credentials.SecretKind, | ||
Name: "creds", | ||
Namespace: corev1.NamespaceDefault, | ||
}, | ||
}, | ||
{ | ||
name: "prismCentralInfo is nil, should not fail", | ||
nutanixCluster: &NutanixCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: corev1.NamespaceDefault, | ||
}, | ||
Spec: NutanixClusterSpec{}, | ||
}, | ||
}, | ||
{ | ||
name: "CredentialRef kind is not kind Secret, should not fail", | ||
nutanixCluster: &NutanixCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: corev1.NamespaceDefault, | ||
}, | ||
Spec: NutanixClusterSpec{ | ||
PrismCentral: &credentials.NutanixPrismEndpoint{ | ||
CredentialRef: &credentials.NutanixCredentialReference{ | ||
Kind: "unknown", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "prismCentralInfo is not nil but CredentialRef is nil, should fail", | ||
nutanixCluster: &NutanixCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: corev1.NamespaceDefault, | ||
}, | ||
Spec: NutanixClusterSpec{ | ||
PrismCentral: &credentials.NutanixPrismEndpoint{ | ||
Address: "address", | ||
}, | ||
}, | ||
}, | ||
expectedErr: fmt.Errorf("credentialRef must be set on prismCentral attribute for cluster test in namespace default"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt // Capture range variable. | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
ref, err := tt.nutanixCluster.GetPrismCentralCredentialRef() | ||
assert.Equal(t, tt.expectedCredentialsRef, ref) | ||
assert.Equal(t, tt.expectedErr, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.