Skip to content

Commit af092f4

Browse files
authored
UT Coverage Improvement (#7)
* Additional Units tests
1 parent ca733ae commit af092f4

31 files changed

+3279
-555
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ push:
6868
docker push ${IMAGE_PATH}
6969

7070
build-push: image push
71-
docker push ${IMAGE_PATH}
7271

7372
.PHONY: coverage
7473
coverage: test

pkg/auth/auth_service.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ import (
2222
"k8s.io/api/core/v1"
2323
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/client-go/kubernetes"
25-
"k8s.io/client-go/rest"
2625
"k8s.io/klog/v2"
2726

2827
"github.com/oracle/oci-native-ingress-controller/pkg/types"
2928
)
3029

3130
const httpClientTimeout = 20 * time.Second
3231

33-
func GetConfigurationProvider(ctx context.Context, opts types.IngressOpts) (common.ConfigurationProvider, error) {
34-
auth, err := RetrieveAuthConfig(ctx, opts, opts.LeaseLockNamespace)
32+
func GetConfigurationProvider(ctx context.Context, opts types.IngressOpts, client kubernetes.Interface) (common.ConfigurationProvider, error) {
33+
auth, err := RetrieveAuthConfig(ctx, opts, opts.LeaseLockNamespace, client)
3534
if err != nil {
36-
klog.Fatalf("Unable to handle authentication parameters", err)
35+
klog.Error("Unable to handle authentication parameters", err)
3736
return nil, err
3837
}
3938
return getConfProviderFromAuth(auth)
@@ -71,7 +70,7 @@ func setHTTPClientTimeout(
7170
}
7271
}
7372

74-
func RetrieveAuthConfig(ctx context.Context, opts types.IngressOpts, namespace string) (*types.Auth, error) {
73+
func RetrieveAuthConfig(ctx context.Context, opts types.IngressOpts, namespace string, client kubernetes.Interface) (*types.Auth, error) {
7574
authType := opts.AuthType
7675
principalType, err := types.MapToPrincipalType(authType)
7776
if err != nil {
@@ -86,27 +85,27 @@ func RetrieveAuthConfig(ctx context.Context, opts types.IngressOpts, namespace s
8685
authConfigSecretName := opts.AuthSecretName
8786

8887
// read it from k8s api
89-
secret, err := readK8sSecret(ctx, namespace, authConfigSecretName)
88+
secret, err := readK8sSecret(ctx, namespace, authConfigSecretName, client)
9089
if err != nil {
91-
klog.Fatalf("Error while reading secret from k8s api", err)
90+
klog.Error("Error while reading secret from k8s api", err)
9291
return nil, fmt.Errorf("error retrieving secret: %v", authConfigSecretName)
9392
}
9493

9594
klog.Infof("secret is retrieved from kubernetes api: %s", authConfigSecretName)
9695

9796
if len(secret.Data) == 0 || len(secret.Data["config"]) == 0 {
98-
klog.Fatalf("Empty Configuration is found in the secret %s", authConfigSecretName)
97+
klog.Error("Empty Configuration is found in the secret %s", authConfigSecretName)
9998
return nil, fmt.Errorf("auth config data is empty: %v", authConfigSecretName)
10099
}
101100
authCfg, err := ParseAuthConfig(secret, authConfigSecretName)
102101
if err != nil {
103-
klog.Fatalf("Missing auth config data: %s", authConfigSecretName)
102+
klog.Error("Missing auth config data: %s", authConfigSecretName)
104103
return nil, fmt.Errorf("missing auth config data: %v", err)
105104
}
106105

107106
err = authCfg.Validate()
108107
if err != nil {
109-
klog.Fatalf("Missing auth config data %s", authConfigSecretName)
108+
klog.Error("Missing auth config data %s", authConfigSecretName)
110109
return nil, fmt.Errorf("missing auth config data: %v", err)
111110
}
112111
auth.Config = *authCfg
@@ -117,40 +116,28 @@ func RetrieveAuthConfig(ctx context.Context, opts types.IngressOpts, namespace s
117116
func ParseAuthConfig(secret *v1.Secret, authConfigSecretName string) (*types.AuthConfig, error) {
118117
authYaml := &types.AuthConfigYaml{}
119118
err := yaml.Unmarshal(secret.Data["config"], &authYaml)
120-
if err != nil {
121-
klog.Fatalf("Invalid auth config data %s", authConfigSecretName)
119+
if err != nil || authYaml.Auth == nil {
120+
klog.Errorf("Invalid auth config data %s", authConfigSecretName)
122121
return nil, fmt.Errorf("invalid auth config data: %v", authConfigSecretName)
123122
}
124123

125124
if len(secret.Data["private-key"]) > 0 {
126125
authYaml.Auth["privateKey"] = string(secret.Data["private-key"])
127126
} else {
128-
klog.Fatalf("Invalid user auth private key %s", authConfigSecretName)
127+
klog.Errorf("Invalid user auth private key %s", authConfigSecretName)
129128
return nil, fmt.Errorf("invalid user auth config data: %v", authConfigSecretName)
130129
}
131130

132131
authCfgYaml, _ := yaml.Marshal(authYaml.Auth)
133132
authCfg := &types.AuthConfig{}
134133
err = yaml.Unmarshal(authCfgYaml, &authCfg)
135134
if err != nil {
136-
klog.Fatalf("Invalid auth config data %s", authConfigSecretName)
135+
klog.Errorf("Invalid auth config data %s", authConfigSecretName)
137136
return nil, fmt.Errorf("invalid auth config data: %v", authConfigSecretName)
138137
}
139138
return authCfg, nil
140139
}
141140

142-
func readK8sSecret(ctx context.Context, namespace string,
143-
secretName string) (*v1.Secret, error) {
144-
clusterCfg, err := rest.InClusterConfig()
145-
if err != nil {
146-
return &v1.Secret{}, fmt.Errorf("can not get cluster config. error: %v", err)
147-
}
148-
149-
clientSet, err := kubernetes.NewForConfig(clusterCfg)
150-
if err != nil {
151-
return &v1.Secret{}, fmt.Errorf("can not initialize kubernetes client. error: %v", err)
152-
}
153-
154-
k8client := clientSet.CoreV1()
155-
return k8client.Secrets(namespace).Get(ctx, secretName, metaV1.GetOptions{})
141+
func readK8sSecret(ctx context.Context, namespace string, secretName string, client kubernetes.Interface) (*v1.Secret, error) {
142+
return client.CoreV1().Secrets(namespace).Get(ctx, secretName, metaV1.GetOptions{})
156143
}

pkg/auth/auth_service_test.go

Lines changed: 98 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,92 @@ package auth
22

33
import (
44
"context"
5-
"encoding/base64"
65
"fmt"
6+
"net/http"
77
"testing"
88

99
. "github.com/onsi/gomega"
1010
"github.com/oracle/oci-native-ingress-controller/pkg/types"
11+
"github.com/oracle/oci-native-ingress-controller/pkg/util"
1112
v1 "k8s.io/api/core/v1"
12-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
fakeclientset "k8s.io/client-go/kubernetes/fake"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6ZXhhbXBsZQogIHRlbmFuY3k6IG9jaWQxLnRlbmFuY3kub2MxLi5hYWFhYWFhYV9leGFtcGxl"
1819
)
1920

21+
func setUp(secret *v1.Secret, setClient bool) *fakeclientset.Clientset {
22+
client := fakeclientset.NewSimpleClientset()
23+
if setClient {
24+
action := "get"
25+
resource := "secrets"
26+
obj := secret
27+
util.FakeClientGetCall(client, action, resource, obj)
28+
}
29+
return client
30+
}
31+
32+
func TestGetConfigurationProviderSuccess(t *testing.T) {
33+
RegisterTestingT(t)
34+
ctx := context.TODO()
35+
opts := types.IngressOpts{
36+
AuthType: "user",
37+
AuthSecretName: "oci-config",
38+
}
39+
configName := "config"
40+
privateKey := "private-key"
41+
secret := util.GetSampleSecret(configName, privateKey, data, PrivateKey)
42+
client := setUp(secret, true)
43+
44+
auth, err := GetConfigurationProvider(ctx, opts, client)
45+
Expect(auth != nil).Should(BeTrue())
46+
Expect(err).Should(BeNil())
47+
}
48+
49+
func TestGetConfigurationProviderFailSecret(t *testing.T) {
50+
RegisterTestingT(t)
51+
ctx := context.TODO()
52+
opts := types.IngressOpts{
53+
AuthType: "user",
54+
AuthSecretName: "oci-config",
55+
}
56+
secret := util.GetSampleSecret("test", "error", data, PrivateKey)
57+
58+
client := setUp(secret, false)
59+
auth, err := GetConfigurationProvider(ctx, opts, client)
60+
Expect(auth == nil).Should(BeTrue())
61+
Expect(err != nil).Should(BeTrue())
62+
Expect(err.Error()).Should(Equal("error retrieving secret: oci-config"))
63+
64+
client = setUp(secret, true)
65+
auth, err = GetConfigurationProvider(ctx, opts, client)
66+
Expect(auth == nil).Should(BeTrue())
67+
Expect(err != nil).Should(BeTrue())
68+
Expect(err.Error()).Should(Equal("auth config data is empty: oci-config"))
69+
70+
secret = util.GetSampleSecret("config", "error", data, PrivateKey)
71+
client = setUp(secret, true)
72+
auth, err = GetConfigurationProvider(ctx, opts, client)
73+
Expect(auth == nil).Should(BeTrue())
74+
Expect(err != nil).Should(BeTrue())
75+
Expect(err.Error()).Should(Equal("missing auth config data: invalid user auth config data: oci-config"))
76+
77+
secret = util.GetSampleSecret("configs", "error", data, PrivateKey)
78+
client = setUp(secret, true)
79+
auth, err = GetConfigurationProvider(ctx, opts, client)
80+
Expect(auth == nil).Should(BeTrue())
81+
Expect(err != nil).Should(BeTrue())
82+
Expect(err.Error()).Should(Equal("auth config data is empty: oci-config"))
83+
}
84+
2085
func TestRetrieveAuthConfigInstanceAuthType(t *testing.T) {
2186
RegisterTestingT(t)
2287
opts := types.IngressOpts{
2388
AuthType: "instance",
2489
}
25-
cfg, err := RetrieveAuthConfig(context.TODO(), opts, "test")
90+
cfg, err := RetrieveAuthConfig(context.TODO(), opts, "test", nil)
2691
Expect(err == nil).Should(BeTrue())
2792
Expect(cfg.Type).Should(Equal(types.Instance))
2893

@@ -33,15 +98,17 @@ func TestRetrieveAuthConfigInstanceAuthTypeTestRetrieveAuthConfigInvalidAuthType
3398
opts := types.IngressOpts{
3499
AuthType: authType,
35100
}
36-
_, err := RetrieveAuthConfig(context.TODO(), opts, "test")
101+
_, err := RetrieveAuthConfig(context.TODO(), opts, "test", nil)
37102
Expect(err != nil).Should(BeTrue())
38103
Expect(err.Error()).Should(Equal(fmt.Sprintf("invalid auth principal type, %s", authType)))
39104

40105
}
41106

42107
func TestParseAuthConfig(t *testing.T) {
43108
RegisterTestingT(t)
44-
secret := getSampleSecret()
109+
configName := "config"
110+
privateKey := "private-key"
111+
secret := util.GetSampleSecret(configName, privateKey, data, PrivateKey)
45112
authCfg, err := ParseAuthConfig(secret, "oci-config")
46113
Expect(err == nil).Should(BeTrue())
47114
Expect(authCfg.TenancyID).Should(Equal("ocid1.tenancy.oc1..aaaaaaaa_example"))
@@ -52,17 +119,30 @@ func TestParseAuthConfig(t *testing.T) {
52119
Expect(err == nil).Should(BeTrue())
53120
}
54121

55-
func getSampleSecret() *v1.Secret {
56-
dat, _ := base64.StdEncoding.DecodeString(data)
57-
secret := &v1.Secret{
58-
ObjectMeta: metav1.ObjectMeta{
59-
Namespace: "test",
60-
Name: "oci-config",
61-
},
62-
Data: map[string][]byte{
63-
"config": []byte(dat),
64-
"private-key": []byte(PrivateKey),
65-
},
66-
}
67-
return secret
122+
func TestParseAuthConfigWithError(t *testing.T) {
123+
RegisterTestingT(t)
124+
secret := util.GetSampleSecret("error", "", data, PrivateKey)
125+
_, err := ParseAuthConfig(secret, "oci-configs")
126+
Expect(err != nil).Should(BeTrue())
127+
Expect(err.Error()).Should(Equal("invalid auth config data: oci-configs"))
128+
129+
secret = util.GetSampleSecret("config", "", data, PrivateKey)
130+
_, err = ParseAuthConfig(secret, "oci-configs")
131+
Expect(err != nil).Should(BeTrue())
132+
Expect(err.Error()).Should(Equal("invalid user auth config data: oci-configs"))
133+
134+
}
135+
136+
func TestSetHTTPClientTimeout(t *testing.T) {
137+
RegisterTestingT(t)
138+
timeout := setHTTPClientTimeout(httpClientTimeout)
139+
Expect(timeout != nil).Should(Equal(true))
140+
dis, err := timeout(&http.Client{})
141+
Expect(dis).Should(Not(BeNil()))
142+
Expect(err).Should(BeNil())
143+
144+
dis, err = timeout(nil)
145+
Expect(dis).Should(BeNil())
146+
Expect(err).Should(Not(BeNil()))
147+
Expect(err.Error()).Should(Equal("unable to modify unknown HTTP client type"))
68148
}

0 commit comments

Comments
 (0)