-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shakti <[email protected]>
- Loading branch information
1 parent
fdafa93
commit ab48614
Showing
8 changed files
with
118 additions
and
59 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
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
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
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,88 @@ | ||
package istio | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestGetPrimaryClusterDynamicClient(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
namespace string | ||
existingSecrets []*v1.Secret | ||
expectedClusterId string | ||
}{ | ||
{ | ||
"TestNoPrimaryClusterSecret", | ||
metav1.NamespaceAll, | ||
nil, | ||
"", | ||
}, | ||
{ | ||
"TestPrimaryClusterSingleSecret", | ||
metav1.NamespaceAll, | ||
[]*v1.Secret{ | ||
makeSecret("secret0", "namespace0", "primary0", []byte("kubeconfig0-0")), | ||
}, | ||
"primary0", | ||
}, | ||
{ | ||
"TestPrimaryClusterMultipleSecrets", | ||
metav1.NamespaceAll, | ||
[]*v1.Secret{ | ||
makeSecret("secret0", "namespace0", "primary0", []byte("kubeconfig0-0")), | ||
makeSecret("secret1", "namespace1", "primary1", []byte("kubeconfig1-1")), | ||
}, | ||
"primary0", | ||
}, | ||
{ | ||
"TestPrimaryClusterNoSecretInNamespaceForNamespacedController", | ||
"argo-rollout-ns", | ||
[]*v1.Secret{ | ||
makeSecret("secret0", "namespace0", "primary0", []byte("kubeconfig0-0")), | ||
}, | ||
"", | ||
}, | ||
{ | ||
"TestPrimaryClusterSingleSecretInNamespaceForNamespacedController", | ||
"argo-rollout-ns", | ||
[]*v1.Secret{ | ||
makeSecret("secret0", "argo-rollout-ns", "primary0", []byte("kubeconfig0-0")), | ||
}, | ||
"primary0", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var existingObjs []runtime.Object | ||
for _, s := range tc.existingSecrets { | ||
existingObjs = append(existingObjs, s) | ||
} | ||
|
||
client := fake.NewSimpleClientset(existingObjs...) | ||
clusterId, _ := GetPrimaryClusterDynamicClient(client, tc.namespace) | ||
assert.Equal(t, tc.expectedClusterId, clusterId) | ||
}) | ||
} | ||
} | ||
|
||
func makeSecret(secret, namespace, clusterID string, kubeconfig []byte) *v1.Secret { | ||
return &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secret, | ||
Namespace: namespace, | ||
Labels: map[string]string{ | ||
PrimaryClusterSecretLabel: "true", | ||
}, | ||
}, | ||
Data: map[string][]byte{ | ||
clusterID: kubeconfig, | ||
}, | ||
} | ||
} |