|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/harvester/vm-dhcp-controller/pkg/config" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParseImageNameAndTag(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + image string |
| 14 | + expected *config.Image |
| 15 | + err bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "valid image with registry and tag", |
| 19 | + image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller:v0.3.3", |
| 20 | + expected: &config.Image{ |
| 21 | + Repository: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller", |
| 22 | + Tag: "v0.3.3", |
| 23 | + }, |
| 24 | + err: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "valid image with only image and tag", |
| 28 | + image: "rancher/harvester-vm-dhcp-controller:v0.3.3", |
| 29 | + expected: &config.Image{ |
| 30 | + Repository: "rancher/harvester-vm-dhcp-controller", |
| 31 | + Tag: "v0.3.3", |
| 32 | + }, |
| 33 | + err: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "valid image without tag", |
| 37 | + image: "rancher/harvester-vm-dhcp-controller", |
| 38 | + expected: &config.Image{ |
| 39 | + Repository: "rancher/harvester-vm-dhcp-controller", |
| 40 | + Tag: "latest", |
| 41 | + }, |
| 42 | + err: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "valid image with port but no tag", |
| 46 | + image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller", |
| 47 | + expected: &config.Image{ |
| 48 | + Repository: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller", |
| 49 | + Tag: "latest", |
| 50 | + }, |
| 51 | + err: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "invalid image with colon but no tag", |
| 55 | + image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller:", |
| 56 | + expected: nil, |
| 57 | + err: true, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "invalid image with multiple colons", |
| 61 | + image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller:v0.3.3:latest", |
| 62 | + expected: nil, |
| 63 | + err: true, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + result, err := parseImageNameAndTag(tt.image) |
| 70 | + if tt.err { |
| 71 | + assert.Error(t, err) |
| 72 | + } else { |
| 73 | + assert.NoError(t, err) |
| 74 | + assert.Equal(t, tt.expected, result) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments