|
| 1 | +package imgsrc |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBuilderAPIURL(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + daemonHost string |
| 14 | + path string |
| 15 | + want string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "ipv4", |
| 19 | + daemonHost: "tcp://192.168.1.10:2375", |
| 20 | + path: "/flyio/v1/extendDeadline", |
| 21 | + want: "http://192.168.1.10:8080/flyio/v1/extendDeadline", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "ipv6", |
| 25 | + daemonHost: "tcp://[fdaa:49:2bd7::2]:2375", |
| 26 | + path: "/flyio/v1/extendDeadline", |
| 27 | + want: "http://[fdaa:49:2bd7::2]:8080/flyio/v1/extendDeadline", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "overlaybd path", |
| 31 | + daemonHost: "tcp://[fdaa:49:2bd7::2]:2375", |
| 32 | + path: "/flyio/v1/buildOverlaybdImage", |
| 33 | + want: "http://[fdaa:49:2bd7::2]:8080/flyio/v1/buildOverlaybdImage", |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tc := range tests { |
| 38 | + t.Run(tc.name, func(t *testing.T) { |
| 39 | + got, err := builderAPIURL(tc.daemonHost, tc.path) |
| 40 | + require.NoError(t, err) |
| 41 | + assert.Equal(t, tc.want, got) |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestBuilderAPIURLError(t *testing.T) { |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + daemonHost string |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "missing host", |
| 53 | + daemonHost: "unix:///var/run/docker.sock", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "invalid url", |
| 57 | + daemonHost: "://bad-url", |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range tests { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + _, err := builderAPIURL(tc.daemonHost, "/flyio/v1/extendDeadline") |
| 64 | + require.Error(t, err) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments