From 3306549749f423911e24e70a33551758f8660a1b Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Thu, 9 Apr 2020 10:11:44 -0400 Subject: [PATCH] Add general contract test for IsEmpty This was the root bug that was causing the over-logging on GCE. --- .../providers/aws/route53/route53_test.go | 8 +++ .../providers/coredns/coredns_test.go | 8 +++ .../google/clouddns/clouddns_test.go | 8 +++ .../providers/openstack/designate/BUILD.bazel | 9 ++- .../openstack/designate/designate_test.go | 30 ++++++++ dnsprovider/pkg/dnsprovider/tests/BUILD.bazel | 5 +- dnsprovider/pkg/dnsprovider/tests/contract.go | 68 +++++++++++++++++++ pkg/resources/digitalocean/dns/dns_test.go | 4 +- 8 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 dnsprovider/pkg/dnsprovider/providers/openstack/designate/designate_test.go create mode 100644 dnsprovider/pkg/dnsprovider/tests/contract.go diff --git a/dnsprovider/pkg/dnsprovider/providers/aws/route53/route53_test.go b/dnsprovider/pkg/dnsprovider/providers/aws/route53/route53_test.go index 00a7f919fef55..ba165e9bc3c37 100644 --- a/dnsprovider/pkg/dnsprovider/providers/aws/route53/route53_test.go +++ b/dnsprovider/pkg/dnsprovider/providers/aws/route53/route53_test.go @@ -298,3 +298,11 @@ func TestResourceRecordSetsDifferentTypes(t *testing.T) { zone := firstZone(t) tests.CommonTestResourceRecordSetsDifferentTypes(t, zone) } + +// TestContract verifies the general interface contract +func TestContract(t *testing.T) { + zone := firstZone(t) + sets := rrs(t, zone) + + tests.TestContract(t, sets) +} diff --git a/dnsprovider/pkg/dnsprovider/providers/coredns/coredns_test.go b/dnsprovider/pkg/dnsprovider/providers/coredns/coredns_test.go index 0fe8938986f98..824cb861c7689 100644 --- a/dnsprovider/pkg/dnsprovider/providers/coredns/coredns_test.go +++ b/dnsprovider/pkg/dnsprovider/providers/coredns/coredns_test.go @@ -263,3 +263,11 @@ func TestResourceRecordSetsDifferentTypes(t *testing.T) { zone := firstZone(t) tests.CommonTestResourceRecordSetsDifferentTypes(t, zone) } + +// TestContract verifies the general interface contract +func TestContract(t *testing.T) { + zone := firstZone(t) + sets := rrs(t, zone) + + tests.TestContract(t, sets) +} diff --git a/dnsprovider/pkg/dnsprovider/providers/google/clouddns/clouddns_test.go b/dnsprovider/pkg/dnsprovider/providers/google/clouddns/clouddns_test.go index ef82c2f765242..c834083f4a0f4 100644 --- a/dnsprovider/pkg/dnsprovider/providers/google/clouddns/clouddns_test.go +++ b/dnsprovider/pkg/dnsprovider/providers/google/clouddns/clouddns_test.go @@ -275,3 +275,11 @@ func TestResourceRecordSetsDifferentTypes(t *testing.T) { zone := firstZone(t) tests.CommonTestResourceRecordSetsDifferentTypes(t, zone) } + +// TestContract verifies the general interface contract +func TestContract(t *testing.T) { + zone := firstZone(t) + sets := rrs(t, zone) + + tests.TestContract(t, sets) +} diff --git a/dnsprovider/pkg/dnsprovider/providers/openstack/designate/BUILD.bazel b/dnsprovider/pkg/dnsprovider/providers/openstack/designate/BUILD.bazel index 44ed7d83d8ccc..b88f90a04b0c6 100644 --- a/dnsprovider/pkg/dnsprovider/providers/openstack/designate/BUILD.bazel +++ b/dnsprovider/pkg/dnsprovider/providers/openstack/designate/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", @@ -25,3 +25,10 @@ go_library( "//vendor/k8s.io/klog:go_default_library", ], ) + +go_test( + name = "go_default_test", + srcs = ["designate_test.go"], + embed = [":go_default_library"], + deps = ["//dnsprovider/pkg/dnsprovider/tests:go_default_library"], +) diff --git a/dnsprovider/pkg/dnsprovider/providers/openstack/designate/designate_test.go b/dnsprovider/pkg/dnsprovider/providers/openstack/designate/designate_test.go new file mode 100644 index 0000000000000..8fb4a3bd1c83a --- /dev/null +++ b/dnsprovider/pkg/dnsprovider/providers/openstack/designate/designate_test.go @@ -0,0 +1,30 @@ +/* +Copyright 2020 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 designate + +import ( + "testing" + + "k8s.io/kops/dnsprovider/pkg/dnsprovider/tests" +) + +// TestContract verifies the general interface contract +func TestContract(t *testing.T) { + sets := &ResourceRecordSets{} + + tests.TestContract(t, sets) +} diff --git a/dnsprovider/pkg/dnsprovider/tests/BUILD.bazel b/dnsprovider/pkg/dnsprovider/tests/BUILD.bazel index c89a65ce54462..16440c50e373c 100644 --- a/dnsprovider/pkg/dnsprovider/tests/BUILD.bazel +++ b/dnsprovider/pkg/dnsprovider/tests/BUILD.bazel @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = ["commontests.go"], + srcs = [ + "commontests.go", + "contract.go", + ], importpath = "k8s.io/kops/dnsprovider/pkg/dnsprovider/tests", visibility = ["//visibility:public"], deps = [ diff --git a/dnsprovider/pkg/dnsprovider/tests/contract.go b/dnsprovider/pkg/dnsprovider/tests/contract.go new file mode 100644 index 0000000000000..113421a43d6f2 --- /dev/null +++ b/dnsprovider/pkg/dnsprovider/tests/contract.go @@ -0,0 +1,68 @@ +/* +Copyright 2020 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 tests + +import ( + "testing" + + "k8s.io/kops/dnsprovider/pkg/dnsprovider" + "k8s.io/kops/dnsprovider/pkg/dnsprovider/rrstype" +) + +// TestContract verifies the general ResourceRecordChangeset contract +func TestContract(t *testing.T, rrsets dnsprovider.ResourceRecordSets) { + + { + changeset := rrsets.StartChangeset() + if !changeset.IsEmpty() { + t.Fatalf("expected new changeset to be empty") + } + + rrs := changeset.ResourceRecordSets().New("foo", []string{"192.168.0.1"}, 1, rrstype.A) + changeset.Add(rrs) + if changeset.IsEmpty() { + t.Fatalf("expected changeset not to be empty after add") + } + } + + { + changeset := rrsets.StartChangeset() + if !changeset.IsEmpty() { + t.Fatalf("expected new changeset to be empty") + } + + rrs := changeset.ResourceRecordSets().New("foo", []string{"192.168.0.1"}, 1, rrstype.A) + changeset.Remove(rrs) + if changeset.IsEmpty() { + t.Fatalf("expected changeset not to be empty after remove") + } + } + + { + changeset := rrsets.StartChangeset() + if !changeset.IsEmpty() { + t.Fatalf("expected new changeset to be empty") + } + + rrs := changeset.ResourceRecordSets().New("foo", []string{"192.168.0.1"}, 1, rrstype.A) + changeset.Upsert(rrs) + if changeset.IsEmpty() { + t.Fatalf("expected changeset not to be empty after upsert") + } + } + +} diff --git a/pkg/resources/digitalocean/dns/dns_test.go b/pkg/resources/digitalocean/dns/dns_test.go index 40b5aa1489747..a7fc68998ef5a 100644 --- a/pkg/resources/digitalocean/dns/dns_test.go +++ b/pkg/resources/digitalocean/dns/dns_test.go @@ -369,6 +369,8 @@ func TestNewResourceRecordSet(t *testing.T) { } func TestResourceRecordChangeset(t *testing.T) { + ctx := context.Background() + fake := &fakeDomainService{} fake.recordsFunc = func(ctx context.Context, domain string, listOpts *godo.ListOptions) ([]godo.DomainRecord, *godo.Response, error) { domainRecords := []godo.DomainRecord{ @@ -456,7 +458,7 @@ func TestResourceRecordChangeset(t *testing.T) { record = rrset.New("to-upsert", []string{"127.0.0.1"}, 3600, rrstype.A) changeset.Upsert(record) - err = changeset.Apply() + err = changeset.Apply(ctx) if err != nil { t.Errorf("error applying changeset: %v", err) }