forked from kyma-project/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for installation/cluster (kyma-project#519)
Signed-off-by: Borja Clemente <[email protected]>
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package installation | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
k8sTesting "k8s.io/client-go/testing" | ||
|
||
"github.com/kyma-project/cli/internal/kube/mocks" | ||
"github.com/stretchr/testify/require" | ||
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 TestGetClusterInfoFromConfigMap(t *testing.T) { | ||
kymaMock := &mocks.KymaKube{} | ||
|
||
// Happy path | ||
k8sMock := fake.NewSimpleClientset( | ||
&v1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kyma-cluster-info", | ||
Namespace: "kube-system", | ||
}, | ||
Data: map[string]string{ | ||
"isLocal": "true", | ||
"provider": "nimbus", | ||
"profile": "test", | ||
"localIP": "0.0.0.0", | ||
"localVMDriver": "hyperfake", | ||
}, | ||
}, | ||
) | ||
kymaMock.On("Static").Return(k8sMock).Once() | ||
|
||
ci, err := GetClusterInfoFromConfigMap(kymaMock) | ||
require.NoError(t, err, "Test case: Happy path") | ||
require.Equal(t, ci.Provider, "nimbus", "Test case: Happy path") | ||
require.Equal(t, ci.LocalIP, "0.0.0.0", "Test case: Happy path") | ||
require.Equal(t, ci.LocalVMDriver, "hyperfake", "Test case: Happy path") | ||
require.Equal(t, ci.Profile, "test", "Test case: Happy path") | ||
require.True(t, ci.IsLocal, "Test case: Happy path") | ||
|
||
// corrupted cluster info | ||
k8sMock = fake.NewSimpleClientset( | ||
&v1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kyma-cluster-info", | ||
Namespace: "kube-system", | ||
}, | ||
Data: map[string]string{ | ||
"isLocal": "dwjkefwb·$%·$hdfbsjd234", | ||
}, | ||
}, | ||
) | ||
kymaMock.On("Static").Return(k8sMock).Once() | ||
|
||
ci, err = GetClusterInfoFromConfigMap(kymaMock) | ||
require.NoError(t, err, "Test case: Cluster info not found") | ||
require.False(t, ci.IsLocal, "Test case: Corrupted cluster info") | ||
|
||
// cluster info not found | ||
k8sMock = fake.NewSimpleClientset() | ||
kymaMock.On("Static").Return(k8sMock).Once() | ||
|
||
ci, err = GetClusterInfoFromConfigMap(kymaMock) | ||
require.NoError(t, err, "Test case: Cluster info not found") | ||
require.Equal(t, ci.Provider, "", "Test case: Cluster info not found") | ||
require.Equal(t, ci.LocalIP, "", "Test case: Cluster info not found") | ||
require.Equal(t, ci.LocalVMDriver, "", "Test case: Cluster info not found") | ||
require.Equal(t, ci.Profile, "", "Test case: Cluster info not found") | ||
require.False(t, ci.IsLocal, "Test case: Cluster info not found") | ||
|
||
// error getting cluster info | ||
k8sMock = fake.NewSimpleClientset() | ||
k8sMock.PrependReactor("get", "configmaps", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) { | ||
return true, nil, errors.New("Something went wrong") | ||
}) | ||
kymaMock.On("Static").Return(k8sMock).Once() | ||
|
||
ci, err = GetClusterInfoFromConfigMap(kymaMock) | ||
require.Error(t, err, "Test case: Error getting cluster info") | ||
} |