|
6 | 6 | "github.com/stretchr/testify/assert" |
7 | 7 | corev1 "k8s.io/api/core/v1" |
8 | 8 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/utils/ptr" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | func TestHostNeedsHardwareInspection(t *testing.T) { |
@@ -461,6 +462,56 @@ func TestGetImageChecksum(t *testing.T) { |
461 | 462 | }, |
462 | 463 | Expected: false, |
463 | 464 | }, |
| 465 | + { |
| 466 | + Scenario: "OCI image without checksum", |
| 467 | + Host: BareMetalHost{ |
| 468 | + ObjectMeta: metav1.ObjectMeta{ |
| 469 | + Name: "myhost", |
| 470 | + Namespace: "myns", |
| 471 | + }, |
| 472 | + Spec: BareMetalHostSpec{ |
| 473 | + Image: &Image{ |
| 474 | + URL: "oci://example.com/image:latest", |
| 475 | + }, |
| 476 | + }, |
| 477 | + }, |
| 478 | + Expected: true, |
| 479 | + ExpectedType: "", |
| 480 | + }, |
| 481 | + { |
| 482 | + Scenario: "OCI image with checksum", |
| 483 | + Host: BareMetalHost{ |
| 484 | + ObjectMeta: metav1.ObjectMeta{ |
| 485 | + Name: "myhost", |
| 486 | + Namespace: "myns", |
| 487 | + }, |
| 488 | + Spec: BareMetalHostSpec{ |
| 489 | + Image: &Image{ |
| 490 | + URL: "oci://example.com/image:latest", |
| 491 | + Checksum: "sha256hash", |
| 492 | + }, |
| 493 | + }, |
| 494 | + }, |
| 495 | + Expected: true, |
| 496 | + ExpectedType: "", |
| 497 | + }, |
| 498 | + { |
| 499 | + Scenario: "live-iso without checksum", |
| 500 | + Host: BareMetalHost{ |
| 501 | + ObjectMeta: metav1.ObjectMeta{ |
| 502 | + Name: "myhost", |
| 503 | + Namespace: "myns", |
| 504 | + }, |
| 505 | + Spec: BareMetalHostSpec{ |
| 506 | + Image: &Image{ |
| 507 | + URL: "http://example.com/image.iso", |
| 508 | + DiskFormat: ptr.To("live-iso"), |
| 509 | + }, |
| 510 | + }, |
| 511 | + }, |
| 512 | + Expected: true, |
| 513 | + ExpectedType: "", |
| 514 | + }, |
464 | 515 | } { |
465 | 516 | t.Run(tc.Scenario, func(t *testing.T) { |
466 | 517 | _, checksumType, actual := tc.Host.Spec.Image.GetChecksum() |
@@ -717,3 +768,100 @@ func TestInspectionDisabled(t *testing.T) { |
717 | 768 | }) |
718 | 769 | } |
719 | 770 | } |
| 771 | + |
| 772 | +func TestIsOCI(t *testing.T) { |
| 773 | + for _, tc := range []struct { |
| 774 | + Scenario string |
| 775 | + Image *Image |
| 776 | + Expected bool |
| 777 | + }{ |
| 778 | + { |
| 779 | + Scenario: "nil image", |
| 780 | + Image: nil, |
| 781 | + Expected: false, |
| 782 | + }, |
| 783 | + { |
| 784 | + Scenario: "OCI image", |
| 785 | + Image: &Image{ |
| 786 | + URL: "oci://example.com/image:latest", |
| 787 | + }, |
| 788 | + Expected: true, |
| 789 | + }, |
| 790 | + { |
| 791 | + Scenario: "OCI image with checksum", |
| 792 | + Image: &Image{ |
| 793 | + URL: "oci://registry.io/namespace/image:tag", |
| 794 | + Checksum: "sha256:abc123", |
| 795 | + }, |
| 796 | + Expected: true, |
| 797 | + }, |
| 798 | + { |
| 799 | + Scenario: "HTTP image", |
| 800 | + Image: &Image{ |
| 801 | + URL: "http://example.com/image.qcow2", |
| 802 | + }, |
| 803 | + Expected: false, |
| 804 | + }, |
| 805 | + { |
| 806 | + Scenario: "HTTPS image", |
| 807 | + Image: &Image{ |
| 808 | + URL: "https://example.com/image.qcow2", |
| 809 | + }, |
| 810 | + Expected: false, |
| 811 | + }, |
| 812 | + { |
| 813 | + Scenario: "empty URL", |
| 814 | + Image: &Image{ |
| 815 | + URL: "", |
| 816 | + }, |
| 817 | + Expected: false, |
| 818 | + }, |
| 819 | + } { |
| 820 | + t.Run(tc.Scenario, func(t *testing.T) { |
| 821 | + actual := tc.Image.IsOCI() |
| 822 | + assert.Equal(t, tc.Expected, actual) |
| 823 | + }) |
| 824 | + } |
| 825 | +} |
| 826 | + |
| 827 | +func TestIsLiveISO(t *testing.T) { |
| 828 | + for _, tc := range []struct { |
| 829 | + Scenario string |
| 830 | + Image *Image |
| 831 | + Expected bool |
| 832 | + }{ |
| 833 | + { |
| 834 | + Scenario: "nil image", |
| 835 | + Image: nil, |
| 836 | + Expected: false, |
| 837 | + }, |
| 838 | + { |
| 839 | + Scenario: "live-iso format", |
| 840 | + Image: &Image{ |
| 841 | + URL: "http://example.com/image.iso", |
| 842 | + DiskFormat: ptr.To("live-iso"), |
| 843 | + }, |
| 844 | + Expected: true, |
| 845 | + }, |
| 846 | + { |
| 847 | + Scenario: "raw format", |
| 848 | + Image: &Image{ |
| 849 | + URL: "http://example.com/image.qcow2", |
| 850 | + DiskFormat: ptr.To("raw"), |
| 851 | + }, |
| 852 | + Expected: false, |
| 853 | + }, |
| 854 | + { |
| 855 | + Scenario: "no format specified", |
| 856 | + Image: &Image{ |
| 857 | + URL: "http://example.com/image.qcow2", |
| 858 | + }, |
| 859 | + Expected: false, |
| 860 | + }, |
| 861 | + } { |
| 862 | + t.Run(tc.Scenario, func(t *testing.T) { |
| 863 | + actual := tc.Image.IsLiveISO() |
| 864 | + assert.Equal(t, tc.Expected, actual) |
| 865 | + }) |
| 866 | + } |
| 867 | +} |
0 commit comments