From aa3881a7336796154f018fb32fb7d6129b5e1085 Mon Sep 17 00:00:00 2001 From: Maartje Eyskens Date: Fri, 3 Jul 2020 10:19:31 +0200 Subject: [PATCH 1/2] Add unit tests for nameserver validation Signed-off-by: Maartje Eyskens --- .../certmanager/validation/util/BUILD.bazel | 8 +- .../validation/util/nameserver_test.go | 87 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 pkg/internal/apis/certmanager/validation/util/nameserver_test.go diff --git a/pkg/internal/apis/certmanager/validation/util/BUILD.bazel b/pkg/internal/apis/certmanager/validation/util/BUILD.bazel index 3e6b436f4..83677db3c 100644 --- a/pkg/internal/apis/certmanager/validation/util/BUILD.bazel +++ b/pkg/internal/apis/certmanager/validation/util/BUILD.bazel @@ -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"], +) diff --git a/pkg/internal/apis/certmanager/validation/util/nameserver_test.go b/pkg/internal/apis/certmanager/validation/util/nameserver_test.go new file mode 100644 index 000000000..fb9d8e4b7 --- /dev/null +++ b/pkg/internal/apis/certmanager/validation/util/nameserver_test.go @@ -0,0 +1,87 @@ +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) + } + }) + } +} From 2c60cee2033b88dfbfdf4ee5f5be47d23cbfd0f0 Mon Sep 17 00:00:00 2001 From: Maartje Eyskens Date: Fri, 3 Jul 2020 10:31:42 +0200 Subject: [PATCH 2/2] Add boilerplate Signed-off-by: Maartje Eyskens --- .../validation/util/nameserver_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/internal/apis/certmanager/validation/util/nameserver_test.go b/pkg/internal/apis/certmanager/validation/util/nameserver_test.go index fb9d8e4b7..4c92d1d14 100644 --- a/pkg/internal/apis/certmanager/validation/util/nameserver_test.go +++ b/pkg/internal/apis/certmanager/validation/util/nameserver_test.go @@ -1,3 +1,19 @@ +/* +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 (