external-dns/tests/integration/source_test.go
Ivan Ka 2c8b57eea4
refactor(test): deduplicate endpoint assertion helpers across source (#6299)
* refactor(test): deduplicate endpoint assertion helpers across source and integration tests

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>

* refactor(test): deduplicate endpoint assertion helpers across source

Co-authored-by: Michel Loiseleur <97035654+mloiseleur@users.noreply.github.com>

---------

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
Co-authored-by: Michel Loiseleur <97035654+mloiseleur@users.noreply.github.com>
2026-03-22 00:56:13 +05:30

86 lines
2.7 KiB
Go

/*
Copyright 2026 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 integration
import (
_ "embed"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/external-dns/internal/testutils"
"sigs.k8s.io/external-dns/tests/integration/toolkit"
)
var (
//go:embed scenarios/tests.yaml
testsYAML []byte
)
func mustLoadScenarios(t *testing.T) *toolkit.TestScenarios {
t.Helper()
testScenarios, err := toolkit.LoadScenarios(testsYAML)
require.NoError(t, err, "failed to load scenarios")
require.NotEmpty(t, testScenarios.Scenarios, "no scenarios found")
return testScenarios
}
func TestParseResources(t *testing.T) {
scenarios := mustLoadScenarios(t)
for _, scenario := range scenarios.Scenarios {
t.Run(scenario.Name, func(t *testing.T) {
parsed, err := toolkit.ParseResources(scenario.Resources)
require.NoError(t, err, "failed to parse resources")
totalParsed := len(parsed.Services) + len(parsed.Ingresses) + len(parsed.Pods) + len(parsed.EndpointSlices)
// Pods and EndpointSlices may be auto-generated from dependencies, so count
// only the explicitly declared resources when checking nothing was silently dropped.
explicitResources := 0
for _, r := range scenario.Resources {
explicitResources++
if r.Dependencies != nil && r.Dependencies.Pods != nil {
// Each Service with pod dependencies generates Pods + one EndpointSlice.
explicitResources += r.Dependencies.Pods.Replicas + 1
}
}
assert.Equal(t, explicitResources, totalParsed, "parsed resource count does not match declared resources")
})
}
}
func TestSourceIntegration(t *testing.T) {
scenarios := mustLoadScenarios(t)
for _, scenario := range scenarios.Scenarios {
t.Run(scenario.Name, func(t *testing.T) {
ctx := t.Context()
client, err := toolkit.LoadResources(ctx, scenario)
require.NoError(t, err, "failed to populate resources")
// Create wrapped source
wrappedSource, err := toolkit.CreateWrappedSource(ctx, client, scenario.Config)
require.NoError(t, err, "failed to create wrapped source")
// Get endpoints
endpoints, err := wrappedSource.Endpoints(ctx)
require.NoError(t, err)
testutils.ValidateEndpoints(t, endpoints, scenario.Expected)
})
}
}