kubeadm: fix flaky-test TestManifestFilesAreEqual

This commit is contained in:
SataQiu 2023-09-12 19:48:02 +08:00
parent e2b03d41c5
commit 15ce5dd990
2 changed files with 31 additions and 9 deletions

View File

@ -29,8 +29,8 @@ import (
"strings"
"sync"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/pmezard/go-difflib/difflib"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
@ -374,7 +374,28 @@ func ManifestFilesAreEqual(path1, path2 string) (bool, string, error) {
if bytes.Equal(hash1, hash2) {
return true, "", nil
}
return false, cmp.Diff(pod2, pod1), nil
manifest1, err := kubeadmutil.MarshalToYaml(pod1, v1.SchemeGroupVersion)
if err != nil {
return false, "", errors.Wrapf(err, "failed to marshal Pod manifest for %q to YAML", path1)
}
manifest2, err := kubeadmutil.MarshalToYaml(pod2, v1.SchemeGroupVersion)
if err != nil {
return false, "", errors.Wrapf(err, "failed to marshal Pod manifest for %q to YAML", path2)
}
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(manifest1)),
B: difflib.SplitLines(string(manifest2)),
}
diffStr, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
return false, "", errors.Wrapf(err, "failed to generate the differences between manifest %q and manifest %q", path1, path2)
}
return false, diffStr, nil
}
// getProbeAddress returns a valid probe address.

View File

@ -766,18 +766,19 @@ func TestManifestFilesAreEqual(t *testing.T) {
podYamls: []string{validPod, validPod2},
expectedResult: false,
expectErr: false,
expectedDiff: string(`
- "2",
+ "1",`),
expectedDiff: `@@ -12 +12 @@
- - image: gcr.io/google_containers/etcd-amd64:3.1.11
+ - image: gcr.io/google_containers/etcd-amd64:3.1.12
`,
},
{
description: "manifests are not equal for adding new defaults",
podYamls: []string{validPod, invalidWithDefaultFields},
expectedResult: false,
expectErr: false,
expectedDiff: string(`
- RestartPolicy: "Always",
+ RestartPolicy: "",`),
expectedDiff: `@@ -14,0 +15 @@
+ restartPolicy: Always
`,
},
{
description: "first manifest doesn't exist",
@ -830,7 +831,7 @@ func TestManifestFilesAreEqual(t *testing.T) {
}
if !strings.Contains(diff, rt.expectedDiff) {
t.Errorf(
"ManifestFilesAreEqual diff doesn't expected\n%s\n\texpected diff: %s\nactual diff: %s",
"ManifestFilesAreEqual diff doesn't expected\n%s\n\texpected diff: %s\n\tactual diff: %s",
rt.description,
rt.expectedDiff,
diff,