|
| 1 | +package containertools |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestBuildCommand(t *testing.T) { |
| 10 | + for _, tt := range []struct { |
| 11 | + Name string |
| 12 | + Factory CommandFactory |
| 13 | + Options BuildOptions |
| 14 | + Args []string |
| 15 | + }{ |
| 16 | + { |
| 17 | + Name: "docker defaults", |
| 18 | + Factory: &DockerCommandFactory{}, |
| 19 | + Options: DefaultBuildOptions(), |
| 20 | + Args: []string{ |
| 21 | + "docker", "build", ".", |
| 22 | + }, |
| 23 | + }, |
| 24 | + { |
| 25 | + Name: "docker unsupported format", |
| 26 | + Factory: &DockerCommandFactory{}, |
| 27 | + Options: BuildOptions{ |
| 28 | + context: ".", |
| 29 | + format: "oci", |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + Name: "docker context", |
| 34 | + Factory: &DockerCommandFactory{}, |
| 35 | + Options: BuildOptions{ |
| 36 | + context: "foo", |
| 37 | + }, |
| 38 | + Args: []string{ |
| 39 | + "docker", "build", "foo", |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "docker dockerfile", |
| 44 | + Factory: &DockerCommandFactory{}, |
| 45 | + Options: BuildOptions{ |
| 46 | + context: ".", |
| 47 | + dockerfile: "foo", |
| 48 | + }, |
| 49 | + Args: []string{ |
| 50 | + "docker", "build", "-f", "foo", ".", |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "docker single tag", |
| 55 | + Factory: &DockerCommandFactory{}, |
| 56 | + Options: BuildOptions{ |
| 57 | + context: ".", |
| 58 | + tags: []string{"foo"}, |
| 59 | + }, |
| 60 | + Args: []string{ |
| 61 | + "docker", "build", "-t", "foo", ".", |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "docker multiple tags", |
| 66 | + Factory: &DockerCommandFactory{}, |
| 67 | + Options: BuildOptions{ |
| 68 | + context: ".", |
| 69 | + tags: []string{"foo", "bar"}, |
| 70 | + }, |
| 71 | + Args: []string{ |
| 72 | + "docker", "build", "-t", "foo", "-t", "bar", ".", |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + Name: "podman defaults", |
| 77 | + Factory: &PodmanCommandFactory{}, |
| 78 | + Options: DefaultBuildOptions(), |
| 79 | + Args: []string{ |
| 80 | + "podman", "build", "--format", "docker", ".", |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + Name: "podman oci format", |
| 85 | + Factory: &PodmanCommandFactory{}, |
| 86 | + Options: BuildOptions{ |
| 87 | + context: ".", |
| 88 | + format: "oci", |
| 89 | + }, |
| 90 | + Args: []string{ |
| 91 | + "podman", "build", "--format", "oci", ".", |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + Name: "podman context", |
| 96 | + Factory: &PodmanCommandFactory{}, |
| 97 | + Options: BuildOptions{ |
| 98 | + context: "foo", |
| 99 | + }, |
| 100 | + Args: []string{ |
| 101 | + "podman", "build", "--format", "docker", "foo", |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + Name: "podman dockerfile", |
| 106 | + Factory: &PodmanCommandFactory{}, |
| 107 | + Options: BuildOptions{ |
| 108 | + context: ".", |
| 109 | + dockerfile: "foo", |
| 110 | + }, |
| 111 | + Args: []string{ |
| 112 | + "podman", "build", "--format", "docker", "-f", "foo", ".", |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + Name: "podman single tag", |
| 117 | + Factory: &PodmanCommandFactory{}, |
| 118 | + Options: BuildOptions{ |
| 119 | + context: ".", |
| 120 | + tags: []string{"foo"}, |
| 121 | + }, |
| 122 | + Args: []string{ |
| 123 | + "podman", "build", "--format", "docker", "-t", "foo", ".", |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + Name: "podman multiple tags", |
| 128 | + Factory: &PodmanCommandFactory{}, |
| 129 | + Options: BuildOptions{ |
| 130 | + context: ".", |
| 131 | + tags: []string{"foo", "bar"}, |
| 132 | + }, |
| 133 | + Args: []string{ |
| 134 | + "podman", "build", "--format", "docker", "-t", "foo", "-t", "bar", ".", |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + Name: "stub defaults", |
| 139 | + Factory: &StubCommandFactory{}, |
| 140 | + Options: DefaultBuildOptions(), |
| 141 | + }, |
| 142 | + } { |
| 143 | + t.Run(tt.Name, func(t *testing.T) { |
| 144 | + require := require.New(t) |
| 145 | + |
| 146 | + cmd, err := tt.Factory.BuildCommand(tt.Options) |
| 147 | + if tt.Args == nil { |
| 148 | + require.Nil(cmd) |
| 149 | + require.Error(err) |
| 150 | + } else { |
| 151 | + require.NotNil(cmd) |
| 152 | + require.NoError(err) |
| 153 | + require.Equal(tt.Args, cmd.Args) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments