mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 20:42:26 +00:00
kubeadm: fix flaky-test TestManifestFilesAreEqual
This commit is contained in:
parent
e2b03d41c5
commit
15ce5dd990
@ -29,8 +29,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/pmezard/go-difflib/difflib"
|
||||||
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
@ -374,7 +374,28 @@ func ManifestFilesAreEqual(path1, path2 string) (bool, string, error) {
|
|||||||
if bytes.Equal(hash1, hash2) {
|
if bytes.Equal(hash1, hash2) {
|
||||||
return true, "", nil
|
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.
|
// getProbeAddress returns a valid probe address.
|
||||||
|
@ -766,18 +766,19 @@ func TestManifestFilesAreEqual(t *testing.T) {
|
|||||||
podYamls: []string{validPod, validPod2},
|
podYamls: []string{validPod, validPod2},
|
||||||
expectedResult: false,
|
expectedResult: false,
|
||||||
expectErr: false,
|
expectErr: false,
|
||||||
expectedDiff: string(`
|
expectedDiff: `@@ -12 +12 @@
|
||||||
- "2",
|
- - image: gcr.io/google_containers/etcd-amd64:3.1.11
|
||||||
+ "1",`),
|
+ - image: gcr.io/google_containers/etcd-amd64:3.1.12
|
||||||
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "manifests are not equal for adding new defaults",
|
description: "manifests are not equal for adding new defaults",
|
||||||
podYamls: []string{validPod, invalidWithDefaultFields},
|
podYamls: []string{validPod, invalidWithDefaultFields},
|
||||||
expectedResult: false,
|
expectedResult: false,
|
||||||
expectErr: false,
|
expectErr: false,
|
||||||
expectedDiff: string(`
|
expectedDiff: `@@ -14,0 +15 @@
|
||||||
- RestartPolicy: "Always",
|
+ restartPolicy: Always
|
||||||
+ RestartPolicy: "",`),
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "first manifest doesn't exist",
|
description: "first manifest doesn't exist",
|
||||||
@ -830,7 +831,7 @@ func TestManifestFilesAreEqual(t *testing.T) {
|
|||||||
}
|
}
|
||||||
if !strings.Contains(diff, rt.expectedDiff) {
|
if !strings.Contains(diff, rt.expectedDiff) {
|
||||||
t.Errorf(
|
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.description,
|
||||||
rt.expectedDiff,
|
rt.expectedDiff,
|
||||||
diff,
|
diff,
|
||||||
|
Loading…
Reference in New Issue
Block a user