Merge pull request #3065 from meyskens/add-unit-nsvalidation

Add unit tests for nameserver validation
This commit is contained in:
jetstack-bot 2020-07-03 11:06:06 +01:00 committed by GitHub
commit dfbbc628da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 1 deletions

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -20,3 +20,9 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["nameserver_test.go"],
embed = [":go_default_library"],
)

View File

@ -0,0 +1,103 @@
/*
Copyright 2020 The Jetstack cert-manager contributors.
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 util
import (
"testing"
)
func TestValidNameserver(t *testing.T) {
tests := []struct {
name string
nameserver string
want string
wantErr bool
}{
{
name: "IPv4 with no port should should return port 53",
nameserver: "8.8.8.8",
want: "8.8.8.8:53",
},
{
name: "IPv4 with : but no port number should return port 53",
nameserver: "8.8.8.8:",
want: "8.8.8.8:53",
},
{
name: "IPv4 with port number should return the same",
nameserver: "8.8.8.8:5353",
want: "8.8.8.8:5353",
},
{
name: "IPv6 with no port should should return port 53",
nameserver: "[2001:db8::1]",
want: "[2001:db8::1]:53",
},
{
name: "IPv6 with : but no port number should return port 53",
nameserver: "[2001:db8::1]:",
want: "[2001:db8::1]:53",
},
{
name: "IPv6 with port number should return the same",
nameserver: "[2001:db8::1]:5353",
want: "[2001:db8::1]:5353",
},
{
name: "DNS name with no port should should return port 53",
nameserver: "nameserver.com",
want: "nameserver.com:53",
},
{
name: "DNS name with : but no port number should return port 53",
nameserver: "nameserver.com:",
want: "nameserver.com:53",
},
{
name: "DNS name with port number should return the same",
nameserver: "nameserver.com:5353",
want: "nameserver.com:5353",
},
{
name: "Non unenclosed IPv6 should error",
nameserver: "2001:db8::1:5353",
wantErr: true,
},
{
name: "Port only should error",
nameserver: ":53",
wantErr: true,
},
{
name: "Empty nameserver should error",
nameserver: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ValidNameserver(tt.nameserver)
if (err != nil) != tt.wantErr {
t.Errorf("ValidNameserver() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ValidNameserver() got = %v, want %v", got, tt.want)
}
})
}
}