-
Notifications
You must be signed in to change notification settings - Fork 514
OCPBUGS-104569: vSphere boot image reconciler overwrites/renames current custom-named templates #6354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jcpowermac
wants to merge
4
commits into
openshift:main
Choose a base branch
from
jcpowermac:vsphere-ova-test-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
OCPBUGS-104569: vSphere boot image reconciler overwrites/renames current custom-named templates #6354
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23110c7
bootimage: fix vSphere reconciler overwriting current custom-named te…
jcpowermac 4271859
test: add vSphere boot image unit and e2e coverage
jcpowermac a83b588
vendor: pull in govmomi simulator/toolbox/vapi packages for test cove…
jcpowermac 85960a9
go.mod: mark github.com/pkg/errors as indirect
jcpowermac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/coreos/stream-metadata-go/stream" | ||
| ) | ||
|
|
||
| var testFileCounter atomic.Int64 | ||
|
|
||
| // newTestArtifact starts an httptest server serving content at a uniquely-named path (so | ||
| // concurrent/sequential test cases never collide in the shared /tmp/imagebased/image_cache | ||
| // directory DownloadOva always uses) and returns a stream.Artifact pointing at it, along with | ||
| // a cleanup func that removes the resulting cached file. | ||
| func newTestArtifact(t *testing.T, content []byte) (*stream.Artifact, *httptest.Server, func()) { | ||
| t.Helper() | ||
|
|
||
| n := testFileCounter.Add(1) | ||
| fileName := fmt.Sprintf("mco-cache-test-%d.ova", n) | ||
|
|
||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| _, _ = w.Write(content) | ||
| })) | ||
| t.Cleanup(srv.Close) | ||
|
|
||
| sum := sha256.Sum256(content) | ||
| artifact := &stream.Artifact{ | ||
| Location: srv.URL + "/" + fileName, | ||
| Sha256: hex.EncodeToString(sum[:]), | ||
| } | ||
|
|
||
| cacheDir, err := getCacheDir() | ||
| if err != nil { | ||
| t.Fatalf("newTestArtifact: failed to resolve cache dir: %v", err) | ||
| } | ||
| cleanup := func() { | ||
| _ = os.Remove(filepath.Join(cacheDir, fileName)) | ||
| } | ||
| t.Cleanup(cleanup) | ||
|
|
||
| return artifact, srv, cleanup | ||
| } | ||
|
|
||
| func TestDownloadOva(t *testing.T) { | ||
| t.Run("fresh download", func(t *testing.T) { | ||
| content := []byte("fake-ova-content-fresh-download") | ||
| artifact, _, _ := newTestArtifact(t, content) | ||
|
|
||
| path, err := DownloadOva(artifact) | ||
| if err != nil { | ||
| t.Fatalf("DownloadOva() unexpected error: %v", err) | ||
| } | ||
| got, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatalf("failed to read downloaded file: %v", err) | ||
| } | ||
| if string(got) != string(content) { | ||
| t.Errorf("downloaded content = %q, want %q", got, content) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("cache hit avoids re-downloading", func(t *testing.T) { | ||
| content := []byte("fake-ova-content-cache-hit") | ||
| artifact, srv, _ := newTestArtifact(t, content) | ||
|
|
||
| firstPath, err := DownloadOva(artifact) | ||
| if err != nil { | ||
| t.Fatalf("DownloadOva() (first call) unexpected error: %v", err) | ||
| } | ||
|
|
||
| // Shut the server down before the second call: if DownloadOva tried to re-fetch instead | ||
| // of serving from cache, it would fail with a connection error. | ||
| srv.Close() | ||
|
|
||
| secondPath, err := DownloadOva(artifact) | ||
| if err != nil { | ||
| t.Fatalf("DownloadOva() (second call) unexpected error: %v", err) | ||
| } | ||
| if secondPath != firstPath { | ||
| t.Errorf("DownloadOva() second call path = %q, want %q (same cached file)", secondPath, firstPath) | ||
| } | ||
| got, err := os.ReadFile(secondPath) | ||
| if err != nil { | ||
| t.Fatalf("failed to read cached file: %v", err) | ||
| } | ||
| if string(got) != string(content) { | ||
| t.Errorf("cached content = %q, want %q", got, content) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("corrupted cache is re-downloaded", func(t *testing.T) { | ||
| content := []byte("fake-ova-content-corruption-repair") | ||
| artifact, _, cleanup := newTestArtifact(t, content) | ||
|
|
||
| cacheDir, err := getCacheDir() | ||
| if err != nil { | ||
| t.Fatalf("failed to resolve cache dir: %v", err) | ||
| } | ||
| name, err := artifact.Name() | ||
| if err != nil { | ||
| t.Fatalf("failed to compute artifact name: %v", err) | ||
| } | ||
| cachedPath := filepath.Join(cacheDir, name) | ||
|
|
||
| // Pre-populate the cache with content that does NOT match artifact.Sha256. | ||
| if err := os.WriteFile(cachedPath, []byte("stale-corrupted-bytes"), 0o644); err != nil { | ||
| t.Fatalf("failed to seed corrupted cache file: %v", err) | ||
| } | ||
| defer cleanup() | ||
|
|
||
| path, err := DownloadOva(artifact) | ||
| if err != nil { | ||
| t.Fatalf("DownloadOva() unexpected error: %v", err) | ||
| } | ||
| got, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatalf("failed to read repaired file: %v", err) | ||
| } | ||
| if string(got) != string(content) { | ||
| t.Errorf("repaired content = %q, want %q (freshly re-downloaded)", got, content) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("download failure propagates", func(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| })) | ||
| t.Cleanup(srv.Close) | ||
|
|
||
| artifact := &stream.Artifact{ | ||
| Location: srv.URL + "/unreachable-mco-test.ova", | ||
| Sha256: "0000000000000000000000000000000000000000000000000000000000000", | ||
| } | ||
| cacheDir, err := getCacheDir() | ||
| if err != nil { | ||
| t.Fatalf("failed to resolve cache dir: %v", err) | ||
| } | ||
| t.Cleanup(func() { _ = os.Remove(filepath.Join(cacheDir, "unreachable-mco-test.ova")) }) | ||
|
|
||
| if _, err := DownloadOva(artifact); err == nil { | ||
| t.Fatalf("DownloadOva() expected an error for a failing download, got nil") | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package bootimage | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/coreos/stream-metadata-go/stream" | ||
| osconfigv1 "github.com/openshift/api/config/v1" | ||
| machinev1beta1 "github.com/openshift/api/machine/v1beta1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ) | ||
|
|
||
| func TestGetMAPIBootImageValue(t *testing.T) { | ||
| const rawProviderSpecJSON = `{"template":"some-raw-provider-spec"}` | ||
|
|
||
| machineSet := &machinev1beta1.MachineSet{ | ||
| Spec: machinev1beta1.MachineSetSpec{ | ||
| Template: machinev1beta1.MachineTemplateSpec{ | ||
| Spec: machinev1beta1.MachineSpec{ | ||
| ProviderSpec: machinev1beta1.ProviderSpec{ | ||
| Value: &runtime.RawExtension{Raw: []byte(rawProviderSpecJSON)}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| vsphereInfra := &osconfigv1.Infrastructure{ | ||
| Status: osconfigv1.InfrastructureStatus{ | ||
| PlatformStatus: &osconfigv1.PlatformStatus{Type: osconfigv1.VSpherePlatformType}, | ||
| }, | ||
| } | ||
| awsInfra := &osconfigv1.Infrastructure{ | ||
| Status: osconfigv1.InfrastructureStatus{ | ||
| PlatformStatus: &osconfigv1.PlatformStatus{Type: osconfigv1.AWSPlatformType}, | ||
| }, | ||
| } | ||
|
|
||
| streamConfigMap := func(t *testing.T, s *stream.Stream) *corev1.ConfigMap { | ||
| t.Helper() | ||
| data, err := json.Marshal(s) | ||
| if err != nil { | ||
| t.Fatalf("failed to marshal test stream: %v", err) | ||
| } | ||
| return &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "coreos-bootimages"}, | ||
| Data: map[string]string{StreamConfigMapKey: string(data)}, | ||
| } | ||
| } | ||
|
|
||
| validVSphereStream := &stream.Stream{ | ||
| Architectures: map[string]stream.Arch{ | ||
| "x86_64": { | ||
| Artifacts: map[string]stream.PlatformArtifacts{ | ||
| "vmware": {Release: "417.94.20250101"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| infra *osconfigv1.Infrastructure | ||
| configMap *corev1.ConfigMap | ||
| arch string | ||
| want string // "" means "fall back to raw providerSpec bytes" | ||
| }{ | ||
| { | ||
| name: "non-vSphere platform: raw providerSpec bytes", | ||
| infra: awsInfra, | ||
| configMap: streamConfigMap(t, validVSphereStream), | ||
| arch: "x86_64", | ||
| }, | ||
| { | ||
| name: "nil infra: raw providerSpec bytes", | ||
| infra: nil, | ||
| configMap: streamConfigMap(t, validVSphereStream), | ||
| arch: "x86_64", | ||
| }, | ||
| { | ||
| name: "nil PlatformStatus: raw providerSpec bytes", | ||
| infra: &osconfigv1.Infrastructure{}, | ||
| configMap: streamConfigMap(t, validVSphereStream), | ||
| arch: "x86_64", | ||
| }, | ||
| { | ||
| name: "nil configMap: raw providerSpec bytes", | ||
| infra: vsphereInfra, | ||
| configMap: nil, | ||
| arch: "x86_64", | ||
| }, | ||
| { | ||
| name: "vSphere with valid release for arch: release string", | ||
| infra: vsphereInfra, | ||
| configMap: streamConfigMap(t, validVSphereStream), | ||
| arch: "x86_64", | ||
| want: "417.94.20250101", | ||
| }, | ||
| { | ||
| name: "vSphere but arch not present in stream: raw providerSpec bytes", | ||
| infra: vsphereInfra, | ||
| configMap: streamConfigMap(t, validVSphereStream), | ||
| arch: "arm64", | ||
| }, | ||
| { | ||
| name: "vSphere but vmware artifact has empty release: raw providerSpec bytes", | ||
| infra: vsphereInfra, | ||
| configMap: streamConfigMap(t, &stream.Stream{ | ||
| Architectures: map[string]stream.Arch{ | ||
| "x86_64": {Artifacts: map[string]stream.PlatformArtifacts{"vmware": {Release: ""}}}, | ||
| }, | ||
| }), | ||
| arch: "x86_64", | ||
| }, | ||
| { | ||
| name: "vSphere but configMap data is unparseable: raw providerSpec bytes", | ||
| infra: vsphereInfra, | ||
| configMap: &corev1.ConfigMap{ | ||
| Data: map[string]string{StreamConfigMapKey: "not-json"}, | ||
| }, | ||
| arch: "x86_64", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got := getMAPIBootImageValue(machineSet, tc.configMap, tc.infra, tc.arch) | ||
| want := rawProviderSpecJSON | ||
| if tc.want != "" { | ||
| want = tc.want | ||
| } | ||
| if string(got) != want { | ||
| t.Errorf("getMAPIBootImageValue() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we roll this into the main vendor commit?