This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add validation and tests for the proxy-url
Signed-off-by: Hector Fernandez <[email protected]>
- Loading branch information
Showing
7 changed files
with
216 additions
and
6 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,77 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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 ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"golang.org/x/net/context" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
var _ = Describe("KubefedCluster", func() { | ||
var ( | ||
key types.NamespacedName | ||
created, fetched *KubeFedCluster | ||
) | ||
|
||
BeforeEach(func() { | ||
|
||
}) | ||
|
||
AfterEach(func() { | ||
|
||
}) | ||
|
||
Context("Create API", func() { | ||
|
||
It("should create a kubefed cluster successfully", func() { | ||
|
||
created = &KubeFedCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: metav1.NamespaceDefault, | ||
}, | ||
Spec: KubeFedClusterSpec{ | ||
APIEndpoint: "https://my.example.com:80/path/to/endpoint", | ||
ProxyURL: "socks5://example.com", | ||
SecretRef: LocalSecretReference{ | ||
Name: "foo", | ||
}, | ||
}, | ||
} | ||
|
||
key, _ = client.ObjectKeyFromObject(created) | ||
|
||
By("creating an API obj") | ||
Expect(k8sClient.Create(context.TODO(), created)).To(Succeed()) | ||
|
||
fetched = &KubeFedCluster{} | ||
Expect(k8sClient.Get(context.TODO(), key, fetched)).To(Succeed()) | ||
Expect(fetched).To(Equal(created)) | ||
|
||
By("deleting the created object") | ||
Expect(k8sClient.Delete(context.TODO(), created)).To(Succeed()) | ||
Expect(k8sClient.Get(context.TODO(), key, created)).ToNot(Succeed()) | ||
}) | ||
|
||
}) | ||
|
||
}) |
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,65 @@ | ||
package v1beta1 | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/printer" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
var cfg *rest.Config | ||
var k8sClient client.Client | ||
var testEnv *envtest.Environment | ||
|
||
func TestAPIs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecsWithDefaultAndCustomReporters(t, | ||
"v1beta1 Suite", | ||
[]Reporter{printer.NewlineReporter{}}) | ||
} | ||
|
||
var _ = BeforeSuite(func(done Done) { | ||
logf.SetLogger(zap.New(func(o *zap.Options) { | ||
o.Development = true | ||
o.DestWritter = GinkgoWriter | ||
})) | ||
|
||
By("bootstrapping test environment") | ||
testEnv = &envtest.Environment{ | ||
CRDInstallOptions: envtest.CRDInstallOptions{ | ||
ErrorIfPathMissing: true, | ||
Paths: []string{ | ||
filepath.Join("..", "..", "..", "..", "charts", "kubefed", "charts", "controllermanager", "crds"), | ||
}, | ||
}, | ||
} | ||
|
||
err := SchemeBuilder.AddToScheme(scheme.Scheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
cfg, err = testEnv.Start() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(cfg).ToNot(BeNil()) | ||
|
||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(k8sClient).ToNot(BeNil()) | ||
|
||
close(done) | ||
}, 60) | ||
|
||
var _ = AfterSuite(func() { | ||
By("tearing down the test environment") | ||
err := testEnv.Stop() | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) |
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 |
---|---|---|
|
@@ -480,6 +480,56 @@ func TestValidateAPIEndpoint(t *testing.T) { | |
} | ||
} | ||
|
||
func TestProxyURL(t *testing.T) { | ||
tests := []struct { | ||
proxyURL string | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
proxyURL: "socks5://example.com", | ||
}, | ||
{ | ||
proxyURL: "https://example.com", | ||
}, | ||
{ | ||
proxyURL: "http://example.com", | ||
}, | ||
{ | ||
proxyURL: "socks6://example.com", | ||
expectedErrMsg: "proxyURL: Invalid value: \"socks6://example.com\": proxy URL scheme must be one of: [http, https, socks5]", | ||
}, | ||
{ | ||
proxyURL: "example.com", | ||
expectedErrMsg: "proxyURL: Invalid value: \"example.com\": proxy URL scheme must be one of: [http, https, socks5]", | ||
}, | ||
{ | ||
proxyURL: "[email protected]", | ||
expectedErrMsg: "proxyURL: Invalid value: \"[email protected]\": proxy URL scheme must be one of: [http, https, socks5]", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
errs := validateProxyURL(test.proxyURL, field.NewPath("proxyURL")) | ||
if len(errs) == 0 && test.expectedErrMsg == "" { | ||
continue | ||
} | ||
if len(errs) == 0 && test.expectedErrMsg != "" { | ||
t.Errorf("[%s] expected failure", test.expectedErrMsg) | ||
} else { | ||
matchedErr := false | ||
for _, err := range errs { | ||
if strings.Contains(err.Error(), test.expectedErrMsg) { | ||
matchedErr = true | ||
break | ||
} | ||
} | ||
if !matchedErr { | ||
t.Errorf("unexpected error: %v, expected: %q", errs, test.expectedErrMsg) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestValidateLocalSecretReference(t *testing.T) { | ||
testCases := []struct { | ||
secretName string | ||
|
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